]> 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>
Fri, 17 Mar 2023 20:27:56 +0000 (21:27 +0100)
CMakeLists.txt
configure
zbuild.h
zutil_p.h

index bb29d76d22c08a20d0ffba22793e2754449ba2c4..a7c6806a5024a2a2b605f944d7a4814980eb3a55 100644 (file)
@@ -388,6 +388,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 998ba3c03a7ab946fa779f9eecdf6e035fb89a5c..cc51d21474535bcf21e31ede8636774c9aceca18 100755 (executable)
--- a/configure
+++ b/configure
@@ -750,6 +750,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 3c5e5fb407c33ec8832240ef22b3969f4be419d4..f0f9202cbfadae144659c5db60f67153587f96b5 100644 (file)
--- a/zbuild.h
+++ b/zbuild.h
@@ -1,6 +1,10 @@
 #ifndef _ZBUILD_H
 #define _ZBUILD_H
 
+#ifndef _ISOC11_SOURCE
+#  define _ISOC11_SOURCE 1 /* aligned_alloc */
+#endif
+
 /* This has to be first include that defines any types */
 #if defined(_MSC_VER)
 #  if defined(_WIN64)
index 55f00611b3c06cbd91f8cda530c80e9a6f441a3c..856b44f90463947c7c763d91e3fa07584fe4a956 100644 (file)
--- a/zutil_p.h
+++ b/zutil_p.h
@@ -9,7 +9,7 @@
 #  define _POSIX_C_SOURCE 200112L  /* For posix_memalign(). */
 #endif
 
-#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>
@@ -27,6 +27,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