Skip to content

Commit

Permalink
change to use s_malloc_usable_size
Browse files Browse the repository at this point in the history
Signed-off-by: Binbin <[email protected]>
  • Loading branch information
enjoy-binbin committed Nov 18, 2024
1 parent d77e81e commit 4770360
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 23 deletions.
20 changes: 8 additions & 12 deletions src/unit/test_sds.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,55 +252,51 @@ int test_sds(int argc, char **argv, int flags) {
return 0;
}

size_t sdsRealAllocSize(sds s) {
return sdsAllocSize(s) + PREFIX_SIZE;
}

int test_typesAndAllocSize(int argc, char **argv, int flags) {
UNUSED(argc);
UNUSED(argv);
UNUSED(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", sdsRealAllocSize(x) == s_malloc_size(sdsAllocPtr(x)));
TEST_ASSERT_MESSAGE("len 31 sdsAllocSize", sdsAllocSize(x) == s_malloc_usable_size(sdsAllocPtr(x)));
sdsfree(x);

x = sdsnewlen(NULL, 32);
TEST_ASSERT_MESSAGE("len 32 type", (x[-1] & SDS_TYPE_MASK) >= SDS_TYPE_8);
TEST_ASSERT_MESSAGE("len 32 sdsAllocSize", sdsRealAllocSize(x) == s_malloc_size(sdsAllocPtr(x)));
TEST_ASSERT_MESSAGE("len 32 sdsAllocSize", sdsAllocSize(x) == s_malloc_usable_size(sdsAllocPtr(x)));
sdsfree(x);

x = sdsnewlen(NULL, 252);
TEST_ASSERT_MESSAGE("len 252 type", (x[-1] & SDS_TYPE_MASK) >= SDS_TYPE_8);
TEST_ASSERT_MESSAGE("len 252 sdsAllocSize", sdsRealAllocSize(x) == s_malloc_size(sdsAllocPtr(x)));
TEST_ASSERT_MESSAGE("len 252 sdsAllocSize", sdsAllocSize(x) == s_malloc_usable_size(sdsAllocPtr(x)));
sdsfree(x);

x = sdsnewlen(NULL, 253);
TEST_ASSERT_MESSAGE("len 253 type", (x[-1] & SDS_TYPE_MASK) == SDS_TYPE_16);
TEST_ASSERT_MESSAGE("len 253 sdsAllocSize", sdsRealAllocSize(x) == s_malloc_size(sdsAllocPtr(x)));
TEST_ASSERT_MESSAGE("len 253 sdsAllocSize", sdsAllocSize(x) == s_malloc_usable_size(sdsAllocPtr(x)));
sdsfree(x);

x = sdsnewlen(NULL, 65530);
TEST_ASSERT_MESSAGE("len 65530 type", (x[-1] & SDS_TYPE_MASK) >= SDS_TYPE_16);
TEST_ASSERT_MESSAGE("len 65530 sdsAllocSize", sdsRealAllocSize(x) == s_malloc_size(sdsAllocPtr(x)));
TEST_ASSERT_MESSAGE("len 65530 sdsAllocSize", sdsAllocSize(x) == s_malloc_usable_size(sdsAllocPtr(x)));
sdsfree(x);

x = sdsnewlen(NULL, 65531);
TEST_ASSERT_MESSAGE("len 65531 type", (x[-1] & SDS_TYPE_MASK) >= SDS_TYPE_32);
TEST_ASSERT_MESSAGE("len 65531 sdsAllocSize", sdsRealAllocSize(x) == s_malloc_size(sdsAllocPtr(x)));
TEST_ASSERT_MESSAGE("len 65531 sdsAllocSize", sdsAllocSize(x) == s_malloc_usable_size(sdsAllocPtr(x)));
sdsfree(x);

#if (LONG_MAX == LLONG_MAX)
if (flags & UNIT_TEST_LARGE_MEMORY) {
x = sdsnewlen(NULL, 4294967286);
TEST_ASSERT_MESSAGE("len 4294967286 type", (x[-1] & SDS_TYPE_MASK) >= SDS_TYPE_32);
TEST_ASSERT_MESSAGE("len 4294967286 sdsAllocSize", sdsRealAllocSize(x) == s_malloc_size(sdsAllocPtr(x)));
TEST_ASSERT_MESSAGE("len 4294967286 sdsAllocSize", sdsAllocSize(x) == s_malloc_usable_size(sdsAllocPtr(x)));
sdsfree(x);

x = sdsnewlen(NULL, 4294967287);
TEST_ASSERT_MESSAGE("len 4294967287 type", (x[-1] & SDS_TYPE_MASK) == SDS_TYPE_64);
TEST_ASSERT_MESSAGE("len 4294967287 sdsAllocSize", sdsRealAllocSize(x) == s_malloc_size(sdsAllocPtr(x)));
TEST_ASSERT_MESSAGE("len 4294967287 sdsAllocSize", sdsAllocSize(x) == s_malloc_usable_size(sdsAllocPtr(x)));
sdsfree(x);
}
#endif
Expand Down
11 changes: 11 additions & 0 deletions src/zmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ 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.
*/
Expand Down
11 changes: 0 additions & 11 deletions src/zmalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,17 +168,6 @@ size_t zmalloc_usable_size(void *ptr);
* obtained from z[*]_usable() family functions, there is no need for this step. */
#define zmalloc_usable_size(p) zmalloc_size(p)

#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

/* derived from https://github.com/systemd/systemd/pull/25688
* We use zmalloc_usable_size() everywhere to use memory blocks, but that is an abuse since the
* malloc_usable_size() isn't meant for this kind of use, it is for diagnostics only. That is also why the
Expand Down

0 comments on commit 4770360

Please sign in to comment.