Redis源码学习(22),sds学习,sds.c(一)

本文详细解析了sds字符串的创建方法,包括sdsnewlen、sdsempty和sdsnew,强调了它们在内存分配、字符串初始化和结束符添加上的实现。同时,介绍了释放sds字符串的sdsfree方法以及清空内容但保留空间的sdsclear方法。sds结构体在管理字符串时考虑了二进制安全和效率,是Redis中广泛使用的字符串表示方式。
摘要由CSDN通过智能技术生成

1 sdsnewlen

1.1 方法说明

  这个方法主要创建一个新的sds字符串。

1.2 方法源代码

/* Create a new sds string with the content specified by the 'init' pointer
 1. and 'initlen'.
 2. If NULL is used for 'init' the string is initialized with zero bytes.
 3.  4. The string is always null-termined (all the sds strings are, always) so
 4. even if you create an sds string with:
 5.  7. mystring = sdsnewlen("abc",3);
 6.  9. You can print the string with printf() as there is an implicit \0 at the
 7. end of the string. However the string is binary safe and can contain
 8. \0 characters in the middle, as the length is stored in the sds header. */
sds sdsnewlen(const void *init, size_t initlen) {
	//声明一个sds 的指针
    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;
	
	//把字符串的长度赋值给sds的len属性
    sh->len = initlen;

	//将剩余空间分配长度设置为0,代表所有空间分配完
    sh->free = 0;
	
	//如果字符串和字符串长度都不为空
	//则将字符串内容通过内存复制到sds中字符数组中
    if (initlen && init)
        memcpy(sh->buf, init, initlen);

	//在字符数组末尾添加结束符
    sh->buf[initlen] = '\0';

	//返回字符数组的指针
    return (char*)sh->buf;
}

1.3 方法理解

方法流程:

  1. 声明一个sds结构体的指针。
  2. 如果字符串不为空,则分配相应的内存空间。
  3. 把字符串的长度赋值给sds的len属性。
  4. 将剩余空间分配长度设置为0,代表所有空间分配完。
  5. 如果字符串和字符串长度都不为空。
  6. 在字符数组末尾添加结束符。
  7. 返回字符数组的指针。

注意事项:

  • zmalloc与zcalloc有区别一个会初始化,一个不会初始化,并且开辟内存方式也略微不同。
  • 这里赋值字符串的时候使用了memcpy,没有使用strcpy因为strcpy遇到’\0‘就会停止。
  • 赋值完字符串之后补充’\0’结束符是为了兼容C语言的一些标准库方法。
  • 分配内存的时候计算空间长度公式:sds结构体的长度+字符串的长度+一个结束符的长度。

2 sdsempty

/* Create an empty (zero length) sds string. Even in this case the string
 * always has an implicit null term. */
sds sdsempty(void) {
    return sdsnewlen("",0);
}

2.1 方法说明

  创建一个空的(零长度)sds字符串。

2.2 方法源代码

/* Create an empty (zero length) sds string. Even in this case the string
 * always has an implicit null term. */
sds sdsempty(void) {
    return sdsnewlen("",0);
}

2.3 方法理解

  这个方法直接调用sdsnewlen,传入了空字符和长度0。

3 sdsnew

3.1 方法说明

  从一个以空结束的C字符串开始创建一个新的sds字符串。

3.2 方法源代码

/* Create a new sds string starting from a null terminated C string. */
sds sdsnew(const char *init) {
    size_t initlen = (init == NULL) ? 0 : strlen(init);
    return sdsnewlen(init, initlen);
}

3.3 方法理解

  这个方法直接调用sdsnewlen,传入了C字符和字符串的长度。

4 sdsfree

4.1 方法说明

  释放sds字符串。如果字符串为NULL,则不执行任何操作。

4.2 方法源代码

/* Free an sds string. No operation is performed if 's' is NULL. */
void sdsfree(sds s) {
    if (s == NULL) return;
    zfree(s-sizeof(struct sdshdr));
}

4.3 方法理解

  调用zfree释放sds字符串

5 sdsclear

5.1 方法说明

  清空当前的sds结构,保留原先的分配空间。

5.2 方法源代码

/* Modify an sds string in-place to make it empty (zero length).
 * However all the existing buffer is not discarded but set as free space
 * so that next append operations will not require allocations up to the
 * number of bytes previously available. */
void sdsclear(sds s) {
	//获取sds结构体指针
    struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
    	
    //设置空间长度为字符串长度
    sh->free += sh->len;
	
	//设置字符串长度为0
    sh->len = 0;
	
	//设置字符串为结束符
    sh->buf[0] = '\0';
}

5.3 方法理解

  1. 获取sds结构体指针
  2. 设置空间长度为字符串长度
  3. 设置字符串长度为0
  4. 设置字符串为结束符

6 总结

  1. sdsnew方法和sdsempty都是调用了sdsnewlen方法来创建一个新的sds字符串。
  2. sds中的字符数组始终会补充一个结束符’\0’,以此兼容C语言的一些字符串方法。
  3. strcpy和memcpy有所区别,前者遇到’\0’会停止复制。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值