From: Hans Kristian Rosbach Date: Thu, 18 Apr 2024 13:28:56 +0000 (+0200) Subject: Clean up memory allocation functions that are no longer used, and its tests. X-Git-Tag: 2.2.0~7 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5b208676f8db6aa169bf7a355226034404a4f503;p=thirdparty%2Fzlib-ng.git Clean up memory allocation functions that are no longer used, and its tests. Co-authored-by: Ilya Leoshkevich --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 060f6cf56..719673fbf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -851,9 +851,6 @@ if(WITH_OPTIM) list(APPEND ZLIB_ARCH_SRCS ${ARCHDIR}/s390_features.c) endif() endif() - if(WITH_DFLTCC_DEFLATE OR WITH_DFLTCC_INFLATE) - list(APPEND ZLIB_ARCH_SRCS ${ARCHDIR}/dfltcc_common.c) - endif() if(WITH_DFLTCC_DEFLATE) add_definitions(-DS390_DFLTCC_DEFLATE) list(APPEND ZLIB_ARCH_SRCS ${ARCHDIR}/dfltcc_deflate.c) diff --git a/arch/s390/Makefile.in b/arch/s390/Makefile.in index 6b4fba777..e994157df 100644 --- a/arch/s390/Makefile.in +++ b/arch/s390/Makefile.in @@ -20,12 +20,6 @@ s390_features.o: s390_features.lo: $(CC) $(SFLAGS) $(INCLUDES) -c -o $@ $(SRCDIR)/s390_features.c -dfltcc_common.o: - $(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $(SRCDIR)/dfltcc_common.c - -dfltcc_common.lo: - $(CC) $(SFLAGS) $(INCLUDES) -c -o $@ $(SRCDIR)/dfltcc_common.c - dfltcc_deflate.o: $(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $(SRCDIR)/dfltcc_deflate.c diff --git a/arch/s390/README.md b/arch/s390/README.md index 0f3329108..57e9ee8f1 100644 --- a/arch/s390/README.md +++ b/arch/s390/README.md @@ -62,9 +62,10 @@ integrated with the rest of zlib-ng using hook macros. DFLTCC takes as arguments a parameter block, an input buffer, an output buffer, and a window. Parameter blocks are stored alongside zlib states; -buffers are forwarded from the caller; and window (which must be page-aligned) -is managed using `ZALLOC_WINDOW()`, `ZCOPY_WINDOW()` and `TRY_FREE_WINDOW()` -macros. +buffers are forwarded from the caller; and window - which must be +4k-aligned and is always 64k large, is managed using the `PAD_WINDOW()`, +`WINDOW_PAD_SIZE`, `HINT_ALIGNED_WINDOW` and `DEFLATE_ADJUST_WINDOW_SIZE()` +and `INFLATE_ADJUST_WINDOW_SIZE()` hooks. Software and hardware window formats do not match, therefore, `deflateSetDictionary()`, `deflateGetDictionary()`, `inflateSetDictionary()` diff --git a/arch/s390/dfltcc_common.c b/arch/s390/dfltcc_common.c deleted file mode 100644 index 78be71811..000000000 --- a/arch/s390/dfltcc_common.c +++ /dev/null @@ -1,40 +0,0 @@ -/* dfltcc_deflate.c - IBM Z DEFLATE CONVERSION CALL general support. */ - -#include "zbuild.h" -#include "dfltcc_common.h" -#include "dfltcc_detail.h" - -/* - Memory management. - - DFLTCC requires parameter blocks and window to be aligned. zlib-ng allows - users to specify their own allocation functions, so using e.g. - `posix_memalign' is not an option. Thus, we overallocate and take the - aligned portion of the buffer. -*/ - -static const int PAGE_ALIGN = 0x1000; - -void Z_INTERNAL *PREFIX(dfltcc_alloc_window)(PREFIX3(streamp) strm, uInt items, uInt size) { - void *p; - void *w; - - /* To simplify freeing, we store the pointer to the allocated buffer right - * before the window. Note that DFLTCC always uses HB_SIZE bytes. - */ - p = ZALLOC(strm, sizeof(void *) + MAX(items * size, HB_SIZE) + PAGE_ALIGN, sizeof(unsigned char)); - if (p == NULL) - return NULL; - w = ALIGN_UP((char *)p + sizeof(void *), PAGE_ALIGN); - *(void **)((char *)w - sizeof(void *)) = p; - return w; -} - -void Z_INTERNAL PREFIX(dfltcc_copy_window)(void *dest, const void *src, size_t n) { - memcpy(dest, src, MAX(n, HB_SIZE)); -} - -void Z_INTERNAL PREFIX(dfltcc_free_window)(PREFIX3(streamp) strm, void *w) { - if (w) - ZFREE(strm, *(void **)((unsigned char *)w - sizeof(void *))); -} diff --git a/arch/s390/dfltcc_common.h b/arch/s390/dfltcc_common.h index 8aec9e945..a6527ab5d 100644 --- a/arch/s390/dfltcc_common.h +++ b/arch/s390/dfltcc_common.h @@ -83,18 +83,6 @@ typedef struct { struct dfltcc_state common; } arch_inflate_state; -void Z_INTERNAL *PREFIX(dfltcc_alloc_window)(PREFIX3(streamp) strm, uInt items, uInt size); -void Z_INTERNAL PREFIX(dfltcc_copy_window)(void *dest, const void *src, size_t n); -void Z_INTERNAL PREFIX(dfltcc_free_window)(PREFIX3(streamp) strm, void *w); - -#define ZALLOC_WINDOW PREFIX(dfltcc_alloc_window) - -#define ZCOPY_WINDOW PREFIX(dfltcc_copy_window) - -#define ZFREE_WINDOW PREFIX(dfltcc_free_window) - -#define TRY_FREE_WINDOW PREFIX(dfltcc_free_window) - /* History buffer size. */ diff --git a/configure b/configure index 4efe9cbd8..6cf61da20 100755 --- a/configure +++ b/configure @@ -1832,11 +1832,6 @@ EOF ARCH_SHARED_OBJS="${ARCH_SHARED_OBJS} s390_features.lo" fi - if test $builddfltccdeflate -eq 1 -o $builddfltccinflate -eq 1; then - ARCH_STATIC_OBJS="${ARCH_STATIC_OBJS} dfltcc_common.o" - ARCH_SHARED_OBJS="${ARCH_SHARED_OBJS} dfltcc_common.lo" - fi - if test $builddfltccdeflate -eq 1; then CFLAGS="${CFLAGS} -DS390_DFLTCC_DEFLATE" SFLAGS="${SFLAGS} -DS390_DFLTCC_DEFLATE" diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 3f0d023eb..a57fb6180 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -189,7 +189,6 @@ if(WITH_GTEST) if(ZLIBNG_ENABLE_TESTS) list(APPEND TEST_SRCS test_adler32.cc # adler32_neon(), etc - test_aligned_alloc.cc # zng_alloc_aligned() test_compare256.cc # compare256_neon(), etc test_crc32.cc # crc32_acle(), etc test_inflate_sync.cc # expects a certain compressed block layout diff --git a/test/test_aligned_alloc.cc b/test/test_aligned_alloc.cc deleted file mode 100644 index 07f99a9da..000000000 --- a/test/test_aligned_alloc.cc +++ /dev/null @@ -1,48 +0,0 @@ -/* test_aligned_alloc.cc - Test zng_alloc_aligned and zng_free_aligned */ - -#include -#include -#include - -extern "C" { -# include "zbuild.h" -# include "zutil.h" -} - -#include - -#include "test_shared.h" - -void *zng_calloc_unaligned(void *opaque, unsigned items, unsigned size) { - uint8_t *pointer = (uint8_t *)calloc(1, (items * size) + 2); - Z_UNUSED(opaque); - if (pointer == NULL) - return pointer; - /* Store whether or not our allocation is aligned */ - *pointer = ((uint64_t)(intptr_t)pointer + 1) % 2 == 0; - pointer++; - if (*pointer) { - /* Return pointer that is off by one */ - pointer++; - } - return (void *)pointer; -} - -void zng_cfree_unaligned(void *opaque, void *ptr) { - uint8_t *pointer = (uint8_t *)ptr; - Z_UNUSED(opaque); - pointer--; - /* Get whether or not our original memory pointer was aligned */ - if (*pointer) { - /* Return original aligned pointer to free() */ - pointer--; - } - free(pointer); -} - -TEST(zalloc, aligned_64) { - void *return_ptr = PREFIX3(alloc_aligned)(zng_calloc_unaligned, 0, 1, 100, 64); - ASSERT_TRUE(return_ptr != NULL); - EXPECT_EQ((intptr_t)return_ptr % 64, 0); - PREFIX3(free_aligned)(zng_cfree_unaligned, 0, return_ptr); -} diff --git a/test/test_compare256.cc b/test/test_compare256.cc index ec2136aeb..e1662cdf0 100644 --- a/test/test_compare256.cc +++ b/test/test_compare256.cc @@ -27,11 +27,11 @@ static inline void compare256_match_check(compare256_func compare256) { uint8_t *str1; uint8_t *str2; - str1 = (uint8_t *)PREFIX3(alloc_aligned)(NULL, NULL, 1, MAX_COMPARE_SIZE, 64); + str1 = (uint8_t *)PREFIX(zcalloc)(NULL, 1, MAX_COMPARE_SIZE); ASSERT_TRUE(str1 != NULL); memset(str1, 'a', MAX_COMPARE_SIZE); - str2 = (uint8_t *)PREFIX3(alloc_aligned)(NULL, NULL, 1, MAX_COMPARE_SIZE, 64); + str2 = (uint8_t *)PREFIX(zcalloc)(NULL, 1, MAX_COMPARE_SIZE); ASSERT_TRUE(str2 != NULL); memset(str2, 'a', MAX_COMPARE_SIZE); @@ -46,8 +46,8 @@ static inline void compare256_match_check(compare256_func compare256) { str2[i] = 'a'; } - PREFIX3(free_aligned)(NULL, NULL, str1); - PREFIX3(free_aligned)(NULL, NULL, str2); + PREFIX(zcfree)(NULL, str1); + PREFIX(zcfree)(NULL, str2); } #define TEST_COMPARE256(name, func, support_flag) \ diff --git a/zutil.c b/zutil.c index ce620e36b..3dba7f78f 100644 --- a/zutil.c +++ b/zutil.c @@ -109,51 +109,3 @@ void Z_INTERNAL PREFIX(zcfree)(void *opaque, void *ptr) { Z_UNUSED(opaque); zng_free(ptr); } - -/* Since we support custom memory allocators, some which might not align memory as we expect, - * we have to ask for extra memory and return an aligned pointer. */ -void Z_INTERNAL *PREFIX3(alloc_aligned)(zng_calloc_func zalloc, void *opaque, unsigned items, unsigned size, unsigned align) { - uintptr_t return_ptr, original_ptr; - uint32_t alloc_size, align_diff; - void *ptr; - - /* If no custom calloc function used then call zlib-ng's aligned calloc */ - if (zalloc == NULL || zalloc == PREFIX(zcalloc)) - return PREFIX(zcalloc)(opaque, items, size); - - /* Allocate enough memory for proper alignment and to store the original memory pointer */ - alloc_size = sizeof(void *) + (items * size) + align; - ptr = zalloc(opaque, 1, alloc_size); - if (!ptr) - return NULL; - - /* Calculate return pointer address with space enough to store original pointer */ - align_diff = align - ((uintptr_t)ptr % align); - return_ptr = (uintptr_t)ptr + align_diff; - if (align_diff < sizeof(void *)) - return_ptr += align; - - /* Store the original pointer for free() */ - original_ptr = return_ptr - sizeof(void *); - memcpy((void *)original_ptr, &ptr, sizeof(void *)); - - /* Return properly aligned pointer in allocation */ - return (void *)return_ptr; -} - -void Z_INTERNAL PREFIX3(free_aligned)(zng_cfree_func zfree, void *opaque, void *ptr) { - /* If no custom cfree function used then call zlib-ng's aligned cfree */ - if (zfree == NULL || zfree == PREFIX(zcfree)) { - PREFIX(zcfree)(opaque, ptr); - return; - } - if (!ptr) - return; - - /* Calculate offset to original memory allocation pointer */ - void *original_ptr = (void *)((uintptr_t)ptr - sizeof(void *)); - void *free_ptr = *(void **)original_ptr; - - /* Free original memory allocation */ - zfree(opaque, free_ptr); -} diff --git a/zutil.h b/zutil.h index fb27c08ee..a6284502d 100644 --- a/zutil.h +++ b/zutil.h @@ -137,12 +137,4 @@ 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); -void Z_INTERNAL *PREFIX3(alloc_aligned)(zng_calloc_func zalloc, void *opaque, unsigned items, unsigned size, unsigned align); -void Z_INTERNAL PREFIX3(free_aligned)(zng_cfree_func zfree, void *opaque, void *ptr); - -#define ZALLOC(strm, items, size) PREFIX3(alloc_aligned)((strm)->zalloc, (strm)->opaque, (items), (size), 64) -#define ZFREE(strm, addr) PREFIX3(free_aligned)((strm)->zfree, (strm)->opaque, (void *)(addr)) - -#define TRY_FREE(s, p) {if (p) ZFREE(s, p);} - #endif /* ZUTIL_H_ */