tracy/zstd/zstd_common.c

84 lines
2.7 KiB
C
Raw Normal View History

2020-02-08 13:56:59 +00:00
/*
2020-06-04 19:03:27 +00:00
* Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
2020-02-08 13:56:59 +00:00
* All rights reserved.
*
* This source code is licensed under both the BSD-style license (found in the
* LICENSE file in the root directory of this source tree) and the GPLv2 (found
* in the COPYING file in the root directory of this source tree).
* You may select, at your option, one of the above-listed licenses.
*/
/*-*************************************
* Dependencies
***************************************/
2020-12-18 14:46:27 +00:00
#define ZSTD_DEPS_NEED_MALLOC
#include "zstd_deps.h" /* ZSTD_malloc, ZSTD_calloc, ZSTD_free, ZSTD_memset */
2020-02-08 13:56:59 +00:00
#include "error_private.h"
#include "zstd_internal.h"
/*-****************************************
* Version
******************************************/
unsigned ZSTD_versionNumber(void) { return ZSTD_VERSION_NUMBER; }
const char* ZSTD_versionString(void) { return ZSTD_VERSION_STRING; }
/*-****************************************
* ZSTD Error Management
******************************************/
#undef ZSTD_isError /* defined within zstd_internal.h */
/*! ZSTD_isError() :
* tells if a return value is an error code
* symbol is required for external callers */
unsigned ZSTD_isError(size_t code) { return ERR_isError(code); }
/*! ZSTD_getErrorName() :
* provides error code string from function result (useful for debugging) */
const char* ZSTD_getErrorName(size_t code) { return ERR_getErrorName(code); }
/*! ZSTD_getError() :
* convert a `size_t` function result into a proper ZSTD_errorCode enum */
ZSTD_ErrorCode ZSTD_getErrorCode(size_t code) { return ERR_getErrorCode(code); }
/*! ZSTD_getErrorString() :
* provides error code string from enum */
const char* ZSTD_getErrorString(ZSTD_ErrorCode code) { return ERR_getErrorString(code); }
/*=**************************************************************
* Custom allocator
****************************************************************/
2020-12-18 14:46:27 +00:00
void* ZSTD_customMalloc(size_t size, ZSTD_customMem customMem)
2020-02-08 13:56:59 +00:00
{
if (customMem.customAlloc)
return customMem.customAlloc(customMem.opaque, size);
2020-12-18 14:46:27 +00:00
return ZSTD_malloc(size);
2020-02-08 13:56:59 +00:00
}
2020-12-18 14:46:27 +00:00
void* ZSTD_customCalloc(size_t size, ZSTD_customMem customMem)
2020-02-08 13:56:59 +00:00
{
if (customMem.customAlloc) {
/* calloc implemented as malloc+memset;
* not as efficient as calloc, but next best guess for custom malloc */
void* const ptr = customMem.customAlloc(customMem.opaque, size);
2020-12-18 14:46:27 +00:00
ZSTD_memset(ptr, 0, size);
2020-02-08 13:56:59 +00:00
return ptr;
}
2020-12-18 14:46:27 +00:00
return ZSTD_calloc(1, size);
2020-02-08 13:56:59 +00:00
}
2020-12-18 14:46:27 +00:00
void ZSTD_customFree(void* ptr, ZSTD_customMem customMem)
2020-02-08 13:56:59 +00:00
{
if (ptr!=NULL) {
if (customMem.customFree)
customMem.customFree(customMem.opaque, ptr);
else
2020-12-18 14:46:27 +00:00
ZSTD_free(ptr);
2020-02-08 13:56:59 +00:00
}
}