redis-SDS 分析

SDS

SDS是 redis 内置的字符串对象,redis没有使用C 自带的字符串结构,而是自己实现 字符串的表示结构。

SDS 数据结构定义

sds.h中定义了 sds的结构
- len 表示字符串的长度 (不包含末尾的空终止字符 ‘\0’)
- alloc 申请的内存能够容纳的最大字符长度,不包含Header结构和最后的空终止字符
- flags header的类型
- []buf 存储的字符

/* Note: sdshdr5 is never used, we just access the flags byte directly.
 * However is here to document the layout of type 5 SDS strings. */
struct __attribute__ ((__packed__)) sdshdr5 {
    unsigned char flags; /* 3 lsb of type, and 5 msb of string length */
    char buf[];
};
struct __attribute__ ((__packed__)) sdshdr8 {
    uint8_t len; /* used */
    uint8_t alloc; /* excluding the header and null terminator */
    unsigned char flags; /* 3 lsb of type, 5 unused bits */
    char buf[];
};
struct __attribute__ ((__packed__)) sdshdr16 {
    uint16_t len; /* used */
    uint16_t alloc; /* excluding the header and null terminator */
    unsigned char flags; /* 3 lsb of type, 5 unused bits */
    char buf[];
};
struct __attribute__ ((__packed__)) sdshdr32 {
    uint32_t len; /* used */
    uint32_t alloc; /* excluding the header and null terminator */
    unsigned char flags; /* 3 lsb of type, 5 unused bits */
    char buf[];
};
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[];
};

针对不同的大小的字符使用了不同的结构体来存储 ,从而节省内存。使用 _attribute_ ((_packed_)) 取消默认的字节对齐,从而能够利用结构顺序地址直接访问成员属性。

SDS的重点特性

  • 使用 len 属性保存sds长度,使得获取 sds 对象长度的计算变为O(1)
  • 使用 sdsMakeRoomFor 、sdsRemoveFreeSpace 函数在对 sds 做拼接或者剪切操作时 动态调整占用内存,避免了缓冲区溢出及内存泄漏
  • 二进制安全(buf[]不仅可以保存字符串,还可以保存任意的二进制数据)
  • 兼容部分C字符串函数(默认会在末尾添加’\0’字符,做到与c的一些函数库兼容)
以sdscatlen函数为例解析部分代码
  /* Append the specified binary-safe string pointed by 't' of 'len' bytes to the
 * end of the specified sds string 's'.
 *
 * After the call, the passed sds string is no longer valid and all the
 * references must be substituted with the new pointer returned by the call. */
sds sdscatlen(sds s, const void *t, size_t len) {
    //获取当前的字符串长度
    size_t curlen = sdslen(s);
    //make 函数内部判断当前空间是否足够,足够则return,不够则申请更多空间
    s = sdsMakeRoomFor(s,len);
    if (s == NULL) return NULL;
    //拷贝拼接的数据
    memcpy(s+curlen, t, len);
    //设置新的len属性
    sdssetlen(s, curlen+len);
    //为了与c语言的string 兼容,默认末尾会添加一个不计入len的 ‘\0’来标识字符串结束
    s[curlen+len] = '\0';
    return s;
}

/* Append the specified null termianted C string to the sds string 's'.
 *
 * After the call, the passed sds string is no longer valid and all the
 * references must be substituted with the new pointer returned by the call. */
sds sdscat(sds s, const char *t) {
    return sdscatlen(s, t, strlen(t));
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卓修武

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值