]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/zstd/lib/common/zstd_common.c
Import zstandard 1.1.4 in base
[FreeBSD/FreeBSD.git] / contrib / zstd / lib / common / zstd_common.c
1 /**
2  * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree. An additional grant
7  * of patent rights can be found in the PATENTS file in the same directory.
8  */
9
10
11
12 /*-*************************************
13 *  Dependencies
14 ***************************************/
15 #include <stdlib.h>         /* malloc */
16 #include "error_private.h"
17 #define ZSTD_STATIC_LINKING_ONLY
18 #include "zstd.h"           /* declaration of ZSTD_isError, ZSTD_getErrorName, ZSTD_getErrorCode, ZSTD_getErrorString, ZSTD_versionNumber */
19
20
21 /*-****************************************
22 *  Version
23 ******************************************/
24 unsigned ZSTD_versionNumber (void) { return ZSTD_VERSION_NUMBER; }
25
26
27 /*-****************************************
28 *  ZSTD Error Management
29 ******************************************/
30 /*! ZSTD_isError() :
31 *   tells if a return value is an error code */
32 unsigned ZSTD_isError(size_t code) { return ERR_isError(code); }
33
34 /*! ZSTD_getErrorName() :
35 *   provides error code string from function result (useful for debugging) */
36 const char* ZSTD_getErrorName(size_t code) { return ERR_getErrorName(code); }
37
38 /*! ZSTD_getError() :
39 *   convert a `size_t` function result into a proper ZSTD_errorCode enum */
40 ZSTD_ErrorCode ZSTD_getErrorCode(size_t code) { return ERR_getErrorCode(code); }
41
42 /*! ZSTD_getErrorString() :
43 *   provides error code string from enum */
44 const char* ZSTD_getErrorString(ZSTD_ErrorCode code) { return ERR_getErrorString(code); }
45
46
47 /*=**************************************************************
48 *  Custom allocator
49 ****************************************************************/
50 /* default uses stdlib */
51 void* ZSTD_defaultAllocFunction(void* opaque, size_t size)
52 {
53     void* address = malloc(size);
54     (void)opaque;
55     return address;
56 }
57
58 void ZSTD_defaultFreeFunction(void* opaque, void* address)
59 {
60     (void)opaque;
61     free(address);
62 }
63
64 void* ZSTD_malloc(size_t size, ZSTD_customMem customMem)
65 {
66     return customMem.customAlloc(customMem.opaque, size);
67 }
68
69 void ZSTD_free(void* ptr, ZSTD_customMem customMem)
70 {
71     if (ptr!=NULL)
72         customMem.customFree(customMem.opaque, ptr);
73 }