1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-only 2 /* 3 * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc. 4 * All rights reserved. 5 * 6 * This source code is licensed under both the BSD-style license (found in the 7 * LICENSE file in the root directory of this source tree) and the GPLv2 (found 8 * in the COPYING file in the root directory of this source tree). 9 * You may select, at your option, one of the above-listed licenses. 10 */ 11 12 13 14 /*-************************************* 15 * Dependencies 16 ***************************************/ 17 #include <stdlib.h> /* malloc, calloc, free */ 18 #include <string.h> /* 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 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 40 /*! ZSTD_getErrorName() : 41 * provides error code string from function result (useful for debugging) */ 42 const char* ZSTD_getErrorName(size_t code) { return ERR_getErrorName(code); } 43 44 /*! ZSTD_getError() : 45 * convert a `size_t` function result into a proper ZSTD_errorCode enum */ 46 ZSTD_ErrorCode ZSTD_getErrorCode(size_t code) { return ERR_getErrorCode(code); } 47 48 /*! ZSTD_getErrorString() : 49 * provides error code string from enum */ 50 const char* ZSTD_getErrorString(ZSTD_ErrorCode code) { return ERR_getErrorString(code); } 51 52 53 54 /*=************************************************************** 55 * Custom allocator 56 ****************************************************************/ 57 void* ZSTD_malloc(size_t size, ZSTD_customMem customMem) 58 { 59 if (customMem.customAlloc) 60 return customMem.customAlloc(customMem.opaque, size); 61 return malloc(size); 62 } 63 64 void* ZSTD_calloc(size_t size, ZSTD_customMem customMem) 65 { 66 if (customMem.customAlloc) { 67 /* calloc implemented as malloc+memset; 68 * not as efficient as calloc, but next best guess for custom malloc */ 69 void* const ptr = customMem.customAlloc(customMem.opaque, size); 70 memset(ptr, 0, size); 71 return ptr; 72 } 73 return calloc(1, size); 74 } 75 76 void ZSTD_free(void* ptr, ZSTD_customMem customMem) 77 { 78 if (ptr!=NULL) { 79 if (customMem.customFree) 80 customMem.customFree(customMem.opaque, ptr); 81 else 82 free(ptr); 83 } 84 } 85