1 /** |
1 /* |
2 * Copyright (c) 2016-present, Yann Collet, Facebook, Inc. |
2 * Copyright (c) 2016-present, Yann Collet, Facebook, Inc. |
3 * All rights reserved. |
3 * All rights reserved. |
4 * |
4 * |
5 * This source code is licensed under the BSD-style license found in the |
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. An additional grant |
6 * LICENSE file in the root directory of this source tree) and the GPLv2 (found |
7 * of patent rights can be found in the PATENTS file in the same directory. |
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. |
8 */ |
9 */ |
9 |
10 |
10 |
11 |
11 |
12 |
12 /*-************************************* |
13 /*-************************************* |
13 * Dependencies |
14 * Dependencies |
14 ***************************************/ |
15 ***************************************/ |
15 #include <stdlib.h> /* malloc */ |
16 #include <stdlib.h> /* malloc, calloc, free */ |
|
17 #include <string.h> /* memset */ |
16 #include "error_private.h" |
18 #include "error_private.h" |
17 #define ZSTD_STATIC_LINKING_ONLY |
19 #include "zstd_internal.h" |
18 #include "zstd.h" /* declaration of ZSTD_isError, ZSTD_getErrorName, ZSTD_getErrorCode, ZSTD_getErrorString, ZSTD_versionNumber */ |
|
19 |
20 |
20 |
21 |
21 /*-**************************************** |
22 /*-**************************************** |
22 * Version |
23 * Version |
23 ******************************************/ |
24 ******************************************/ |
24 unsigned ZSTD_versionNumber (void) { return ZSTD_VERSION_NUMBER; } |
25 unsigned ZSTD_versionNumber(void) { return ZSTD_VERSION_NUMBER; } |
|
26 |
|
27 const char* ZSTD_versionString(void) { return ZSTD_VERSION_STRING; } |
25 |
28 |
26 |
29 |
27 /*-**************************************** |
30 /*-**************************************** |
28 * ZSTD Error Management |
31 * ZSTD Error Management |
29 ******************************************/ |
32 ******************************************/ |
30 /*! ZSTD_isError() : |
33 /*! ZSTD_isError() : |
31 * tells if a return value is an error code */ |
34 * tells if a return value is an error code */ |
32 unsigned ZSTD_isError(size_t code) { return ERR_isError(code); } |
35 unsigned ZSTD_isError(size_t code) { return ERR_isError(code); } |
33 |
36 |
34 /*! ZSTD_getErrorName() : |
37 /*! ZSTD_getErrorName() : |
35 * provides error code string from function result (useful for debugging) */ |
38 * provides error code string from function result (useful for debugging) */ |
36 const char* ZSTD_getErrorName(size_t code) { return ERR_getErrorName(code); } |
39 const char* ZSTD_getErrorName(size_t code) { return ERR_getErrorName(code); } |
37 |
40 |
38 /*! ZSTD_getError() : |
41 /*! ZSTD_getError() : |
39 * convert a `size_t` function result into a proper ZSTD_errorCode enum */ |
42 * convert a `size_t` function result into a proper ZSTD_errorCode enum */ |
40 ZSTD_ErrorCode ZSTD_getErrorCode(size_t code) { return ERR_getErrorCode(code); } |
43 ZSTD_ErrorCode ZSTD_getErrorCode(size_t code) { return ERR_getErrorCode(code); } |
41 |
44 |
42 /*! ZSTD_getErrorString() : |
45 /*! ZSTD_getErrorString() : |
43 * provides error code string from enum */ |
46 * provides error code string from enum */ |
44 const char* ZSTD_getErrorString(ZSTD_ErrorCode code) { return ERR_getErrorName(code); } |
47 const char* ZSTD_getErrorString(ZSTD_ErrorCode code) { return ERR_getErrorString(code); } |
|
48 |
|
49 /*! g_debuglog_enable : |
|
50 * turn on/off debug traces (global switch) */ |
|
51 #if defined(ZSTD_DEBUG) && (ZSTD_DEBUG >= 2) |
|
52 int g_debuglog_enable = 1; |
|
53 #endif |
45 |
54 |
46 |
55 |
47 /*=************************************************************** |
56 /*=************************************************************** |
48 * Custom allocator |
57 * Custom allocator |
49 ****************************************************************/ |
58 ****************************************************************/ |
50 /* default uses stdlib */ |
59 void* ZSTD_malloc(size_t size, ZSTD_customMem customMem) |
51 void* ZSTD_defaultAllocFunction(void* opaque, size_t size) |
|
52 { |
60 { |
53 void* address = malloc(size); |
61 if (customMem.customAlloc) |
54 (void)opaque; |
62 return customMem.customAlloc(customMem.opaque, size); |
55 return address; |
63 return malloc(size); |
56 } |
64 } |
57 |
65 |
58 void ZSTD_defaultFreeFunction(void* opaque, void* address) |
66 void* ZSTD_calloc(size_t size, ZSTD_customMem customMem) |
59 { |
67 { |
60 (void)opaque; |
68 if (customMem.customAlloc) { |
61 free(address); |
69 /* calloc implemented as malloc+memset; |
62 } |
70 * not as efficient as calloc, but next best guess for custom malloc */ |
63 |
71 void* const ptr = customMem.customAlloc(customMem.opaque, size); |
64 void* ZSTD_malloc(size_t size, ZSTD_customMem customMem) |
72 memset(ptr, 0, size); |
65 { |
73 return ptr; |
66 return customMem.customAlloc(customMem.opaque, size); |
74 } |
|
75 return calloc(1, size); |
67 } |
76 } |
68 |
77 |
69 void ZSTD_free(void* ptr, ZSTD_customMem customMem) |
78 void ZSTD_free(void* ptr, ZSTD_customMem customMem) |
70 { |
79 { |
71 if (ptr!=NULL) |
80 if (ptr!=NULL) { |
72 customMem.customFree(customMem.opaque, ptr); |
81 if (customMem.customFree) |
|
82 customMem.customFree(customMem.opaque, ptr); |
|
83 else |
|
84 free(ptr); |
|
85 } |
73 } |
86 } |