字符串(sds.h/sds.c)
Redis只会使用C字符串作为字面量,在大多数情况下,
Redis使用SDS(Simple Dynamic String,简单动态字符串)作为字符串表示。
1.SDS优点
比起C字符串,SDS具有以下优点:
- 常数复杂度获取字符串长度。
- 杜绝缓冲区溢出。
- 减少修改字符串长度时所需的内存重分配次数。
- 二进制安全。
- 兼容部分C字符串函数。
2.备注
| 12
 3
 4
 5
 6
 
 | struct __attribute__ ((__packed__)) sdshdr64 {uint64_t len; /* used */
 uint64_t alloc; /* excluding the header and null terminator */
 unsigned char flags; /* 3 lsb of type, 5 unused bits */
 char buf[];
 };
 
 | 
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 
 | sdsHdrSizesdsReqType
 sdsnewlen
 sdsempty
 sdsnew
 sdsdup
 sdsfree
 sdsupdatelen
 sdsclear
 sdsMakeRoomFor
 sdsRemoveFreeSpace
 sdsAllocSize
 sdsAllocPtr
 sdsIncrLen
 sdsgrowzero
 sdscatlen
 sdscat
 sdscatsds
 sdscpylen
 sdscpy
 SDS_LLSTR_SIZE
 sdsll2str
 sdsull2str
 sdsfromlonglong
 sdscatvprintf
 sdscatprintf
 sdscatfmt
 sdstrim
 sdsrange
 sdstolower
 sdstoupper
 sdscmp
 sdssplitlen
 sdsfreesplitres
 sdscatrepr
 is_hex_digit
 hex_digit_to_int
 sdssplitargs
 sdsmapchars
 sdsjoin
 sdsjoinsds
 sds_malloc
 sds_realloc
 sds_free
 
 |