You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

zstd_common.c 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright (c) Meta Platforms, Inc. and affiliates.
  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. #define ZSTD_DEPS_NEED_MALLOC
  14. #include "zstd_deps.h" /* ZSTD_malloc, ZSTD_calloc, ZSTD_free, ZSTD_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_customMalloc(size_t size, ZSTD_customMem customMem)
  43. {
  44. if (customMem.customAlloc)
  45. return customMem.customAlloc(customMem.opaque, size);
  46. return ZSTD_malloc(size);
  47. }
  48. void* ZSTD_customCalloc(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. ZSTD_memset(ptr, 0, size);
  55. return ptr;
  56. }
  57. return ZSTD_calloc(1, size);
  58. }
  59. void ZSTD_customFree(void* ptr, ZSTD_customMem customMem)
  60. {
  61. if (ptr!=NULL) {
  62. if (customMem.customFree)
  63. customMem.customFree(customMem.opaque, ptr);
  64. else
  65. ZSTD_free(ptr);
  66. }
  67. }