zstd_common.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright (c) 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 <linux/module.h>
  14. #define ZSTD_DEPS_NEED_MALLOC
  15. #include "zstd_deps.h" /* ZSTD_malloc, ZSTD_calloc, ZSTD_free, ZSTD_memset */
  16. #include "error_private.h"
  17. #include "zstd_internal.h"
  18. /*-****************************************
  19. * Version
  20. ******************************************/
  21. unsigned ZSTD_versionNumber(void) { return ZSTD_VERSION_NUMBER; }
  22. const char* ZSTD_versionString(void) { return ZSTD_VERSION_STRING; }
  23. /*-****************************************
  24. * ZSTD Error Management
  25. ******************************************/
  26. #undef ZSTD_isError /* defined within zstd_internal.h */
  27. /*! ZSTD_isError() :
  28. * tells if a return value is an error code
  29. * symbol is required for external callers */
  30. unsigned ZSTD_isError(size_t code) { return ERR_isError(code); }
  31. EXPORT_SYMBOL_GPL(ZSTD_isError);
  32. /*! ZSTD_getErrorName() :
  33. * provides error code string from function result (useful for debugging) */
  34. const char* ZSTD_getErrorName(size_t code) { return ERR_getErrorName(code); }
  35. EXPORT_SYMBOL_GPL(ZSTD_getErrorName);
  36. /*! ZSTD_getError() :
  37. * convert a `size_t` function result into a proper ZSTD_errorCode enum */
  38. ZSTD_ErrorCode ZSTD_getErrorCode(size_t code) { return ERR_getErrorCode(code); }
  39. EXPORT_SYMBOL_GPL(ZSTD_getErrorCode);
  40. /*! ZSTD_getErrorString() :
  41. * provides error code string from enum */
  42. const char* ZSTD_getErrorString(ZSTD_ErrorCode code) { return ERR_getErrorString(code); }
  43. /*=**************************************************************
  44. * Custom allocator
  45. ****************************************************************/
  46. void* ZSTD_customMalloc(size_t size, ZSTD_customMem customMem)
  47. {
  48. if (customMem.customAlloc)
  49. return customMem.customAlloc(customMem.opaque, size);
  50. return ZSTD_malloc(size);
  51. }
  52. EXPORT_SYMBOL_GPL(ZSTD_customMalloc);
  53. void* ZSTD_customCalloc(size_t size, ZSTD_customMem customMem)
  54. {
  55. if (customMem.customAlloc) {
  56. /* calloc implemented as malloc+memset;
  57. * not as efficient as calloc, but next best guess for custom malloc */
  58. void* const ptr = customMem.customAlloc(customMem.opaque, size);
  59. ZSTD_memset(ptr, 0, size);
  60. return ptr;
  61. }
  62. return ZSTD_calloc(1, size);
  63. }
  64. EXPORT_SYMBOL_GPL(ZSTD_customCalloc);
  65. void ZSTD_customFree(void* ptr, ZSTD_customMem customMem)
  66. {
  67. if (ptr!=NULL) {
  68. if (customMem.customFree)
  69. customMem.customFree(customMem.opaque, ptr);
  70. else
  71. ZSTD_free(ptr);
  72. }
  73. }
  74. EXPORT_SYMBOL_GPL(ZSTD_customFree);
  75. MODULE_LICENSE("Dual BSD/GPL");
  76. MODULE_DESCRIPTION("Zstd Common");