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