2 * Copyright (c) Yann Collet, Facebook, Inc.
5 * This source code is licensed under both the BSD-style license (found in the
6 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7 * in the COPYING file in the root directory of this source tree).
8 * You may select, at your option, one of the above-listed licenses.
11 /*-*************************************
13 ***************************************/
14 #define ZSTD_DEPS_NEED_MALLOC
15 #include "zstd_deps.h" /* ZSTD_malloc, ZSTD_calloc, ZSTD_free, ZSTD_memset */
16 #include "error_private.h"
17 #include "zstd_internal.h"
19 /*-****************************************
21 ******************************************/
22 unsigned ZSTD_versionNumber(void) { return ZSTD_VERSION_NUMBER; }
24 const char* ZSTD_versionString(void) { return ZSTD_VERSION_STRING; }
26 /*-****************************************
27 * ZSTD Error Management
28 ******************************************/
29 #undef ZSTD_isError /* defined within zstd_internal.h */
31 * tells if a return value is an error code
32 * symbol is required for external callers */
33 unsigned ZSTD_isError(size_t code) { return ERR_isError(code); }
35 /*! ZSTD_getErrorName() :
36 * provides error code string from function result (useful for debugging) */
37 const char* ZSTD_getErrorName(size_t code) { return ERR_getErrorName(code); }
40 * convert a `size_t` function result into a proper ZSTD_errorCode enum */
41 ZSTD_ErrorCode ZSTD_getErrorCode(size_t code) { return ERR_getErrorCode(code); }
43 /*! ZSTD_getErrorString() :
44 * provides error code string from enum */
45 const char* ZSTD_getErrorString(ZSTD_ErrorCode code) { return ERR_getErrorString(code); }
47 /*=**************************************************************
49 ****************************************************************/
50 void* ZSTD_customMalloc(size_t size, ZSTD_customMem customMem)
52 if (customMem.customAlloc)
53 return customMem.customAlloc(customMem.opaque, size);
54 return ZSTD_malloc(size);
57 void* ZSTD_customCalloc(size_t size, ZSTD_customMem customMem)
59 if (customMem.customAlloc) {
60 /* calloc implemented as malloc+memset;
61 * not as efficient as calloc, but next best guess for custom malloc */
62 void* const ptr = customMem.customAlloc(customMem.opaque, size);
63 ZSTD_memset(ptr, 0, size);
66 return ZSTD_calloc(1, size);
69 void ZSTD_customFree(void* ptr, ZSTD_customMem customMem)
72 if (customMem.customFree)
73 customMem.customFree(customMem.opaque, ptr);