aboutsummaryrefslogtreecommitdiff
path: root/zutil.h
diff options
context:
space:
mode:
authorMark Adler <git@madler.net>2026-01-05 01:15:38 -0600
committerMark Adler <git@madler.net>2026-01-05 15:03:04 -0600
commitc267ef7306fe9fc1399833e3556cd9798dec2eb0 (patch)
treebc18574b8ea4fc37befdf0174fb5f04472911f6f /zutil.h
parent916dc1ac351795c9bf86a3d19c3667b014b9d28e (diff)
downloadzlib-c267ef7306fe9fc1399833e3556cd9798dec2eb0.tar.gz
zlib-c267ef7306fe9fc1399833e3556cd9798dec2eb0.tar.bz2
zlib-c267ef7306fe9fc1399833e3556cd9798dec2eb0.zip
Use atomics to build inflate fixed tables once.
This moves the once code from crc32.c to zutil.c, and uses it also for building the inflate fixed tables when BUILDFIXED is defined. The fixed tables are now housed in inftrees.c, shared by inflate.c and infback.c. The once() function is now external, and so is renamed to z_once() to avoid name collisions. If either BUILDFIXED or DYNAMIC_CRC_TABLE is defined, and atomics are not available, then a warning is issued noting that zlib is not thread-safe.
Diffstat (limited to 'zutil.h')
-rw-r--r--zutil.h26
1 files changed, 26 insertions, 0 deletions
diff --git a/zutil.h b/zutil.h
index 27ca693..94fa1af 100644
--- a/zutil.h
+++ b/zutil.h
@@ -254,4 +254,30 @@ extern z_const char * const z_errmsg[10]; /* indexed by 2-zlib_error */
254#define ZSWAP32(q) ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \ 254#define ZSWAP32(q) ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
255 (((q) & 0xff00) << 8) + (((q) & 0xff) << 24)) 255 (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
256 256
257#ifdef MAKEFIXED
258# ifndef BUILDFIXED
259# define BUILDFIXED
260# endif
261#endif
262#if defined(BUILDFIXED) || defined(DYNAMIC_CRC_TABLE)
263/* Structure for z_once(), which must be initialized with Z_ONCE_INIT. */
264typedef struct z_once_s z_once_t;
265void ZLIB_INTERNAL z_once(z_once_t *state, void (*init)(void));
266#if defined(__STDC__) && __STDC_VERSION__ >= 201112L && \
267 !defined(__STDC_NO_ATOMICS__)
268#include <stdatomic.h>
269struct z_once_s {
270 atomic_flag begun;
271 atomic_int done;
272};
273#define Z_ONCE_INIT {ATOMIC_FLAG_INIT, 0}
274#else /* no atomics! */
275struct z_once_s {
276 volatile int begun;
277 volatile int done;
278};
279#define Z_ONCE_INIT {0, 0}
280#endif
281#endif
282
257#endif /* ZUTIL_H */ 283#endif /* ZUTIL_H */