]> git.ipfire.org Git - thirdparty/zlib-ng.git/commitdiff
Support aligned alloc/free functions for Windows and define them only if MZ_ZALLOC...
authorNathan Moinvaziri <nathan@nathanm.com>
Tue, 28 Jan 2020 06:10:49 +0000 (22:10 -0800)
committerHans Kristian Rosbach <hk-github@circlestorm.org>
Fri, 7 Feb 2020 18:33:30 +0000 (19:33 +0100)
zutil.c

diff --git a/zutil.c b/zutil.c
index e2da87f9d56766f5f030e3357f33b53cc40ad376..8e2e66444f5b0e79a8722ab07f2c5dafa8f8236a 100644 (file)
--- a/zutil.c
+++ b/zutil.c
@@ -10,9 +10,6 @@
 #ifdef WITH_GZFILEOP
 #  include "gzguts.h"
 #endif
-#ifndef UNALIGNED_OK
-#  include <malloc.h>
-#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 <malloc.h>
+#  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 */