]> git.ipfire.org Git - thirdparty/zlib-ng.git/commitdiff
Remove usage of aligned alloc implementations and instead use calloc. calloc 1911/head
authorHans Kristian Rosbach <hk-git@circlestorm.org>
Tue, 6 May 2025 13:28:32 +0000 (15:28 +0200)
committerHans Kristian Rosbach <hk-git@circlestorm.org>
Tue, 6 May 2025 14:10:14 +0000 (16:10 +0200)
Using calloc has advantages like ensuring memory is zeroed and depending
on OS implementation can improve speed and cache-locality by
pre-zeroing or only zeroing pages that are actually touched.

20 files changed:
CMakeLists.txt
configure
deflate.c
gzlib.c
gzread.c.in
gzwrite.c
test/benchmarks/benchmark_adler32.cc
test/benchmarks/benchmark_adler32_copy.cc
test/benchmarks/benchmark_compare256.cc
test/benchmarks/benchmark_compare256_rle.cc
test/benchmarks/benchmark_compress.cc
test/benchmarks/benchmark_crc32.cc
test/benchmarks/benchmark_slidehash.cc
test/benchmarks/benchmark_uncompress.cc
test/test_crc32.cc
zlib_name_mangling-ng.h.in
zlib_name_mangling.h.in
zutil.c
zutil.h
zutil_p.h [deleted file]

index 7afa5319b4bc4a28f54dd464a7088a309d359c2d..541d36c0e7a9a863c7934dd3b744b8b89898009d 100644 (file)
@@ -456,49 +456,6 @@ if(NOT HAVE_STRERROR)
 endif()
 set(CMAKE_REQUIRED_FLAGS)
 
-#
-# Check for aligned memory allocation support: POSIX
-#
-set(CMAKE_REQUIRED_DEFINITIONS -D_POSIX_C_SOURCE=200112L -D_ISOC11_SOURCE=1)
-set(CMAKE_REQUIRED_FLAGS "${ADDITIONAL_CHECK_FLAGS}")
-check_symbol_exists(posix_memalign stdlib.h HAVE_POSIX_MEMALIGN)
-if(HAVE_POSIX_MEMALIGN)
-    add_definitions(-DHAVE_POSIX_MEMALIGN)
-endif()
-set(CMAKE_REQUIRED_FLAGS)
-set(CMAKE_REQUIRED_DEFINITIONS)
-
-#
-# Check for aligned memory allocation support: C11
-#
-set(CMAKE_REQUIRED_DEFINITIONS -D_POSIX_C_SOURCE=200112L -D_ISOC11_SOURCE=1)
-set(CMAKE_REQUIRED_FLAGS "${ADDITIONAL_CHECK_FLAGS}")
-check_symbol_exists(aligned_alloc stdlib.h HAVE_ALIGNED_ALLOC)
-if(HAVE_ALIGNED_ALLOC)
-    add_definitions(-DHAVE_ALIGNED_ALLOC)
-endif()
-set(CMAKE_REQUIRED_FLAGS)
-set(CMAKE_REQUIRED_DEFINITIONS)
-
-#
-# Check for aligned memory allocation support: GNU extension
-# Note: memalign() is deprecated in glibc
-#
-check_symbol_exists(memalign malloc.h HAVE_MEMALIGN)
-if(HAVE_MEMALIGN)
-    add_definitions(-DHAVE_MEMALIGN)
-endif()
-set(CMAKE_REQUIRED_DEFINITIONS)
-
-#
-# Check for aligned memory allocation support: MSVC extensions
-#
-check_symbol_exists(_aligned_malloc malloc.h HAVE__ALIGNED_MALLOC)
-if(HAVE__ALIGNED_MALLOC)
-    add_definitions(-DHAVE__ALIGNED_MALLOC)
-endif()
-set(CMAKE_REQUIRED_DEFINITIONS)
-
 #
 # Check for x86 __cpuid / __cpuid_count support: GNU extensions
 # gcc/clang and MSVC have __cpuid, so check __cpuid_count instead
@@ -1250,7 +1207,6 @@ set(ZLIB_PRIVATE_HDRS
     zendian.h
     zmemory.h
     zutil.h
-    zutil_p.h
 )
 set(ZLIB_SRCS
     arch/generic/adler32_c.c
index 869cc5c3eb62fc887c8b35280ffd6574679b4685..5c365b169b24758e68885ca2802c356843d513ac 100755 (executable)
--- a/configure
+++ b/configure
@@ -731,91 +731,6 @@ EOF
 fi
 echo >> configure.log
 
-
-# check for aligned memory allocation support: POSIX
-cat > $test.c <<EOF
-#define _POSIX_C_SOURCE 200112L
-#define _ISOC11_SOURCE 1
-#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 $ADDITIONAL_CHECK_FLAGS -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 aligned memory allocation support: C11
-cat > $test.c <<EOF
-#define _POSIX_C_SOURCE 200112L
-#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 $ADDITIONAL_CHECK_FLAGS -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 aligned memory allocation support: GNU extension
-# Note: memalign() is deprecated in glibc
-cat > $test.c <<EOF
-#include <malloc.h>
-int main(void) {
-  void *ptr = memalign(64, 10);
-  if (ptr)
-    free(ptr);
-  return 0;
-}
-EOF
-if try $CC $CFLAGS -o $test $test.c $LDSHAREDLIBC; then
-  echo "Checking for memalign... Yes." | tee -a configure.log
-  CFLAGS="${CFLAGS} -DHAVE_MEMALIGN"
-  SFLAGS="${SFLAGS} -DHAVE_MEMALIGN"
-else
-  echo "Checking for memalign... No." | tee -a configure.log
-fi
-echo >> configure.log
-
-# check for aligned memory allocation support: MSVC extensions
-cat > $test.c <<EOF
-#include <malloc.h>
-int main(void) {
-  void *ptr = _aligned_malloc(10, 64);
-  if (ptr)
-    _aligned_free(ptr);
-  return 0;
-}
-EOF
-if try $CC $CFLAGS -o $test $test.c $LDSHAREDLIBC; then
-  echo "Checking for _aligned_malloc... Yes." | tee -a configure.log
-  CFLAGS="${CFLAGS} -DHAVE__ALIGNED_MALLOC"
-  SFLAGS="${SFLAGS} -DHAVE__ALIGNED_MALLOC"
-else
-  echo "Checking for _aligned_malloc... No." | tee -a configure.log
-fi
-echo >> configure.log
-
-
 # check for cpuid support: GNU extensions
 cat > $test.c <<EOF
 #include <cpuid.h>
@@ -870,7 +785,6 @@ else
 fi
 echo >> configure.log
 
-
 # check for strerror() for use by gz* functions
 cat > $test.c <<EOF
 #include <string.h>
index 630cce2553b9233ac31cd400260c30d6ae7504fb..fefdd6027e125308e4538eb52a7259b580348326 100644 (file)
--- a/deflate.c
+++ b/deflate.c
@@ -258,8 +258,6 @@ Z_INTERNAL deflate_allocs* alloc_deflate(PREFIX3(stream) *strm, int windowBits,
     alloc_bufs->pending_buf = (unsigned char *)HINT_ALIGNED_64(buff + pending_pos);
     alloc_bufs->state = (deflate_state *)HINT_ALIGNED_16(buff + state_pos);
 
-    memset((char *)alloc_bufs->prev, 0, prev_size);
-
     return alloc_bufs;
 }
 
diff --git a/gzlib.c b/gzlib.c
index b8a506b6a51f31c058dbb4d4e84cb6a5383c11a3..17466dba7773d396eda048ea8f4f5e40bf5aba94 100644 (file)
--- a/gzlib.c
+++ b/gzlib.c
@@ -4,7 +4,7 @@
  */
 
 #include "zbuild.h"
-#include "zutil_p.h"
+#include "zutil.h"
 #include "gzguts.h"
 
 #if defined(_WIN32)
index 1fc7b370fde38d246c0c99ff9d802bd069502cb3..6cd877631389a8783c4f2853150fc7cab0369705 100644 (file)
@@ -4,7 +4,7 @@
  */
 
 #include "zbuild.h"
-#include "zutil_p.h"
+#include "zutil.h"
 #include "gzguts.h"
 
 /* Local functions */
index 08e0ce9aab2a401073f851d265d39c9755fda6c7..c23b946f7ca0e3400a9ee9ed672af879aee5c4ef 100644 (file)
--- a/gzwrite.c
+++ b/gzwrite.c
@@ -4,7 +4,7 @@
  */
 
 #include "zbuild.h"
-#include "zutil_p.h"
+#include "zutil.h"
 #include <stdarg.h>
 #include "gzguts.h"
 
index b1278950d0766bcf48cefb65ff5078c2701296ca..4550325738795a5493a7946a078fc2f068ab48b3 100644 (file)
@@ -10,7 +10,7 @@
 
 extern "C" {
 #  include "zbuild.h"
-#  include "zutil_p.h"
+#  include "zutil.h"
 #  include "arch_functions.h"
 #  include "../test_cpu_features.h"
 }
index bca8df18a891b9de90722078f159a6794c6b28bf..fc851a4bc5d8c8293953a5073856619faffb0163 100644 (file)
@@ -11,7 +11,7 @@
 
 extern "C" {
 #  include "zbuild.h"
-#  include "zutil_p.h"
+#  include "zutil.h"
 #  include "arch_functions.h"
 #  include "../test_cpu_features.h"
 }
index 8ed2d0eb3dbd4e272c3543b21e431437db14ecc7..8f28a390a8bf11e9eca3c23ceadc65a1ef8a253f 100644 (file)
@@ -9,7 +9,7 @@
 
 extern "C" {
 #  include "zbuild.h"
-#  include "zutil_p.h"
+#  include "zutil.h"
 #  include "arch_functions.h"
 #  include "../test_cpu_features.h"
 #  include "arch/generic/compare256_p.h"
index 5e6bb643c8077896f00109c95d8865ed5be8a56f..abc1cf8a22a33db01db68de3d2722d98be6c54d0 100644 (file)
@@ -9,7 +9,7 @@
 
 extern "C" {
 #  include "zbuild.h"
-#  include "zutil_p.h"
+#  include "zutil.h"
 #  include "compare256_rle.h"
 }
 
index 400a7869e4805862e53faa6c3cf876891a915f6e..c14fd09426f366abc0033e12253e697f41e14e82 100644 (file)
@@ -9,7 +9,7 @@
 
 extern "C" {
 #  include "zbuild.h"
-#  include "zutil_p.h"
+#  include "zutil.h"
 #  if defined(ZLIB_COMPAT)
 #    include "zlib.h"
 #  else
index 23a1dc196f6e0bde5104d47b1d9b8eb3fd35723a..490c13b5b50a59ca58d0e5a2a7aac546729fd7ae 100644 (file)
@@ -10,7 +10,7 @@
 
 extern "C" {
 #  include "zbuild.h"
-#  include "zutil_p.h"
+#  include "zutil.h"
 #  include "arch_functions.h"
 #  include "../test_cpu_features.h"
 }
index 4e9b20ee3fa028b6fe287bb0f8bd59feadd8336d..c5f976dd8e5da2655d3b3f29ac9986c42ceb4cbe 100644 (file)
@@ -9,7 +9,7 @@
 
 extern "C" {
 #  include "zbuild.h"
-#  include "zutil_p.h"
+#  include "zutil.h"
 #  include "deflate.h"
 #  include "arch_functions.h"
 #  include "../test_cpu_features.h"
index 5653a350dac8943585da379106511163b0654515..99b0bed2478696fd9087d60ef95bb36cd36b1a9b 100644 (file)
@@ -9,7 +9,7 @@
 
 extern "C" {
 #  include "zbuild.h"
-#  include "zutil_p.h"
+#  include "zutil.h"
 #  if defined(ZLIB_COMPAT)
 #    include "zlib.h"
 #  else
index 2f768d0c4e31d274fd381d1176c9a2866409d1e1..60699fd7a8fe9b989113166aac765ec5e7383185 100644 (file)
@@ -9,7 +9,6 @@
 #include <string.h>
 #include <stdlib.h>
 #include "zutil.h"
-#include "zutil_p.h"
 
 extern "C" {
 #  include "zbuild.h"
index e90904a7375dd5074226323c17727fdbbb07e6e5..b0a8b8e5ca1d66f7f4f7e002c45174ead9edc848 100644 (file)
 #define zng_vstring            @ZLIB_SYMBOL_PREFIX@zng_vstring
 #define zng_zError             @ZLIB_SYMBOL_PREFIX@zng_zError
 
-#define zng_alloc_aligned      @ZLIB_SYMBOL_PREFIX@zng_alloc_aligned
-#define zng_free_aligned       @ZLIB_SYMBOL_PREFIX@zng_free_aligned
 #define zng_get_crc_table      @ZLIB_SYMBOL_PREFIX@zng_get_crc_table
 #define zng_inflateSyncPoint   @ZLIB_SYMBOL_PREFIX@zng_inflateSyncPoint
 #define zng_inflateUndermine   @ZLIB_SYMBOL_PREFIX@zng_inflateUndermine
index f49615818114ea67e046a95af0c4f40a351eceb0..703a11e4600a43a7e0bf27fb5d6f48f89486afe1 100644 (file)
 #define zlibng_version        @ZLIB_SYMBOL_PREFIX@zlibng_version
 
 /* zlib-ng specific symbols */
-#define zng_alloc_aligned     @ZLIB_SYMBOL_PREFIX@zng_alloc_aligned
-#define zng_free_aligned      @ZLIB_SYMBOL_PREFIX@zng_free_aligned
 
 #endif /* ZLIB_NAME_MANGLING_H */
diff --git a/zutil.c b/zutil.c
index 99818c3a7b9e04614a65b14bc9edab8aed9b9010..7f6ff2f8376d9556404c237a480c5ccfb4ef7e1b 100644 (file)
--- a/zutil.c
+++ b/zutil.c
@@ -4,7 +4,6 @@
  */
 
 #include "zbuild.h"
-#include "zutil_p.h"
 #include "zutil.h"
 
 z_const char * const PREFIX(z_errmsg)[10] = {
@@ -100,6 +99,8 @@ const char * Z_EXPORT PREFIX(zError)(int err) {
     return ERR_MSG(err);
 }
 
+// Zlib-ng's default alloc/free implementation, used unless
+// application supplies its own alloc/free functions.
 void Z_INTERNAL *PREFIX(zcalloc)(void *opaque, unsigned items, unsigned size) {
     Z_UNUSED(opaque);
     return zng_alloc((size_t)items * (size_t)size);
diff --git a/zutil.h b/zutil.h
index a6284502d047e8af74a5d0159da9825b80625208..8c65dbba2396b24637035004a00fc3ae2748fc1c 100644 (file)
--- a/zutil.h
+++ b/zutil.h
@@ -137,4 +137,12 @@ void Z_INTERNAL  PREFIX(zcfree)(void *opaque, void *ptr);
 typedef void *zng_calloc_func(void *opaque, unsigned items, unsigned size);
 typedef void  zng_cfree_func(void *opaque, void *ptr);
 
+static inline void *zng_alloc(size_t size) {
+    return calloc(1, size);
+}
+
+static inline void zng_free(void *ptr) {
+    free(ptr);
+}
+
 #endif /* ZUTIL_H_ */
diff --git a/zutil_p.h b/zutil_p.h
deleted file mode 100644 (file)
index 8f596eb..0000000
--- a/zutil_p.h
+++ /dev/null
@@ -1,51 +0,0 @@
-/* zutil_p.h -- Private inline functions used internally in zlib-ng
- * For conditions of distribution and use, see copyright notice in zlib.h
- */
-
-#ifndef ZUTIL_P_H
-#define ZUTIL_P_H
-
-#if defined(HAVE_POSIX_MEMALIGN) || defined(HAVE_ALIGNED_ALLOC)
-#  include <stdlib.h>
-#elif defined(__FreeBSD__)
-#  include <stdlib.h>
-#  include <malloc_np.h>
-#elif defined(HAVE_MEMALIGN) || defined(HAVE__ALIGNED_MALLOC)
-#  include <malloc.h>
-#else
-/* Fallback, when no other aligned allocation function was found */
-#  include <stdlib.h>
-#endif
-
-/* Function to allocate 16 or 64-byte aligned memory */
-static inline void *zng_alloc(size_t size) {
-#ifdef HAVE_ALIGNED_ALLOC
-    /* Size must be a multiple of alignment */
-    size = (size + (64 - 1)) & ~(64 - 1);
-    return (void *)aligned_alloc(64, size);  /* Defined in C11 */
-#elif defined(HAVE_POSIX_MEMALIGN)
-    void *ptr;
-    return posix_memalign(&ptr, 64, size) ? NULL : ptr;
-#elif defined(HAVE__ALIGNED_MALLOC)
-    /* Fallback: Use MSVC extensions: _aligned_malloc() / _aligned_free() */
-    return (void *)_aligned_malloc(size, 64);
-#elif defined(HAVE_MEMALIGN)
-    /* Fallback: Use deprecated GNU extension: memalign() */
-    return (void *)memalign(64, size);
-#else
-    /* Fallback: Use a normal allocation (On macOS, alignment is 16 bytes) */
-    /* zlib-ng adjust allocations for [de]compression to be properly aligned */
-    return (void *)malloc(size);
-#endif
-}
-
-/* Function that can free aligned memory */
-static inline void zng_free(void *ptr) {
-#if defined(HAVE__ALIGNED_MALLOC) && !defined(HAVE_POSIX_MEMALIGN) && !defined(HAVE_ALIGNED_ALLOC)
-    _aligned_free(ptr);
-#else
-    free(ptr);
-#endif
-}
-
-#endif