From 1999cc5106e0b98ce3508aa59af98708a281c018 Mon Sep 17 00:00:00 2001 From: Johannes Löthberg Date: Sat, 17 Jan 2015 23:45:52 +0100 Subject: sds.c Replace all in-place calculations with sdsheader --- src/sds.c | 28 ++++++++++++++-------------- src/sds.h | 4 ++-- 2 files changed, 16 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/sds.c b/src/sds.c index 1fdee37..d844b59 100644 --- a/src/sds.c +++ b/src/sds.c @@ -82,7 +82,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; - free(s-sizeof(struct sdshdr)); + free(sdsheader(s)); } /* Set the sds string length to the length as obtained with strlen(), so @@ -100,7 +100,7 @@ void sdsfree(sds s) { * the output will be "6" as the string was modified but the logical length * remains 6 bytes. */ void sdsupdatelen(sds s) { - struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr))); + struct sdshdr *sh = sdsheader(s); int reallen = strlen(s); sh->free += (sh->len-reallen); sh->len = reallen; @@ -111,7 +111,7 @@ void sdsupdatelen(sds s) { * so that next append operations will not require allocations up to the * number of bytes previously available. */ void sdsclear(sds s) { - struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr))); + struct sdshdr *sh = sdsheader(s); sh->free += sh->len; sh->len = 0; sh->buf[0] = '\0'; @@ -130,7 +130,7 @@ sds sdsMakeRoomFor(sds s, size_t addlen) { if (free >= addlen) return s; len = sdslen(s); - sh = (void*) (s-(sizeof(struct sdshdr))); + sh = sdsheader(s); newlen = (len+addlen); if (newlen < SDS_MAX_PREALLOC) newlen *= 2; @@ -152,7 +152,7 @@ sds sdsMakeRoomFor(sds s, size_t addlen) { sds sdsRemoveFreeSpace(sds s) { struct sdshdr *sh; - sh = (void*) (s-(sizeof(struct sdshdr))); + sh = sdsheader(s); sh = realloc(sh, sizeof(struct sdshdr)+sh->len+1); sh->free = 0; return sh->buf; @@ -166,7 +166,7 @@ sds sdsRemoveFreeSpace(sds s) { * 4) The implicit null term. */ size_t sdsAllocSize(sds s) { - struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr))); + struct sdshdr *sh = sdsheader(s); return sizeof(*sh)+sh->len+sh->free+1; } @@ -195,7 +195,7 @@ size_t sdsAllocSize(sds s) { * sdsIncrLen(s, nread); */ void sdsIncrLen(sds s, int incr) { - struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr))); + struct sdshdr *sh = sdsheader(s); assert(sh->free >= incr); sh->len += incr; @@ -210,7 +210,7 @@ void sdsIncrLen(sds s, int incr) { * if the specified length is smaller than the current length, no operation * is performed. */ sds sdsgrowzero(sds s, size_t len) { - struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr))); + struct sdshdr *sh = sdsheader(s); size_t totlen, curlen = sh->len; if (len <= curlen) return s; @@ -218,7 +218,7 @@ sds sdsgrowzero(sds s, size_t len) { if (s == NULL) return NULL; /* Make sure added region doesn't contain garbage */ - sh = (void*)(s-(sizeof(struct sdshdr))); + sh = sdsheader(s); memset(s+curlen,0,(len-curlen+1)); /* also set trailing \0 byte */ totlen = sh->len+sh->free; sh->len = len; @@ -237,7 +237,7 @@ sds sdscatlen(sds s, const void *t, size_t len) { s = sdsMakeRoomFor(s,len); if (s == NULL) return NULL; - sh = (void*) (s-(sizeof(struct sdshdr))); + sh = sdsheader(s); memcpy(s+curlen, t, len); sh->len = curlen+len; sh->free = sh->free-len; @@ -264,13 +264,13 @@ sds sdscatsds(sds s, const sds t) { /* Destructively modify the sds string 's' to hold the specified binary * safe string pointed by 't' of length 'len' bytes. */ sds sdscpylen(sds s, const char *t, size_t len) { - struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr))); + struct sdshdr *sh = sdsheader(s); size_t totlen = sh->free+sh->len; if (totlen < len) { s = sdsMakeRoomFor(s,len-sh->len); if (s == NULL) return NULL; - sh = (void*) (s-(sizeof(struct sdshdr))); + sh = sdsheader(s); totlen = sh->free+sh->len; } memcpy(s, t, len); @@ -350,7 +350,7 @@ sds sdscatprintf(sds s, const char *fmt, ...) { * Output will be just "Hello World". */ void sdstrim(sds s, const char *cset) { - struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr))); + struct sdshdr *sh = sdsheader(s); char *start, *end, *sp, *ep; size_t len; @@ -382,7 +382,7 @@ void sdstrim(sds s, const char *cset) { * sdsrange(s,1,-1); => "ello World" */ void sdsrange(sds s, int start, int end) { - struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr))); + struct sdshdr *sh = sdsheader(s); size_t newlen, len = sdslen(s); if (len == 0) return; diff --git a/src/sds.h b/src/sds.h index ac99b80..4c7ae5f 100644 --- a/src/sds.h +++ b/src/sds.h @@ -46,12 +46,12 @@ static inline struct sdshdr * sdsheader(const sds s) { } static inline size_t sdslen(const sds s) { - struct sdshdr *sh = s - (sizeof (struct sdshdr)); + struct sdshdr *sh = sdsheader(s); return sh->len; } static inline size_t sdsavail(const sds s) { - struct sdshdr *sh = s - (sizeof (struct sdshdr)); + struct sdshdr *sh = sdsheader(s); return sh->free; } -- cgit v1.2.3-54-g00ecf