summaryrefslogtreecommitdiff
path: root/src/lua51/lstring.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lua51/lstring.c')
-rw-r--r--src/lua51/lstring.c36
1 files changed, 22 insertions, 14 deletions
diff --git a/src/lua51/lstring.c b/src/lua51/lstring.c
index 73d34c4..cadbb4b 100644
--- a/src/lua51/lstring.c
+++ b/src/lua51/lstring.c
@@ -25,19 +25,19 @@ void luaS_resize (lua_State *L, int newsize) {
GCObject **newhash;
stringtable *tb;
int i;
- if (G(L)->gcstate == GCSsweepstring)//如果GC在回收字符串阶段,不要rehash
+ if (G(L)->gcstate == GCSsweepstring)//如果GC在回收字符串阶段,不要rehash,等GC完了再搞
return; /* cannot resize during GC traverse */
newhash = luaM_newvector(L, newsize, GCObject *); //建立一个新的散列桶,并清空
tb = &G(L)->strt;//旧的散列桶
for (i=0; i<newsize; i++) newhash[i] = NULL;
- //遍历就的散列桶,并填入新的散列桶
+ //遍历旧的散列桶,并填入新的散列桶
/* rehash */
for (i=0; i<tb->size; i++) {
- GCObject *p = tb->hash[i];
+ GCObject *p = tb->hash[i]; // 某个桶
while (p) { /* for each node in the list */
GCObject *next = p->gch.next; /* save next */
- unsigned int h = gco2ts(p)->hash;
- //新的散列值
+ unsigned int h = gco2ts(p)->hash; //c 保存hash值
+ //新的散列值,并加入桶中
int h1 = lmod(h, newsize); /* new position */
lua_assert(cast_int(h%newsize) == lmod(h, newsize));
p->gch.next = newhash[h1];//c 接在同一个hash的最前面 /* chain it */
@@ -45,37 +45,42 @@ void luaS_resize (lua_State *L, int newsize) {
p = next;
}
}
+ // 释放旧散列桶
luaM_freearray(L, tb->hash, tb->size, TString *);
tb->size = newsize;
- tb->hash = newhash;
+ tb->hash = newhash; // 设置新散列桶
}
-//c 新建字符串
+//c 新建字符串并加入散列桶
static TString *newlstr (lua_State *L, const char *str, size_t l,
unsigned int h) {
TString *ts;
stringtable *tb;
if (l+1 > (MAX_SIZET - sizeof(TString))/sizeof(char))
luaM_toobig(L);
+ //c 同时申请了TString和字符串内存,字符串内容会紧跟在TString结构后边
ts = cast(TString *, luaM_malloc(L, (l+1)*sizeof(char)+sizeof(TString)));
ts->tsv.len = l;
ts->tsv.hash = h;
ts->tsv.marked = luaC_white(G(L));
ts->tsv.tt = LUA_TSTRING;
ts->tsv.reserved = 0;
- memcpy(ts+1, str, l*sizeof(char));
+
+ memcpy(ts+1, str, l*sizeof(char)); //c 复制字符串内容
((char *)(ts+1))[l] = '\0'; /* ending 0 */
- tb = &G(L)->strt;
- // 计算hash值
- h = lmod(h, tb->size);
+
+ tb = &G(L)->strt; //c 散列桶
+
+ h = lmod(h, tb->size); //c 计算hash值并将这个字符串加入桶
ts->tsv.next = tb->hash[h]; /* chain new entry */
tb->hash[h] = obj2gco(ts);
tb->nuse++;
- //c 给字符串通扩容,如果字符串数量大于桶容量
- //c 给桶扩容为2倍
+
+ //c 如果字符串数量大于桶容量,给字符串通扩容,给桶扩容为2倍
if (tb->nuse > cast(lu_int32, tb->size) && tb->size <= MAX_INT/2)
luaS_resize(L, tb->size*2); /* too crowded */
+
return ts;
}
@@ -89,11 +94,14 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
//c 如果字符串非常长,不要逐位计算散列值,每step步取一个字符计算即可
for (l1=l; l1>=step; l1-=step) /* compute hash */
h = h ^ ((h<<5)+(h>>2)+cast(unsigned char, str[l1-1]));
+ //c 遍历这个hash对应的散列桶
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)) {//c 如果散列值相同,用memcmp快速对比
+ //c 如果长度相同,用memcmp快速对比
+ if (ts->tsv.len == l && (memcmp(str, getstr(ts), l) == 0)) {
+ //c 这个字符串已经存在,直接返回这个引用
//c 如果字符串被标记了回收(gch.marked),重新标记它不要回收
/* string may be dead */
if (isdead(G(L), o)) changewhite(o);