diff --git a/.redis/.redis.cbp b/.redis/.redis.cbp new file mode 100644 index 0000000..10d4e3c --- /dev/null +++ b/.redis/.redis.cbp @@ -0,0 +1,1424 @@ + + + + + + diff --git a/.redis/.redis.layout b/.redis/.redis.layout new file mode 100644 index 0000000..b70a453 --- /dev/null +++ b/.redis/.redis.layout @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/src/sds.c b/src/sds.c index 1189716..a1b6189 100644 --- a/src/sds.c +++ b/src/sds.c @@ -89,21 +89,21 @@ static inline char sdsReqType(size_t string_size) { sds sdsnewlen(const void *init, size_t initlen) { void *sh; sds s; - char type = sdsReqType(initlen); + char type = sdsReqType(initlen); // 根据字符串长度选择不同烦人类型。 /* Empty strings are usually created in order to append. Use type 8 * since type 5 is not good at this. */ if (type == SDS_TYPE_5 && initlen == 0) type = SDS_TYPE_8; - int hdrlen = sdsHdrSize(type); - unsigned char *fp; /* flags pointer. */ + int hdrlen = sdsHdrSize(type);// 计算不同头部所需的不同长度 + unsigned char *fp; /* 指向flags的指针 */ - sh = s_malloc(hdrlen+initlen+1); + sh = s_malloc(hdrlen+initlen+1); // "+1" 是为了结束符'\0' if (sh == NULL) return NULL; if (init==SDS_NOINIT) init = NULL; else if (!init) memset(sh, 0, hdrlen+initlen+1); - s = (char*)sh+hdrlen; - fp = ((unsigned char*)s)-1; + s = (char*)sh+hdrlen; // s是指向buf的指针 + fp = ((unsigned char*)s)-1; // s 是柔性数组buf的指针,-1指向flags switch(type) { case SDS_TYPE_5: { *fp = type | (initlen << SDS_TYPE_BITS); @@ -140,7 +140,7 @@ sds sdsnewlen(const void *init, size_t initlen) { } if (initlen && init) memcpy(s, init, initlen); - s[initlen] = '\0'; + s[initlen] = '\0';// 添加末尾的结束 return s; } @@ -164,7 +164,7 @@ sds sdsdup(const sds s) { /* Free an sds string. No operation is performed if 's' is NULL. */ void sdsfree(sds s) { 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 @@ -191,8 +191,8 @@ void sdsupdatelen(sds s) { * so that next append operations will not require allocations up to the * number of bytes previously available. */ void sdsclear(sds s) { - sdssetlen(s, 0); - s[0] = '\0'; + sdssetlen(s, 0);// 统计值len归零 + s[0] = '\0'; // 清空buf } /* 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'. * * 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) { size_t curlen = sdslen(s); s = sdsMakeRoomFor(s,len); if (s == NULL) return NULL; - memcpy(s+curlen, t, len); + memcpy(s+curlen, t, len);// 直接拼接保证了二进制安全 sdssetlen(s, curlen+len); - s[curlen+len] = '\0'; + s[curlen+len] = '\0';// 加上结束符 return s; } diff --git a/src/sds.h b/src/sds.h index adcc12c..8f8a742 100644 --- a/src/sds.h +++ b/src/sds.h @@ -45,32 +45,32 @@ typedef char *sds; /* 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[]; + unsigned char flags; /* 低三位存储类型,高5为存储长度 */ + 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[]; + uint8_t len; /* 已使用长度,用1字节存储 */ + uint8_t alloc; /* 总长度,用1 字节存储 */ + unsigned char flags; /* 低三位存储类型,搞5位预留 */ + 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[]; + uint16_t len; /* 已使用长度,用2字节存储 */ + uint16_t alloc; /* 总长度,用2字节存储 */ + unsigned char flags; /* 低三位存储类型,高5位预留 */ + 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[]; + uint32_t len; /* 已使用的长度,用4字节存储 */ + uint32_t alloc; /* 总长度,用4字节存储 */ + unsigned char flags; /* 低三位存储类型,高5位预留 */ + 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[]; + uint64_t len; /* 已使用长度,用8字节存储 */ + uint64_t alloc; /* 总长度,用8字节存储 */ + unsigned char flags; /* 低三位存储类型,高5位预留*/ + char buf[];/* 柔性数组,存放实际内容 */ }; #define SDS_TYPE_5 0