diff --git a/src/object.c b/src/object.c index f696f5f556..e953faf6b1 100644 --- a/src/object.c +++ b/src/object.c @@ -95,8 +95,7 @@ robj *createObjectWithKeyAndExpire(int type, void *ptr, const sds key, long long /* Copy embedded key. */ if (o->hasembkey) { *data++ = sdsHdrSize(key_sds_type); - size_t remaining_space = bufsize - (data - (char *)(void *)o); - sdswrite(data, remaining_space, key_sds_type, key, key_sds_len); + sdswrite(data, key_sds_size, key_sds_type, key, key_sds_len); } return o; @@ -151,7 +150,8 @@ static robj *createEmbeddedStringObjectWithKeyAndExpire(const char *val_ptr, size_t key_sds_len = key != NULL ? sdslen(key) : 0; char key_sds_type = key != NULL ? sdsReqType(key_sds_len) : 0; size_t key_sds_size = key != NULL ? sdsReqSize(key_sds_len, key_sds_type) : 0; - size_t min_size = sizeof(robj); + size_t val_sds_size = sdsReqSize(val_len, SDS_TYPE_8); + size_t min_size = sizeof(robj) + val_sds_size; if (expire != -1) { min_size += sizeof(long long); } @@ -159,8 +159,6 @@ static robj *createEmbeddedStringObjectWithKeyAndExpire(const char *val_ptr, /* Size of embedded key, incl. 1 byte for prefixed sds hdr size. */ min_size += 1 + key_sds_size; } - /* Size of embedded value (EMBSTR) including \0 term. */ - min_size += sdsReqSize(val_len, SDS_TYPE_8); /* Allocate and set the declared fields. */ size_t bufsize = 0; @@ -196,8 +194,7 @@ static robj *createEmbeddedStringObjectWithKeyAndExpire(const char *val_ptr, } /* Copy embedded value (EMBSTR). */ - size_t remaining_space = bufsize - (data - (char *)(void *)o); - o->ptr = sdswrite(data, remaining_space, SDS_TYPE_8, val_ptr, val_len); + o->ptr = sdswrite(data, val_sds_size, SDS_TYPE_8, val_ptr, val_len); return o; } diff --git a/src/sds.c b/src/sds.c index b21458b21c..2f40c9dc9c 100644 --- a/src/sds.c +++ b/src/sds.c @@ -113,18 +113,18 @@ sds _sdsnewlen(const void *init, size_t initlen, int trymalloc) { : s_malloc_usable(hdrlen + initlen + 1, &bufsize); if (sh == NULL) return NULL; + adjustTypeIfNeeded(&type, &hdrlen, bufsize); return sdswrite(sh, bufsize, type, init, initlen); } /* Writes an sds with type `type` into a buffer `buf` of size `bufsize`. Returns - * an sds handle to the string within the buffer. Use `sdsReqType(len)` to + * an sds handle to the string within the buffer. Use `sdsReqType(length)` to * compute the type and `sdsReqSize(length, type)` to compute the required * buffer size. You can use a larger `bufsize` than required, but usable size * can't be greater than `sdsTypeMaxSize(type)`. */ sds sdswrite(char *buf, size_t bufsize, char type, const char *init, size_t initlen) { - int hdrlen = sdsHdrSize(type); - adjustTypeIfNeeded(&type, &hdrlen, bufsize); assert(bufsize >= sdsReqSize(initlen, type)); + int hdrlen = sdsHdrSize(type); size_t usable = bufsize - hdrlen - 1; sds s = buf + hdrlen; unsigned char *fp = ((unsigned char *)s) - 1; /* flags pointer. */ @@ -429,7 +429,7 @@ size_t sdsAllocSize(const_sds s) { if (type == SDS_TYPE_5) { return s_malloc_usable_size(sdsAllocPtr(s)); } else { - return sdsMemUsage(s); + return sdsHdrSize(type) + sdsalloc(s) + 1; } } diff --git a/src/sds.h b/src/sds.h index 19d3f62416..137ccaf262 100644 --- a/src/sds.h +++ b/src/sds.h @@ -247,13 +247,6 @@ sds sdsResize(sds s, size_t size, int would_regrow); size_t sdsAllocSize(const_sds s); void *sdsAllocPtr(const_sds s); -/* Returns the size of the sds representation in memory. Typically, this is the - * same as the allocation size, except for sds type 5 and for sds strings - * embedded in other objects. */ -static inline size_t sdsMemUsage(const_sds s) { - return sdsHdrSize(sdsType(s)) + sdsalloc(s) + 1; -} - /* Returns the minimum required size to store an sds string of the given length * and type. */ static inline size_t sdsReqSize(size_t len, char type) {