godot/thirdparty/zstd/common/zstd_common.c

84 lines
2.6 KiB
C
Raw Normal View History

/*
2020-09-18 19:38:36 +00:00
* Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
* 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).
2017-10-26 20:41:47 +00:00
* You may select, at your option, one of the above-listed licenses.
*/
/*-*************************************
* Dependencies
***************************************/
2017-07-22 21:46:05 +00:00
#include <stdlib.h> /* malloc, calloc, free */
#include <string.h> /* memset */
#include "error_private.h"
2017-10-26 20:41:47 +00:00
#include "zstd_internal.h"
/*-****************************************
* Version
******************************************/
2017-07-22 21:46:05 +00:00
unsigned ZSTD_versionNumber(void) { return ZSTD_VERSION_NUMBER; }
const char* ZSTD_versionString(void) { return ZSTD_VERSION_STRING; }
/*-****************************************
* ZSTD Error Management
******************************************/
2019-01-04 00:30:03 +00:00
#undef ZSTD_isError /* defined within zstd_internal.h */
/*! ZSTD_isError() :
2019-01-04 00:30:03 +00:00
* 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() :
2018-01-13 12:50:59 +00:00
* provides error code string from function result (useful for debugging) */
const char* ZSTD_getErrorName(size_t code) { return ERR_getErrorName(code); }
/*! ZSTD_getError() :
2018-01-13 12:50:59 +00:00
* 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() :
2018-01-13 12:50:59 +00:00
* provides error code string from enum */
const char* ZSTD_getErrorString(ZSTD_ErrorCode code) { return ERR_getErrorString(code); }
2018-01-13 12:50:59 +00:00
/*=**************************************************************
* Custom allocator
****************************************************************/
2017-07-22 21:46:05 +00:00
void* ZSTD_malloc(size_t size, ZSTD_customMem customMem)
{
2017-07-22 21:46:05 +00:00
if (customMem.customAlloc)
return customMem.customAlloc(customMem.opaque, size);
return malloc(size);
}
2017-07-22 21:46:05 +00:00
void* ZSTD_calloc(size_t size, ZSTD_customMem customMem)
{
2017-07-22 21:46:05 +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);
memset(ptr, 0, size);
return ptr;
}
return calloc(1, size);
}
void ZSTD_free(void* ptr, ZSTD_customMem customMem)
{
2017-07-22 21:46:05 +00:00
if (ptr!=NULL) {
if (customMem.customFree)
customMem.customFree(customMem.opaque, ptr);
else
free(ptr);
}
}