sds增加注释

This commit is contained in:
LingZhaoHui 2020-09-09 21:37:20 +08:00
parent 2818922112
commit 916f63d44a
4 changed files with 1478 additions and 31 deletions

1424
.redis/.redis.cbp Normal file

File diff suppressed because it is too large Load Diff

20
.redis/.redis.layout Normal file
View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_layout_file>
<FileVersion major="1" minor="0" />
<ActiveTarget name="Debug" />
<File name="../utils/corrupt_rdb.c" open="0" top="0" tabpos="0" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="338" topLine="0" />
</Cursor>
</File>
<File name="../release.h" open="0" top="0" tabpos="0" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="103" topLine="0" />
</Cursor>
</File>
<File name="../src/server.c" open="1" top="1" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="187643" topLine="5003" />
</Cursor>
</File>
</CodeBlocks_layout_file>

View File

@ -89,21 +89,21 @@ static inline char sdsReqType(size_t string_size) {
sds sdsnewlen(const void *init, size_t initlen) { sds sdsnewlen(const void *init, size_t initlen) {
void *sh; void *sh;
sds s; sds s;
char type = sdsReqType(initlen); char type = sdsReqType(initlen); // 根据字符串长度选择不同烦人类型。
/* Empty strings are usually created in order to append. Use type 8 /* Empty strings are usually created in order to append. Use type 8
* since type 5 is not good at this. */ * since type 5 is not good at this. */
if (type == SDS_TYPE_5 && initlen == 0) type = SDS_TYPE_8; if (type == SDS_TYPE_5 && initlen == 0) type = SDS_TYPE_8;
int hdrlen = sdsHdrSize(type); int hdrlen = sdsHdrSize(type);// 计算不同头部所需的不同长度
unsigned char *fp; /* flags pointer. */ unsigned char *fp; /* 指向flags的指针 */
sh = s_malloc(hdrlen+initlen+1); sh = s_malloc(hdrlen+initlen+1); // "+1" 是为了结束符'\0'
if (sh == NULL) return NULL; if (sh == NULL) return NULL;
if (init==SDS_NOINIT) if (init==SDS_NOINIT)
init = NULL; init = NULL;
else if (!init) else if (!init)
memset(sh, 0, hdrlen+initlen+1); memset(sh, 0, hdrlen+initlen+1);
s = (char*)sh+hdrlen; s = (char*)sh+hdrlen; // s是指向buf的指针
fp = ((unsigned char*)s)-1; fp = ((unsigned char*)s)-1; // s 是柔性数组buf的指针-1指向flags
switch(type) { switch(type) {
case SDS_TYPE_5: { case SDS_TYPE_5: {
*fp = type | (initlen << SDS_TYPE_BITS); *fp = type | (initlen << SDS_TYPE_BITS);
@ -140,7 +140,7 @@ sds sdsnewlen(const void *init, size_t initlen) {
} }
if (initlen && init) if (initlen && init)
memcpy(s, init, initlen); memcpy(s, init, initlen);
s[initlen] = '\0'; s[initlen] = '\0';// 添加末尾的结束
return s; return s;
} }
@ -164,7 +164,7 @@ sds sdsdup(const sds s) {
/* Free an sds string. No operation is performed if 's' is NULL. */ /* Free an sds string. No operation is performed if 's' is NULL. */
void sdsfree(sds s) { void sdsfree(sds s) {
if (s == NULL) return; if (s == NULL) return;
s_free((char*)s-sdsHdrSize(s[-1])); s_free((char*)s-sdsHdrSize(s[-1]));// 此处直接释放内存
} }
/* Set the sds string length to the length as obtained with strlen(), so /* Set the sds string length to the length as obtained with strlen(), so
@ -191,8 +191,8 @@ void sdsupdatelen(sds s) {
* so that next append operations will not require allocations up to the * so that next append operations will not require allocations up to the
* number of bytes previously available. */ * number of bytes previously available. */
void sdsclear(sds s) { void sdsclear(sds s) {
sdssetlen(s, 0); sdssetlen(s, 0);// 统计值len归零
s[0] = '\0'; s[0] = '\0'; // 清空buf
} }
/* Enlarge the free space at the end of the sds string so that the caller /* Enlarge the free space at the end of the sds string so that the caller
@ -393,15 +393,18 @@ sds sdsgrowzero(sds s, size_t len) {
* end of the specified sds string 's'. * end of the specified sds string 's'.
* *
* After the call, the passed sds string is no longer valid and all the * 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. */ * references must be substituted with the new pointer returned by the call.
*
* t的内容和指针s的内容拼接在一起
* */
sds sdscatlen(sds s, const void *t, size_t len) { sds sdscatlen(sds s, const void *t, size_t len) {
size_t curlen = sdslen(s); size_t curlen = sdslen(s);
s = sdsMakeRoomFor(s,len); s = sdsMakeRoomFor(s,len);
if (s == NULL) return NULL; if (s == NULL) return NULL;
memcpy(s+curlen, t, len); memcpy(s+curlen, t, len);// 直接拼接保证了二进制安全
sdssetlen(s, curlen+len); sdssetlen(s, curlen+len);
s[curlen+len] = '\0'; s[curlen+len] = '\0';// 加上结束符
return s; return s;
} }

View File

@ -45,32 +45,32 @@ typedef char *sds;
/* Note: sdshdr5 is never used, we just access the flags byte directly. /* Note: sdshdr5 is never used, we just access the flags byte directly.
* However is here to document the layout of type 5 SDS strings. */ * However is here to document the layout of type 5 SDS strings. */
struct __attribute__ ((__packed__)) sdshdr5 { struct __attribute__ ((__packed__)) sdshdr5 {
unsigned char flags; /* 3 lsb of type, and 5 msb of string length */ unsigned char flags; /* 低三位存储类型高5为存储长度 */
char buf[]; char buf[]; /* 柔性数组,存放实际内容 */
}; };
struct __attribute__ ((__packed__)) sdshdr8 { struct __attribute__ ((__packed__)) sdshdr8 {
uint8_t len; /* used */ uint8_t len; /* 已使用长度用1字节存储 */
uint8_t alloc; /* excluding the header and null terminator */ uint8_t alloc; /* 总长度用1 字节存储 */
unsigned char flags; /* 3 lsb of type, 5 unused bits */ unsigned char flags; /* 低三位存储类型搞5位预留 */
char buf[]; char buf[];/* 柔性数组,存放实际内容 */
}; };
struct __attribute__ ((__packed__)) sdshdr16 { struct __attribute__ ((__packed__)) sdshdr16 {
uint16_t len; /* used */ uint16_t len; /* 已使用长度用2字节存储 */
uint16_t alloc; /* excluding the header and null terminator */ uint16_t alloc; /* 总长度用2字节存储 */
unsigned char flags; /* 3 lsb of type, 5 unused bits */ unsigned char flags; /* 低三位存储类型高5位预留 */
char buf[]; char buf[]; /* 柔性数组,存放实际内容 */
}; };
struct __attribute__ ((__packed__)) sdshdr32 { struct __attribute__ ((__packed__)) sdshdr32 {
uint32_t len; /* used */ uint32_t len; /* 已使用的长度用4字节存储 */
uint32_t alloc; /* excluding the header and null terminator */ uint32_t alloc; /* 总长度用4字节存储 */
unsigned char flags; /* 3 lsb of type, 5 unused bits */ unsigned char flags; /* 低三位存储类型高5位预留 */
char buf[]; char buf[]; /* 柔性数组,存放实际内容 */
}; };
struct __attribute__ ((__packed__)) sdshdr64 { struct __attribute__ ((__packed__)) sdshdr64 {
uint64_t len; /* used */ uint64_t len; /* 已使用长度用8字节存储 */
uint64_t alloc; /* excluding the header and null terminator */ uint64_t alloc; /* 总长度用8字节存储 */
unsigned char flags; /* 3 lsb of type, 5 unused bits */ unsigned char flags; /* 低三位存储类型高5位预留*/
char buf[]; char buf[];/* 柔性数组,存放实际内容 */
}; };
#define SDS_TYPE_5 0 #define SDS_TYPE_5 0