zbuild.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. #ifndef _ZBUILD_H
  2. #define _ZBUILD_H
  3. #define _POSIX_SOURCE 1 /* fileno */
  4. #ifndef _POSIX_C_SOURCE
  5. # define _POSIX_C_SOURCE 200809L /* snprintf, posix_memalign, strdup */
  6. #endif
  7. #ifndef _ISOC11_SOURCE
  8. # define _ISOC11_SOURCE 1 /* aligned_alloc */
  9. #endif
  10. #ifdef __OpenBSD__
  11. # define _BSD_SOURCE 1
  12. #endif
  13. #include <stddef.h>
  14. #include <string.h>
  15. #include <stdlib.h>
  16. #include <stdint.h>
  17. /* Determine compiler version of C Standard */
  18. #ifdef __STDC_VERSION__
  19. # if __STDC_VERSION__ >= 199901L
  20. # ifndef STDC99
  21. # define STDC99
  22. # endif
  23. # endif
  24. # if __STDC_VERSION__ >= 201112L
  25. # ifndef STDC11
  26. # define STDC11
  27. # endif
  28. # endif
  29. #endif
  30. #ifndef Z_HAS_ATTRIBUTE
  31. # if defined(__has_attribute)
  32. # define Z_HAS_ATTRIBUTE(a) __has_attribute(a)
  33. # else
  34. # define Z_HAS_ATTRIBUTE(a) 0
  35. # endif
  36. #endif
  37. #ifndef Z_FALLTHROUGH
  38. # if Z_HAS_ATTRIBUTE(__fallthrough__) || (defined(__GNUC__) && (__GNUC__ >= 7))
  39. # define Z_FALLTHROUGH __attribute__((__fallthrough__))
  40. # else
  41. # define Z_FALLTHROUGH do {} while(0) /* fallthrough */
  42. # endif
  43. #endif
  44. #ifndef Z_TARGET
  45. # if Z_HAS_ATTRIBUTE(__target__)
  46. # define Z_TARGET(x) __attribute__((__target__(x)))
  47. # else
  48. # define Z_TARGET(x)
  49. # endif
  50. #endif
  51. /* This has to be first include that defines any types */
  52. #if defined(_MSC_VER)
  53. # if defined(_WIN64)
  54. typedef __int64 ssize_t;
  55. # else
  56. typedef long ssize_t;
  57. # endif
  58. # if defined(_WIN64)
  59. #define SSIZE_MAX _I64_MAX
  60. # else
  61. #define SSIZE_MAX LONG_MAX
  62. # endif
  63. #endif
  64. /* MS Visual Studio does not allow inline in C, only C++.
  65. But it provides __inline instead, so use that. */
  66. #if defined(_MSC_VER) && !defined(inline) && !defined(__cplusplus)
  67. # define inline __inline
  68. #endif
  69. #if defined(ZLIB_COMPAT)
  70. # define PREFIX(x) x
  71. # define PREFIX2(x) ZLIB_ ## x
  72. # define PREFIX3(x) z_ ## x
  73. # define PREFIX4(x) x ## 64
  74. # define zVersion zlibVersion
  75. #else
  76. # define PREFIX(x) zng_ ## x
  77. # define PREFIX2(x) ZLIBNG_ ## x
  78. # define PREFIX3(x) zng_ ## x
  79. # define PREFIX4(x) zng_ ## x
  80. # define zVersion zlibng_version
  81. # define z_size_t size_t
  82. #endif
  83. /* In zlib-compat some functions and types use unsigned long, but zlib-ng use size_t */
  84. #if defined(ZLIB_COMPAT)
  85. # define z_uintmax_t unsigned long
  86. #else
  87. # define z_uintmax_t size_t
  88. #endif
  89. /* Minimum of a and b. */
  90. #define MIN(a, b) ((a) > (b) ? (b) : (a))
  91. /* Maximum of a and b. */
  92. #define MAX(a, b) ((a) < (b) ? (b) : (a))
  93. /* Ignore unused variable warning */
  94. #define Z_UNUSED(var) (void)(var)
  95. #if defined(HAVE_VISIBILITY_INTERNAL)
  96. # define Z_INTERNAL __attribute__((visibility ("internal")))
  97. #elif defined(HAVE_VISIBILITY_HIDDEN)
  98. # define Z_INTERNAL __attribute__((visibility ("hidden")))
  99. #else
  100. # define Z_INTERNAL
  101. #endif
  102. /* Symbol versioning helpers, allowing multiple versions of a function to exist.
  103. * Functions using this must also be added to zlib-ng.map for each version.
  104. * Double @@ means this is the default for newly compiled applications to link against.
  105. * Single @ means this is kept for backwards compatibility.
  106. * This is only used for Zlib-ng native API, and only on platforms supporting this.
  107. */
  108. #if defined(HAVE_SYMVER)
  109. # define ZSYMVER(func,alias,ver) __asm__(".symver " func ", " alias "@ZLIB_NG_" ver);
  110. # define ZSYMVER_DEF(func,alias,ver) __asm__(".symver " func ", " alias "@@ZLIB_NG_" ver);
  111. #else
  112. # define ZSYMVER(func,alias,ver)
  113. # define ZSYMVER_DEF(func,alias,ver)
  114. #endif
  115. #ifndef __cplusplus
  116. # define Z_REGISTER register
  117. #else
  118. # define Z_REGISTER
  119. #endif
  120. /* Reverse the bytes in a value. Use compiler intrinsics when
  121. possible to take advantage of hardware implementations. */
  122. #if defined(_MSC_VER) && (_MSC_VER >= 1300)
  123. # include <stdlib.h>
  124. # pragma intrinsic(_byteswap_ulong)
  125. # define ZSWAP16(q) _byteswap_ushort(q)
  126. # define ZSWAP32(q) _byteswap_ulong(q)
  127. # define ZSWAP64(q) _byteswap_uint64(q)
  128. #elif defined(__clang__) || (defined(__GNUC__) && \
  129. (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)))
  130. # define ZSWAP16(q) __builtin_bswap16(q)
  131. # define ZSWAP32(q) __builtin_bswap32(q)
  132. # define ZSWAP64(q) __builtin_bswap64(q)
  133. #elif defined(__GNUC__) && (__GNUC__ >= 2) && defined(__linux__)
  134. # include <byteswap.h>
  135. # define ZSWAP16(q) bswap_16(q)
  136. # define ZSWAP32(q) bswap_32(q)
  137. # define ZSWAP64(q) bswap_64(q)
  138. #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
  139. # include <sys/endian.h>
  140. # define ZSWAP16(q) bswap16(q)
  141. # define ZSWAP32(q) bswap32(q)
  142. # define ZSWAP64(q) bswap64(q)
  143. #elif defined(__OpenBSD__)
  144. # include <sys/endian.h>
  145. # define ZSWAP16(q) swap16(q)
  146. # define ZSWAP32(q) swap32(q)
  147. # define ZSWAP64(q) swap64(q)
  148. #elif defined(__INTEL_COMPILER)
  149. /* ICC does not provide a two byte swap. */
  150. # define ZSWAP16(q) ((((q) & 0xff) << 8) | (((q) & 0xff00) >> 8))
  151. # define ZSWAP32(q) _bswap(q)
  152. # define ZSWAP64(q) _bswap64(q)
  153. #else
  154. # define ZSWAP16(q) ((((q) & 0xff) << 8) | (((q) & 0xff00) >> 8))
  155. # define ZSWAP32(q) ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
  156. (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
  157. # define ZSWAP64(q) \
  158. (((q & 0xFF00000000000000u) >> 56u) | \
  159. ((q & 0x00FF000000000000u) >> 40u) | \
  160. ((q & 0x0000FF0000000000u) >> 24u) | \
  161. ((q & 0x000000FF00000000u) >> 8u) | \
  162. ((q & 0x00000000FF000000u) << 8u) | \
  163. ((q & 0x0000000000FF0000u) << 24u) | \
  164. ((q & 0x000000000000FF00u) << 40u) | \
  165. ((q & 0x00000000000000FFu) << 56u))
  166. #endif
  167. /* Only enable likely/unlikely if the compiler is known to support it */
  168. #if (defined(__GNUC__) && (__GNUC__ >= 3)) || defined(__INTEL_COMPILER) || defined(__clang__)
  169. # define LIKELY_NULL(x) __builtin_expect((x) != 0, 0)
  170. # define LIKELY(x) __builtin_expect(!!(x), 1)
  171. # define UNLIKELY(x) __builtin_expect(!!(x), 0)
  172. #else
  173. # define LIKELY_NULL(x) x
  174. # define LIKELY(x) x
  175. # define UNLIKELY(x) x
  176. #endif /* (un)likely */
  177. #if defined(HAVE_ATTRIBUTE_ALIGNED)
  178. # define ALIGNED_(x) __attribute__ ((aligned(x)))
  179. #elif defined(_MSC_VER)
  180. # define ALIGNED_(x) __declspec(align(x))
  181. #endif
  182. #ifdef HAVE_BUILTIN_ASSUME_ALIGNED
  183. # define HINT_ALIGNED(p,n) __builtin_assume_aligned((void *)(p),(n))
  184. #else
  185. # define HINT_ALIGNED(p,n) (p)
  186. #endif
  187. #define HINT_ALIGNED_16(p) HINT_ALIGNED((p),16)
  188. #define HINT_ALIGNED_64(p) HINT_ALIGNED((p),64)
  189. #define HINT_ALIGNED_4096(p) HINT_ALIGNED((p),4096)
  190. /* PADSZ returns needed bytes to pad bpos to pad size
  191. * PAD_NN calculates pad size and adds it to bpos, returning the result.
  192. * All take an integer or a pointer as bpos input.
  193. */
  194. #define PADSZ(bpos, pad) (((pad) - ((uintptr_t)(bpos) % (pad))) % (pad))
  195. #define PAD_16(bpos) ((bpos) + PADSZ((bpos),16))
  196. #define PAD_64(bpos) ((bpos) + PADSZ((bpos),64))
  197. #define PAD_4096(bpos) ((bpos) + PADSZ((bpos),4096))
  198. /* Diagnostic functions */
  199. #ifdef ZLIB_DEBUG
  200. # include <stdio.h>
  201. extern int Z_INTERNAL z_verbose;
  202. extern void Z_INTERNAL z_error(const char *m);
  203. # define Assert(cond, msg) {if (!(cond)) z_error(msg);}
  204. # define Trace(x) {if (z_verbose >= 0) fprintf x;}
  205. # define Tracev(x) {if (z_verbose > 0) fprintf x;}
  206. # define Tracevv(x) {if (z_verbose > 1) fprintf x;}
  207. # define Tracec(c, x) {if (z_verbose > 0 && (c)) fprintf x;}
  208. # define Tracecv(c, x) {if (z_verbose > 1 && (c)) fprintf x;}
  209. #else
  210. # define Assert(cond, msg)
  211. # define Trace(x)
  212. # define Tracev(x)
  213. # define Tracevv(x)
  214. # define Tracec(c, x)
  215. # define Tracecv(c, x)
  216. #endif
  217. #ifndef NO_UNALIGNED
  218. # if defined(__x86_64__) || defined(_M_X64) || defined(__amd64__) || defined(_M_AMD64)
  219. # define UNALIGNED_OK
  220. # define UNALIGNED64_OK
  221. # elif defined(__i386__) || defined(__i486__) || defined(__i586__) || \
  222. defined(__i686__) || defined(_X86_) || defined(_M_IX86)
  223. # define UNALIGNED_OK
  224. # elif defined(__aarch64__) || defined(_M_ARM64) || defined(_M_ARM64EC)
  225. # if (defined(__GNUC__) && defined(__ARM_FEATURE_UNALIGNED)) || !defined(__GNUC__)
  226. # define UNALIGNED_OK
  227. # define UNALIGNED64_OK
  228. # endif
  229. # elif defined(__arm__) || (_M_ARM >= 7)
  230. # if (defined(__GNUC__) && defined(__ARM_FEATURE_UNALIGNED)) || !defined(__GNUC__)
  231. # define UNALIGNED_OK
  232. # endif
  233. # elif defined(__powerpc64__) || defined(__ppc64__)
  234. # if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  235. # define UNALIGNED_OK
  236. # define UNALIGNED64_OK
  237. # endif
  238. # endif
  239. #endif
  240. #if defined(__has_feature)
  241. # if __has_feature(address_sanitizer)
  242. # define Z_ADDRESS_SANITIZER 1
  243. # endif
  244. #elif defined(__SANITIZE_ADDRESS__)
  245. # define Z_ADDRESS_SANITIZER 1
  246. #endif
  247. /*
  248. * __asan_loadN() and __asan_storeN() calls are inserted by compilers in order to check memory accesses.
  249. * They can be called manually too, with the following caveats:
  250. * gcc says: "warning: implicit declaration of function ‘...’"
  251. * g++ says: "error: new declaration ‘...’ ambiguates built-in declaration ‘...’"
  252. * Accommodate both.
  253. */
  254. #ifdef Z_ADDRESS_SANITIZER
  255. #ifndef __cplusplus
  256. void __asan_loadN(void *, long);
  257. void __asan_storeN(void *, long);
  258. #endif
  259. #else
  260. # define __asan_loadN(a, size) do { Z_UNUSED(a); Z_UNUSED(size); } while (0)
  261. # define __asan_storeN(a, size) do { Z_UNUSED(a); Z_UNUSED(size); } while (0)
  262. #endif
  263. #if defined(__has_feature)
  264. # if __has_feature(memory_sanitizer)
  265. # define Z_MEMORY_SANITIZER 1
  266. # include <sanitizer/msan_interface.h>
  267. # endif
  268. #endif
  269. #ifndef Z_MEMORY_SANITIZER
  270. # define __msan_check_mem_is_initialized(a, size) do { Z_UNUSED(a); Z_UNUSED(size); } while (0)
  271. # define __msan_unpoison(a, size) do { Z_UNUSED(a); Z_UNUSED(size); } while (0)
  272. #endif
  273. /* Notify sanitizer runtime about an upcoming read access. */
  274. #define instrument_read(a, size) do { \
  275. void *__a = (void *)(a); \
  276. long __size = size; \
  277. __asan_loadN(__a, __size); \
  278. __msan_check_mem_is_initialized(__a, __size); \
  279. } while (0)
  280. /* Notify sanitizer runtime about an upcoming write access. */
  281. #define instrument_write(a, size) do { \
  282. void *__a = (void *)(a); \
  283. long __size = size; \
  284. __asan_storeN(__a, __size); \
  285. } while (0)
  286. /* Notify sanitizer runtime about an upcoming read/write access. */
  287. #define instrument_read_write(a, size) do { \
  288. void *__a = (void *)(a); \
  289. long __size = size; \
  290. __asan_storeN(__a, __size); \
  291. __msan_check_mem_is_initialized(__a, __size); \
  292. } while (0)
  293. #endif