From c636fc6cd25e455a75dca24ac08ba736f62db6c8 Mon Sep 17 00:00:00 2001 From: antirez Date: Thu, 6 Feb 2014 16:51:30 +0100 Subject: SDS Header pointer math rewritten in a more elegant form. As suggested by unwind in the Hacker News site the calculation of "sh" could be improved. In his own words: "Since the entire idea is that the pointer on the left-hand side is to the type whose size should be subtracted, I think it's better not to repeat the type but to "lock it" to the pointer instead. This also (of course) means we can drop the parenthesis with sizeof, since those are only needed when its argument is a type name." --- sds.c | 34 +++++++++++++++++----------------- sds.h | 4 ++-- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/sds.c b/sds.c index c0d4a3e..5306f1f 100644 --- a/sds.c +++ b/sds.c @@ -52,9 +52,9 @@ sds sdsnewlen(const void *init, size_t initlen) { struct sdshdr *sh; if (init) { - sh = malloc(sizeof(struct sdshdr)+initlen+1); + sh = malloc(sizeof *sh+initlen+1); } else { - sh = calloc(sizeof(struct sdshdr)+initlen+1,1); + sh = calloc(sizeof *sh+initlen+1,1); } if (sh == NULL) return NULL; sh->len = initlen; @@ -103,7 +103,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 = (void*) (s-sizeof *sh);; int reallen = strlen(s); sh->free += (sh->len-reallen); sh->len = reallen; @@ -114,7 +114,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 = (void*) (s-sizeof *sh);; sh->free += sh->len; sh->len = 0; sh->buf[0] = '\0'; @@ -133,13 +133,13 @@ sds sdsMakeRoomFor(sds s, size_t addlen) { if (free >= addlen) return s; len = sdslen(s); - sh = (void*) (s-(sizeof(struct sdshdr))); + sh = (void*) (s-sizeof *sh);; newlen = (len+addlen); if (newlen < SDS_MAX_PREALLOC) newlen *= 2; else newlen += SDS_MAX_PREALLOC; - newsh = realloc(sh, sizeof(struct sdshdr)+newlen+1); + newsh = realloc(sh, sizeof *newsh+newlen+1); if (newsh == NULL) return NULL; newsh->free = newlen - len; @@ -155,8 +155,8 @@ sds sdsMakeRoomFor(sds s, size_t addlen) { sds sdsRemoveFreeSpace(sds s) { struct sdshdr *sh; - sh = (void*) (s-(sizeof(struct sdshdr))); - sh = realloc(sh, sizeof(struct sdshdr)+sh->len+1); + sh = (void*) (s-sizeof *sh);; + sh = realloc(sh, sizeof *sh+sh->len+1); sh->free = 0; return sh->buf; } @@ -169,7 +169,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 = (void*) (s-sizeof *sh);; return sizeof(*sh)+sh->len+sh->free+1; } @@ -198,7 +198,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 = (void*) (s-sizeof *sh);; assert(sh->free >= incr); sh->len += incr; @@ -213,7 +213,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 = (void*) (s-sizeof *sh); size_t totlen, curlen = sh->len; if (len <= curlen) return s; @@ -221,7 +221,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 = (void*)(s-sizeof *sh); memset(s+curlen,0,(len-curlen+1)); /* also set trailing \0 byte */ totlen = sh->len+sh->free; sh->len = len; @@ -240,7 +240,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 = (void*) (s-sizeof *sh);; memcpy(s+curlen, t, len); sh->len = curlen+len; sh->free = sh->free-len; @@ -267,13 +267,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 = (void*) (s-sizeof *sh);; 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 = (void*) (s-sizeof *sh);; totlen = sh->free+sh->len; } memcpy(s, t, len); @@ -353,7 +353,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 = (void*) (s-sizeof *sh);; char *start, *end, *sp, *ep; size_t len; @@ -385,7 +385,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 = (void*) (s-sizeof *sh);; size_t newlen, len = sdslen(s); if (len == 0) return; diff --git a/sds.h b/sds.h index 54c47e0..ab6fc9c 100644 --- a/sds.h +++ b/sds.h @@ -45,12 +45,12 @@ struct sdshdr { }; static inline size_t sdslen(const sds s) { - struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr))); + struct sdshdr *sh = (void*)(s-sizeof *sh); return sh->len; } static inline size_t sdsavail(const sds s) { - struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr))); + struct sdshdr *sh = (void*)(s-sizeof *sh); return sh->free; } -- cgit v1.2.3-70-g09d2