Coverage Report

Created: 2026-07-25 06:58

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