2 * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
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
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
17 /*-*************************************
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>
24 /*=**************************************************************
26 ****************************************************************/
28 #define stack_push(stack, size) \
30 void *const ptr = ZSTD_PTR_ALIGN((stack)->ptr); \
31 (stack)->ptr = (char *)ptr + (size); \
32 (stack)->ptr <= (stack)->end ? ptr : NULL; \
35 ZSTD_customMem ZSTD_initStack(void *workspace, size_t workspaceSize)
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};
44 /* Initialize the stack */
45 stack->ptr = workspace;
46 stack->end = (char *)workspace + workspaceSize;
47 stack_push(stack, sizeof(ZSTD_stack));
51 void *ZSTD_stackAllocAll(void *opaque, size_t *size)
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);
58 void *ZSTD_stackAlloc(void *opaque, size_t size)
60 ZSTD_stack *stack = (ZSTD_stack *)opaque;
61 return stack_push(stack, size);
63 void ZSTD_stackFree(void *opaque, void *address)
69 void *ZSTD_malloc(size_t size, ZSTD_customMem customMem) { return customMem.customAlloc(customMem.opaque, size); }
71 void ZSTD_free(void *ptr, ZSTD_customMem customMem)
74 customMem.customFree(customMem.opaque, ptr);