2 * Copyright (c) Meta Platforms, Inc. and affiliates.
5 * This source code is licensed under both the BSD-style license (found in the
6 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7 * in the COPYING file in the root directory of this source tree).
8 * You may select, at your option, one of the above-listed licenses.
11 /* This file provides custom allocation primitives
14 #define ZSTD_DEPS_NEED_MALLOC
15 #include "zstd_deps.h" /* ZSTD_malloc, ZSTD_calloc, ZSTD_free, ZSTD_memset */
17 #include "mem.h" /* MEM_STATIC */
18 #define ZSTD_STATIC_LINKING_ONLY
19 #include "../zstd.h" /* ZSTD_customMem */
21 #ifndef ZSTD_ALLOCATIONS_H
22 #define ZSTD_ALLOCATIONS_H
24 /* custom memory allocation functions */
26 MEM_STATIC void* ZSTD_customMalloc(size_t size, ZSTD_customMem customMem)
28 if (customMem.customAlloc)
29 return customMem.customAlloc(customMem.opaque, size);
30 return ZSTD_malloc(size);
33 MEM_STATIC void* ZSTD_customCalloc(size_t size, ZSTD_customMem customMem)
35 if (customMem.customAlloc) {
36 /* calloc implemented as malloc+memset;
37 * not as efficient as calloc, but next best guess for custom malloc */
38 void* const ptr = customMem.customAlloc(customMem.opaque, size);
39 ZSTD_memset(ptr, 0, size);
42 return ZSTD_calloc(1, size);
45 MEM_STATIC void ZSTD_customFree(void* ptr, ZSTD_customMem customMem)
48 if (customMem.customFree)
49 customMem.customFree(customMem.opaque, ptr);
55 #endif /* ZSTD_ALLOCATIONS_H */