blob: 667f4a27fc695b46ceca6dbcdb293c30917dba0a [file] [log] [blame]
Scott Bakere7144bc2019-10-01 14:16:47 -07001/*
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******************************************/
Scott Bakerf579f132019-10-24 14:31:41 -070033#undef ZSTD_isError /* defined within zstd_internal.h */
Scott Bakere7144bc2019-10-01 14:16:47 -070034/*! ZSTD_isError() :
Scott Bakerf579f132019-10-24 14:31:41 -070035 * tells if a return value is an error code
36 * symbol is required for external callers */
Scott Bakere7144bc2019-10-01 14:16:47 -070037unsigned ZSTD_isError(size_t code) { return ERR_isError(code); }
38
39/*! ZSTD_getErrorName() :
40 * provides error code string from function result (useful for debugging) */
41const char* ZSTD_getErrorName(size_t code) { return ERR_getErrorName(code); }
42
43/*! ZSTD_getError() :
44 * convert a `size_t` function result into a proper ZSTD_errorCode enum */
45ZSTD_ErrorCode ZSTD_getErrorCode(size_t code) { return ERR_getErrorCode(code); }
46
47/*! ZSTD_getErrorString() :
48 * provides error code string from enum */
49const char* ZSTD_getErrorString(ZSTD_ErrorCode code) { return ERR_getErrorString(code); }
50
Scott Bakere7144bc2019-10-01 14:16:47 -070051
52
53/*=**************************************************************
54* Custom allocator
55****************************************************************/
56void* ZSTD_malloc(size_t size, ZSTD_customMem customMem)
57{
58 if (customMem.customAlloc)
59 return customMem.customAlloc(customMem.opaque, size);
60 return malloc(size);
61}
62
63void* ZSTD_calloc(size_t size, ZSTD_customMem customMem)
64{
65 if (customMem.customAlloc) {
66 /* calloc implemented as malloc+memset;
67 * not as efficient as calloc, but next best guess for custom malloc */
68 void* const ptr = customMem.customAlloc(customMem.opaque, size);
69 memset(ptr, 0, size);
70 return ptr;
71 }
72 return calloc(1, size);
73}
74
75void ZSTD_free(void* ptr, ZSTD_customMem customMem)
76{
77 if (ptr!=NULL) {
78 if (customMem.customFree)
79 customMem.customFree(customMem.opaque, ptr);
80 else
81 free(ptr);
82 }
83}