1 /* SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause */
2 /*
3  * Copyright (c) Meta Platforms, Inc. and affiliates.
4  * All rights reserved.
5  *
6  * This source code is licensed under both the BSD-style license (found in the
7  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
8  * in the COPYING file in the root directory of this source tree).
9  * You may select, at your option, one of the above-listed licenses.
10  */
11 
12 /* This file provides custom allocation primitives
13  */
14 
15 #define ZSTD_DEPS_NEED_MALLOC
16 #include "zstd_deps.h"   /* ZSTD_malloc, ZSTD_calloc, ZSTD_free, ZSTD_memset */
17 
18 #include "compiler.h" /* MEM_STATIC */
19 #define ZSTD_STATIC_LINKING_ONLY
20 #include <linux/zstd.h> /* ZSTD_customMem */
21 
22 #ifndef ZSTD_ALLOCATIONS_H
23 #define ZSTD_ALLOCATIONS_H
24 
25 /* custom memory allocation functions */
26 
27 MEM_STATIC void* ZSTD_customMalloc(size_t size, ZSTD_customMem customMem)
28 {
29     if (customMem.customAlloc)
30         return customMem.customAlloc(customMem.opaque, size);
31     return ZSTD_malloc(size);
32 }
33 
34 MEM_STATIC void* ZSTD_customCalloc(size_t size, ZSTD_customMem customMem)
35 {
36     if (customMem.customAlloc) {
37         /* calloc implemented as malloc+memset;
38          * not as efficient as calloc, but next best guess for custom malloc */
39         void* const ptr = customMem.customAlloc(customMem.opaque, size);
40         ZSTD_memset(ptr, 0, size);
41         return ptr;
42     }
43     return ZSTD_calloc(1, size);
44 }
45 
46 MEM_STATIC void ZSTD_customFree(void* ptr, ZSTD_customMem customMem)
47 {
48     if (ptr!=NULL) {
49         if (customMem.customFree)
50             customMem.customFree(customMem.opaque, ptr);
51         else
52             ZSTD_free(ptr);
53     }
54 }
55 
56 #endif /* ZSTD_ALLOCATIONS_H */
57