]>
Commit | Line | Data |
---|---|---|
37b3c998 | 1 | /* |
7c673cae FG |
2 | * Copyright (c) 2016-present, Yann Collet, Facebook, Inc. |
3 | * All rights reserved. | |
4 | * | |
37b3c998 TL |
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. | |
7c673cae FG |
9 | */ |
10 | ||
11 | ||
12 | ||
13 | /*-************************************* | |
14 | * Dependencies | |
15 | ***************************************/ | |
37b3c998 TL |
16 | #include <stdlib.h> /* malloc, calloc, free */ |
17 | #include <string.h> /* memset */ | |
7c673cae | 18 | #include "error_private.h" |
37b3c998 | 19 | #include "zstd_internal.h" |
7c673cae FG |
20 | |
21 | ||
22 | /*-**************************************** | |
23 | * Version | |
24 | ******************************************/ | |
37b3c998 TL |
25 | unsigned ZSTD_versionNumber(void) { return ZSTD_VERSION_NUMBER; } |
26 | ||
27 | const char* ZSTD_versionString(void) { return ZSTD_VERSION_STRING; } | |
7c673cae FG |
28 | |
29 | ||
30 | /*-**************************************** | |
31 | * ZSTD Error Management | |
32 | ******************************************/ | |
33 | /*! ZSTD_isError() : | |
34 | * tells if a return value is an error code */ | |
35 | unsigned ZSTD_isError(size_t code) { return ERR_isError(code); } | |
36 | ||
37 | /*! ZSTD_getErrorName() : | |
38 | * provides error code string from function result (useful for debugging) */ | |
39 | const char* ZSTD_getErrorName(size_t code) { return ERR_getErrorName(code); } | |
40 | ||
41 | /*! ZSTD_getError() : | |
42 | * convert a `size_t` function result into a proper ZSTD_errorCode enum */ | |
43 | ZSTD_ErrorCode ZSTD_getErrorCode(size_t code) { return ERR_getErrorCode(code); } | |
44 | ||
45 | /*! ZSTD_getErrorString() : | |
46 | * provides error code string from enum */ | |
37b3c998 | 47 | const char* ZSTD_getErrorString(ZSTD_ErrorCode code) { return ERR_getErrorString(code); } |
7c673cae FG |
48 | |
49 | ||
50 | /*=************************************************************** | |
51 | * Custom allocator | |
52 | ****************************************************************/ | |
37b3c998 | 53 | void* ZSTD_malloc(size_t size, ZSTD_customMem customMem) |
7c673cae | 54 | { |
37b3c998 TL |
55 | if (customMem.customAlloc) |
56 | return customMem.customAlloc(customMem.opaque, size); | |
57 | return malloc(size); | |
7c673cae FG |
58 | } |
59 | ||
37b3c998 | 60 | void* ZSTD_calloc(size_t size, ZSTD_customMem customMem) |
7c673cae | 61 | { |
37b3c998 TL |
62 | if (customMem.customAlloc) { |
63 | /* calloc implemented as malloc+memset; | |
64 | * not as efficient as calloc, but next best guess for custom malloc */ | |
65 | void* const ptr = customMem.customAlloc(customMem.opaque, size); | |
66 | memset(ptr, 0, size); | |
67 | return ptr; | |
68 | } | |
69 | return calloc(1, size); | |
7c673cae FG |
70 | } |
71 | ||
72 | void ZSTD_free(void* ptr, ZSTD_customMem customMem) | |
73 | { | |
37b3c998 TL |
74 | if (ptr!=NULL) { |
75 | if (customMem.customFree) | |
76 | customMem.customFree(customMem.opaque, ptr); | |
77 | else | |
78 | free(ptr); | |
79 | } | |
7c673cae | 80 | } |