Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright (c) Yann Collet, Facebook, Inc.
0003  * All rights reserved.
0004  *
0005  * This source code is licensed under both the BSD-style license (found in the
0006  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
0007  * in the COPYING file in the root directory of this source tree).
0008  * You may select, at your option, one of the above-listed licenses.
0009  */
0010 
0011 
0012 
0013 /*-*************************************
0014 *  Dependencies
0015 ***************************************/
0016 #define ZSTD_DEPS_NEED_MALLOC
0017 #include "zstd_deps.h"   /* ZSTD_malloc, ZSTD_calloc, ZSTD_free, ZSTD_memset */
0018 #include "error_private.h"
0019 #include "zstd_internal.h"
0020 
0021 
0022 /*-****************************************
0023 *  Version
0024 ******************************************/
0025 unsigned ZSTD_versionNumber(void) { return ZSTD_VERSION_NUMBER; }
0026 
0027 const char* ZSTD_versionString(void) { return ZSTD_VERSION_STRING; }
0028 
0029 
0030 /*-****************************************
0031 *  ZSTD Error Management
0032 ******************************************/
0033 #undef ZSTD_isError   /* defined within zstd_internal.h */
0034 /*! ZSTD_isError() :
0035  *  tells if a return value is an error code
0036  *  symbol is required for external callers */
0037 unsigned ZSTD_isError(size_t code) { return ERR_isError(code); }
0038 
0039 /*! ZSTD_getErrorName() :
0040  *  provides error code string from function result (useful for debugging) */
0041 const char* ZSTD_getErrorName(size_t code) { return ERR_getErrorName(code); }
0042 
0043 /*! ZSTD_getError() :
0044  *  convert a `size_t` function result into a proper ZSTD_errorCode enum */
0045 ZSTD_ErrorCode ZSTD_getErrorCode(size_t code) { return ERR_getErrorCode(code); }
0046 
0047 /*! ZSTD_getErrorString() :
0048  *  provides error code string from enum */
0049 const char* ZSTD_getErrorString(ZSTD_ErrorCode code) { return ERR_getErrorString(code); }
0050 
0051 
0052 
0053 /*=**************************************************************
0054 *  Custom allocator
0055 ****************************************************************/
0056 void* ZSTD_customMalloc(size_t size, ZSTD_customMem customMem)
0057 {
0058     if (customMem.customAlloc)
0059         return customMem.customAlloc(customMem.opaque, size);
0060     return ZSTD_malloc(size);
0061 }
0062 
0063 void* ZSTD_customCalloc(size_t size, ZSTD_customMem customMem)
0064 {
0065     if (customMem.customAlloc) {
0066         /* calloc implemented as malloc+memset;
0067          * not as efficient as calloc, but next best guess for custom malloc */
0068         void* const ptr = customMem.customAlloc(customMem.opaque, size);
0069         ZSTD_memset(ptr, 0, size);
0070         return ptr;
0071     }
0072     return ZSTD_calloc(1, size);
0073 }
0074 
0075 void ZSTD_customFree(void* ptr, ZSTD_customMem customMem)
0076 {
0077     if (ptr!=NULL) {
0078         if (customMem.customFree)
0079             customMem.customFree(customMem.opaque, ptr);
0080         else
0081             ZSTD_free(ptr);
0082     }
0083 }