From 98666ba14a3a9a1f927dbc56090cbc7816c19504 Mon Sep 17 00:00:00 2001 From: Nathan Moinvaziri Date: Mon, 27 Jan 2020 22:10:49 -0800 Subject: [PATCH] Support aligned alloc/free functions for Windows and define them only if MZ_ZALLOC is not defined. --- zutil.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/zutil.c b/zutil.c index e2da87f9..8e2e6644 100644 --- a/zutil.c +++ b/zutil.c @@ -10,9 +10,6 @@ #ifdef WITH_GZFILEOP # include "gzguts.h" #endif -#ifndef UNALIGNED_OK -# include -#endif const char * const zng_errmsg[10] = { (const char *)"need dictionary", /* Z_NEED_DICT 2 */ @@ -111,10 +108,22 @@ const char * ZEXPORT PREFIX(zError)(int err) { #ifndef MY_ZCALLOC /* Any system without a special alloc function */ -void ZLIB_INTERNAL *zng_calloc(void *opaque, unsigned items, unsigned size) { +#ifndef UNALIGNED_OK +# include +# if defined(_WIN32) +# define zng_align_alloc(align, size) _aligned_malloc(size, align) +# define zng_align_free(ptr) _aligned_free(ptr) +# else +# define zng_align_alloc memalign +# define zng_align_free(ptr) free(ptr) +# endif +#endif + +void ZLIB_INTERNAL *zng_calloc(void *opaque, unsigned items, unsigned size) +{ (void)opaque; #ifndef UNALIGNED_OK - return memalign(16, items * size); + return zng_align_alloc(16, items * size); #else return sizeof(unsigned int) > 2 ? (void *)malloc(items * size) : (void *)calloc(items, size); @@ -123,7 +132,11 @@ void ZLIB_INTERNAL *zng_calloc(void *opaque, unsigned items, unsigned size) { void ZLIB_INTERNAL zng_cfree(void *opaque, void *ptr) { (void)opaque; +#ifndef UNALIGNED_OK + zng_align_free(ptr); +#else free(ptr); +#endif } #endif /* MY_ZCALLOC */ -- 2.47.2