From: Cameron Cawley Date: Thu, 13 Oct 2022 13:59:18 +0000 (+0100) Subject: Support using aligned_alloc() for memory allocation X-Git-Tag: 2.1.0-beta1~136 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b3dcf11b4204a16fde71fa8224d7b7054e225b93;p=thirdparty%2Fzlib-ng.git Support using aligned_alloc() for memory allocation --- diff --git a/CMakeLists.txt b/CMakeLists.txt index ca395b2da..642d35f42 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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() diff --git a/configure b/configure index a64e1403a..552148caa 100755 --- a/configure +++ b/configure @@ -706,6 +706,25 @@ else fi echo >> configure.log +cat > $test.c < +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 < diff --git a/zbuild.h b/zbuild.h index c96d48b31..7fa4322ab 100644 --- 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 #include diff --git a/zutil_p.h b/zutil_p.h index fdfb4438a..318b4d901 100644 --- 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 #elif defined(__FreeBSD__) # include @@ -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