sds模块的具体实现:

1、sdsnewlen 根据参数生成一个sds字符串

 1 sds sdsnewlen(const void *init, size_t initlen) 
 2 {
 3     struct sdshdr *sh;
 4   //如果初始化的内容为NULL,则生成一个内容只有一个\0的串,但是长度不会变,还是传入的长度。zmalloc和zcalloc的功能一样,这个地方为啥还要调用不同的呢。
 5     if (init) {
 6         sh = zmalloc(sizeof(struct sdshdr)+initlen+1);
 7     } else {
 8         sh = zcalloc(sizeof(struct sdshdr)+initlen+1);
 9     }
10     if (sh == NULL) return NULL;//申请失败返回NULL
11     sh->len = initlen;//初始化长度为传入值。
12     sh->free = 0;//设定剩余长度为0
13     if (initlen && init)
14         memcpy(sh->buf, init, initlen);//初始化值
15     sh->buf[initlen] = '\0';填写最后的结尾符
16     return (char*)sh->buf;
17 }

未完待续。。。

相关文章:

  • 2022-12-23
  • 2019-10-06
  • 2021-01-12
  • 2021-08-26
  • 2022-02-13
  • 2021-11-17
  • 2022-12-23
  • 2021-10-18
猜你喜欢
  • 2021-12-10
  • 2022-12-23
  • 2022-12-23
  • 2019-12-22
  • 2021-07-27
  • 2018-10-31
  • 2022-12-23
相关资源
相似解决方案