]> git.ipfire.org Git - thirdparty/zlib-ng.git/commitdiff
Support using aligned_alloc() for memory allocation
authorCameron Cawley <ccawley2011@gmail.com>
Thu, 13 Oct 2022 13:59:18 +0000 (14:59 +0100)
committerHans Kristian Rosbach <hk-github@circlestorm.org>
Tue, 1 Nov 2022 12:26:36 +0000 (13:26 +0100)
CMakeLists.txt
configure
zbuild.h
zutil_p.h

index ca395b2da632379915ab7307ea202d8cab902ab8..642d35f42b64798657ed3ee8501d53eb2f04c345 100644 (file)
@@ -343,6 +343,12 @@ if(HAVE_POSIX_MEMALIGN)
     add_definitions(-DHAVE_POSIX_MEMALIGN)
 endif()
 set(CMAKE_REQUIRED_DEFINITIONS)
+set(CMAKE_REQUIRED_DEFINITIONS -D_ISOC11_SOURCE=1)
+check_function_exists(aligned_alloc HAVE_ALIGNED_ALLOC)
+if(HAVE_ALIGNED_ALLOC)
+    add_definitions(-DHAVE_ALIGNED_ALLOC)
+endif()
+set(CMAKE_REQUIRED_DEFINITIONS)
 
 if(WITH_SANITIZER STREQUAL "Address")
     add_address_sanitizer()
index a64e1403ace4b924e9273c1c2293f4d59b2d7c66..552148caae2c04acdc7019ecf251a87a20b23472 100755 (executable)
--- a/configure
+++ b/configure
@@ -706,6 +706,25 @@ else
 fi
 echo >> configure.log
 
+cat > $test.c <<EOF
+#define _ISOC11_SOURCE 1
+#include <stdlib.h>
+int main(void) {
+  void *ptr = aligned_alloc(64, 10);
+  if (ptr)
+    free(ptr);
+  return 0;
+}
+EOF
+if try $CC $CFLAGS -o $test $test.c $LDSHAREDLIBC; then
+  echo "Checking for aligned_alloc... Yes." | tee -a configure.log
+  CFLAGS="${CFLAGS} -DHAVE_ALIGNED_ALLOC"
+  SFLAGS="${SFLAGS} -DHAVE_ALIGNED_ALLOC"
+else
+  echo "Checking for aligned_alloc... No." | tee -a configure.log
+fi
+echo >> configure.log
+
 # check for strerror() for use by gz* functions
 cat > $test.c <<EOF
 #include <string.h>
index c96d48b31f07e643d58e4d3ff1c6ca4b4107f436..7fa4322ab527c1407aef432f472680c664012dd8 100644 (file)
--- a/zbuild.h
+++ b/zbuild.h
@@ -5,6 +5,9 @@
 #ifndef _POSIX_C_SOURCE
 #  define _POSIX_C_SOURCE 200809L /* snprintf, posix_memalign, strdup */
 #endif
+#ifndef _ISOC11_SOURCE
+#  define _ISOC11_SOURCE 1 /* aligned_alloc */
+#endif
 
 #include <stddef.h>
 #include <string.h>
index fdfb4438a939735d2ce2c8b2278ca8b9ede17bdf..318b4d9011ed85f89045abd4a38618864e005e73 100644 (file)
--- a/zutil_p.h
+++ b/zutil_p.h
@@ -5,7 +5,7 @@
 #ifndef ZUTIL_P_H
 #define ZUTIL_P_H
 
-#if defined(__APPLE__) || defined(HAVE_POSIX_MEMALIGN)
+#if defined(__APPLE__) || defined(HAVE_POSIX_MEMALIGN) || defined(HAVE_ALIGNED_ALLOC)
 #  include <stdlib.h>
 #elif defined(__FreeBSD__)
 #  include <stdlib.h>
@@ -23,6 +23,8 @@ static inline void *zng_alloc(size_t size) {
     return (void *)_aligned_malloc(size, 64);
 #elif defined(__APPLE__)
     return (void *)malloc(size);     /* MacOS always aligns to 16 bytes */
+#elif defined(HAVE_ALIGNED_ALLOC)
+    return (void *)aligned_alloc(64, size);
 #else
     return (void *)memalign(64, size);
 #endif