typedef struct _zend_string zend_string;
zend_string是_zend_string的別名,_zend_string內容如下:
struct _zend_string {
zend_refcounted_h gc;
zend_ulong h; /* hash value */
size_t len;
char val[1];
};
- zend_string定義了四個字段:
gc, h, len, val[1]
,先看下gc的定義,其zend_refcounted_h是一個結構體:
typedef struct _zend_refcounted_h {
uint32_t refcount; /* reference counter 32-bit*/
union {
struct {
ZEND_ENDIAN_LOHI_3(
zend_uchar type, // 類型標識
zend_uchar flags, /* used for strings & objects */
uint16_t gc_info) /* keeps GC root number (or 0) and color */
} v;
uint32_t type_info;
} u;
} zend_refcounted_h;
struct _zend_refcounted {
zend_refcounted_h gc;
};
該結構體用於垃圾回收機制,後續再做分析。
-
h是typedef uint64_t zend_ulong類型的一個變量,保存字符串對應的哈希值,其後續會用在數組裏面。
-
len和val[1]用於標識字符串,c語言中字符串的表示形式可以以\0結尾,通過遍歷得到字符串長度,但是其非二進制安全,如字符串中本身就包含\0,那麼該字符串\0後面的字符串會被截斷,這裏len用於保存字符串的長度, val是一個柔性數組。實現的字符串是二進制安全的。
-
常量字符串的refcount默認是0, 變量字符串的refcount默認是1, 變量字符串是寫時複製的,只有在修改字符串的時候纔會進行拷貝,否則是將兩個變量指向同一內存地址,refcount進行加1。