Skip to content

Commit

Permalink
Clean up memory allocation functions that are no longer used , and it…
Browse files Browse the repository at this point in the history
…s tests.
  • Loading branch information
Dead2 committed Apr 23, 2024
1 parent 95bf38f commit 719ca7a
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 109 deletions.
1 change: 0 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,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
Expand Down
48 changes: 0 additions & 48 deletions test/test_aligned_alloc.cc

This file was deleted.

8 changes: 4 additions & 4 deletions test/test_compare256.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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) \
Expand Down
48 changes: 0 additions & 48 deletions zutil.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
8 changes: 0 additions & 8 deletions zutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_ */

0 comments on commit 719ca7a

Please sign in to comment.