/* ** Common type for all collectable objects */ typedefstructGCObjectGCObject;
/* ** Common Header for all collectable objects (in macro form, to be ** included in other objects) */ #define CommonHeader GCObject *next; lu_byte tt; lu_byte marked
/* ** Common type has only the common header */ structGCObject { CommonHeader; };
/* ** Header for string value; string bytes follow the end of this structure ** (aligned according to 'UTString'; see next). */ typedefstructTString { CommonHeader; lu_byte extra; /* reserved words for short strings; "has hash" for longs */ lu_byte shrlen; /* length for short strings */ unsignedint hash; union { size_t lnglen; /* length for long strings */ structTString *hnext;/* linked list for hash table */ } u; } TString;
为确保内存对齐使用结构体UTString又套了一层:
1 2 3 4 5 6 7
/* ** Ensures that address after this type is always fully aligned. */ typedefunion UTString { L_Umaxalign dummy; /* ensures maximum alignment for strings */ TString tsv; } UTString;
在lstring.c中有创建字符串的函数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* ** creates a new string object */ static TString *createstrobj(lua_State *L, size_t l, int tag, unsignedint h){ TString *ts; GCObject *o; size_t totalsize; /* total size of TString object */ totalsize = sizelstring(l); o = luaC_newobj(L, tag, totalsize); ts = gco2ts(o); ts->hash = h; ts->extra = 0; getstr(ts)[l] = '