redis源码分析(3)sds
sds是redis中用来处理字符串的数据结构。sds的定义在sds.h中:1 typedef char *sds;
简洁明了!简明扼要!(X,玩我呢是吧!这特么不就是c中的字符串么?!)。像redis这种高端大气上档次的服务器显然不会这么的幼稚。在sds的定义之后,还有一个结构体:
1 struct sdshdr {
2 int len;
3 int free;
4 char buf[];
5 }
有len,有free,这就有点意思了。很明显,根据这个结构体的定义,这是sds的header,用来存储sds的信息。注意最后的buf定义,这个buf数组没有设置长度。这是为神马呢?在gcc中,这种方式可以使得buf成为一个可变的数组,也就是说,可以扩展buf同时又保证在使用的时候,感觉buf始终在struct sdshdr中。有点啰嗦,其实可以用下图展示:
sdshdr sds
| |
V V
----------------------------
|len | free | buf … |
----------------------------
这个就是sds的内存分布图。struct sdshdr这个结构体放在了真正的数据之前,且是紧挨着的。这样,通过buf引用的数组其实就是后面的数据。这个是利用了c中数组访问的特点。
下面我们来看看如何创建一个sds:
1 /* Create a new sds string with the content specified by the 'init' pointer
2* and 'initlen'.
3* If NULL is used for 'init' the string is initialized with zero bytes.
4*
5* The string is always null-termined (all the sds strings are, always) so
6* even if you create an sds string with:
7*
8* mystring = sdsnewlen("abc",3");
9*
10* You can print the string with printf() as there is an implicit \0 at the
11* end of the string. However the string is binary safe and can contain
12* \0 characters in the middle, as the length is stored in the sds header. */
13 sds sdsnewlen(const void *init, size_t initlen) {
14 struct sdshdr *sh;
15
16 if (init) {
17 sh = zmalloc(sizeof(struct sdshdr)+initlen+1);
18 } else {
19 sh = zcalloc(sizeof(struct sdshdr)+initlen+1);
20 }
21 if (sh == NULL) return NULL;
22 sh->len = initlen;
23 sh->free = 0;
24 if (initlen && init)
25 memcpy(sh->buf, init, initlen);
26 sh->buf = '\0';
27 return (char*)sh->buf;
28 }
重点是这句(zcalloc也一样,只是分配内存的时候顺带初始化为0):
1 sh = zmalloc(sizeof(struct sdshdr)+initlen+1)
创建一个sds的时候,实际申请的内存大小为sdshdr的大小,加上调用者希望的sds的大小,再加一。另外,zmalloc的返回值直接赋值给了sh,sh是struct sdshdr。那么,在创建一个sds的时候,将sds的struct sdshdr放到了真正的数据的前面,这样可以通过buf引用到后面的数据。多加一个一是为了保证有地方放'\0'。根据注释,sds默认以'\0'结尾,且可以存放二进制的数据,因为struct sdshdr中存放了数据的长度。在sdsnewlen的最后,返回的是(char\*)sh->buf,也就是说sds实际指向的就是一个char\*数组。**所有可以对char\*的操作也同时可以操作sds**。
那sds的长度等信息如何获取呢?看下面的代码:
1 static inline size_t sdslen(const sds s) {
2 struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr)));
3 return sh->len;
4 }
5
6 static inline size_t sdsavail(const sds s) {
7 struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr)));
8 return sh->free;
9 }
这两个函数分别是获取sds的实际长度和可用空间。核心代码就是这句:
1 struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr)));
将sds的地址减去struct sdshdr的长度然后赋值给sh,这就得到了sds对应的struct sdshdr。根据前面的内存分布图,struct sdshdr始终是在数据的前面,一次很容易得到struct sdshdr的地址。得到了struct sdshdr的地址之后,其他的就很简单了。
sds支持动态的扩展空间,sdsMakeRoomFor这个函数用来扩展sds的空间:
1 /* Enlarge the free space at the end of the sds string so that the caller
2* is sure that after calling this function can overwrite up to addlen
3* bytes after the end of the string, plus one more byte for nul term.
4*
5* Note: this does not change the *length* of the sds string as returned
6* by sdslen(), but only the free buffer space we have. */
7 sds sdsMakeRoomFor(sds s, size_t addlen) {
8 struct sdshdr *sh, *newsh;
9 size_t free = sdsavail(s);
10 size_t len, newlen;
11
12 if (free >= addlen) return s;
13 len = sdslen(s);
14 sh = (void*) (s-(sizeof(struct sdshdr)));
15 newlen = (len+addlen);
16 if (newlen < SDS_MAX_PREALLOC)
17 newlen *= 2;
18 else
19 newlen += SDS_MAX_PREALLOC;
20 newsh = zrealloc(sh, sizeof(struct sdshdr)+newlen+1);
21 if (newsh == NULL) return NULL;
22
23 newsh->free = newlen - len;
24 return newsh->buf;
25 }
这个函数保证sds至少有addlen长度的空间可用。这个函数体现了sds的空间扩展策略。如果有足够的空间,则直接返回。如果空间不够,当len+addlen小于SDS_MAX_PREALLOC时,将空间扩展到(len+addlen)\*2。当len+addlen大于SDS_MAX_PREALLOC,将空间扩展到len+addlen+SDS_MAX_PREALLOC。sds的扩展考虑了实际需要的空间大小,扩展的效率要高一些。如果每次扩大原来的二倍,当需要的空间大于初始空间二倍时,需要多次的扩展操作,也就意味着多次的zrealloc操作。sds的扩展可以在任何情况下一次扩展到位。
sds最大的特点就是所有可以对char\*的操作都可以操作sds,这在实际使用sds的的时候可以带来很多方便。比如,从socket中读取数据存储到sds中,可以如下操作:
1 /* sds s */
2 int oldlen = sdslen(s);
3 s = sdsMakeRoomFor(s, BUFFER_SIZE);
4 nread = read(fd, s+oldlen, BUFFER_SIZE);
5 sdsIncrLen(s, nread);
在调用read的时候,可以把sds看做是char\*来处理(实际上sds就是char\*)。当然,最后一定要调用sdsIncrLen来修正sds的长度。
页:
[1]