blob: 17df097dcd5df18201e59bdd374f529b38380ae1 [file] [log] [blame]
Brandon Maierdbe88da2023-01-12 10:27:45 -06001/*
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
Brandon Maierdbe88da2023-01-12 10:27:45 -060011/*-*************************************
12* Dependencies
13***************************************/
14#define ZSTD_DEPS_NEED_MALLOC
15#include "zstd_deps.h" /* ZSTD_malloc, ZSTD_calloc, ZSTD_free, ZSTD_memset */
16#include "error_private.h"
17#include "zstd_internal.h"
18
Brandon Maierdbe88da2023-01-12 10:27:45 -060019/*-****************************************
20* Version
21******************************************/
22unsigned ZSTD_versionNumber(void) { return ZSTD_VERSION_NUMBER; }
23
24const char* ZSTD_versionString(void) { return ZSTD_VERSION_STRING; }
25
Brandon Maierdbe88da2023-01-12 10:27:45 -060026/*-****************************************
27* ZSTD Error Management
28******************************************/
29#undef ZSTD_isError /* defined within zstd_internal.h */
30/*! ZSTD_isError() :
31 * tells if a return value is an error code
32 * symbol is required for external callers */
33unsigned ZSTD_isError(size_t code) { return ERR_isError(code); }
34
35/*! ZSTD_getErrorName() :
36 * provides error code string from function result (useful for debugging) */
37const char* ZSTD_getErrorName(size_t code) { return ERR_getErrorName(code); }
38
39/*! ZSTD_getError() :
40 * convert a `size_t` function result into a proper ZSTD_errorCode enum */
41ZSTD_ErrorCode ZSTD_getErrorCode(size_t code) { return ERR_getErrorCode(code); }
42
43/*! ZSTD_getErrorString() :
44 * provides error code string from enum */
45const char* ZSTD_getErrorString(ZSTD_ErrorCode code) { return ERR_getErrorString(code); }
46
Brandon Maierdbe88da2023-01-12 10:27:45 -060047/*=**************************************************************
48* Custom allocator
49****************************************************************/
50void* ZSTD_customMalloc(size_t size, ZSTD_customMem customMem)
51{
52 if (customMem.customAlloc)
53 return customMem.customAlloc(customMem.opaque, size);
54 return ZSTD_malloc(size);
55}
56
57void* ZSTD_customCalloc(size_t size, ZSTD_customMem customMem)
58{
59 if (customMem.customAlloc) {
60 /* calloc implemented as malloc+memset;
61 * not as efficient as calloc, but next best guess for custom malloc */
62 void* const ptr = customMem.customAlloc(customMem.opaque, size);
63 ZSTD_memset(ptr, 0, size);
64 return ptr;
65 }
66 return ZSTD_calloc(1, size);
67}
68
69void ZSTD_customFree(void* ptr, ZSTD_customMem customMem)
70{
71 if (ptr!=NULL) {
72 if (customMem.customFree)
73 customMem.customFree(customMem.opaque, ptr);
74 else
75 ZSTD_free(ptr);
76 }
77}