Redis内部数据结构详解之简单动态字符串(sds)
- "font-family:Courier New;">typedef struct Node{
- int len;
- char str[5];
- }Node;
- typedef struct Node2{
- int len;
- char str[];
- }Node2;
- sizeof(char*) = 4
- sizeof(Node*) = 4
- sizeof(Node) = 12
- sizeof(Node2) = 4
- "font-family:Courier New;">typedef char *sds;
- struct sdshdr {
- int len; //buf已占用的长度,即当前字符串长度值
- int free; //buf空余可用的长度,append时使用
- char buf[]; //实际保存字符串数据
- };
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
- "font-family:Courier New;">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;
- }
- "font-family:Courier New;">/*init: C字符串,initlen:C字符串的长度*/
- sds sdsnewlen(const void *init, size_t initlen) {
- struct sdshdr *sh;
- if (init) {
- sh = zmalloc(sizeof(struct sdshdr)+initlen+1);
- } else {
- sh = zcalloc(sizeof(struct sdshdr)+initlen+1);
- }
- if (sh == NULL) return NULL;
- sh->len = initlen;
- sh->free = 0;
- if (initlen && init)
- memcpy(sh->buf, init, initlen);
- sh->buf[initlen] = '\0';
- return (char*)sh->buf;
- }
- /* Create a new sds string starting from a null termined C string. */
- sds sdsnew(const char *init) {
- size_t initlen = (init == NULL) ? 0 : strlen(init);
- return sdsnewlen(init, initlen);
- }
- "font-family:Courier New;">/* Enlarge the free space at the end of the sds string so that the caller
- * is sure that after calling this function can overwrite up to addlen
- * bytes after the end of the string, plus one more byte for nul term.
- *
- * Note: this does not change the *length* of the sds string as returned
- * by sdslen(), but only the free buffer space we have. */
- //对sdshdr的buf进行扩展
- sds sdsMakeRoomFor(sds s, size_t addlen) {
- struct sdshdr *sh, *newsh;
- size_t free = sdsavail(s); //查看当前sds空余的长度
- size_t len, newlen;
- if (free >= addlen) return s; //不需要扩展
- len = sdslen(s); //得到当前sds字符串的长度
- sh = (void*) (s-(sizeof(struct sdshdr))); //得到sdshdr首地址
- newlen = (len+addlen); //追加之后sds新的长度
- if (newlen < SDS_MAX_PREALLOC) //SDS_MAX_PREALLOC (1024*1024),扩展的具体方法
- newlen *= 2;
- else
- newlen += SDS_MAX_PREALLOC;
- newsh = zrealloc(sh, sizeof(struct sdshdr)+newlen+1); //重新分配内存
- if (newsh == NULL) return NULL;//分配内存失败
- newsh->free = newlen - len; //新的sds空余长度
- return newsh->buf;
- }
1) 可以在O(1)的时间复杂度得到字符串的长度
2) 可以高效的执行append追加字符串操作
3) 二进制安全
来源:http://blog.csdn.net/acceptedxukai/article/details/17482611
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
