/src/barebox/lib/zstd/zstd_common.c
Line | Count | Source |
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 | 55.8k | ({ \ |
30 | 55.8k | void *const ptr = ZSTD_PTR_ALIGN((stack)->ptr); \ |
31 | 55.8k | (stack)->ptr = (char *)ptr + (size); \ |
32 | 55.8k | (stack)->ptr <= (stack)->end ? ptr : NULL; \ |
33 | 55.8k | }) |
34 | | |
35 | | ZSTD_customMem ZSTD_initStack(void *workspace, size_t workspaceSize) |
36 | 11.1k | { |
37 | 11.1k | ZSTD_customMem stackMem = {ZSTD_stackAlloc, ZSTD_stackFree, workspace}; |
38 | 11.1k | ZSTD_stack *stack = (ZSTD_stack *)workspace; |
39 | | /* Verify preconditions */ |
40 | 11.1k | if (!workspace || workspaceSize < sizeof(ZSTD_stack) || workspace != ZSTD_PTR_ALIGN(workspace)) { |
41 | 0 | ZSTD_customMem error = {NULL, NULL, NULL}; |
42 | 0 | return error; |
43 | 0 | } |
44 | | /* Initialize the stack */ |
45 | 11.1k | stack->ptr = workspace; |
46 | 11.1k | stack->end = (char *)workspace + workspaceSize; |
47 | 11.1k | stack_push(stack, sizeof(ZSTD_stack)); |
48 | 11.1k | return stackMem; |
49 | 11.1k | } |
50 | | |
51 | | void *ZSTD_stackAllocAll(void *opaque, size_t *size) |
52 | 0 | { |
53 | 0 | ZSTD_stack *stack = (ZSTD_stack *)opaque; |
54 | 0 | *size = (BYTE const *)stack->end - (BYTE *)ZSTD_PTR_ALIGN(stack->ptr); |
55 | 0 | return stack_push(stack, *size); |
56 | 0 | } |
57 | | |
58 | | void *ZSTD_stackAlloc(void *opaque, size_t size) |
59 | 44.6k | { |
60 | 44.6k | ZSTD_stack *stack = (ZSTD_stack *)opaque; |
61 | 44.6k | return stack_push(stack, size); |
62 | 44.6k | } |
63 | | void ZSTD_stackFree(void *opaque, void *address) |
64 | 0 | { |
65 | 0 | (void)opaque; |
66 | 0 | (void)address; |
67 | 0 | } |
68 | | |
69 | 44.6k | 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 | 0 | { |
73 | 0 | if (ptr != NULL) |
74 | 0 | customMem.customFree(customMem.opaque, ptr); |
75 | 0 | } |