xref: /freebsd/sys/contrib/zstd/lib/common/zstd_common.c (revision 052d3c129019c2f03488f7cb7399580091f9a713)
10c16b537SWarner Losh /*
20c16b537SWarner Losh  * Copyright (c) 2016-present, 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 ***************************************/
160c16b537SWarner Losh #include <stdlib.h>      /* malloc, calloc, free */
170c16b537SWarner Losh #include <string.h>      /* 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 ******************************************/
330c16b537SWarner Losh /*! ZSTD_isError() :
340c16b537SWarner Losh  *  tells if a return value is an error code */
350c16b537SWarner Losh unsigned ZSTD_isError(size_t code) { return ERR_isError(code); }
360c16b537SWarner Losh 
370c16b537SWarner Losh /*! ZSTD_getErrorName() :
380c16b537SWarner Losh  *  provides error code string from function result (useful for debugging) */
390c16b537SWarner Losh const char* ZSTD_getErrorName(size_t code) { return ERR_getErrorName(code); }
400c16b537SWarner Losh 
410c16b537SWarner Losh /*! ZSTD_getError() :
420c16b537SWarner Losh  *  convert a `size_t` function result into a proper ZSTD_errorCode enum */
430c16b537SWarner Losh ZSTD_ErrorCode ZSTD_getErrorCode(size_t code) { return ERR_getErrorCode(code); }
440c16b537SWarner Losh 
450c16b537SWarner Losh /*! ZSTD_getErrorString() :
460c16b537SWarner Losh  *  provides error code string from enum */
470c16b537SWarner Losh const char* ZSTD_getErrorString(ZSTD_ErrorCode code) { return ERR_getErrorString(code); }
48*052d3c12SConrad Meyer 
49*052d3c12SConrad Meyer /*! g_debuglog_enable :
50*052d3c12SConrad Meyer  *  turn on/off debug traces (global switch) */
51*052d3c12SConrad Meyer #if defined(ZSTD_DEBUG) && (ZSTD_DEBUG >= 2)
52*052d3c12SConrad Meyer int g_debuglog_enable = 1;
53*052d3c12SConrad Meyer #endif
540c16b537SWarner Losh 
550c16b537SWarner Losh 
560c16b537SWarner Losh /*=**************************************************************
570c16b537SWarner Losh *  Custom allocator
580c16b537SWarner Losh ****************************************************************/
590c16b537SWarner Losh void* ZSTD_malloc(size_t size, ZSTD_customMem customMem)
600c16b537SWarner Losh {
610c16b537SWarner Losh     if (customMem.customAlloc)
620c16b537SWarner Losh         return customMem.customAlloc(customMem.opaque, size);
630c16b537SWarner Losh     return malloc(size);
640c16b537SWarner Losh }
650c16b537SWarner Losh 
660c16b537SWarner Losh void* ZSTD_calloc(size_t size, ZSTD_customMem customMem)
670c16b537SWarner Losh {
680c16b537SWarner Losh     if (customMem.customAlloc) {
690c16b537SWarner Losh         /* calloc implemented as malloc+memset;
700c16b537SWarner Losh          * not as efficient as calloc, but next best guess for custom malloc */
710c16b537SWarner Losh         void* const ptr = customMem.customAlloc(customMem.opaque, size);
720c16b537SWarner Losh         memset(ptr, 0, size);
730c16b537SWarner Losh         return ptr;
740c16b537SWarner Losh     }
750c16b537SWarner Losh     return calloc(1, size);
760c16b537SWarner Losh }
770c16b537SWarner Losh 
780c16b537SWarner Losh void ZSTD_free(void* ptr, ZSTD_customMem customMem)
790c16b537SWarner Losh {
800c16b537SWarner Losh     if (ptr!=NULL) {
810c16b537SWarner Losh         if (customMem.customFree)
820c16b537SWarner Losh             customMem.customFree(customMem.opaque, ptr);
830c16b537SWarner Losh         else
840c16b537SWarner Losh             free(ptr);
850c16b537SWarner Losh     }
860c16b537SWarner Losh }
87