10c16b537SWarner Losh /* 237f1f268SConrad Meyer * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc. 30c16b537SWarner Losh * All rights reserved. 40c16b537SWarner Losh * 50c16b537SWarner Losh * This source code is licensed under both the BSD-style license (found in the 60c16b537SWarner Losh * LICENSE file in the root directory of this source tree) and the GPLv2 (found 70c16b537SWarner Losh * in the COPYING file in the root directory of this source tree). 80c16b537SWarner Losh * You may select, at your option, one of the above-listed licenses. 90c16b537SWarner Losh */ 100c16b537SWarner Losh 110c16b537SWarner Losh 120c16b537SWarner Losh 130c16b537SWarner Losh /*-************************************* 140c16b537SWarner Losh * Dependencies 150c16b537SWarner Losh ***************************************/ 16*f7cd7fe5SConrad Meyer #define ZSTD_DEPS_NEED_MALLOC 17*f7cd7fe5SConrad Meyer #include "zstd_deps.h" /* ZSTD_malloc, ZSTD_calloc, ZSTD_free, ZSTD_memset */ 180c16b537SWarner Losh #include "error_private.h" 190c16b537SWarner Losh #include "zstd_internal.h" 200c16b537SWarner Losh 210c16b537SWarner Losh 220c16b537SWarner Losh /*-**************************************** 230c16b537SWarner Losh * Version 240c16b537SWarner Losh ******************************************/ 250c16b537SWarner Losh unsigned ZSTD_versionNumber(void) { return ZSTD_VERSION_NUMBER; } 260c16b537SWarner Losh 270c16b537SWarner Losh const char* ZSTD_versionString(void) { return ZSTD_VERSION_STRING; } 280c16b537SWarner Losh 290c16b537SWarner Losh 300c16b537SWarner Losh /*-**************************************** 310c16b537SWarner Losh * ZSTD Error Management 320c16b537SWarner Losh ******************************************/ 33a0483764SConrad Meyer #undef ZSTD_isError /* defined within zstd_internal.h */ 340c16b537SWarner Losh /*! ZSTD_isError() : 35a0483764SConrad Meyer * tells if a return value is an error code 36a0483764SConrad Meyer * symbol is required for external callers */ 370c16b537SWarner Losh unsigned ZSTD_isError(size_t code) { return ERR_isError(code); } 380c16b537SWarner Losh 390c16b537SWarner Losh /*! ZSTD_getErrorName() : 400c16b537SWarner Losh * provides error code string from function result (useful for debugging) */ 410c16b537SWarner Losh const char* ZSTD_getErrorName(size_t code) { return ERR_getErrorName(code); } 420c16b537SWarner Losh 430c16b537SWarner Losh /*! ZSTD_getError() : 440c16b537SWarner Losh * convert a `size_t` function result into a proper ZSTD_errorCode enum */ 450c16b537SWarner Losh ZSTD_ErrorCode ZSTD_getErrorCode(size_t code) { return ERR_getErrorCode(code); } 460c16b537SWarner Losh 470c16b537SWarner Losh /*! ZSTD_getErrorString() : 480c16b537SWarner Losh * provides error code string from enum */ 490c16b537SWarner Losh const char* ZSTD_getErrorString(ZSTD_ErrorCode code) { return ERR_getErrorString(code); } 50052d3c12SConrad Meyer 510c16b537SWarner Losh 520c16b537SWarner Losh 530c16b537SWarner Losh /*=************************************************************** 540c16b537SWarner Losh * Custom allocator 550c16b537SWarner Losh ****************************************************************/ 56*f7cd7fe5SConrad Meyer void* ZSTD_customMalloc(size_t size, ZSTD_customMem customMem) 570c16b537SWarner Losh { 580c16b537SWarner Losh if (customMem.customAlloc) 590c16b537SWarner Losh return customMem.customAlloc(customMem.opaque, size); 60*f7cd7fe5SConrad Meyer return ZSTD_malloc(size); 610c16b537SWarner Losh } 620c16b537SWarner Losh 63*f7cd7fe5SConrad Meyer void* ZSTD_customCalloc(size_t size, ZSTD_customMem customMem) 640c16b537SWarner Losh { 650c16b537SWarner Losh if (customMem.customAlloc) { 660c16b537SWarner Losh /* calloc implemented as malloc+memset; 670c16b537SWarner Losh * not as efficient as calloc, but next best guess for custom malloc */ 680c16b537SWarner Losh void* const ptr = customMem.customAlloc(customMem.opaque, size); 69*f7cd7fe5SConrad Meyer ZSTD_memset(ptr, 0, size); 700c16b537SWarner Losh return ptr; 710c16b537SWarner Losh } 72*f7cd7fe5SConrad Meyer return ZSTD_calloc(1, size); 730c16b537SWarner Losh } 740c16b537SWarner Losh 75*f7cd7fe5SConrad Meyer void ZSTD_customFree(void* ptr, ZSTD_customMem customMem) 760c16b537SWarner Losh { 770c16b537SWarner Losh if (ptr!=NULL) { 780c16b537SWarner Losh if (customMem.customFree) 790c16b537SWarner Losh customMem.customFree(customMem.opaque, ptr); 800c16b537SWarner Losh else 81*f7cd7fe5SConrad Meyer ZSTD_free(ptr); 820c16b537SWarner Losh } 830c16b537SWarner Losh } 84