I am reading the source code of redis. Here is the code:
typedef char *sds;
struct sdshdr {
unsigned int len;
unsigned int free;
char buf[];
};
static inline size_t sdslen(const sds s) {
struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr)));
return sh->len;
}
static inline size_t sdsavail(const sds s) {
struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr)));
return sh->free;
}
About this code, I have some issue:
- Why is the output of
sizeof(struct sdshdr)
8? Why ischar buf[]
not included? - I can't understand the functions
size_t sdslen
andsdsavail
. Why dostruct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr)));
?