各种系统中的string的实现
C的char[]来实现的string在某些时候不是贴别方便,所以我查看了Redis,PHP,Nginx中的string的实现,大致一样,Redis更注重可变性,所以为提前预留了空间给string,让我想到了Golang的切片,哈哈
Redis String
struct sdshdr {
int len;
int free;
char buf[];
};
PHP String in zval
typedef union _zvalue_value {
long lval; /* long value */
double dval; /* double value */
struct {
char *val;
int len;
} str;
HashTable *ht; /* hash table value */
zend_object_value obj;
} zvalue_value;
Nginx String
typedef struct {
size_t len;
u_char *data;
} ngx_str_t;
哦,忽略了一个细节,PHP和Nginx使用的char指针,Redis使用的伸缩型数组成员,C99的,C99之前也可以这样写
struct sdshdr {
int len;
int free;
char buf[0];
};