TString *luaS_newlstr(lua_State *L, constchar *str, size_t l) { GCObject *o; unsignedint h = cast(unsignedint, 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(unsignedchar, str[l1-1])); for (o = G(L)->strt.hash[lmod(h, G(L)->strt.size)]; o != NULL; o = gch(o)->next) { TString *ts = rawgco2ts(o); if (h == ts->tsv.hash && ts->tsv.len == l && (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) { if (isdead(G(L), o)) /* string is dead (but was not collected yet)? */ changewhite(o); /* resurrect it */ return ts; } } return newlstr(L, str, l, h); /* not found; create a new string */ }
Lua 5.3.6 随机生成随机数种子。
1 2 3 4 5 6 7 8 9 10 11 12
#define luai_makeseed() cast(unsigned int, time(NULL)) // create the seed staticunsignedintmakeseed(lua_State* L) { char buff[4 * sizeof(size_t)]; unsignedint h = luai_makeseed(); int p = 0; addbuff(buff, p, L); /* heap variable */ addbuff(buff, p, &h); /* local variable */ addbuff(buff, p, luaO_nilobject); /* global variable */ addbuff(buff, p, &lua_newstate); /* public function */ return luaS_hash(buff, p, h); }