2017-08-18 16:52:05 -07:00
|
|
|
/*
|
2016-08-30 10:04:33 -07:00
|
|
|
* Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
2017-08-18 16:52:05 -07:00
|
|
|
* 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-09-08 00:09:23 -07:00
|
|
|
* You may select, at your option, one of the above-listed licenses.
|
2016-08-30 10:04:33 -07:00
|
|
|
*/
|
2016-04-22 08:14:25 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
2016-05-12 06:55:26 -07:00
|
|
|
/*-*************************************
|
2016-04-22 08:14:25 -07:00
|
|
|
* Dependencies
|
|
|
|
***************************************/
|
2017-05-30 16:12:06 -07:00
|
|
|
#include <stdlib.h> /* malloc, calloc, free */
|
|
|
|
#include <string.h> /* memset */
|
2016-04-22 08:14:25 -07:00
|
|
|
#include "error_private.h"
|
2017-08-24 18:09:50 -07:00
|
|
|
#include "zstd_internal.h"
|
2016-05-12 06:55:26 -07:00
|
|
|
|
|
|
|
|
|
|
|
/*-****************************************
|
|
|
|
* Version
|
|
|
|
******************************************/
|
2017-06-02 14:24:58 -07:00
|
|
|
unsigned ZSTD_versionNumber(void) { return ZSTD_VERSION_NUMBER; }
|
2016-04-22 08:14:25 -07:00
|
|
|
|
2017-05-16 16:12:23 -07:00
|
|
|
const char* ZSTD_versionString(void) { return ZSTD_VERSION_STRING; }
|
|
|
|
|
2016-04-22 08:14:25 -07:00
|
|
|
|
|
|
|
/*-****************************************
|
|
|
|
* ZSTD Error Management
|
|
|
|
******************************************/
|
|
|
|
/*! ZSTD_isError() :
|
2017-11-13 02:19:36 -08:00
|
|
|
* tells if a return value is an error code */
|
2016-04-22 08:14:25 -07:00
|
|
|
unsigned ZSTD_isError(size_t code) { return ERR_isError(code); }
|
|
|
|
|
2016-05-12 06:55:26 -07:00
|
|
|
/*! ZSTD_getErrorName() :
|
2017-11-13 02:19:36 -08:00
|
|
|
* provides error code string from function result (useful for debugging) */
|
2016-05-12 06:55:26 -07:00
|
|
|
const char* ZSTD_getErrorName(size_t code) { return ERR_getErrorName(code); }
|
|
|
|
|
2016-04-22 08:14:25 -07:00
|
|
|
/*! ZSTD_getError() :
|
2017-11-13 02:19:36 -08:00
|
|
|
* convert a `size_t` function result into a proper ZSTD_errorCode enum */
|
2016-05-10 08:47:11 -07:00
|
|
|
ZSTD_ErrorCode ZSTD_getErrorCode(size_t code) { return ERR_getErrorCode(code); }
|
2016-04-22 08:14:25 -07:00
|
|
|
|
2016-05-12 06:55:26 -07:00
|
|
|
/*! ZSTD_getErrorString() :
|
2017-11-13 02:19:36 -08:00
|
|
|
* provides error code string from enum */
|
2017-02-08 15:31:47 -08:00
|
|
|
const char* ZSTD_getErrorString(ZSTD_ErrorCode code) { return ERR_getErrorString(code); }
|
2016-05-10 08:47:11 -07:00
|
|
|
|
2017-11-13 02:19:36 -08:00
|
|
|
/*! g_debuglog_enable :
|
|
|
|
* turn on/off debug traces (global switch) */
|
|
|
|
#if defined(ZSTD_DEBUG) && (ZSTD_DEBUG >= 2)
|
2018-05-29 14:07:25 -07:00
|
|
|
int g_debuglevel = ZSTD_DEBUG;
|
2017-11-13 02:19:36 -08:00
|
|
|
#endif
|
|
|
|
|
2016-06-02 04:04:18 -07:00
|
|
|
|
2016-08-28 21:05:43 -07:00
|
|
|
/*=**************************************************************
|
|
|
|
* Custom allocator
|
|
|
|
****************************************************************/
|
|
|
|
void* ZSTD_malloc(size_t size, ZSTD_customMem customMem)
|
|
|
|
{
|
2017-05-30 16:12:06 -07:00
|
|
|
if (customMem.customAlloc)
|
|
|
|
return customMem.customAlloc(customMem.opaque, size);
|
|
|
|
return malloc(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void* ZSTD_calloc(size_t size, ZSTD_customMem customMem)
|
|
|
|
{
|
|
|
|
if (customMem.customAlloc) {
|
|
|
|
/* calloc implemented as malloc+memset;
|
2017-06-02 14:01:21 -07:00
|
|
|
* not as efficient as calloc, but next best guess for custom malloc */
|
2017-05-30 16:12:06 -07:00
|
|
|
void* const ptr = customMem.customAlloc(customMem.opaque, size);
|
|
|
|
memset(ptr, 0, size);
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
return calloc(1, size);
|
2016-08-28 21:05:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void ZSTD_free(void* ptr, ZSTD_customMem customMem)
|
|
|
|
{
|
2017-05-30 16:12:06 -07:00
|
|
|
if (ptr!=NULL) {
|
|
|
|
if (customMem.customFree)
|
|
|
|
customMem.customFree(customMem.opaque, ptr);
|
|
|
|
else
|
|
|
|
free(ptr);
|
|
|
|
}
|
2016-08-28 21:05:43 -07:00
|
|
|
}
|