zstd_common.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. #undef ZSTD_isError /* defined within zstd_internal.h */
  26. /*! ZSTD_isError() :
  27. * tells if a return value is an error code
  28. * symbol is required for external callers */
  29. unsigned ZSTD_isError(size_t code) { return ERR_isError(code); }
  30. /*! ZSTD_getErrorName() :
  31. * provides error code string from function result (useful for debugging) */
  32. const char* ZSTD_getErrorName(size_t code) { return ERR_getErrorName(code); }
  33. /*! ZSTD_getError() :
  34. * convert a `size_t` function result into a proper ZSTD_errorCode enum */
  35. ZSTD_ErrorCode ZSTD_getErrorCode(size_t code) { return ERR_getErrorCode(code); }
  36. /*! ZSTD_getErrorString() :
  37. * provides error code string from enum */
  38. const char* ZSTD_getErrorString(ZSTD_ErrorCode code) { return ERR_getErrorString(code); }
  39. /*=**************************************************************
  40. * Custom allocator
  41. ****************************************************************/
  42. void* ZSTD_malloc(size_t size, ZSTD_customMem customMem)
  43. {
  44. if (customMem.customAlloc)
  45. return customMem.customAlloc(customMem.opaque, size);
  46. return malloc(size);
  47. }
  48. void* ZSTD_calloc(size_t size, ZSTD_customMem customMem)
  49. {
  50. if (customMem.customAlloc) {
  51. /* calloc implemented as malloc+memset;
  52. * not as efficient as calloc, but next best guess for custom malloc */
  53. void* const ptr = customMem.customAlloc(customMem.opaque, size);
  54. memset(ptr, 0, size);
  55. return ptr;
  56. }
  57. return calloc(1, size);
  58. }
  59. void ZSTD_free(void* ptr, ZSTD_customMem customMem)
  60. {
  61. if (ptr!=NULL) {
  62. if (customMem.customFree)
  63. customMem.customFree(customMem.opaque, ptr);
  64. else
  65. free(ptr);
  66. }
  67. }