zstd_common.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. /*=**************************************************************
  38. * Custom allocator
  39. ****************************************************************/
  40. void* ZSTD_malloc(size_t size, ZSTD_customMem customMem)
  41. {
  42. if (customMem.customAlloc)
  43. return customMem.customAlloc(customMem.opaque, size);
  44. return malloc(size);
  45. }
  46. void* ZSTD_calloc(size_t size, ZSTD_customMem customMem)
  47. {
  48. if (customMem.customAlloc) {
  49. /* calloc implemented as malloc+memset;
  50. * not as efficient as calloc, but next best guess for custom malloc */
  51. void* const ptr = customMem.customAlloc(customMem.opaque, size);
  52. memset(ptr, 0, size);
  53. return ptr;
  54. }
  55. return calloc(1, size);
  56. }
  57. void ZSTD_free(void* ptr, ZSTD_customMem customMem)
  58. {
  59. if (ptr!=NULL) {
  60. if (customMem.customFree)
  61. customMem.customFree(customMem.opaque, ptr);
  62. else
  63. free(ptr);
  64. }
  65. }