aboutsummaryrefslogtreecommitdiffstats
path: root/sds.h
diff options
context:
space:
mode:
authorantirez <antirez@gmail.com>2014-02-06 16:51:30 +0100
committerantirez <antirez@gmail.com>2014-02-06 16:56:41 +0100
commitc636fc6cd25e455a75dca24ac08ba736f62db6c8 (patch)
tree20c43054f404f201785306b3bbcb123724010f99 /sds.h
parentc135fed2f965bf73b7672e86d85ff8bf5f740a4c (diff)
downloadsds-c636fc6cd25e455a75dca24ac08ba736f62db6c8.tar.xz
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."
Diffstat (limited to 'sds.h')
-rw-r--r--sds.h4
1 files changed, 2 insertions, 2 deletions
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;
}