From 5b9ef856e70f2615f7f017f171885755cb835e47 Mon Sep 17 00:00:00 2001 From: Binbin Date: Sat, 16 Nov 2024 22:13:03 +0800 Subject: [PATCH] f --- src/sds.c | 6 +++--- src/unit/test_sds.c | 1 + src/unit/test_zmalloc.c | 2 ++ src/zmalloc.c | 11 ----------- src/zmalloc.h | 11 +++++++++++ 5 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/sds.c b/src/sds.c index 4dd7d709aa3..b9d0383435a 100644 --- a/src/sds.c +++ b/src/sds.c @@ -213,7 +213,7 @@ size_t sdscopytobuffer(unsigned char *buf, size_t buf_len, sds s, uint8_t *hdr_s /* Free an sds string. No operation is performed if 's' is NULL. */ void sdsfree(sds s) { if (s == NULL) return; - s_free_with_size(sdsAllocPtr(s), sdsAllocSize(s)); + s_free_with_size(sdsAllocPtr(s), sdsAllocSize(s) - PREFIX_SIZE); } /* Set the sds string length to the length as obtained with strlen(), so @@ -429,9 +429,9 @@ size_t sdsAllocSize(sds s) { char type = s[-1] & SDS_TYPE_MASK; /* SDS_TYPE_5 header doesn't contain the size of the allocation */ if (type == SDS_TYPE_5) { - return s_malloc_usable_size(sdsAllocPtr(s)); + return s_malloc_usable_size(sdsAllocPtr(s)) + PREFIX_SIZE; } else { - return sdsHdrSize(type) + sdsalloc(s) + 1; + return sdsHdrSize(type) + sdsalloc(s) + 1 + PREFIX_SIZE; } } diff --git a/src/unit/test_sds.c b/src/unit/test_sds.c index b97d0d9d321..caec53c49b3 100644 --- a/src/unit/test_sds.c +++ b/src/unit/test_sds.c @@ -259,6 +259,7 @@ int test_typesAndAllocSize(int argc, char **argv, int flags) { sds x = sdsnewlen(NULL, 31); TEST_ASSERT_MESSAGE("len 31 type", (x[-1] & SDS_TYPE_MASK) == SDS_TYPE_5); + TEST_ASSERT_MESSAGE("len 31 sdsAllocSize", sdsAllocSize(x) == s_malloc_size(sdsAllocPtr(x))); sdsfree(x); x = sdsnewlen(NULL, 32); diff --git a/src/unit/test_zmalloc.c b/src/unit/test_zmalloc.c index 6c1d03e8e14..08444a157ed 100644 --- a/src/unit/test_zmalloc.c +++ b/src/unit/test_zmalloc.c @@ -6,6 +6,8 @@ int test_zmallocInitialUsedMemory(int argc, char **argv, int flags) { UNUSED(argv); UNUSED(flags); + /* If this fails, it may be that other tests have failed and the memory has not been released. */ + TEST_PRINT_INFO("test_zmallocInitialUsedMemory; used: %zu\n", zmalloc_used_memory()); TEST_ASSERT(zmalloc_used_memory() == 0); return 0; diff --git a/src/zmalloc.c b/src/zmalloc.c index e18fa8bac24..2245e391806 100644 --- a/src/zmalloc.c +++ b/src/zmalloc.c @@ -56,17 +56,6 @@ void zlibc_free(void *ptr) { #define UNUSED(x) ((void)(x)) -#ifdef HAVE_MALLOC_SIZE -#define PREFIX_SIZE (0) -#else -/* Use at least 8 bytes alignment on all systems. */ -#if SIZE_MAX < 0xffffffffffffffffull -#define PREFIX_SIZE 8 -#else -#define PREFIX_SIZE (sizeof(size_t)) -#endif -#endif - /* When using the libc allocator, use a minimum allocation size to match the * jemalloc behavior that doesn't return NULL in this case. */ diff --git a/src/zmalloc.h b/src/zmalloc.h index 9b51f4c8665..5e7ad981dd8 100644 --- a/src/zmalloc.h +++ b/src/zmalloc.h @@ -37,6 +37,17 @@ #define __xstr(s) __str(s) #define __str(s) #s +#ifdef HAVE_MALLOC_SIZE +#define PREFIX_SIZE (0) +#else +/* Use at least 8 bytes alignment on all systems. */ +#if SIZE_MAX < 0xffffffffffffffffull +#define PREFIX_SIZE 8 +#else +#define PREFIX_SIZE (sizeof(size_t)) +#endif +#endif + #if defined(USE_TCMALLOC) #define ZMALLOC_LIB ("tcmalloc-" __xstr(TC_VERSION_MAJOR) "." __xstr(TC_VERSION_MINOR)) #include