一、sds格式
sds header定义:
1 struct sdshdr { 2 unsigned int len; 3 unsigned int free; 4 char buf[]; 5 };
sizeof(struct sdshdr)= 2*sizeof(unsigned int), char buf[]等价于char buf[0], 仅对编译器有效,并不实际占用存储。
其中len是使用的长度,free是剩余的长度,再加上一个C语言中的' '结束符
sizeof(buf) = len + free + 1, 格式如下:
二、sds基本操作
1、创建sds对象
1 sds sdsnewlen(const void *init, size_t initlen) { 2 struct sdshdr *sh; 3 4 if (init) { 5 sh = zmalloc(sizeof(struct sdshdr)+initlen+1); 6 } else { 7 sh = zcalloc(sizeof(struct sdshdr)+initlen+1); 8 } 9 if (sh == NULL) return NULL; 10 sh->len = initlen; 11 sh->free = 0; 12 if (initlen && init) 13 memcpy(sh->buf, init, initlen); 14 sh->buf[initlen] = '