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