0.简单概念与引用阅读
SDS(Simple Dynamic Strings)
SDS的特点:扩容方便且二进制安全.
1.源码路径
D:\005-01-代码\001-开源项目源码\007-redis\redis-6.0.8.tar\redis-6.0.8\redis-6.0.8\src\sds.h
D:\005-01-代码\001-开源项目源码\007-redis\redis-6.0.8.tar\redis-6.0.8\redis-6.0.8\src\sds.c
2.源码阅读与理解
2.1 各个规格的简单动态数组的定义
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[];
};
2.2 sdshdr5
2.2.1 结构
flags占1个字节,其低3位(bit)表示type,高5位(bit)表示长度,能表示的长度区间为0~31(25-1),
flags后面就是字符串的内容。
2.3 提供的操作函数
static inline size_t sdslen(const sds s);
static inline size_t sdsavail(const sds s);
static inline void sdssetlen(sds s, size_t newlen) ;
static inline void sdsinclen(sds s, size_t inc) ;
static inline size_t sdsalloc(const sds s);
static inline void sdssetalloc(sds s, size_t newlen);
sds sdsnewlen(const void *init, size_t initlen);
sds sdsnew(const char *init);
sds sdsempty(void);
sds sdsdup(const sds s);
void sdsfree(sds s);
sds sdsgrowzero(sds s, size_t len);
sds sdscatlen(sds s, const void *t, size_t len);
sds sdscat(sds s, const char *t);
sds sdscatsds(sds s, const sds t);
sds sdscpylen(sds s, const char *t, size_t len);
sds sdscpy(sds s, const char *t);
sds sdscatvprintf(sds s, const char *fmt, va_list ap);
#ifdef __GNUC__
sds sdscatprintf(sds s, const char *fmt, ...)
__attribute__((format(printf, 2, 3)));
#else
sds sdscatprintf(sds s, const char *fmt, ...);
#endif
sds sdscatfmt(sds s, char const *fmt, ...);
sds sdstrim(sds s, const char *cset);
void sdsrange(sds s, ssize_t start, ssize_t end);
void sdsupdatelen(sds s);
void sdsclear(sds s);
int sdscmp(const sds s1, const sds s2);
sds *sdssplitlen(const char *s, ssize_t len, const char *sep, int seplen, int *count);
void sdsfreesplitres(sds *tokens, int count);
void sdstolower(sds s);
void sdstoupper(sds s);
sds sdsfromlonglong(long long value);
sds sdscatrepr(sds s, const char *p, size_t len);
sds *sdssplitargs(const char *line, int *argc);
sds sdsmapchars(sds s, const char *from, const char *to, size_t setlen);
sds sdsjoin(char **argv, int argc, char *sep);
sds sdsjoinsds(sds *argv, int argc, const char *sep, size_t seplen);
/* Low level functions exposed to the user API */
sds sdsMakeRoomFor(sds s, size_t addlen);
void sdsIncrLen(sds s, ssize_t incr);
sds sdsRemoveFreeSpace(sds s);
size_t sdsAllocSize(sds s);
void *sdsAllocPtr(sds s);
void *sds_malloc(size_t size);
void *sds_realloc(void *ptr, size_t size);
void sds_free(void *ptr);
#ifdef REDIS_TEST
int sdsTest(int argc, char *argv[]);
#endif
#endif
3.源码中的优秀设计分析
3.1 字节对齐手动设置成1
对于这些sdshdr5,sdshdr8,sdshdr16,sdshdr32,sdshdr64结构体来说,它们都被
__attribute__ ((__packed__))关键字所修饰,这么做的好处:
(1)节省内存,例如sdshdr32可节省3个字节;
(2)SDS返回给上层的,不是结构体首地址,而是指向内容的buf指针。因为此时按1字节对齐,故SDS创建成功
后,无论是sdshdr8、sdshdr16还是sdshdr32,都能通过(char*)sh+hdrlen得到buf指针地址(其中hdrlen是
结构体长度,通过sizeof计算得到)。修饰后,无论是sdshdr8、sdshdr16还是sdshdr32,都能通过buf[-1]找
到flags,因为此时按1字节对齐。若没有packed的修饰,还需要对不同结构进行处理,实现更复杂.
__attribute__ ((__packed__))关键字
4.代码测试与学习
/home/muten/module/my-redis-learning/001-sds-test.c
https://gitee.com/muten/my-redis-learning/blob/master/001-sds-test.c
#include <stdio.h>
typedef unsigned char uint8_t;
typedef unsigned int uint32_t;
typedef
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[];
}MySdshdr8;
typedef
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[];
}MySdshdr32;
typedef
struct BadSdshdr32 {
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[];
}MyBadSdshdr32;
int main(){
printf("sizeof(MySdshdr8) is %d.\n",sizeof(MySdshdr8));
printf("sizeof(MySdshdr32) is %d.\n",sizeof(MySdshdr32));
printf("sizeof(MyBadSdshdr32) is %d.\n",sizeof(MyBadSdshdr32));
return 0;
}