zstd_common.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright (c) 2016-present, 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. /*-*************************************
  11. * Dependencies
  12. ***************************************/
  13. #include <stdlib.h> /* malloc, calloc, free */
  14. #include <string.h> /* memset */
  15. #include "error_private.h"
  16. #include "zstd_internal.h"
  17. /*-****************************************
  18. * Version
  19. ******************************************/
  20. unsigned ZSTD_versionNumber(void) { return ZSTD_VERSION_NUMBER; }
  21. const char* ZSTD_versionString(void) { return ZSTD_VERSION_STRING; }
  22. /*-****************************************
  23. * ZSTD Error Management
  24. ******************************************/
  25. /*! ZSTD_isError() :
  26. * tells if a return value is an error code */
  27. unsigned ZSTD_isError(size_t code) { return ERR_isError(code); }
  28. /*! ZSTD_getErrorName() :
  29. * provides error code string from function result (useful for debugging) */
  30. const char* ZSTD_getErrorName(size_t code) { return ERR_getErrorName(code); }
  31. /*! ZSTD_getError() :
  32. * convert a `size_t` function result into a proper ZSTD_errorCode enum */
  33. ZSTD_ErrorCode ZSTD_getErrorCode(size_t code) { return ERR_getErrorCode(code); }
  34. /*! ZSTD_getErrorString() :
  35. * provides error code string from enum */
  36. const char* ZSTD_getErrorString(ZSTD_ErrorCode code) { return ERR_getErrorString(code); }
  37. /*! g_debuglog_enable :
  38. * turn on/off debug traces (global switch) */
  39. #if defined(ZSTD_DEBUG) && (ZSTD_DEBUG >= 2)
  40. int g_debuglog_enable = 1;
  41. #endif
  42. /*=**************************************************************
  43. * Custom allocator
  44. ****************************************************************/
  45. void* ZSTD_malloc(size_t size, ZSTD_customMem customMem)
  46. {
  47. if (customMem.customAlloc)
  48. return customMem.customAlloc(customMem.opaque, size);
  49. return malloc(size);
  50. }
  51. void* ZSTD_calloc(size_t size, ZSTD_customMem customMem)
  52. {
  53. if (customMem.customAlloc) {
  54. /* calloc implemented as malloc+memset;
  55. * not as efficient as calloc, but next best guess for custom malloc */
  56. void* const ptr = customMem.customAlloc(customMem.opaque, size);
  57. memset(ptr, 0, size);
  58. return ptr;
  59. }
  60. return calloc(1, size);
  61. }
  62. void ZSTD_free(void* ptr, ZSTD_customMem customMem)
  63. {
  64. if (ptr!=NULL) {
  65. if (customMem.customFree)
  66. customMem.customFree(customMem.opaque, ptr);
  67. else
  68. free(ptr);
  69. }
  70. }