summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatt Stancliff <matt@genges.com>2014-12-10 15:40:34 -0500
committerMatt Stancliff <matt@genges.com>2014-12-23 09:31:04 -0500
commit23a93d5c5417d071c90e8cb4e29a630ee32a67b4 (patch)
tree768d83a55f41bc2f23f33542a387f22ac16ee576
parentcb8032d6c9e92d710e0d56d2e9309666417e47a7 (diff)
downloadredis-23a93d5c5417d071c90e8cb4e29a630ee32a67b4.tar.gz
Add sdsnative()
Use the existing memory space for an SDS to convert it to a regular character buffer so we don't need to allocate duplicate space just to extract a usable buffer for native operations.
-rw-r--r--src/sds.c11
-rw-r--r--src/sds.h1
2 files changed, 12 insertions, 0 deletions
diff --git a/src/sds.c b/src/sds.c
index 05ee0ad56..3626dd524 100644
--- a/src/sds.c
+++ b/src/sds.c
@@ -88,6 +88,17 @@ void sdsfree(sds s) {
zfree(s-sizeof(struct sdshdr));
}
+/* Remove sds header so we can use buffer as malloc'd byte array.
+ * Returns the contents of 's' usable as a full malloc'd C string. */
+char *sdsnative(sds s) {
+ if (!s) return NULL;
+
+ size_t len = sdslen(s);
+ char *base = s-sizeof(struct sdshdr);
+ memmove(base, s, len);
+ return zrealloc(base, len);
+}
+
/* 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 93dd4f28e..756ae0b53 100644
--- a/src/sds.h
+++ b/src/sds.h
@@ -60,6 +60,7 @@ sds sdsempty(void);
size_t sdslen(const sds s);
sds sdsdup(const sds s);
void sdsfree(sds s);
+char *sdsnative(sds s);
size_t sdsavail(const sds s);
sds sdsgrowzero(sds s, size_t len);
sds sdscatlen(sds s, const void *t, size_t len);