root/lib/zstd/zstd_common.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. ZSTD_initStack
  2. ZSTD_stackAllocAll
  3. ZSTD_stackAlloc
  4. ZSTD_stackFree
  5. ZSTD_malloc
  6. ZSTD_free

   1 /**
   2  * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
   3  * All rights reserved.
   4  *
   5  * This source code is licensed under the BSD-style license found in the
   6  * LICENSE file in the root directory of https://github.com/facebook/zstd.
   7  * An additional grant of patent rights can be found in the PATENTS file in the
   8  * same directory.
   9  *
  10  * This program is free software; you can redistribute it and/or modify it under
  11  * the terms of the GNU General Public License version 2 as published by the
  12  * Free Software Foundation. This program is dual-licensed; you may select
  13  * either version 2 of the GNU General Public License ("GPL") or BSD license
  14  * ("BSD").
  15  */
  16 
  17 /*-*************************************
  18 *  Dependencies
  19 ***************************************/
  20 #include "error_private.h"
  21 #include "zstd_internal.h" /* declaration of ZSTD_isError, ZSTD_getErrorName, ZSTD_getErrorCode, ZSTD_getErrorString, ZSTD_versionNumber */
  22 #include <linux/kernel.h>
  23 
  24 /*=**************************************************************
  25 *  Custom allocator
  26 ****************************************************************/
  27 
  28 #define stack_push(stack, size)                                 \
  29         ({                                                      \
  30                 void *const ptr = ZSTD_PTR_ALIGN((stack)->ptr); \
  31                 (stack)->ptr = (char *)ptr + (size);            \
  32                 (stack)->ptr <= (stack)->end ? ptr : NULL;      \
  33         })
  34 
  35 ZSTD_customMem ZSTD_initStack(void *workspace, size_t workspaceSize)
  36 {
  37         ZSTD_customMem stackMem = {ZSTD_stackAlloc, ZSTD_stackFree, workspace};
  38         ZSTD_stack *stack = (ZSTD_stack *)workspace;
  39         /* Verify preconditions */
  40         if (!workspace || workspaceSize < sizeof(ZSTD_stack) || workspace != ZSTD_PTR_ALIGN(workspace)) {
  41                 ZSTD_customMem error = {NULL, NULL, NULL};
  42                 return error;
  43         }
  44         /* Initialize the stack */
  45         stack->ptr = workspace;
  46         stack->end = (char *)workspace + workspaceSize;
  47         stack_push(stack, sizeof(ZSTD_stack));
  48         return stackMem;
  49 }
  50 
  51 void *ZSTD_stackAllocAll(void *opaque, size_t *size)
  52 {
  53         ZSTD_stack *stack = (ZSTD_stack *)opaque;
  54         *size = (BYTE const *)stack->end - (BYTE *)ZSTD_PTR_ALIGN(stack->ptr);
  55         return stack_push(stack, *size);
  56 }
  57 
  58 void *ZSTD_stackAlloc(void *opaque, size_t size)
  59 {
  60         ZSTD_stack *stack = (ZSTD_stack *)opaque;
  61         return stack_push(stack, size);
  62 }
  63 void ZSTD_stackFree(void *opaque, void *address)
  64 {
  65         (void)opaque;
  66         (void)address;
  67 }
  68 
  69 void *ZSTD_malloc(size_t size, ZSTD_customMem customMem) { return customMem.customAlloc(customMem.opaque, size); }
  70 
  71 void ZSTD_free(void *ptr, ZSTD_customMem customMem)
  72 {
  73         if (ptr != NULL)
  74                 customMem.customFree(customMem.opaque, ptr);
  75 }

/* [<][>][^][v][top][bottom][index][help] */