1/*
2 * Copyright (c) 2016-present, 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 <stdlib.h> /* malloc, calloc, free */
17#include <string.h> /* memset */
18#include "error_private.h"
19#include "zstd_internal.h"
20
21
22/*-****************************************
23* Version
24******************************************/
25unsigned ZSTD_versionNumber(void) { return ZSTD_VERSION_NUMBER; }
26
27const char* ZSTD_versionString(void) { return ZSTD_VERSION_STRING; }
28
29
30/*-****************************************
31* ZSTD Error Management
32******************************************/
33/*! ZSTD_isError() :
34 * tells if a return value is an error code */
35unsigned ZSTD_isError(size_t code) { return ERR_isError(code); }
36
37/*! ZSTD_getErrorName() :
38 * provides error code string from function result (useful for debugging) */
39const 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 */
43ZSTD_ErrorCode ZSTD_getErrorCode(size_t code) { return ERR_getErrorCode(code); }
44
45/*! ZSTD_getErrorString() :
46 * provides error code string from enum */
47const 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)
52int g_debuglog_enable = 1;
53#endif
54
55
56/*=**************************************************************
57* Custom allocator
58****************************************************************/
59void* ZSTD_malloc(size_t size, ZSTD_customMem customMem)
60{
61 if (customMem.customAlloc)
62 return customMem.customAlloc(customMem.opaque, size);
63 return malloc(size);
64}
65
66void* ZSTD_calloc(size_t size, ZSTD_customMem customMem)
67{
68 if (customMem.customAlloc) {
69 /* calloc implemented as malloc+memset;
70 * not as efficient as calloc, but next best guess for custom malloc */
71 void* const ptr = customMem.customAlloc(customMem.opaque, size);
72 memset(ptr, 0, size);
73 return ptr;
74 }
75 return calloc(1, size);
76}
77
78void ZSTD_free(void* ptr, ZSTD_customMem customMem)
79{
80 if (ptr!=NULL) {
81 if (customMem.customFree)
82 customMem.customFree(customMem.opaque, ptr);
83 else
84 free(ptr);
85 }
86}
87