lua table重新进行内存分配的问题
static void resize (lua_State *L, Table *t, int nasize, int nhsize) {
int i;
int oldasize = t->sizearray;
int oldhsize = t->lsizenode;
Node *nold = t->node; /* save old hash ... */
if (nasize > oldasize) /* array part must grow? */
setarrayvector(L, t, nasize);
/* create new hash part with appropriate size */
setnodevector(L, t, nhsize);
if (nasize < oldasize) { /* array part must shrink? */
t->sizearray = nasize;
/* re-insert elements from vanishing slice */
for (i=nasize; i<oldasize; i++) {
if (!ttisnil(&t->array[i]))
setobjt2t(L, luaH_setnum(L, t, i+1), &t->array[i]);
}
/* shrink array */
luaM_reallocvector(L, t->array, oldasize, nasize, TValue);
}
/* re-insert elements from hash part */
for (i = twoto(oldhsize) - 1; i >= 0; i--) { //这里重新对nodehash表进行赋值
Node *old = nold+i;
if (!ttisnil(gval(old)))
setobjt2t(L, luaH_set(L, t, key2tval(old)), gval(old));
}
if (nold != dummynode)
luaM_freearray(L, nold, twoto(oldhsize), Node); /* free old array */
}
有没有人对lua源码有研究的,我是看到lua table内存进行重新分配的地方。函数代码resize我贴出来了,有个疑问地方就是在对node hash表进行赋值的时(21-24行),这里我们到只对头节点进行数据迁移,其链表后面的数据却没有迁移到新的表中的。觉得自己应该是有什么地方错过了,但是看了很久也没有看明白。比较Lua中stringtable中的resize的时候,就对其GCObject hash表中的链表进行了遍历。求大神。。。
level0
9 years, 5 months ago