1/* zutil_p.h -- Private inline functions used internally in zlib-ng
2 *
3 */
4
5#ifndef ZUTIL_P_H
6#define ZUTIL_P_H
7
8#if defined(HAVE_POSIX_MEMALIGN) && !defined(_POSIX_C_SOURCE)
9# define _POSIX_C_SOURCE 200112L /* For posix_memalign(). */
10#endif
11
12#if defined(__APPLE__) || defined(HAVE_POSIX_MEMALIGN)
13# include <stdlib.h>
14#elif defined(__FreeBSD__)
15# include <stdlib.h>
16# include <malloc_np.h>
17#else
18# include <malloc.h>
19#endif
20
21/* Function to allocate 16 or 64-byte aligned memory */
22static inline void *zng_alloc(size_t size) {
23#ifdef HAVE_POSIX_MEMALIGN
24 void *ptr;
25 return posix_memalign(memptr: &ptr, alignment: 64, size: size) ? NULL : ptr;
26#elif defined(_WIN32)
27 return (void *)_aligned_malloc(size, 64);
28#elif defined(__APPLE__)
29 return (void *)malloc(size); /* MacOS always aligns to 16 bytes */
30#else
31 return (void *)memalign(64, size);
32#endif
33}
34
35/* Function that can free aligned memory */
36static inline void zng_free(void *ptr) {
37#if defined(_WIN32)
38 _aligned_free(ptr);
39#else
40 free(ptr: ptr);
41#endif
42}
43
44#endif
45