Redis 设计与实现 3:字符串 SDS

在 Redis 中,字符串都用自定义的结构简单动态字符串(Simple Dynamic Strings,SDS)。
Redis 中使用到的字符串都是用 SDS,例如 key、string 类型的值、sorted set 的 member、hash 的 field 等等等等。。。

数据结构
旧版本的结构
在 3.2 版本之前,sds 的定义是这样的:

struct sdshdr {
// buf 数组中已使用的字节数量,也就是 sds 本身的字符串长度
unsigned int len;
// buf 数组中未使用的字节数量
unsigned int free;
// 字节数组,用于保存字符串
char buf[];
};
旧版本 SDS 结构示例

这样的结构有几个好处:

单独记录长度len,获取字符串长度的时间复杂度是 O(1) 。传统的 C 字符串获取长度需要遍历字符串,直到遇到\0,时间复杂度是 O(N)。
buf 数组末尾遵循 C 字符串以 \0 结尾的惯例,可以兼容 C 处理字符串的函数。
减少修改字符串带来的内存重分配次数,Redis 使用了 空间预分配(预先申请大一点点的空间) 和 空间惰性释放(字符串变短修改len字段即可)来减少字符串修改引起的内存重新分配。
不以\0为结尾的判断,二进制安全。因为图片等二进制数据中,可能包含\0,传统 C 字符串一遇到 \0 就认为字符串结束了,会导致不能完整保存。
缺点:

len 和 free 的定义用了 4 个字节,可以表示 2^32 的长度。但是我们实际使用的字符串,往往没有那么长。4 个字节造成了浪费。
新版本的结构
旧版本中我们说到,len 和 free 的缺点是用了太长的变量,新版本解决了这个问题。
我们来看一下新版本的 SDS 结构。

在 Redis 3.2 版本之后,Redis 将 SDS 划分为 5 种类型:

类型 字节 位
sdshdr5 < 1 <8
sdshdr8 1 8
sdshdr16 2 16
sdshdr32 4 32
sdshdr64 8 64
新版本新增加了一个 flags 字段来标识类型,长度 1 字节(8 位)。
类型只占用了前 3 位。在 sdshdr5 中,后 5 位用来保存字符串的长度。其他类型后 5 位没有用。

struct attribute ((packed)) sdshdr5 {
unsigned char flags; /* 前 3 位保存类型,后 5 位保存字符串长度 /
char buf[];
};
struct attribute ((packed)) sdshdr8 {
uint8_t len; /
字符串长度,1 字节 8 位 /
uint8_t alloc; /
申请的总长度,1 字节 8 位 /
unsigned char flags; /
前 3 位保存类型,后 5 位未使用 /
char buf[];
};
struct attribute ((packed)) sdshdr16 {
uint16_t len; /
字符串长度,2 字节 16 位 /
uint16_t alloc; /
申请的总长度,2 字节 16 位 /
unsigned char flags; /
前 3 位保存类型,后 5 位未使用 /
char buf[];
};
struct attribute ((packed)) sdshdr32 {
uint32_t len; /
字符串长度,4 字节 32 位 /
uint32_t alloc; /
申请的总长度,4 字节 32 位 /
unsigned char flags; /
前 3 位保存类型,后 5 位未使用 /
char buf[];
};
struct attribute ((packed)) sdshdr64 {
uint64_t len; /
字符串长度,8 字节 64 位 /
uint64_t alloc; /
申请的总长度,8 字节 64 位 /
unsigned char flags; /
前 3 位保存类型,后 5 位未使用 */
char buf[];
};
旧版本 SDS 结构示例

优点:

旧版本相对于传统 C 字符串的优点,新版本都有
相对于旧版本,新版本可以通过字符串的长度,选择不同的结构,可以节约内存
使用 attribute ((packed)) ,让编译器取消结构在编译过程中的优化对齐,按照实际占用字节数进行对齐,可以节约内存
SDS 的初始化
sds 的定义,跟传统的C语言字符串保持类型兼容 char *。但是 sds 是二进制安全的,中间可能包含\0。

sds.h

typedef char *sds;
sds.c

// 初始化 sds
sds sdsnewlen(const void *init, size_t initlen) {
// 指向 sdshdr 开始地方的指针
void *sh;
// sds 实际是一个指针,指向 buf 开始的位置
sds s;
// 根据初始化的长度,返回 sds 的类型
char type = sdsReqType(initlen);
// initlen == 0,是空字符串,空字符串往往就是用来往后添加字节的,使用 SDS_TYPE_8 比 SDS_TYPE_5 更好
if (type == SDS_TYPE_5 && initlen == 0) type = SDS_TYPE_8;
// 根据类型获取 struct sdshdr 的长度
int hdrlen = sdsHdrSize(type);
// flags 字段的指针
unsigned char fp;
https://github.com/a9r036f3/kptbymhxiy/discussions/781
https://github.com/fdxr69x2/lfwjghixlp/discussions/785
https://github.com/a9r036f3/kptbymhxiy/discussions/782
https://github.com/fdxr69x2/lfwjghixlp/discussions/786
https://github.com/a9r036f3/kptbymhxiy/discussions/783
https://github.com/fdxr69x2/lfwjghixlp/discussions/787
https://github.com/a9r036f3/kptbymhxiy/discussions/784
https://github.com/fdxr69x2/lfwjghixlp/discussions/788
https://github.com/a9r036f3/kptbymhxiy/discussions/785
https://github.com/fdxr69x2/lfwjghixlp/discussions/789
https://github.com/a9r036f3/kptbymhxiy/discussions/786
https://github.com/fdxr69x2/lfwjghixlp/discussions/790
https://github.com/a9r036f3/kptbymhxiy/discussions/787
https://github.com/fdxr69x2/lfwjghixlp/discussions/791
https://github.com/a9r036f3/kptbymhxiy/discussions/788
https://github.com/fdxr69x2/lfwjghixlp/discussions/792
https://github.com/a9r036f3/kptbymhxiy/discussions/789
https://github.com/fdxr69x2/lfwjghixlp/discussions/793
https://github.com/a9r036f3/kptbymhxiy/discussions/790
https://github.com/a9r036f3/kptbymhxiy/discussions/791
https://github.com/fdxr69x2/lfwjghixlp/discussions/794
https://github.com/a9r036f3/kptbymhxiy/discussions/792
https://github.com/fdxr69x2/lfwjghixlp/discussions/795
https://github.com/a9r036f3/kptbymhxiy/discussions/793
https://github.com/fdxr69x2/lfwjghixlp/discussions/796
https://github.com/91sipp68/jgvawurmpd/discussions/303
https://github.com/l32nyw20/pmtfvfmhdz/discussions/294
https://github.com/l32nyw20/pmtfvfmhdz/discussions/295
https://github.com/91sipp68/jgvawurmpd/discussions/304
https://github.com/91sipp68/jgvawurmpd/discussions/305
https://github.com/l32nyw20/pmtfvfmhdz/discussions/296
https://github.com/l32nyw20/pmtfvfmhdz/discussions/297
https://github.com/91sipp68/jgvawurmpd/discussions/306
https://github.com/91sipp68/jgvawurmpd/discussions/307
https://github.com/91sipp68/jgvawurmpd/discussions/308
https://github.com/l32nyw20/pmtfvfmhdz/discussions/298
https://github.com/91sipp68/jgvawurmpd/discussions/309
https://github.com/91sipp68/jgvawurmpd/discussions/310
https://github.com/l32nyw20/pmtfvfmhdz/discussions/299
https://github.com/l32nyw20/pmtfvfmhdz/discussions/300
https://github.com/91sipp68/jgvawurmpd/discussions/311
https://github.com/91sipp68/jgvawurmpd/discussions/312
https://github.com/l32nyw20/pmtfvfmhdz/discussions/301
https://github.com/l32nyw20/pmtfvfmhdz/discussions/302
https://github.com/91sipp68/jgvawurmpd/discussions/313
https://github.com/l32nyw20/pmtfvfmhdz/discussions/303
https://github.com/91sipp68/jgvawurmpd/discussions/314
https://github.com/l32nyw20/pmtfvfmhdz/discussions/304
https://github.com/91sipp68/jgvawurmpd/discussions/315
https://github.com/l32nyw20/pmtfvfmhdz/discussions/305
https://github.com/91sipp68/jgvawurmpd/discussions/316
https://github.com/l32nyw20/pmtfvfmhdz/discussions/306
https://github.com/91sipp68/jgvawurmpd/discussions/317
https://github.com/l32nyw20/pmtfvfmhdz/discussions/307
https://github.com/91sipp68/jgvawurmpd/discussions/318
https://github.com/l32nyw20/pmtfvfmhdz/discussions/308
https://github.com/91sipp68/jgvawurmpd/discussions/319
https://github.com/91sipp68/jgvawurmpd/discussions/320
https://github.com/l32nyw20/pmtfvfmhdz/discussions/309
https://github.com/91sipp68/jgvawurmpd/discussions/321
https://github.com/l32nyw20/pmtfvfmhdz/discussions/310
https://github.com/91sipp68/jgvawurmpd/discussions/322
https://github.com/l32nyw20/pmtfvfmhdz/discussions/311
https://github.com/91sipp68/jgvawurmpd/discussions/323
https://github.com/l32nyw20/pmtfvfmhdz/discussions/312
https://github.com/91sipp68/jgvawurmpd/discussions/324
https://github.com/l32nyw20/pmtfvfmhdz/discussions/313
https://github.com/91sipp68/jgvawurmpd/discussions/325
https://github.com/l32nyw20/pmtfvfmhdz/discussions/314
https://github.com/91sipp68/jgvawurmpd/discussions/326
https://github.com/l32nyw20/pmtfvfmhdz/discussions/315
https://github.com/91sipp68/jgvawurmpd/discussions/327
https://github.com/l32nyw20/pmtfvfmhdz/discussions/316
https://github.com/91sipp68/jgvawurmpd/discussions/328
https://github.com/l32nyw20/pmtfvfmhdz/discussions/317
https://github.com/l32nyw20/pmtfvfmhdz/discussions/318
https://github.com/91sipp68/jgvawurmpd/discussions/329
https://github.com/91sipp68/jgvawurmpd/discussions/330
https://github.com/l32nyw20/pmtfvfmhdz/discussions/319
https://github.com/l32nyw20/pmtfvfmhdz/discussions/320
https://github.com/91sipp68/jgvawurmpd/discussions/331
https://github.com/91sipp68/jgvawurmpd/discussions/332
https://github.com/l32nyw20/pmtfvfmhdz/discussions/321
https://github.com/91sipp68/jgvawurmpd/discussions/333
https://github.com/l32nyw20/pmtfvfmhdz/discussions/322
https://github.com/l32nyw20/pmtfvfmhdz/discussions/323
https://github.com/91sipp68/jgvawurmpd/discussions/334
https://github.com/91sipp68/jgvawurmpd/discussions/335
https://github.com/l32nyw20/pmtfvfmhdz/discussions/324
https://github.com/91sipp68/jgvawurmpd/discussions/336
https://github.com/l32nyw20/pmtfvfmhdz/discussions/325
https://github.com/91sipp68/jgvawurmpd/discussions/337
https://github.com/l32nyw20/pmtfvfmhdz/discussions/326
https://github.com/91sipp68/jgvawurmpd/discussions/338
https://github.com/l32nyw20/pmtfvfmhdz/discussions/327
https://github.com/91sipp68/jgvawurmpd/discussions/339
https://github.com/l32nyw20/pmtfvfmhdz/discussions/328
https://github.com/91sipp68/jgvawurmpd/discussions/340
https://github.com/91sipp68/jgvawurmpd/discussions/341
https://github.com/l32nyw20/pmtfvfmhdz/discussions/329
https://github.com/91sipp68/jgvawurmpd/discussions/342
https://github.com/l32nyw20/pmtfvfmhdz/discussions/330
https://github.com/91sipp68/jgvawurmpd/discussions/343
https://github.com/l32nyw20/pmtfvfmhdz/discussions/331
https://github.com/l32nyw20/pmtfvfmhdz/discussions/332
https://github.com/l32nyw20/pmtfvfmhdz/discussions/333
https://github.com/l32nyw20/pmtfvfmhdz/discussions/334
https://github.com/l32nyw20/pmtfvfmhdz/discussions/335
https://github.com/l32nyw20/pmtfvfmhdz/discussions/336
https://github.com/91sipp68/jgvawurmpd/discussions/344
https://github.com/91sipp68/jgvawurmpd/discussions/345
https://github.com/l32nyw20/pmtfvfmhdz/discussions/337
https://github.com/91sipp68/jgvawurmpd/discussions/346
https://github.com/91sipp68/jgvawurmpd/discussions/347
https://github.com/l32nyw20/pmtfvfmhdz/discussions/338
https://github.com/91sipp68/jgvawurmpd/discussions/348
https://github.com/91sipp68/jgvawurmpd/discussions/349
https://github.com/l32nyw20/pmtfvfmhdz/discussions/339
https://github.com/91sipp68/jgvawurmpd/discussions/350
https://github.com/l32nyw20/pmtfvfmhdz/discussions/340
https://github.com/91sipp68/jgvawurmpd/discussions/351
https://github.com/l32nyw20/pmtfvfmhdz/discussions/341
https://github.com/91sipp68/jgvawurmpd/discussions/352
https://github.com/l32nyw20/pmtfvfmhdz/discussions/342
https://github.com/91sipp68/jgvawurmpd/discussions/353
https://github.com/l32nyw20/pmtfvfmhdz/discussions/343
https://github.com/91sipp68/jgvawurmpd/discussions/354
https://github.com/l32nyw20/pmtfvfmhdz/discussions/344
https://github.com/l32nyw20/pmtfvfmhdz/discussions/345
https://github.com/91sipp68/jgvawurmpd/discussions/355
https://github.com/l32nyw20/pmtfvfmhdz/discussions/346
https://github.com/91sipp68/jgvawurmpd/discussions/356
https://github.com/l32nyw20/pmtfvfmhdz/discussions/347
https://github.com/91sipp68/jgvawurmpd/discussions/357
https://github.com/91sipp68/jgvawurmpd/discussions/358
https://github.com/a9r036f3/kptbymhxiy/discussions/794
https://github.com/fdxr69x2/lfwjghixlp/discussions/797
https://github.com/fdxr69x2/lfwjghixlp/discussions/798
https://github.com/a9r036f3/kptbymhxiy/discussions/795
https://github.com/fdxr69x2/lfwjghixlp/discussions/799
https://github.com/a9r036f3/kptbymhxiy/discussions/796
https://github.com/a9r036f3/kptbymhxiy/discussions/797
https://github.com/fdxr69x2/lfwjghixlp/discussions/800
https://github.com/fdxr69x2/lfwjghixlp/discussions/801
https://github.com/a9r036f3/kptbymhxiy/discussions/798
https://github.com/fdxr69x2/lfwjghixlp/discussions/802
https://github.com/a9r036f3/kptbymhxiy/discussions/799
https://github.com/fdxr69x2/lfwjghixlp/discussions/803
https://github.com/a9r036f3/kptbymhxiy/discussions/800
https://github.com/fdxr69x2/lfwjghixlp/discussions/804
https://github.com/a9r036f3/kptbymhxiy/discussions/801
https://github.com/fdxr69x2/lfwjghixlp/discussions/805
https://github.com/a9r036f3/kptbymhxiy/discussions/802
https://github.com/fdxr69x2/lfwjghixlp/discussions/806
https://github.com/a9r036f3/kptbymhxiy/discussions/803
https://github.com/fdxr69x2/lfwjghixlp/discussions/807
https://github.com/a9r036f3/kptbymhxiy/discussions/804
https://github.com/a9r036f3/kptbymhxiy/discussions/805
https://github.com/fdxr69x2/lfwjghixlp/discussions/808
https://github.com/a9r036f3/kptbymhxiy/discussions/806
https://github.com/fdxr69x2/lfwjghixlp/discussions/809
https://github.com/a9r036f3/kptbymhxiy/discussions/807
https://github.com/fdxr69x2/lfwjghixlp/discussions/810
https://github.com/a9r036f3/kptbymhxiy/discussions/808
https://github.com/fdxr69x2/lfwjghixlp/discussions/811
https://github.com/a9r036f3/kptbymhxiy/discussions/809
https://github.com/fdxr69x2/lfwjghixlp/discussions/812
https://github.com/a9r036f3/kptbymhxiy/discussions/810
https://github.com/fdxr69x2/lfwjghixlp/discussions/813
https://github.com/a9r036f3/kptbymhxiy/discussions/811
https://github.com/fdxr69x2/lfwjghixlp/discussions/814
https://github.com/a9r036f3/kptbymhxiy/discussions/812
https://github.com/fdxr69x2/lfwjghixlp/discussions/815
https://github.com/a9r036f3/kptbymhxiy/discussions/813
https://github.com/fdxr69x2/lfwjghixlp/discussions/816
https://github.com/a9r036f3/kptbymhxiy/discussions/814
https://github.com/fdxr69x2/lfwjghixlp/discussions/817
https://github.com/a9r036f3/kptbymhxiy/discussions/815
https://github.com/fdxr69x2/lfwjghixlp/discussions/818
https://github.com/a9r036f3/kptbymhxiy/discussions/816
https://github.com/fdxr69x2/lfwjghixlp/discussions/819
https://github.com/a9r036f3/kptbymhxiy/discussions/817
https://github.com/fdxr69x2/lfwjghixlp/discussions/820
https://github.com/a9r036f3/kptbymhxiy/discussions/818
https://github.com/fdxr69x2/lfwjghixlp/discussions/821
https://github.com/a9r036f3/kptbymhxiy/discussions/819
https://github.com/fdxr69x2/lfwjghixlp/discussions/822
https://github.com/a9r036f3/kptbymhxiy/discussions/820
https://github.com/fdxr69x2/lfwjghixlp/discussions/823
https://github.com/a9r036f3/kptbymhxiy/discussions/821
https://github.com/fdxr69x2/lfwjghixlp/discussions/824
https://github.com/a9r036f3/kptbymhxiy/discussions/822
https://github.com/fdxr69x2/lfwjghixlp/discussions/825
https://github.com/a9r036f3/kptbymhxiy/discussions/823
https://github.com/fdxr69x2/lfwjghixlp/discussions/826
https://github.com/a9r036f3/kptbymhxiy/discussions/824
https://github.com/fdxr69x2/lfwjghixlp/discussions/827
https://github.com/fdxr69x2/lfwjghixlp/discussions/828
https://github.com/a9r036f3/kptbymhxiy/discussions/825
https://github.com/fdxr69x2/lfwjghixlp/discussions/829
https://github.com/a9r036f3/kptbymhxiy/discussions/826
https://github.com/fdxr69x2/lfwjghixlp/discussions/830
https://github.com/a9r036f3/kptbymhxiy/discussions/827
https://github.com/fdxr69x2/lfwjghixlp/discussions/831
https://github.com/a9r036f3/kptbymhxiy/discussions/828
https://github.com/fdxr69x2/lfwjghixlp/discussions/832
https://github.com/a9r036f3/kptbymhxiy/discussions/829
https://github.com/fdxr69x2/lfwjghixlp/discussions/833
https://github.com/a9r036f3/kptbymhxiy/discussions/830
https://github.com/fdxr69x2/lfwjghixlp/discussions/834
https://github.com/a9r036f3/kptbymhxiy/discussions/831
https://github.com/a9r036f3/kptbymhxiy/discussions/832
https://github.com/fdxr69x2/lfwjghixlp/discussions/835
https://github.com/a9r036f3/kptbymhxiy/discussions/833
https://github.com/fdxr69x2/lfwjghixlp/discussions/836
https://github.com/a9r036f3/kptbymhxiy/discussions/834
https://github.com/fdxr69x2/lfwjghixlp/discussions/837
https://github.com/a9r036f3/kptbymhxiy/discussions/835
https://github.com/fdxr69x2/lfwjghixlp/discussions/838
https://github.com/fdxr69x2/lfwjghixlp/discussions/839
https://github.com/a9r036f3/kptbymhxiy/discussions/836
https://github.com/fdxr69x2/lfwjghixlp/discussions/840
https://github.com/a9r036f3/kptbymhxiy/discussions/837
https://github.com/fdxr69x2/lfwjghixlp/discussions/841
https://github.com/a9r036f3/kptbymhxiy/discussions/838
https://github.com/fdxr69x2/lfwjghixlp/discussions/842
https://github.com/a9r036f3/kptbymhxiy/discussions/839
https://github.com/fdxr69x2/lfwjghixlp/discussions/843
https://github.com/a9r036f3/kptbymhxiy/discussions/840
https://github.com/fdxr69x2/lfwjghixlp/discussions/844
https://github.com/a9r036f3/kptbymhxiy/discussions/841
https://github.com/fdxr69x2/lfwjghixlp/discussions/845
https://github.com/a9r036f3/kptbymhxiy/discussions/842
https://github.com/fdxr69x2/lfwjghixlp/discussions/846
https://github.com/a9r036f3/kptbymhxiy/discussions/843
https://github.com/fdxr69x2/lfwjghixlp/discussions/847
https://github.com/a9r036f3/kptbymhxiy/discussions/844
https://github.com/fdxr69x2/lfwjghixlp/discussions/848
https://github.com/a9r036f3/kptbymhxiy/discussions/845
https://github.com/a9r036f3/kptbymhxiy/discussions/846
https://github.com/fdxr69x2/lfwjghixlp/discussions/849
https://github.com/fdxr69x2/lfwjghixlp/discussions/850
https://github.com/a9r036f3/kptbymhxiy/discussions/847
https://github.com/fdxr69x2/lfwjghixlp/discussions/851
https://github.com/a9r036f3/kptbymhxiy/discussions/848
https://github.com/fdxr69x2/lfwjghixlp/discussions/852
https://github.com/a9r036f3/kptbymhxiy/discussions/849
https://github.com/fdxr69x2/lfwjghixlp/discussions/853
https://github.com/a9r036f3/kptbymhxiy/discussions/850
https://github.com/fdxr69x2/lfwjghixlp/discussions/854
https://github.com/a9r036f3/kptbymhxiy/discussions/851
https://github.com/fdxr69x2/lfwjghixlp/discussions/855
https://github.com/a9r036f3/kptbymhxiy/discussions/852
https://github.com/fdxr69x2/lfwjghixlp/discussions/856
https://github.com/a9r036f3/kptbymhxiy/discussions/853
https://github.com/fdxr69x2/lfwjghixlp/discussions/857
https://github.com/a9r036f3/kptbymhxiy/discussions/854
https://github.com/fdxr69x2/lfwjghixlp/discussions/858
https://github.com/a9r036f3/kptbymhxiy/discussions/855
https://github.com/a9r036f3/kptbymhxiy/discussions/856
https://github.com/fdxr69x2/lfwjghixlp/discussions/859
https://github.com/a9r036f3/kptbymhxiy/discussions/857
https://github.com/fdxr69x2/lfwjghixlp/discussions/860
https://github.com/a9r036f3/kptbymhxiy/discussions/858
https://github.com/fdxr69x2/lfwjghixlp/discussions/861
https://github.com/a9r036f3/kptbymhxiy/discussions/859
https://github.com/fdxr69x2/lfwjghixlp/discussions/862
https://github.com/a9r036f3/kptbymhxiy/discussions/860
https://github.com/fdxr69x2/lfwjghixlp/discussions/863
https://github.com/a9r036f3/kptbymhxiy/discussions/861
https://github.com/fdxr69x2/lfwjghixlp/discussions/864
https://github.com/a9r036f3/kptbymhxiy/discussions/862
https://github.com/fdxr69x2/lfwjghixlp/discussions/865
https://github.com/a9r036f3/kptbymhxiy/discussions/863
https://github.com/fdxr69x2/lfwjghixlp/discussions/866
https://github.com/a9r036f3/kptbymhxiy/discussions/864
https://github.com/fdxr69x2/lfwjghixlp/discussions/867
https://github.com/a9r036f3/kptbymhxiy/discussions/865
https://github.com/fdxr69x2/lfwjghixlp/discussions/868
https://github.com/a9r036f3/kptbymhxiy/discussions/866
https://github.com/fdxr69x2/lfwjghixlp/discussions/869
https://github.com/a9r036f3/kptbymhxiy/discussions/867
https://github.com/fdxr69x2/lfwjghixlp/discussions/870
https://github.com/a9r036f3/kptbymhxiy/discussions/868
https://github.com/fdxr69x2/lfwjghixlp/discussions/871
https://github.com/a9r036f3/kptbymhxiy/discussions/869
https://github.com/fdxr69x2/lfwjghixlp/discussions/872
https://github.com/a9r036f3/kptbymhxiy/discussions/870
https://github.com/fdxr69x2/lfwjghixlp/discussions/873
https://github.com/a9r036f3/kptbymhxiy/discussions/871
https://github.com/fdxr69x2/lfwjghixlp/discussions/874
https://github.com/a9r036f3/kptbymhxiy/discussions/872
https://github.com/fdxr69x2/lfwjghixlp/discussions/875
https://github.com/a9r036f3/kptbymhxiy/discussions/873
https://github.com/fdxr69x2/lfwjghixlp/discussions/876
https://github.com/fdxr69x2/lfwjghixlp/discussions/877
https://github.com/a9r036f3/kptbymhxiy/discussions/874
https://github.com/fdxr69x2/lfwjghixlp/discussions/878
https://github.com/a9r036f3/kptbymhxiy/discussions/875
https://github.com/fdxr69x2/lfwjghixlp/discussions/879
https://github.com/a9r036f3/kptbymhxiy/discussions/876
https://github.com/fdxr69x2/lfwjghixlp/discussions/880
https://github.com/a9r036f3/kptbymhxiy/discussions/877
https://github.com/fdxr69x2/lfwjghixlp/discussions/881
https://github.com/a9r036f3/kptbymhxiy/discussions/878
https://github.com/fdxr69x2/lfwjghixlp/discussions/882
https://github.com/a9r036f3/kptbymhxiy/discussions/879
https://github.com/fdxr69x2/lfwjghixlp/discussions/883
https://github.com/a9r036f3/kptbymhxiy/discussions/880
https://github.com/fdxr69x2/lfwjghixlp/discussions/884
https://github.com/fdxr69x2/lfwjghixlp/discussions/885
https://github.com/a9r036f3/kptbymhxiy/discussions/881
https://github.com/fdxr69x2/lfwjghixlp/discussions/886
https://github.com/a9r036f3/kptbymhxiy/discussions/882
https://github.com/fdxr69x2/lfwjghixlp/discussions/887
https://github.com/a9r036f3/kptbymhxiy/discussions/883
https://github.com/fdxr69x2/lfwjghixlp/discussions/888
https://github.com/a9r036f3/kptbymhxiy/discussions/884
https://github.com/l32nyw20/pmtfvfmhdz/discussions/348
https://github.com/l32nyw20/pmtfvfmhdz/discussions/349
https://github.com/91sipp68/jgvawurmpd/discussions/359
https://github.com/91sipp68/jgvawurmpd/discussions/360
https://github.com/l32nyw20/pmtfvfmhdz/discussions/350
https://github.com/91sipp68/jgvawurmpd/discussions/361
https://github.com/91sipp68/jgvawurmpd/discussions/362
https://github.com/l32nyw20/pmtfvfmhdz/discussions/351
https://github.com/l32nyw20/pmtfvfmhdz/discussions/352
https://github.com/91sipp68/jgvawurmpd/discussions/363
https://github.com/l32nyw20/pmtfvfmhdz/discussions/353
https://github.com/91sipp68/jgvawurmpd/discussions/364
https://github.com/l32nyw20/pmtfvfmhdz/discussions/354
https://github.com/l32nyw20/pmtfvfmhdz/discussions/355
https://github.com/91sipp68/jgvawurmpd/discussions/365
https://github.com/91sipp68/jgvawurmpd/discussions/366
https://github.com/l32nyw20/pmtfvfmhdz/discussions/356
https://github.com/91sipp68/jgvawurmpd/discussions/367
https://github.com/91sipp68/jgvawurmpd/discussions/368
https://github.com/91sipp68/jgvawurmpd/discussions/369
https://github.com/l32nyw20/pmtfvfmhdz/discussions/357
https://github.com/91sipp68/jgvawurmpd/discussions/370
https://github.com/l32nyw20/pmtfvfmhdz/discussions/358
https://github.com/91sipp68/jgvawurmpd/discussions/371
https://github.com/l32nyw20/pmtfvfmhdz/discussions/359
https://github.com/91sipp68/jgvawurmpd/discussions/372
https://github.com/91sipp68/jgvawurmpd/discussions/373
https://github.com/l32nyw20/pmtfvfmhdz/discussions/360
https://github.com/91sipp68/jgvawurmpd/discussions/374
https://github.com/l32nyw20/pmtfvfmhdz/discussions/361
https://github.com/l32nyw20/pmtfvfmhdz/discussions/362
https://github.com/l32nyw20/pmtfvfmhdz/discussions/363
https://github.com/91sipp68/jgvawurmpd/discussions/375
https://github.com/l32nyw20/pmtfvfmhdz/discussions/364
https://github.com/l32nyw20/pmtfvfmhdz/discussions/365
https://github.com/91sipp68/jgvawurmpd/discussions/376
https://github.com/l32nyw20/pmtfvfmhdz/discussions/366
https://github.com/91sipp68/jgvawurmpd/discussions/377
https://github.com/l32nyw20/pmtfvfmhdz/discussions/367
https://github.com/91sipp68/jgvawurmpd/discussions/378
https://github.com/l32nyw20/pmtfvfmhdz/discussions/368
https://github.com/91sipp68/jgvawurmpd/discussions/379
https://github.com/l32nyw20/pmtfvfmhdz/discussions/369
https://github.com/91sipp68/jgvawurmpd/discussions/380
https://github.com/l32nyw20/pmtfvfmhdz/discussions/370
https://github.com/91sipp68/jgvawurmpd/discussions/381
https://github.com/91sipp68/jgvawurmpd/discussions/382
https://github.com/l32nyw20/pmtfvfmhdz/discussions/371
https://github.com/91sipp68/jgvawurmpd/discussions/383
https://github.com/l32nyw20/pmtfvfmhdz/discussions/372
https://github.com/91sipp68/jgvawurmpd/discussions/384
https://github.com/l32nyw20/pmtfvfmhdz/discussions/373
https://github.com/l32nyw20/pmtfvfmhdz/discussions/374
https://github.com/91sipp68/jgvawurmpd/discussions/385
https://github.com/l32nyw20/pmtfvfmhdz/discussions/375
https://github.com/l32nyw20/pmtfvfmhdz/discussions/376
https://github.com/l32nyw20/pmtfvfmhdz/discussions/377
https://github.com/91sipp68/jgvawurmpd/discussions/386
https://github.com/l32nyw20/pmtfvfmhdz/discussions/378
https://github.com/91sipp68/jgvawurmpd/discussions/387
https://github.com/l32nyw20/pmtfvfmhdz/discussions/379
https://github.com/l32nyw20/pmtfvfmhdz/discussions/380
https://github.com/91sipp68/jgvawurmpd/discussions/388
https://github.com/l32nyw20/pmtfvfmhdz/discussions/381
https://github.com/91sipp68/jgvawurmpd/discussions/389
https://github.com/91sipp68/jgvawurmpd/discussions/390
https://github.com/l32nyw20/pmtfvfmhdz/discussions/382
https://github.com/l32nyw20/pmtfvfmhdz/discussions/383
https://github.com/91sipp68/jgvawurmpd/discussions/391
https://github.com/l32nyw20/pmtfvfmhdz/discussions/384
https://github.com/l32nyw20/pmtfvfmhdz/discussions/385
https://github.com/l32nyw20/pmtfvfmhdz/discussions/386
https://github.com/91sipp68/jgvawurmpd/discussions/392
https://github.com/91sipp68/jgvawurmpd/discussions/393
https://github.com/l32nyw20/pmtfvfmhdz/discussions/387
https://github.com/91sipp68/jgvawurmpd/discussions/394
https://github.com/l32nyw20/pmtfvfmhdz/discussions/388
https://github.com/91sipp68/jgvawurmpd/discussions/395
https://github.com/l32nyw20/pmtfvfmhdz/discussions/389
https://github.com/91sipp68/jgvawurmpd/discussions/396
https://github.com/l32nyw20/pmtfvfmhdz/discussions/390
https://github.com/91sipp68/jgvawurmpd/discussions/397
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/889
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/885
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/890
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/886
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/891
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/887
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/892
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/888
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/893
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/889
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/894
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/890
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/895
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/891
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/896
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/892
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/897
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/893
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/898
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/894
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/899
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/895
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/900
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/896
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/901
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/897
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/902
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/898
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/903
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/899
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/904
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/905
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/900
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/906
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/901
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/907
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/902
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/908
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/903
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/909
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/904
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/910
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/905
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/911
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/906
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/912
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/913
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/907
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/914
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/908
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/915
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/909
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/916
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/917
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/910
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/918
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/911
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/919
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/912
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/920
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/913
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/921
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/914
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/922
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/915
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/923
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/916
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/924
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/917
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/925
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/918
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/926
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/919
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/927
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/920
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/928
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/929
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/921
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/930
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/922
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/923
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/931
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/924
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/932
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/925
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/933
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/926
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/934
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/927
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/935
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/936
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/928
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/937
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/929
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/938
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/930
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/931
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/939
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/932
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/940
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/933
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/941
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/934
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/942
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/935
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/943
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/936
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/937
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/944
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/945
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/938
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/946
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/939
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/947
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/940
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/941
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/948
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/942
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/949
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/950
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/943
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/951
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/944
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/952
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/945
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/946
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/953
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/947
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/954
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/955
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/948
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/956
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/949
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/957
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/950
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/958
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/951
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/959
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/960
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/952
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/953
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/961
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/962
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/954
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/963
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/955
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/964
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/956
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/965
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/966
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/957
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/967
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/958
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/968
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/959
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/969
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/960
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/961
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/970
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/971
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/962
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/972
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/963
https://ggithub.com/fdxr69x2/lfwjghixlp/discussions/973
https://ggithub.com/a9r036f3/kptbymhxiy/discussions/964
https://ggithub.com/91sipp68/jgvawurmpd/discussions/398
https://ggithub.com/l32nyw20/pmtfvfmhdz/discussions/391
https://ggithub.com/91sipp68/jgvawurmpd/discussions/399
https://ggithub.com/l32nyw20/pmtfvfmhdz/discussions/392
https://ggithub.com/l32nyw20/pmtfvfmhdz/discussions/393
https://ggithub.com/91sipp68/jgvawurmpd/discussions/400
https://ggithub.com/91sipp68/jgvawurmpd/discussions/401
https://ggithub.com/l32nyw20/pmtfvfmhdz/discussions/394
https://ggithub.com/91sipp68/jgvawurmpd/discussions/402
https://ggithub.com/l32nyw20/pmtfvfmhdz/discussions/395
https://ggithub.com/91sipp68/jgvawurmpd/discussions/403
https://ggithub.com/91sipp68/jgvawurmpd/discussions/404
https://ggithub.com/l32nyw20/pmtfvfmhdz/discussions/396
https://ggithub.com/91sipp68/jgvawurmpd/discussions/405
https://ggithub.com/l32nyw20/pmtfvfmhdz/discussions/397
https://ggithub.com/91sipp68/jgvawurmpd/discussions/406
https://ggithub.com/l32nyw20/pmtfvfmhdz/discussions/398
https://ggithub.com/91sipp68/jgvawurmpd/discussions/407
https://ggithub.com/l32nyw20/pmtfvfmhdz/discussions/399
https://ggithub.com/91sipp68/jgvawurmpd/discussions/408
https://ggithub.com/91sipp68/jgvawurmpd/discussions/409
https://ggithub.com/l32nyw20/pmtfvfmhdz/discussions/400
https://ggithub.com/91sipp68/jgvawurmpd/discussions/410
https://ggithub.com/l32nyw20/pmtfvfmhdz/discussions/401
https://ggithub.com/91sipp68/jgvawurmpd/discussions/411
https://ggithub.com/91sipp68/jgvawurmpd/discussions/412
https://ggithub.com/l32nyw20/pmtfvfmhdz/discussions/402
https://ggithub.com/91sipp68/jgvawurmpd/discussions/413
https://ggithub.com/l32nyw20/pmtfvfmhdz/discussions/403
https://ggithub.com/l32nyw20/pmtfvfmhdz/discussions/404
https://ggithub.com/91sipp68/jgvawurmpd/discussions/414
https://ggithub.com/91sipp68/jgvawurmpd/discussions/415
https://ggithub.com/91sipp68/jgvawurmpd/discussions/416
https://ggithub.com/91sipp68/jgvawurmpd/discussions/417
https://ggithub.com/l32nyw20/pmtfvfmhdz/discussions/405
https://ggithub.com/l32nyw20/pmtfvfmhdz/discussions/406
https://ggithub.com/91sipp68/jgvawurmpd/discussions/418
https://ggithub.com/l32nyw20/pmtfvfmhdz/discussions/407
https://ggithub.com/l32nyw20/pmtfvfmhdz/discussions/408
https://ggithub.com/91sipp68/jgvawurmpd/discussions/419
https://ggithub.com/91sipp68/jgvawurmpd/discussions/420
https://ggithub.com/l32nyw20/pmtfvfmhdz/discussions/409
https://ggithub.com/91sipp68/jgvawurmpd/discussions/421
https://ggithub.com/l32nyw20/pmtfvfmhdz/discussions/410
https://ggithub.com/91sipp68/jgvawurmpd/discussions/422
https://ggithub.com/l32nyw20/pmtfvfmhdz/discussions/411
https://ggithub.com/91sipp68/jgvawurmpd/discussions/423
https://ggithub.com/l32nyw20/pmtfvfmhdz/discussions/413
https://ggithub.com/91sipp68/jgvawurmpd/discussions/424
https://ggithub.com/l32nyw20/pmtfvfmhdz/discussions/414
https://ggithub.com/91sipp68/jgvawurmpd/discussions/425
https://ggithub.com/l32nyw20/pmtfvfmhdz/discussions/415
https://ggithub.com/91sipp68/jgvawurmpd/discussions/426
https://ggithub.com/l32nyw20/pmtfvfmhdz/discussions/416
https://ggithub.com/91sipp68/jgvawurmpd/discussions/427
https://ggithub.com/l32nyw20/pmtfvfmhdz/discussions/417
https://ggithub.com/91sipp68/jgvawurmpd/discussions/428
https://ggithub.com/l32nyw20/pmtfvfmhdz/discussions/418
https://ggithub.com/91sipp68/jgvawurmpd/discussions/429
https://ggithub.com/l32nyw20/pmtfvfmhdz/discussions/419
https://ggithub.com/91sipp68/jgvawurmpd/discussions/430
// 开始分配空间,+1 是为了最后一个的结束符号 \0
sh = s_malloc(hdrlen+initlen+1);
if (sh == NULL) return NULL;
// const char SDS_NOINIT = “SDS_NOINIT”;
if (init==SDS_NOINIT)
init = NULL;
else if (!init)
// 不是 init 则清空 sh 的内存
memset(sh, 0, hdrlen+initlen+1);
// s 指向了 buf 开始的地址
// 从上面结构可以看出,内存地址的顺序: len, alloc, flag, buf
// 因为 buf 本身不占用空间,hdrlen 实际上就是结构的头(len、alloc、flags)
s = (char
)sh+hdrlen;
// flags 占用 1 个字节,所以 s 退一位就是 flags 的开始位置了
fp = ((unsigned char
)s)-1;
switch(type) {
case SDS_TYPE_5: {
// #define SDS_TYPE_BITS 3
// 前 3 位保存类型,后 5 位保存长度
*fp = type | (initlen << SDS_TYPE_BITS);
break;
}
case SDS_TYPE_8: {
// define SDS_HDR_VAR(T,s) struct sdshdr##T sh = (void)((s)-(sizeof(struct sdshdr##T)));
// sh 变量赋值了 struct sdshdr
SDS_HDR_VAR(8,s);
sh->len = initlen;
sh->alloc = initlen;
*fp = type;
break;
}
// 下面是对 SDS_TYPE_16、SDS_TYPE_32、SDS_TYPE_64 的初始化,跟 SDS_TYPE_8 的类似,篇幅有限,省略…
}
// 如果 init 非空,则把 init 字符串赋值给 s,实际上也是 buf 的初始化
if (initlen && init)
memcpy(s, init, initlen);
// 最后加一个结束标志 \0
s[initlen] = ‘\0’;
return s;
}
SDS 的扩/缩容
扩容
扩容就不跟初始化一样写注释写得那么详细了,直接拉最重要的几句代码就行。

sds sdsMakeRoomFor(sds s, size_t addlen) {
// #define SDS_MAX_PREALLOC (1024*1024)
// 当新的长度小于 1M 的时候,长度会增长一倍
// 当新的长度达到 1M 之后,最多就增长 1M 了
if (newlen < SDS_MAX_PREALLOC)
newlen *= 2;
else
newlen += SDS_MAX_PREALLOC;
// …
}
缩容
sds 缩短不会真正缩小 buf,而是只改长度而已,类型也不变。

sds.c

// 删掉字符串的左右字符中指定的字符
sds sdstrim(sds s, const char *cset) {
char *start, *end, *sp, *ep;
size_t len;

sp = start = s;
ep = end = s+sdslen(s)-1;
while(sp <= end && strchr(cset, *sp)) sp++;
while(ep > sp && strchr(cset, *ep)) ep--;
len = (sp > ep) ? 0 : ((ep-sp)+1);
if (s != sp) memmove(s, sp, len);

// 结尾符
s[len] = '\0';
// 缩短长度
sdssetlen(s,len);
return s;

}
sds.h

static inline void sdssetlen(sds s, size_t newlen) {
// 设置sds长度,只是修改 sdshdr 结构中的长度字段,类型不会变
unsigned char flags = s[-1];
switch(flags&SDS_TYPE_MASK) {
case SDS_TYPE_5:
{
unsigned char fp = ((unsigned char)s)-1;
*fp = (unsigned char)(SDS_TYPE_5 | (newlen << SDS_TYPE_BITS));
}
break;
case SDS_TYPE_8:
SDS_HDR(8,s)->len = (uint8_t)newlen;
break;
case SDS_TYPE_16:
SDS_HDR(16,s)->len = (uint16_t)newlen;
break;
case SDS_TYPE_32:
SDS_HDR(32,s)->len = (uint32_t)newlen;
break;
case SDS_TYPE_64:
SDS_HDR(64,s)->len = (uint64_t)newlen;
break;
}
}

你可能感兴趣的:(Redis 设计与实现 3:字符串 SDS)