Path: blob/main/sys/contrib/openzfs/module/zstd/lib/common/allocations.h
178701 views
// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-only1/*2* Copyright (c) Meta Platforms, Inc. and affiliates.3* All rights reserved.4*5* This source code is licensed under both the BSD-style license (found in the6* LICENSE file in the root directory of this source tree) and the GPLv2 (found7* 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.9*/1011/* This file provides custom allocation primitives12*/1314#define ZSTD_DEPS_NEED_MALLOC15#include "zstd_deps.h" /* ZSTD_malloc, ZSTD_calloc, ZSTD_free, ZSTD_memset */1617#include "compiler.h" /* MEM_STATIC */18#define ZSTD_STATIC_LINKING_ONLY19#include "../zstd.h" /* ZSTD_customMem */2021#ifndef ZSTD_ALLOCATIONS_H22#define ZSTD_ALLOCATIONS_H2324/* custom memory allocation functions */2526MEM_STATIC void* ZSTD_customMalloc(size_t size, ZSTD_customMem customMem)27{28if (customMem.customAlloc)29return customMem.customAlloc(customMem.opaque, size);30return ZSTD_malloc(size);31}3233MEM_STATIC void* ZSTD_customCalloc(size_t size, ZSTD_customMem customMem)34{35if (customMem.customAlloc) {36/* calloc implemented as malloc+memset;37* not as efficient as calloc, but next best guess for custom malloc */38void* const ptr = customMem.customAlloc(customMem.opaque, size);39ZSTD_memset(ptr, 0, size);40return ptr;41}42return ZSTD_calloc(1, size);43}4445MEM_STATIC void ZSTD_customFree(void* ptr, ZSTD_customMem customMem)46{47if (ptr!=NULL) {48if (customMem.customFree)49customMem.customFree(customMem.opaque, ptr);50else51ZSTD_free(ptr);52}53}5455#endif /* ZSTD_ALLOCATIONS_H */565758