Path: blob/main/sys/contrib/zstd/lib/common/zstd_common.c
48378 views
/*1* Copyright (c) Yann Collet, Facebook, Inc.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*/9101112/*-*************************************13* Dependencies14***************************************/15#define ZSTD_DEPS_NEED_MALLOC16#include "zstd_deps.h" /* ZSTD_malloc, ZSTD_calloc, ZSTD_free, ZSTD_memset */17#include "error_private.h"18#include "zstd_internal.h"192021/*-****************************************22* Version23******************************************/24unsigned ZSTD_versionNumber(void) { return ZSTD_VERSION_NUMBER; }2526const char* ZSTD_versionString(void) { return ZSTD_VERSION_STRING; }272829/*-****************************************30* ZSTD Error Management31******************************************/32#undef ZSTD_isError /* defined within zstd_internal.h */33/*! ZSTD_isError() :34* tells if a return value is an error code35* symbol is required for external callers */36unsigned ZSTD_isError(size_t code) { return ERR_isError(code); }3738/*! ZSTD_getErrorName() :39* provides error code string from function result (useful for debugging) */40const char* ZSTD_getErrorName(size_t code) { return ERR_getErrorName(code); }4142/*! ZSTD_getError() :43* convert a `size_t` function result into a proper ZSTD_errorCode enum */44ZSTD_ErrorCode ZSTD_getErrorCode(size_t code) { return ERR_getErrorCode(code); }4546/*! ZSTD_getErrorString() :47* provides error code string from enum */48const char* ZSTD_getErrorString(ZSTD_ErrorCode code) { return ERR_getErrorString(code); }49505152/*=**************************************************************53* Custom allocator54****************************************************************/55void* ZSTD_customMalloc(size_t size, ZSTD_customMem customMem)56{57if (customMem.customAlloc)58return customMem.customAlloc(customMem.opaque, size);59return ZSTD_malloc(size);60}6162void* ZSTD_customCalloc(size_t size, ZSTD_customMem customMem)63{64if (customMem.customAlloc) {65/* calloc implemented as malloc+memset;66* not as efficient as calloc, but next best guess for custom malloc */67void* const ptr = customMem.customAlloc(customMem.opaque, size);68ZSTD_memset(ptr, 0, size);69return ptr;70}71return ZSTD_calloc(1, size);72}7374void ZSTD_customFree(void* ptr, ZSTD_customMem customMem)75{76if (ptr!=NULL) {77if (customMem.customFree)78customMem.customFree(customMem.opaque, ptr);79else80ZSTD_free(ptr);81}82}838485