Skip to content

Commit

Permalink
Fix sdsAllocSize for SDS_TYPE_5
Browse files Browse the repository at this point in the history
sdsalloc returns sds's length for SDS_TYPE_5. It's not correct for
SDS_TYPE_5.

This patch makes sdsAllocSize call zmalloc_size for
SDS_TYPE_5. sdsalloc is a lower level function that continues to
return length for SDS_TYPE_5.

Signed-off-by: Vadym Khoptynets <vadymkh@amazon.com>
  • Loading branch information
poiuj committed May 9, 2024
1 parent f226d87 commit 779ba8c
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions src/sds.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "sds.h"
#include "sdsalloc.h"
#include "util.h"
#include "zmalloc.h"

const char *SDS_NOINIT = "SDS_NOINIT";

Expand Down Expand Up @@ -178,13 +179,7 @@ sds sdsdup(const sds s) {
/* Free an sds string. No operation is performed if 's' is NULL. */
void sdsfree(sds s) {
if (s == NULL) return;

/* SDS_TYPE_5 header doesn't contain the size of the allocation */
if ((s[-1] & SDS_TYPE_MASK) == SDS_TYPE_5) {
s_free(sdsAllocPtr(s));
} else {
s_free_with_size(sdsAllocPtr(s), sdsAllocSize(s));
}
s_free_with_size(sdsAllocPtr(s), sdsAllocSize(s));
}

/* Set the sds string length to the length as obtained with strlen(), so
Expand Down Expand Up @@ -337,7 +332,7 @@ sds sdsResize(sds s, size_t size, int would_regrow) {
* type. */
int use_realloc = (oldtype==type || (type < oldtype && type > SDS_TYPE_8));
size_t newlen = use_realloc ? oldhdrlen+size+1 : hdrlen+size+1;
size_t usable = sdsalloc(s);
size_t usable = sdsAllocSize(s);

if (use_realloc) {
int alloc_already_optimal = 0;
Expand All @@ -346,18 +341,18 @@ sds sdsResize(sds s, size_t size, int would_regrow) {
* We aim to avoid calling realloc() when using Jemalloc if there is no
* change in the allocation size, as it incurs a cost even if the
* allocation size stays the same. */
alloc_already_optimal = (je_nallocx(newlen, 0) == zmalloc_size(sh));
alloc_already_optimal = (je_nallocx(newlen, 0) == usable);
#endif
if (!alloc_already_optimal) {
newsh = s_realloc_usable(sh, newlen, &usable);
if (newsh == NULL) return NULL;
usable = usable - oldhdrlen - 1;
s = (char*)newsh+oldhdrlen;
}
usable -= (oldhdrlen + 1);
} else {
newsh = s_malloc_usable(newlen, &usable);
if (newsh == NULL) return NULL;
usable = usable - hdrlen - 1;
usable -= (hdrlen + 1);
memcpy((char*)newsh+hdrlen, s, len);
s_free(sh);
s = (char*)newsh+hdrlen;
Expand All @@ -377,8 +372,13 @@ sds sdsResize(sds s, size_t size, int would_regrow) {
* 4) The implicit null term.
*/
size_t sdsAllocSize(sds s) {
size_t alloc = sdsalloc(s);
return sdsHdrSize(s[-1])+alloc+1;
char header = s[-1];
/* SDS_TYPE_5 header doesn't contain the size of the allocation */
if ((header & SDS_TYPE_MASK) == SDS_TYPE_5) {
return zmalloc_size(sdsAllocPtr(s));
} else {
return sdsHdrSize(header) + sdsalloc(s) + 1;
}
}

/* Return the pointer of the actual SDS allocation (normally SDS strings
Expand Down

0 comments on commit 779ba8c

Please sign in to comment.