zstd_common.c
86 lines
| 2.7 KiB
| text/x-c
|
CLexer
Gregory Szorc
|
r37513 | /* | ||
Gregory Szorc
|
r30434 | * Copyright (c) 2016-present, Yann Collet, Facebook, Inc. | ||
* All rights reserved. | ||||
* | ||||
Gregory Szorc
|
r37513 | * 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. | ||||
Gregory Szorc
|
r30434 | */ | ||
/*-************************************* | ||||
* Dependencies | ||||
***************************************/ | ||||
Gregory Szorc
|
r37513 | #include <stdlib.h> /* malloc, calloc, free */ | ||
#include <string.h> /* memset */ | ||||
Gregory Szorc
|
r30434 | #include "error_private.h" | ||
Gregory Szorc
|
r37513 | #include "zstd_internal.h" | ||
Gregory Szorc
|
r30434 | |||
/*-**************************************** | ||||
* Version | ||||
******************************************/ | ||||
Gregory Szorc
|
r37513 | unsigned ZSTD_versionNumber(void) { return ZSTD_VERSION_NUMBER; } | ||
const char* ZSTD_versionString(void) { return ZSTD_VERSION_STRING; } | ||||
Gregory Szorc
|
r30434 | |||
/*-**************************************** | ||||
* ZSTD Error Management | ||||
******************************************/ | ||||
/*! ZSTD_isError() : | ||||
Gregory Szorc
|
r37513 | * tells if a return value is an error code */ | ||
Gregory Szorc
|
r30434 | unsigned ZSTD_isError(size_t code) { return ERR_isError(code); } | ||
/*! ZSTD_getErrorName() : | ||||
Gregory Szorc
|
r37513 | * provides error code string from function result (useful for debugging) */ | ||
Gregory Szorc
|
r30434 | const char* ZSTD_getErrorName(size_t code) { return ERR_getErrorName(code); } | ||
/*! ZSTD_getError() : | ||||
Gregory Szorc
|
r37513 | * convert a `size_t` function result into a proper ZSTD_errorCode enum */ | ||
Gregory Szorc
|
r30434 | ZSTD_ErrorCode ZSTD_getErrorCode(size_t code) { return ERR_getErrorCode(code); } | ||
/*! ZSTD_getErrorString() : | ||||
Gregory Szorc
|
r37513 | * provides error code string from enum */ | ||
const char* ZSTD_getErrorString(ZSTD_ErrorCode code) { return ERR_getErrorString(code); } | ||||
/*! g_debuglog_enable : | ||||
* turn on/off debug traces (global switch) */ | ||||
#if defined(ZSTD_DEBUG) && (ZSTD_DEBUG >= 2) | ||||
int g_debuglog_enable = 1; | ||||
#endif | ||||
Gregory Szorc
|
r30434 | |||
/*=************************************************************** | ||||
* Custom allocator | ||||
****************************************************************/ | ||||
Gregory Szorc
|
r37513 | void* ZSTD_malloc(size_t size, ZSTD_customMem customMem) | ||
Gregory Szorc
|
r30434 | { | ||
Gregory Szorc
|
r37513 | if (customMem.customAlloc) | ||
return customMem.customAlloc(customMem.opaque, size); | ||||
return malloc(size); | ||||
Gregory Szorc
|
r30434 | } | ||
Gregory Szorc
|
r37513 | void* ZSTD_calloc(size_t size, ZSTD_customMem customMem) | ||
Gregory Szorc
|
r30434 | { | ||
Gregory Szorc
|
r37513 | 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); | ||||
Gregory Szorc
|
r30434 | } | ||
void ZSTD_free(void* ptr, ZSTD_customMem customMem) | ||||
{ | ||||
Gregory Szorc
|
r37513 | if (ptr!=NULL) { | ||
if (customMem.customFree) | ||||
customMem.customFree(customMem.opaque, ptr); | ||||
else | ||||
free(ptr); | ||||
} | ||||
Gregory Szorc
|
r30434 | } | ||