Path: blob/master/Utilities/cmzstd/lib/common/allocations.h
3158 views
/*1* Copyright (c) Meta Platforms, Inc. and affiliates.2* All rights reserved.3*4* This source code is licensed under both the BSD-style license (found in the5* LICENSE file in the root directory of this source tree) and the GPLv2 (found6* in the COPYING file in the root directory of this source tree).7* You may select, at your option, one of the above-listed licenses.8*/910/* This file provides custom allocation primitives11*/1213#define ZSTD_DEPS_NEED_MALLOC14#include "zstd_deps.h" /* ZSTD_malloc, ZSTD_calloc, ZSTD_free, ZSTD_memset */1516#include "mem.h" /* MEM_STATIC */17#define ZSTD_STATIC_LINKING_ONLY18#include "../zstd.h" /* ZSTD_customMem */1920#ifndef ZSTD_ALLOCATIONS_H21#define ZSTD_ALLOCATIONS_H2223/* custom memory allocation functions */2425MEM_STATIC void* ZSTD_customMalloc(size_t size, ZSTD_customMem customMem)26{27if (customMem.customAlloc)28return customMem.customAlloc(customMem.opaque, size);29return ZSTD_malloc(size);30}3132MEM_STATIC void* ZSTD_customCalloc(size_t size, ZSTD_customMem customMem)33{34if (customMem.customAlloc) {35/* calloc implemented as malloc+memset;36* not as efficient as calloc, but next best guess for custom malloc */37void* const ptr = customMem.customAlloc(customMem.opaque, size);38ZSTD_memset(ptr, 0, size);39return ptr;40}41return ZSTD_calloc(1, size);42}4344MEM_STATIC void ZSTD_customFree(void* ptr, ZSTD_customMem customMem)45{46if (ptr!=NULL) {47if (customMem.customFree)48customMem.customFree(customMem.opaque, ptr);49else50ZSTD_free(ptr);51}52}5354#endif /* ZSTD_ALLOCATIONS_H */555657