From 0afda933c164523be0c738bce2c7d74a11b4aa7e Mon Sep 17 00:00:00 2001 From: jschmieg Date: Tue, 30 Jun 2020 07:27:09 -0400 Subject: [PATCH] Free argv strings explicitly from Dram --- src/object.c | 15 ++++++++++++++- src/sds.c | 6 ++++++ src/sds.h | 1 + src/sdsalloc.h | 1 + src/server.h | 1 + 5 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/object.c b/src/object.c index 853e36e5f41..1addb6364bd 100644 --- a/src/object.c +++ b/src/object.c @@ -286,6 +286,11 @@ void freeStringObject(robj *o) { sdsfree(o->ptr); } } +void freeStringObjectDram(robj *o) { + if (o->encoding == OBJ_ENCODING_RAW) { + sdsfreeDram(o->ptr); + } +} void freeListObject(robj *o) { if (o->encoding == OBJ_ENCODING_QUICKLIST) { @@ -356,7 +361,15 @@ void incrRefCount(robj *o) { static void _decrRefCount(robj *o, int on_dram) { if (o->refcount == 1) { switch(o->type) { - case OBJ_STRING: freeStringObject(o); break; + case OBJ_STRING: + if (on_dram == OBJ_MEMORY_GENERAL) { + freeStringObject(o); + } + else { + freeStringObjectDram(o); + } + break; + case OBJ_LIST: freeListObject(o); break; case OBJ_SET: freeSetObject(o); break; case OBJ_ZSET: freeZsetObject(o); break; diff --git a/src/sds.c b/src/sds.c index d7c657268f1..d0ddfe0c812 100644 --- a/src/sds.c +++ b/src/sds.c @@ -185,6 +185,12 @@ void sdsfree(sds s) { s_free((char*)s-sdsHdrSize(s[-1])); } +/* Free an sds string. No operation is performed if 's' is NULL. */ +void sdsfreeDram(sds s) { + if (s == NULL) return; + s_freeDram((char*)s-sdsHdrSize(s[-1])); +} + /* Set the sds string length to the length as obtained with strlen(), so * considering as content only up to the first null term character. * diff --git a/src/sds.h b/src/sds.h index 5bdeedad78c..f667a2b8ee1 100644 --- a/src/sds.h +++ b/src/sds.h @@ -221,6 +221,7 @@ sds sdsempty(void); sds sdsdramempty(void); sds sdsdup(const sds s); void sdsfree(sds s); +void sdsfreeDram(sds s); sds sdsgrowzero(sds s, size_t len); sds sdscatlen(sds s, const void *t, size_t len); sds sdscat(sds s, const char *t); diff --git a/src/sdsalloc.h b/src/sdsalloc.h index 7c7a6a80f09..2a11563669a 100644 --- a/src/sdsalloc.h +++ b/src/sdsalloc.h @@ -43,6 +43,7 @@ #define s_malloc zmalloc #define s_realloc zrealloc #define s_free zfree +#define s_freeDram zfree_dram #define s_dram_malloc zmalloc_dram #endif diff --git a/src/server.h b/src/server.h index f54cdc630a0..063b0d817aa 100644 --- a/src/server.h +++ b/src/server.h @@ -1724,6 +1724,7 @@ void incrRefCount(robj *o); robj *makeObjectShared(robj *o); robj *resetRefCount(robj *obj); void freeStringObject(robj *o); +void freeStringObjectDram(robj *o); void freeListObject(robj *o); void freeSetObject(robj *o); void freeZsetObject(robj *o);