Path: blob/main/sys/contrib/openzfs/module/zstd/lib/common/zstd_common.c
48774 views
// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-only1/*2* Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.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*/10111213/*-*************************************14* Dependencies15***************************************/16#include <stdlib.h> /* malloc, calloc, free */17#include <string.h> /* memset */18#include "error_private.h"19#include "zstd_internal.h"202122/*-****************************************23* Version24******************************************/25unsigned ZSTD_versionNumber(void) { return ZSTD_VERSION_NUMBER; }2627const char* ZSTD_versionString(void) { return ZSTD_VERSION_STRING; }282930/*-****************************************31* ZSTD Error Management32******************************************/3334/*! ZSTD_isError() :35* tells if a return value is an error code36* symbol is required for external callers */37unsigned ZSTD_isError(size_t code) { return ERR_isError(code); }3839/*! ZSTD_getErrorName() :40* provides error code string from function result (useful for debugging) */41const char* ZSTD_getErrorName(size_t code) { return ERR_getErrorName(code); }4243/*! ZSTD_getError() :44* convert a `size_t` function result into a proper ZSTD_errorCode enum */45ZSTD_ErrorCode ZSTD_getErrorCode(size_t code) { return ERR_getErrorCode(code); }4647/*! ZSTD_getErrorString() :48* provides error code string from enum */49const char* ZSTD_getErrorString(ZSTD_ErrorCode code) { return ERR_getErrorString(code); }50515253/*=**************************************************************54* Custom allocator55****************************************************************/56void* ZSTD_malloc(size_t size, ZSTD_customMem customMem)57{58if (customMem.customAlloc)59return customMem.customAlloc(customMem.opaque, size);60return malloc(size);61}6263void* ZSTD_calloc(size_t size, ZSTD_customMem customMem)64{65if (customMem.customAlloc) {66/* calloc implemented as malloc+memset;67* not as efficient as calloc, but next best guess for custom malloc */68void* const ptr = customMem.customAlloc(customMem.opaque, size);69memset(ptr, 0, size);70return ptr;71}72return calloc(1, size);73}7475void ZSTD_free(void* ptr, ZSTD_customMem customMem)76{77if (ptr!=NULL) {78if (customMem.customFree)79customMem.customFree(customMem.opaque, ptr);80else81free(ptr);82}83}848586