Coverage Report

Created: 2026-05-28 06:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/zlib-ng/zbuild.h
Line
Count
Source
1
#ifndef _ZBUILD_H
2
#define _ZBUILD_H
3
4
#define _POSIX_SOURCE 1  /* fileno */
5
#ifndef _POSIX_C_SOURCE
6
#  define _POSIX_C_SOURCE 200809L /* snprintf, posix_memalign, strdup */
7
#endif
8
#ifndef _ISOC11_SOURCE
9
#  define _ISOC11_SOURCE 1 /* aligned_alloc */
10
#endif
11
#ifdef __OpenBSD__
12
#  define _BSD_SOURCE 1
13
#endif
14
#ifndef _LARGEFILE64_SOURCE
15
#  define _LARGEFILE64_SOURCE 1 /* request off64_t, lseek64 on glibc; check _LFS64_LARGEFILE */
16
#endif
17
18
#include <stddef.h>
19
#include <string.h>
20
#include <stdlib.h>
21
#include <stdint.h>
22
#include <stdio.h>
23
24
#include "zarch.h"
25
26
/* Determine compiler version of C Standard */
27
#ifdef __STDC_VERSION__
28
#  if __STDC_VERSION__ >= 199901L
29
#    ifndef STDC99
30
#      define STDC99
31
#    endif
32
#  endif
33
#  if __STDC_VERSION__ >= 201112L
34
#    ifndef STDC11
35
#      define STDC11
36
#    endif
37
#  endif
38
#endif
39
40
#ifndef Z_HAS_ATTRIBUTE
41
#  if defined(__has_attribute)
42
#    define Z_HAS_ATTRIBUTE(a) __has_attribute(a)
43
#  else
44
#    define Z_HAS_ATTRIBUTE(a) 0
45
#  endif
46
#endif
47
48
#ifndef Z_FALLTHROUGH
49
#  if Z_HAS_ATTRIBUTE(__fallthrough__) || (defined(__GNUC__) && (__GNUC__ >= 7))
50
95.8k
#    define Z_FALLTHROUGH __attribute__((__fallthrough__))
51
#  else
52
#    define Z_FALLTHROUGH do {} while(0) /* fallthrough */
53
#  endif
54
#endif
55
56
/* Hint to compiler that a block of code is unreachable, typically in a switch default condition */
57
#ifndef Z_UNREACHABLE
58
#  if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L
59
#    if !defined(unreachable) && defined(_MSC_VER)
60
#      define Z_UNREACHABLE() __assume(0)
61
#    else
62
#      define Z_UNREACHABLE() unreachable()           // C23 approach
63
#    endif
64
#  elif (defined(__GNUC__) && (__GNUC__ >= 5)) || defined(__clang__)
65
0
#    define Z_UNREACHABLE() __builtin_unreachable()
66
#  else
67
#    define Z_UNREACHABLE()
68
#  endif
69
#endif
70
71
#ifndef Z_TARGET
72
#  if Z_HAS_ATTRIBUTE(__target__)
73
#    define Z_TARGET(x) __attribute__((__target__(x)))
74
#  else
75
#    define Z_TARGET(x)
76
#  endif
77
#endif
78
79
/* This has to be first include that defines any types */
80
#if defined(_MSC_VER)
81
#  if defined(_WIN64)
82
    typedef __int64 ssize_t;
83
#  else
84
    typedef long ssize_t;
85
#  endif
86
87
#  if defined(_WIN64)
88
    #define SSIZE_MAX _I64_MAX
89
#  else
90
    #define SSIZE_MAX LONG_MAX
91
#  endif
92
#endif
93
94
/* A forced inline decorator */
95
#if defined(_MSC_VER)
96
#  define Z_FORCEINLINE __forceinline
97
#elif defined(__GNUC__)
98
#  define Z_FORCEINLINE inline __attribute__((always_inline))
99
#else
100
    /* It won't actually force inlining but it will suggest it */
101
#  define Z_FORCEINLINE inline
102
#endif
103
104
/* MS Visual Studio does not allow inline in C, only C++.
105
   But it provides __inline instead, so use that. */
106
#if defined(_MSC_VER) && !defined(inline) && !defined(__cplusplus)
107
#  define inline __inline
108
#endif
109
110
#if defined(ZLIB_COMPAT)
111
#  define PREFIX(x) x
112
#  define PREFIX2(x) ZLIB_ ## x
113
#  define PREFIX3(x) z_ ## x
114
#  define PREFIX4(x) x ## 64
115
#  define zVersion zlibVersion
116
#else
117
8.91k
#  define PREFIX(x) zng_ ## x
118
0
#  define PREFIX2(x) ZLIBNG_ ## x
119
450k
#  define PREFIX3(x) zng_ ## x
120
#  define PREFIX4(x) zng_ ## x
121
#  define zVersion zlibng_version
122
8.92k
#  define z_size_t size_t
123
#endif
124
125
/* In zlib-compat some functions and types use unsigned long, but zlib-ng use size_t */
126
#if defined(ZLIB_COMPAT)
127
#  define z_uintmax_t unsigned long
128
#else
129
4.46k
#  define z_uintmax_t size_t
130
#endif
131
132
/* In zlib-compat headers some function return values and parameter types use int or unsigned, but zlib-ng headers use
133
   int32_t and uint32_t, which will cause type mismatch when compiling zlib-ng if int32_t is long and uint32_t is
134
   unsigned long */
135
#if defined(ZLIB_COMPAT)
136
#  define z_int32_t int
137
#  define z_uint32_t unsigned int
138
#else
139
#  define z_int32_t int32_t
140
#  define z_uint32_t uint32_t
141
#endif
142
143
/* Minimum of a and b. */
144
157k
#define MIN(a, b) ((a) > (b) ? (b) : (a))
145
/* Maximum of a and b. */
146
26.8k
#define MAX(a, b) ((a) < (b) ? (b) : (a))
147
/* Absolute value of a. */
148
100k
#define ABS(a) ((a) < 0 ? -(a) : (a))
149
/* Ignore unused variable warning */
150
124M
#define Z_UNUSED(var) (void)(var)
151
152
/* Force the compiler to treat variable as modified. Empty asm statement with a "+r" constraint prevents
153
   the compiler from reordering or eliminating loads into the variable. This can help keep critical latency
154
   chains in the hot path from being shortened or optimized away. */
155
#if (defined(__GNUC__) || defined(__clang__)) && \
156
        (defined(ARCH_X86) || (defined(ARCH_ARM) && defined(ARCH_64BIT)))
157
126M
#  define Z_TOUCH(var) __asm__ ("" : "+r"(var))
158
#else
159
#  define Z_TOUCH(var) (void)(var)
160
#endif
161
162
#if defined(HAVE_VISIBILITY_INTERNAL)
163
#  define Z_INTERNAL __attribute__((visibility ("internal")))
164
#elif defined(HAVE_VISIBILITY_HIDDEN)
165
#  define Z_INTERNAL __attribute__((visibility ("hidden")))
166
#else
167
#  define Z_INTERNAL
168
#endif
169
170
/* Symbol versioning helpers, allowing multiple versions of a function to exist.
171
 * Functions using this must also be added to zlib-ng.map for each version.
172
 * Double @@ means this is the default for newly compiled applications to link against.
173
 * Single @ means this is kept for backwards compatibility.
174
 * This is only used for Zlib-ng native API, and only on platforms supporting this.
175
 */
176
#if defined(HAVE_SYMVER)
177
#  define ZSYMVER(func,alias,ver) __asm__(".symver " func ", " alias "@ZLIB_NG_" ver);
178
#  define ZSYMVER_DEF(func,alias,ver) __asm__(".symver " func ", " alias "@@ZLIB_NG_" ver);
179
#else
180
#  define ZSYMVER(func,alias,ver)
181
#  define ZSYMVER_DEF(func,alias,ver)
182
#endif
183
184
#ifndef __cplusplus
185
#  define Z_REGISTER register
186
#else
187
#  define Z_REGISTER
188
#endif
189
190
#if defined(_MSC_VER)
191
#  define Z_RESTRICT __restrict
192
#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
193
#  define Z_RESTRICT restrict
194
#else
195
#  define Z_RESTRICT __restrict__
196
#endif
197
198
/* Reverse the bytes in a value. Use compiler intrinsics when
199
   possible to take advantage of hardware implementations. */
200
#if defined(_MSC_VER) && (_MSC_VER >= 1300)
201
#  include <stdlib.h>
202
#  pragma intrinsic(_byteswap_ulong)
203
#  define ZSWAP16(q) _byteswap_ushort(q)
204
#  define ZSWAP32(q) _byteswap_ulong(q)
205
#  define ZSWAP64(q) _byteswap_uint64(q)
206
207
#elif defined(__clang__) || (defined(__GNUC__) && \
208
        (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)))
209
4.45k
#  define ZSWAP16(q) __builtin_bswap16(q)
210
4.45k
#  define ZSWAP32(q) __builtin_bswap32(q)
211
#  define ZSWAP64(q) __builtin_bswap64(q)
212
213
#elif defined(__GNUC__) && (__GNUC__ >= 2) && defined(__linux__)
214
#  include <byteswap.h>
215
#  define ZSWAP16(q) bswap_16(q)
216
#  define ZSWAP32(q) bswap_32(q)
217
#  define ZSWAP64(q) bswap_64(q)
218
219
#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
220
#  include <sys/endian.h>
221
#  define ZSWAP16(q) bswap16(q)
222
#  define ZSWAP32(q) bswap32(q)
223
#  define ZSWAP64(q) bswap64(q)
224
#elif defined(__OpenBSD__)
225
#  include <sys/endian.h>
226
#  define ZSWAP16(q) swap16(q)
227
#  define ZSWAP32(q) swap32(q)
228
#  define ZSWAP64(q) swap64(q)
229
#elif defined(__INTEL_COMPILER)
230
/* ICC does not provide a two byte swap. */
231
#  define ZSWAP16(q) ((((q) & 0xff) << 8) | (((q) & 0xff00) >> 8))
232
#  define ZSWAP32(q) _bswap(q)
233
#  define ZSWAP64(q) _bswap64(q)
234
235
#else
236
#  define ZSWAP16(q) ((((q) & 0xff) << 8) | (((q) & 0xff00) >> 8))
237
#  define ZSWAP32(q) ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
238
                     (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
239
#  define ZSWAP64(q)                           \
240
         (((q & 0xFF00000000000000u) >> 56u) | \
241
          ((q & 0x00FF000000000000u) >> 40u) | \
242
          ((q & 0x0000FF0000000000u) >> 24u) | \
243
          ((q & 0x000000FF00000000u) >> 8u)  | \
244
          ((q & 0x00000000FF000000u) << 8u)  | \
245
          ((q & 0x0000000000FF0000u) << 24u) | \
246
          ((q & 0x000000000000FF00u) << 40u) | \
247
          ((q & 0x00000000000000FFu) << 56u))
248
#endif
249
250
/* Only enable likely/unlikely if the compiler is known to support it */
251
#if (defined(__GNUC__) && (__GNUC__ >= 3)) || defined(__INTEL_COMPILER) || defined(__clang__)
252
#  define LIKELY_NULL(x)        __builtin_expect((x) != 0, 0)
253
871M
#  define LIKELY(x)             __builtin_expect(!!(x), 1)
254
497M
#  define UNLIKELY(x)           __builtin_expect(!!(x), 0)
255
#else
256
#  define LIKELY_NULL(x)        x
257
#  define LIKELY(x)             x
258
#  define UNLIKELY(x)           x
259
#endif /* (un)likely */
260
261
#if defined(HAVE_ATTRIBUTE_ALIGNED)
262
8.91k
#  define ALIGNED_(x) __attribute__ ((aligned(x)))
263
#elif defined(_MSC_VER)
264
#  define ALIGNED_(x) __declspec(align(x))
265
#else
266
/* TODO: Define ALIGNED_ for your compiler */
267
#  define ALIGNED_(x)
268
#endif
269
270
#ifdef HAVE_BUILTIN_ASSUME_ALIGNED
271
40.1k
#  define HINT_ALIGNED(p,n) __builtin_assume_aligned((void *)(p),(n))
272
#else
273
#  define HINT_ALIGNED(p,n) (p)
274
#endif
275
4.45k
#define HINT_ALIGNED_16(p) HINT_ALIGNED((p),16)
276
35.6k
#define HINT_ALIGNED_64(p) HINT_ALIGNED((p),64)
277
#define HINT_ALIGNED_4096(p) HINT_ALIGNED((p),4096)
278
279
/* Number of bytes needed to align ptr to the next alignment boundary */
280
#define ALIGN_DIFF(ptr, align) \
281
0
    (((uintptr_t)(align) - ((uintptr_t)(ptr) & ((align) - 1))) & ((align) - 1))
282
283
/* Round up value to the nearest multiple of align (align must be power of 2) */
284
#define ALIGN_UP(value, align) \
285
    (((value) + ((align) - 1)) & ~((align) - 1))
286
287
/* Round down value to the nearest multiple of align (align must be power of 2) */
288
#define ALIGN_DOWN(value, align) \
289
90.6k
    ((value) & ~((align) - 1))
290
291
/* PADSZ returns needed bytes to pad bpos to pad size
292
 * PAD_NN calculates pad size and adds it to bpos, returning the result.
293
 * All take an integer or a pointer as bpos input.
294
 */
295
49.0k
#define PADSZ(bpos, pad) (((pad) - ((uintptr_t)(bpos) % (pad))) % (pad))
296
8.91k
#define PAD_16(bpos) ((bpos) + PADSZ((bpos),16))
297
40.1k
#define PAD_64(bpos) ((bpos) + PADSZ((bpos),64))
298
#define PAD_4096(bpos) ((bpos) + PADSZ((bpos),4096))
299
300
/* Diagnostic functions */
301
#ifdef ZLIB_DEBUG
302
   extern int Z_INTERNAL z_verbose;
303
   extern void Z_INTERNAL z_error(const char *m);
304
#  define Assert(cond, msg) {int _cond = (cond); if (!(_cond)) z_error(msg);}
305
#  define Trace(x) {if (z_verbose >= 0) fprintf x;}
306
#  define Tracev(x) {if (z_verbose > 0) fprintf x;}
307
#  define Tracevv(x) {if (z_verbose > 1) fprintf x;}
308
#  define Tracec(c, x) {if (z_verbose > 0 && (c)) fprintf x;}
309
#  define Tracecv(c, x) {if (z_verbose > 1 && (c)) fprintf x;}
310
#else
311
#  define Assert(cond, msg)
312
#  define Trace(x)
313
#  define Tracev(x)
314
#  define Tracevv(x)
315
#  define Tracec(c, x)
316
#  define Tracecv(c, x)
317
#endif
318
319
/* OPTIMAL_CMP values determine the comparison width:
320
 * 64: Best for 64-bit architectures with unaligned access
321
 * 32: Best for 32-bit architectures with unaligned access
322
 * 16: Safe default for unknown architectures
323
 * 8:  Safe fallback for architectures without unaligned access
324
 * Note: The unaligned access mentioned is cpu-support, this allows compiler or
325
 *       separate unaligned intrinsics to utilize safe unaligned access, without
326
 *       utilizing unaligned C pointers that are known to have undefined behavior.
327
 */
328
#if !defined(OPTIMAL_CMP)
329
#  ifdef ARCH_64BIT
330
#    ifdef ARCH_ARM
331
#      if defined(__ARM_FEATURE_UNALIGNED) || defined(_WIN32)
332
#        define OPTIMAL_CMP 64
333
#      else
334
#        define OPTIMAL_CMP 8
335
#      endif
336
#    else
337
#      define OPTIMAL_CMP 64
338
#    endif
339
#  elif defined(ARCH_32BIT)
340
#    ifdef ARCH_ARM
341
#      if defined(__ARM_FEATURE_UNALIGNED) || defined(_WIN32)
342
#        define OPTIMAL_CMP 32
343
#      else
344
#        define OPTIMAL_CMP 8
345
#      endif
346
#    else
347
#      define OPTIMAL_CMP 32
348
#    endif
349
#  endif
350
#endif
351
#if !defined(OPTIMAL_CMP)
352
#  define OPTIMAL_CMP 16
353
#endif
354
355
#endif