1 // SPDX-License-Identifier: (GPL-2.0 or BSD-3-Clause-Clear)
3 * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
7 /*-*************************************
9 ***************************************/
10 #include "error_private.h"
11 #include "zstd_internal.h" /* declaration of ZSTD_isError, ZSTD_getErrorName, ZSTD_getErrorCode, ZSTD_getErrorString, ZSTD_versionNumber */
12 #include <linux/kernel.h>
14 /*=**************************************************************
16 ****************************************************************/
18 #define stack_push(stack, size) \
20 void *const ptr = ZSTD_PTR_ALIGN((stack)->ptr); \
21 (stack)->ptr = (char *)ptr + (size); \
22 (stack)->ptr <= (stack)->end ? ptr : NULL; \
25 ZSTD_customMem ZSTD_initStack(void *workspace, size_t workspaceSize)
27 ZSTD_customMem stackMem = {ZSTD_stackAlloc, ZSTD_stackFree, workspace};
28 ZSTD_stack *stack = (ZSTD_stack *)workspace;
29 /* Verify preconditions */
30 if (!workspace || workspaceSize < sizeof(ZSTD_stack) || workspace != ZSTD_PTR_ALIGN(workspace)) {
31 ZSTD_customMem error = {NULL, NULL, NULL};
34 /* Initialize the stack */
35 stack->ptr = workspace;
36 stack->end = (char *)workspace + workspaceSize;
37 stack_push(stack, sizeof(ZSTD_stack));
41 void *ZSTD_stackAllocAll(void *opaque, size_t *size)
43 ZSTD_stack *stack = (ZSTD_stack *)opaque;
44 *size = (BYTE const *)stack->end - (BYTE *)ZSTD_PTR_ALIGN(stack->ptr);
45 return stack_push(stack, *size);
48 void *ZSTD_stackAlloc(void *opaque, size_t size)
50 ZSTD_stack *stack = (ZSTD_stack *)opaque;
51 return stack_push(stack, size);
53 void ZSTD_stackFree(void *opaque, void *address)
59 void *ZSTD_malloc(size_t size, ZSTD_customMem customMem) { return customMem.customAlloc(customMem.opaque, size); }
61 void ZSTD_free(void *ptr, ZSTD_customMem customMem)
64 customMem.customFree(customMem.opaque, ptr);