SDS头文件及作用
- sds.h: sds声明- sdsalloc.h: 为sds分配内存
源码文件
sds.h中有这样一行代码
很清晰、明了,sds其实就是char*。
最新的6.2分支的代码:
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__))的设置是告诉编译器取消字节对齐,则结构体的大小就是按照结构体成员实际大小相加得到的。
Redis是在3.2版本(包括3.2)之后把sdshdr改为现在这样的。
……
阅读全文