Coverage Report

Created: 2026-04-12 06:41

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