zstd_deps.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause */
  2. /*
  3. * Copyright (c) Facebook, Inc.
  4. * All rights reserved.
  5. *
  6. * This source code is licensed under both the BSD-style license (found in the
  7. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  8. * in the COPYING file in the root directory of this source tree).
  9. * You may select, at your option, one of the above-listed licenses.
  10. */
  11. /*
  12. * This file provides common libc dependencies that zstd requires.
  13. * The purpose is to allow replacing this file with a custom implementation
  14. * to compile zstd without libc support.
  15. */
  16. /* Need:
  17. * NULL
  18. * INT_MAX
  19. * UINT_MAX
  20. * ZSTD_memcpy()
  21. * ZSTD_memset()
  22. * ZSTD_memmove()
  23. */
  24. #ifndef ZSTD_DEPS_COMMON
  25. #define ZSTD_DEPS_COMMON
  26. #include <linux/limits.h>
  27. #include <linux/stddef.h>
  28. #define ZSTD_memcpy(d,s,n) __builtin_memcpy((d),(s),(n))
  29. #define ZSTD_memmove(d,s,n) __builtin_memmove((d),(s),(n))
  30. #define ZSTD_memset(d,s,n) __builtin_memset((d),(s),(n))
  31. #endif /* ZSTD_DEPS_COMMON */
  32. /*
  33. * Define malloc as always failing. That means the user must
  34. * either use ZSTD_customMem or statically allocate memory.
  35. * Need:
  36. * ZSTD_malloc()
  37. * ZSTD_free()
  38. * ZSTD_calloc()
  39. */
  40. #ifdef ZSTD_DEPS_NEED_MALLOC
  41. #ifndef ZSTD_DEPS_MALLOC
  42. #define ZSTD_DEPS_MALLOC
  43. #define ZSTD_malloc(s) ({ (void)(s); NULL; })
  44. #define ZSTD_free(p) ((void)(p))
  45. #define ZSTD_calloc(n,s) ({ (void)(n); (void)(s); NULL; })
  46. #endif /* ZSTD_DEPS_MALLOC */
  47. #endif /* ZSTD_DEPS_NEED_MALLOC */
  48. /*
  49. * Provides 64-bit math support.
  50. * Need:
  51. * U64 ZSTD_div64(U64 dividend, U32 divisor)
  52. */
  53. #ifdef ZSTD_DEPS_NEED_MATH64
  54. #ifndef ZSTD_DEPS_MATH64
  55. #define ZSTD_DEPS_MATH64
  56. #include <linux/math64.h>
  57. static uint64_t ZSTD_div64(uint64_t dividend, uint32_t divisor) {
  58. return div_u64(dividend, divisor);
  59. }
  60. #endif /* ZSTD_DEPS_MATH64 */
  61. #endif /* ZSTD_DEPS_NEED_MATH64 */
  62. /*
  63. * This is only requested when DEBUGLEVEL >= 1, meaning
  64. * it is disabled in production.
  65. * Need:
  66. * assert()
  67. */
  68. #ifdef ZSTD_DEPS_NEED_ASSERT
  69. #ifndef ZSTD_DEPS_ASSERT
  70. #define ZSTD_DEPS_ASSERT
  71. #include <linux/kernel.h>
  72. #define assert(x) WARN_ON(!(x))
  73. #endif /* ZSTD_DEPS_ASSERT */
  74. #endif /* ZSTD_DEPS_NEED_ASSERT */
  75. /*
  76. * This is only requested when DEBUGLEVEL >= 2, meaning
  77. * it is disabled in production.
  78. * Need:
  79. * ZSTD_DEBUG_PRINT()
  80. */
  81. #ifdef ZSTD_DEPS_NEED_IO
  82. #ifndef ZSTD_DEPS_IO
  83. #define ZSTD_DEPS_IO
  84. #include <linux/printk.h>
  85. #define ZSTD_DEBUG_PRINT(...) pr_debug(__VA_ARGS__)
  86. #endif /* ZSTD_DEPS_IO */
  87. #endif /* ZSTD_DEPS_NEED_IO */
  88. /*
  89. * Only requested when MSAN is enabled.
  90. * Need:
  91. * intptr_t
  92. */
  93. #ifdef ZSTD_DEPS_NEED_STDINT
  94. #ifndef ZSTD_DEPS_STDINT
  95. #define ZSTD_DEPS_STDINT
  96. /*
  97. * The Linux Kernel doesn't provide intptr_t, only uintptr_t, which
  98. * is an unsigned long.
  99. */
  100. typedef long intptr_t;
  101. #endif /* ZSTD_DEPS_STDINT */
  102. #endif /* ZSTD_DEPS_NEED_STDINT */