summaryrefslogtreecommitdiff
path: root/zutil.c
diff options
context:
space:
mode:
authorNathan Moinvaziri <nathan@nathanm.com>2020-01-27 22:10:49 -0800
committerHans Kristian Rosbach <hk-github@circlestorm.org>2020-02-07 19:33:30 +0100
commit98666ba14a3a9a1f927dbc56090cbc7816c19504 (patch)
treebff856c961506c8904f8740f4f488b3cd993c7f0 /zutil.c
parentfeff87a53e3cf5baab5e270d01d1331593cbada3 (diff)
Support aligned alloc/free functions for Windows and define them only if MZ_ZALLOC is not defined.
Diffstat (limited to 'zutil.c')
-rw-r--r--zutil.c23
1 files changed, 18 insertions, 5 deletions
diff --git a/zutil.c b/zutil.c
index e2da87f..8e2e664 100644
--- 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 */