zstd_deps.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. /* This file provides common libc dependencies that zstd requires.
  11. * The purpose is to allow replacing this file with a custom implementation
  12. * to compile zstd without libc support.
  13. */
  14. /* Need:
  15. * NULL
  16. * INT_MAX
  17. * UINT_MAX
  18. * ZSTD_memcpy()
  19. * ZSTD_memset()
  20. * ZSTD_memmove()
  21. */
  22. #ifndef ZSTD_DEPS_COMMON
  23. #define ZSTD_DEPS_COMMON
  24. /* Even though we use qsort_r only for the dictionary builder, the macro
  25. * _GNU_SOURCE has to be declared *before* the inclusion of any standard
  26. * header and the script 'combine.sh' combines the whole zstd source code
  27. * in a single file.
  28. */
  29. #if defined(__linux) || defined(__linux__) || defined(linux) || defined(__gnu_linux__) || \
  30. defined(__CYGWIN__) || defined(__MSYS__)
  31. #if !defined(_GNU_SOURCE) && !defined(__ANDROID__) /* NDK doesn't ship qsort_r(). */
  32. #define _GNU_SOURCE
  33. #endif
  34. #endif
  35. #include <limits.h>
  36. #include <stddef.h>
  37. #include <string.h>
  38. #if defined(__GNUC__) && __GNUC__ >= 4
  39. # define ZSTD_memcpy(d,s,l) __builtin_memcpy((d),(s),(l))
  40. # define ZSTD_memmove(d,s,l) __builtin_memmove((d),(s),(l))
  41. # define ZSTD_memset(p,v,l) __builtin_memset((p),(v),(l))
  42. #else
  43. # define ZSTD_memcpy(d,s,l) memcpy((d),(s),(l))
  44. # define ZSTD_memmove(d,s,l) memmove((d),(s),(l))
  45. # define ZSTD_memset(p,v,l) memset((p),(v),(l))
  46. #endif
  47. #endif /* ZSTD_DEPS_COMMON */
  48. /* Need:
  49. * ZSTD_malloc()
  50. * ZSTD_free()
  51. * ZSTD_calloc()
  52. */
  53. #ifdef ZSTD_DEPS_NEED_MALLOC
  54. #ifndef ZSTD_DEPS_MALLOC
  55. #define ZSTD_DEPS_MALLOC
  56. #include <stdlib.h>
  57. #define ZSTD_malloc(s) malloc(s)
  58. #define ZSTD_calloc(n,s) calloc((n), (s))
  59. #define ZSTD_free(p) free((p))
  60. #endif /* ZSTD_DEPS_MALLOC */
  61. #endif /* ZSTD_DEPS_NEED_MALLOC */
  62. /*
  63. * Provides 64-bit math support.
  64. * Need:
  65. * U64 ZSTD_div64(U64 dividend, U32 divisor)
  66. */
  67. #ifdef ZSTD_DEPS_NEED_MATH64
  68. #ifndef ZSTD_DEPS_MATH64
  69. #define ZSTD_DEPS_MATH64
  70. #define ZSTD_div64(dividend, divisor) ((dividend) / (divisor))
  71. #endif /* ZSTD_DEPS_MATH64 */
  72. #endif /* ZSTD_DEPS_NEED_MATH64 */
  73. /* Need:
  74. * assert()
  75. */
  76. #ifdef ZSTD_DEPS_NEED_ASSERT
  77. #ifndef ZSTD_DEPS_ASSERT
  78. #define ZSTD_DEPS_ASSERT
  79. #include <assert.h>
  80. #endif /* ZSTD_DEPS_ASSERT */
  81. #endif /* ZSTD_DEPS_NEED_ASSERT */
  82. /* Need:
  83. * ZSTD_DEBUG_PRINT()
  84. */
  85. #ifdef ZSTD_DEPS_NEED_IO
  86. #ifndef ZSTD_DEPS_IO
  87. #define ZSTD_DEPS_IO
  88. #include <stdio.h>
  89. #define ZSTD_DEBUG_PRINT(...) fprintf(stderr, __VA_ARGS__)
  90. #endif /* ZSTD_DEPS_IO */
  91. #endif /* ZSTD_DEPS_NEED_IO */
  92. /* Only requested when <stdint.h> is known to be present.
  93. * Need:
  94. * intptr_t
  95. */
  96. #ifdef ZSTD_DEPS_NEED_STDINT
  97. #ifndef ZSTD_DEPS_STDINT
  98. #define ZSTD_DEPS_STDINT
  99. #include <stdint.h>
  100. #endif /* ZSTD_DEPS_STDINT */
  101. #endif /* ZSTD_DEPS_NEED_STDINT */