本篇文章,主要探討一下lua中的字串緩衝管理(涉及到的檔案 lstring.c )。
在lua的9種資料類型中,字串是屬於可以被GC回收的類型。在lua中,操作字串實際上是在操作字串引用,當字串不在被使用的時候,GC會通過一定演算法回收。
--lua9種資料類型:
#define LUA_TNIL0<br />#define LUA_TBOOLEAN1<br />#define LUA_TLIGHTUSERDATA2<br />#define LUA_TNUMBER3<br />#define LUA_TSTRING4<br />#define LUA_TTABLE5<br />#define LUA_TFUNCTION6<br />#define LUA_TUSERDATA7<br />#define LUA_TTHREAD8
字串分配
--源碼:
static TString *newlstr (lua_State *L, const char *str, size_t l,<br /> unsigned int h) {<br /> TString *ts;<br /> stringtable *tb;<br /> if (l+1 > (MAX_SIZET - sizeof(TString))/sizeof(char))<br /> luaM_toobig(L);<br /> ts = cast(TString *, luaM_malloc(L, (l+1)*sizeof(char)+sizeof(TString)));<br /> ts->tsv.len = l;<br /> ts->tsv.hash = h;<br /> ts->tsv.marked = luaC_white(G(L));<br /> ts->tsv.tt = LUA_TSTRING;<br /> ts->tsv.reserved = 0;<br /> memcpy(ts+1, str, l*sizeof(char));<br /> ((char *)(ts+1))[l] = '/0'; /* ending 0 */<br /> tb = &G(L)->strt;<br /> h = lmod(h, tb->size);<br /> ts->tsv.next = tb->hash[h]; /* chain new entry */<br /> tb->hash[h] = obj2gco(ts);<br /> tb->nuse++;<br /> if (tb->nuse > cast(lu_int32, tb->size) && tb->size <= MAX_INT/2)<br /> luaS_resize(L, tb->size*2); /* too crowded */<br /> return ts;<br />}<br />
ts = cast(TString *, luaM_malloc(L, (l+1)*sizeof(char)+sizeof(TString)));
--該行代碼申請放置字串的記憶體,luaM_malloc是申請記憶體的一個宏定義,調用上篇文章中提到的luaM_realloc_ 函數來來分配記憶體,在lua中字串是用結構
/*<br />** String headers for string table<br />*/<br />typedef union TString {<br /> L_Umaxalign dummy; /* ensures maximum alignment for strings */<br /> struct {<br /> CommonHeader;<br /> lu_byte reserved;<br /> unsigned int hash;<br /> size_t len;<br /> } tsv;<br />} TString;</p><p>#define CommonHeaderGCObject *next; lu_byte tt; lu_byte marked
表示的,所以申請的記憶體大小是(l+1)*sizeof(char)+sizeof(TString),記憶體分布:TString+str+'/0',然後給TString賦值hash code, len,marted,reserved,拷貝字串。
tb = &G(L)->strt;
h = lmod(h, tb->size);
ts->tsv.next = tb->hash[h]; /* chain new entry */
tb->hash[h] = obj2gco(ts);
tb->nuse++;
--接下來,將分配的字串掛到全域字串管理對象strt上
if (tb->nuse > cast(lu_int32, tb->size) && tb->size <= MAX_INT/2)
luaS_resize(L, tb->size*2); /* too crowded */
--對字串大小的擴充
字串緩衝
--源碼:
TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {<br /> GCObject *o;<br /> unsigned int h = cast(unsigned int, l); /* seed */<br /> size_t step = (l>>5)+1; /* if string is too long, don't hash all its chars */<br /> size_t l1;<br /> for (l1=l; l1>=step; l1-=step) /* compute hash */<br /> h = h ^ ((h<<5)+(h>>2)+cast(unsigned char, str[l1-1]));<br /> for (o = G(L)->strt.hash[lmod(h, G(L)->strt.size)];<br /> o != NULL;<br /> o = o->gch.next) {<br /> TString *ts = rawgco2ts(o);<br /> if (ts->tsv.len == l && (memcmp(str, getstr(ts), l) == 0)) {<br /> /* string may be dead */<br /> if (isdead(G(L), o)) changewhite(o);<br /> return ts;<br /> }<br /> }<br /> return newlstr(L, str, l, h); /* not found */<br />}<br />
unsigned int h = cast(unsigned int, l); /* seed */
size_t step = (l>>5)+1; /* if string is too long, don't hash all its chars */
size_t l1;
for (l1=l; l1>=step; l1-=step) /* compute hash */
h = h ^ ((h<<5)+(h>>2)+cast(unsigned char, str[l1-1]));
---根據字串長度l和字串內容str計算字串hash code,字串hash code用作該字串的key,後續用作字串緩衝key,這個演算法可以保證key-value一一對應。
for (o = G(L)->strt.hash[lmod(h, G(L)->strt.size)];
o != NULL;
o = o->gch.next) {
TString *ts = rawgco2ts(o);
if (ts->tsv.len == l && (memcmp(str, getstr(ts), l) == 0)) {
/* string may be dead */
if (isdead(G(L), o)) changewhite(o);
return ts;
}
}
--在字串全域管理對象中尋找該字串,如果查到就使用該字串返回,否則:
return newlstr(L, str, l, h);
申請記憶體放置字串。