xref: /openbmc/linux/lib/zstd/common/zstd_common.c (revision 519b58bb)
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 
13 /*-*************************************
14 *  Dependencies
15 ***************************************/
16 #include <linux/module.h>
17 #define ZSTD_DEPS_NEED_MALLOC
18 #include "zstd_deps.h"   /* ZSTD_malloc, ZSTD_calloc, ZSTD_free, ZSTD_memset */
19 #include "error_private.h"
20 #include "zstd_internal.h"
21 
22 
23 /*-****************************************
24 *  Version
25 ******************************************/
26 unsigned ZSTD_versionNumber(void) { return ZSTD_VERSION_NUMBER; }
27 
28 const char* ZSTD_versionString(void) { return ZSTD_VERSION_STRING; }
29 
30 
31 /*-****************************************
32 *  ZSTD Error Management
33 ******************************************/
34 #undef ZSTD_isError   /* defined within zstd_internal.h */
35 /*! ZSTD_isError() :
36  *  tells if a return value is an error code
37  *  symbol is required for external callers */
38 unsigned ZSTD_isError(size_t code) { return ERR_isError(code); }
39 EXPORT_SYMBOL_GPL(ZSTD_isError);
40 
41 /*! ZSTD_getErrorName() :
42  *  provides error code string from function result (useful for debugging) */
43 const char* ZSTD_getErrorName(size_t code) { return ERR_getErrorName(code); }
44 EXPORT_SYMBOL_GPL(ZSTD_getErrorName);
45 
46 /*! ZSTD_getError() :
47  *  convert a `size_t` function result into a proper ZSTD_errorCode enum */
48 ZSTD_ErrorCode ZSTD_getErrorCode(size_t code) { return ERR_getErrorCode(code); }
49 EXPORT_SYMBOL_GPL(ZSTD_getErrorCode);
50 
51 /*! ZSTD_getErrorString() :
52  *  provides error code string from enum */
53 const char* ZSTD_getErrorString(ZSTD_ErrorCode code) { return ERR_getErrorString(code); }
54 
55 
56 
57 /*=**************************************************************
58 *  Custom allocator
59 ****************************************************************/
60 void* ZSTD_customMalloc(size_t size, ZSTD_customMem customMem)
61 {
62     if (customMem.customAlloc)
63         return customMem.customAlloc(customMem.opaque, size);
64     return ZSTD_malloc(size);
65 }
66 EXPORT_SYMBOL_GPL(ZSTD_customMalloc);
67 
68 void* ZSTD_customCalloc(size_t size, ZSTD_customMem customMem)
69 {
70     if (customMem.customAlloc) {
71         /* calloc implemented as malloc+memset;
72          * not as efficient as calloc, but next best guess for custom malloc */
73         void* const ptr = customMem.customAlloc(customMem.opaque, size);
74         ZSTD_memset(ptr, 0, size);
75         return ptr;
76     }
77     return ZSTD_calloc(1, size);
78 }
79 EXPORT_SYMBOL_GPL(ZSTD_customCalloc);
80 
81 void ZSTD_customFree(void* ptr, ZSTD_customMem customMem)
82 {
83     if (ptr!=NULL) {
84         if (customMem.customFree)
85             customMem.customFree(customMem.opaque, ptr);
86         else
87             ZSTD_free(ptr);
88     }
89 }
90 EXPORT_SYMBOL_GPL(ZSTD_customFree);
91 
92 MODULE_LICENSE("Dual BSD/GPL");
93 MODULE_DESCRIPTION("Zstd Common");
94