]> git.ipfire.org Git - thirdparty/zlib-ng.git/commitdiff
Added build system check for posix_memalign support.
authorNathan Moinvaziri <nathan@nathanm.com>
Sat, 26 Jun 2021 00:23:34 +0000 (17:23 -0700)
committerHans Kristian Rosbach <hk-github@circlestorm.org>
Wed, 7 Jul 2021 17:55:08 +0000 (19:55 +0200)
Co-authored-by: concatime <concatime@users.noreply@github.com>
Co-authored-by: Mika Lindqvist <postmaster@raasu.org>
CMakeLists.txt
configure
zutil.c
zutil_p.h

index 1530e4f911c9f581941cb955d6d281f855b50997..51b4555d00103deb5e0ce7269d3f8aae7d80c94a 100644 (file)
@@ -323,6 +323,12 @@ check_function_exists(strerror HAVE_STRERROR)
 if(NOT HAVE_STRERROR)
     add_definitions(-DNO_STRERROR)
 endif()
+set(CMAKE_REQUIRED_DEFINITIONS -D _POSIX_C_SOURCE=200112L)
+check_function_exists(posix_memalign HAVE_POSIX_MEMALIGN)
+if(HAVE_POSIX_MEMALIGN)
+    add_definitions(-DHAVE_POSIX_MEMALIGN)
+endif()
+set(CMAKE_REQUIRED_DEFINITIONS)
 
 if(WITH_SANITIZER STREQUAL "Address")
     add_address_sanitizer()
index 0dc1dd6a76acefab0b985953ce3f45f7c4a1af69..3c278cfcddd786fc9862bf09a53154e18c46d652 100755 (executable)
--- a/configure
+++ b/configure
@@ -747,7 +747,26 @@ EOF
     echo "Checking for fseeko... No." | tee -a configure.log
   fi
 fi
+echo >> configure.log
 
+cat > $test.c <<EOF
+#define _POSIX_C_SOURCE 200112L
+#include <stdlib.h>
+int main(void) {
+  void *ptr = 0;
+  int ret = posix_memalign(&ptr, 64, 10);
+  if (ptr)
+    free(ptr);
+  return ret;
+}
+EOF
+if try $CC $CFLAGS -o $test $test.c $LDSHAREDLIBC; then
+  echo "Checking for posix_memalign... Yes." | tee -a configure.log
+  CFLAGS="${CFLAGS} -DHAVE_POSIX_MEMALIGN"
+  SFLAGS="${SFLAGS} -DHAVE_POSIX_MEMALIGN"
+else
+  echo "Checking for posix_memalign... No." | tee -a configure.log
+fi
 echo >> configure.log
 
 # check for strerror() for use by gz* functions
diff --git a/zutil.c b/zutil.c
index 53e43b0dea1a9e035fcd51880841517d18b3f534..18db2fe00f6cc5ed5bf33555b556292a95d08330 100644 (file)
--- a/zutil.c
+++ b/zutil.c
@@ -4,8 +4,8 @@
  */
 
 #include "zbuild.h"
-#include "zutil.h"
 #include "zutil_p.h"
+#include "zutil.h"
 
 z_const char * const PREFIX(z_errmsg)[10] = {
     (z_const char *)"need dictionary",     /* Z_NEED_DICT       2  */
index c8122607ea824283d49db23456d2a6972c8cfeab..55f00611b3c06cbd91f8cda530c80e9a6f441a3c 100644 (file)
--- a/zutil_p.h
+++ b/zutil_p.h
@@ -5,7 +5,11 @@
 #ifndef ZUTIL_P_H
 #define ZUTIL_P_H
 
-#if defined(__APPLE__) || defined(__OpenBSD__)
+#if defined(HAVE_POSIX_MEMALIGN) && !defined(_POSIX_C_SOURCE)
+#  define _POSIX_C_SOURCE 200112L  /* For posix_memalign(). */
+#endif
+
+#if defined(__APPLE__) || defined(HAVE_POSIX_MEMALIGN)
 #  include <stdlib.h>
 #elif defined(__FreeBSD__)
 #  include <stdlib.h>
@@ -16,7 +20,7 @@
 
 /* Function to allocate 16 or 64-byte aligned memory */
 static inline void *zng_alloc(size_t size) {
-#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
+#ifdef HAVE_POSIX_MEMALIGN
     void *ptr;
     return posix_memalign(&ptr, 64, size) ? NULL : ptr;
 #elif defined(_WIN32)