先看LUA中关于字符串TString的源码:
/*
** Header for string value; string bytes follow the end of this structure
** (aligned according to 'UTString'; see next).
*/
typedef struct TString {
CommonHeader;//可GC对象的头
lu_byte extra; /* reserved words for short strings; "has hash" for longs */ //标记是否是虚拟机保留的字符串,如果是短字符串,1就是lua中保留的字符串(关键字),不可GC;长字符串1表示已经hash.
lu_byte shrlen; /* length for short strings *///短字符串的长度
unsigned int hash;//字符串的hash值,字符串的比较可以通过hash值
union {
size_t lnglen; /* length for long strings */ //长字符串的长度
struct TString *hnext; /* linked list for hash table *///指向下一个字符串
} u;
} TString;
/*
** Ensures that address after this type is always fully aligned.
**L_Umaxalign是一个宏,用来保证UTString结构里的TString按照这个长度来对齐
*/
typedef union UTString {
L_Umaxalign dummy; /* ensures maximum alignment for strings */
TString tsv;
} UTString;/*
** Header fo