1 /*
2 * Copyright (c) Yann Collet, Facebook, Inc.
3 * All rights reserved.
4 *
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.
9 */
10
11 /*-*************************************
12 * Dependencies
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"
18
19 /*-****************************************
20 * Version
21 ******************************************/
ZSTD_versionNumber(void)22 unsigned ZSTD_versionNumber(void) { return ZSTD_VERSION_NUMBER; }
23
ZSTD_versionString(void)24 const char* ZSTD_versionString(void) { return ZSTD_VERSION_STRING; }
25
26 /*-****************************************
27 * ZSTD Error Management
28 ******************************************/
29 #undef ZSTD_isError /* defined within zstd_internal.h */
30 /*! ZSTD_isError() :
31 * tells if a return value is an error code
32 * symbol is required for external callers */
ZSTD_isError(size_t code)33 unsigned ZSTD_isError(size_t code) { return ERR_isError(code); }
34
35 /*! ZSTD_getErrorName() :
36 * provides error code string from function result (useful for debugging) */
ZSTD_getErrorName(size_t code)37 const char* ZSTD_getErrorName(size_t code) { return ERR_getErrorName(code); }
38
39 /*! ZSTD_getError() :
40 * convert a `size_t` function result into a proper ZSTD_errorCode enum */
ZSTD_getErrorCode(size_t code)41 ZSTD_ErrorCode ZSTD_getErrorCode(size_t code) { return ERR_getErrorCode(code); }
42
43 /*! ZSTD_getErrorString() :
44 * provides error code string from enum */
ZSTD_getErrorString(ZSTD_ErrorCode code)45 const char* ZSTD_getErrorString(ZSTD_ErrorCode code) { return ERR_getErrorString(code); }
46
47 /*=**************************************************************
48 * Custom allocator
49 ****************************************************************/
ZSTD_customMalloc(size_t size,ZSTD_customMem customMem)50 void* ZSTD_customMalloc(size_t size, ZSTD_customMem customMem)
51 {
52 if (customMem.customAlloc)
53 return customMem.customAlloc(customMem.opaque, size);
54 return ZSTD_malloc(size);
55 }
56
ZSTD_customCalloc(size_t size,ZSTD_customMem customMem)57 void* ZSTD_customCalloc(size_t size, ZSTD_customMem customMem)
58 {
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);
64 return ptr;
65 }
66 return ZSTD_calloc(1, size);
67 }
68
ZSTD_customFree(void * ptr,ZSTD_customMem customMem)69 void ZSTD_customFree(void* ptr, ZSTD_customMem customMem)
70 {
71 if (ptr!=NULL) {
72 if (customMem.customFree)
73 customMem.customFree(customMem.opaque, ptr);
74 else
75 ZSTD_free(ptr);
76 }
77 }
78