Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
enjoy-binbin committed Nov 16, 2024
1 parent aa2dd3e commit 5b9ef85
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/sds.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}

Expand Down
1 change: 1 addition & 0 deletions src/unit/test_sds.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/unit/test_zmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 0 additions & 11 deletions src/zmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
11 changes: 11 additions & 0 deletions src/zmalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <gperftools/tcmalloc.h>
Expand Down

0 comments on commit 5b9ef85

Please sign in to comment.