xref: /redis-3.2.3/deps/lua/src/lstring.c (revision 21d3294c)
1*21d3294cSantirez /*
2*21d3294cSantirez ** $Id: lstring.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $
3*21d3294cSantirez ** String table (keeps all strings handled by Lua)
4*21d3294cSantirez ** See Copyright Notice in lua.h
5*21d3294cSantirez */
6*21d3294cSantirez 
7*21d3294cSantirez 
8*21d3294cSantirez #include <string.h>
9*21d3294cSantirez 
10*21d3294cSantirez #define lstring_c
11*21d3294cSantirez #define LUA_CORE
12*21d3294cSantirez 
13*21d3294cSantirez #include "lua.h"
14*21d3294cSantirez 
15*21d3294cSantirez #include "lmem.h"
16*21d3294cSantirez #include "lobject.h"
17*21d3294cSantirez #include "lstate.h"
18*21d3294cSantirez #include "lstring.h"
19*21d3294cSantirez 
20*21d3294cSantirez 
21*21d3294cSantirez 
luaS_resize(lua_State * L,int newsize)22*21d3294cSantirez void luaS_resize (lua_State *L, int newsize) {
23*21d3294cSantirez   GCObject **newhash;
24*21d3294cSantirez   stringtable *tb;
25*21d3294cSantirez   int i;
26*21d3294cSantirez   if (G(L)->gcstate == GCSsweepstring)
27*21d3294cSantirez     return;  /* cannot resize during GC traverse */
28*21d3294cSantirez   newhash = luaM_newvector(L, newsize, GCObject *);
29*21d3294cSantirez   tb = &G(L)->strt;
30*21d3294cSantirez   for (i=0; i<newsize; i++) newhash[i] = NULL;
31*21d3294cSantirez   /* rehash */
32*21d3294cSantirez   for (i=0; i<tb->size; i++) {
33*21d3294cSantirez     GCObject *p = tb->hash[i];
34*21d3294cSantirez     while (p) {  /* for each node in the list */
35*21d3294cSantirez       GCObject *next = p->gch.next;  /* save next */
36*21d3294cSantirez       unsigned int h = gco2ts(p)->hash;
37*21d3294cSantirez       int h1 = lmod(h, newsize);  /* new position */
38*21d3294cSantirez       lua_assert(cast_int(h%newsize) == lmod(h, newsize));
39*21d3294cSantirez       p->gch.next = newhash[h1];  /* chain it */
40*21d3294cSantirez       newhash[h1] = p;
41*21d3294cSantirez       p = next;
42*21d3294cSantirez     }
43*21d3294cSantirez   }
44*21d3294cSantirez   luaM_freearray(L, tb->hash, tb->size, TString *);
45*21d3294cSantirez   tb->size = newsize;
46*21d3294cSantirez   tb->hash = newhash;
47*21d3294cSantirez }
48*21d3294cSantirez 
49*21d3294cSantirez 
newlstr(lua_State * L,const char * str,size_t l,unsigned int h)50*21d3294cSantirez static TString *newlstr (lua_State *L, const char *str, size_t l,
51*21d3294cSantirez                                        unsigned int h) {
52*21d3294cSantirez   TString *ts;
53*21d3294cSantirez   stringtable *tb;
54*21d3294cSantirez   if (l+1 > (MAX_SIZET - sizeof(TString))/sizeof(char))
55*21d3294cSantirez     luaM_toobig(L);
56*21d3294cSantirez   ts = cast(TString *, luaM_malloc(L, (l+1)*sizeof(char)+sizeof(TString)));
57*21d3294cSantirez   ts->tsv.len = l;
58*21d3294cSantirez   ts->tsv.hash = h;
59*21d3294cSantirez   ts->tsv.marked = luaC_white(G(L));
60*21d3294cSantirez   ts->tsv.tt = LUA_TSTRING;
61*21d3294cSantirez   ts->tsv.reserved = 0;
62*21d3294cSantirez   memcpy(ts+1, str, l*sizeof(char));
63*21d3294cSantirez   ((char *)(ts+1))[l] = '\0';  /* ending 0 */
64*21d3294cSantirez   tb = &G(L)->strt;
65*21d3294cSantirez   h = lmod(h, tb->size);
66*21d3294cSantirez   ts->tsv.next = tb->hash[h];  /* chain new entry */
67*21d3294cSantirez   tb->hash[h] = obj2gco(ts);
68*21d3294cSantirez   tb->nuse++;
69*21d3294cSantirez   if (tb->nuse > cast(lu_int32, tb->size) && tb->size <= MAX_INT/2)
70*21d3294cSantirez     luaS_resize(L, tb->size*2);  /* too crowded */
71*21d3294cSantirez   return ts;
72*21d3294cSantirez }
73*21d3294cSantirez 
74*21d3294cSantirez 
luaS_newlstr(lua_State * L,const char * str,size_t l)75*21d3294cSantirez TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
76*21d3294cSantirez   GCObject *o;
77*21d3294cSantirez   unsigned int h = cast(unsigned int, l);  /* seed */
78*21d3294cSantirez   size_t step = (l>>5)+1;  /* if string is too long, don't hash all its chars */
79*21d3294cSantirez   size_t l1;
80*21d3294cSantirez   for (l1=l; l1>=step; l1-=step)  /* compute hash */
81*21d3294cSantirez     h = h ^ ((h<<5)+(h>>2)+cast(unsigned char, str[l1-1]));
82*21d3294cSantirez   for (o = G(L)->strt.hash[lmod(h, G(L)->strt.size)];
83*21d3294cSantirez        o != NULL;
84*21d3294cSantirez        o = o->gch.next) {
85*21d3294cSantirez     TString *ts = rawgco2ts(o);
86*21d3294cSantirez     if (ts->tsv.len == l && (memcmp(str, getstr(ts), l) == 0)) {
87*21d3294cSantirez       /* string may be dead */
88*21d3294cSantirez       if (isdead(G(L), o)) changewhite(o);
89*21d3294cSantirez       return ts;
90*21d3294cSantirez     }
91*21d3294cSantirez   }
92*21d3294cSantirez   return newlstr(L, str, l, h);  /* not found */
93*21d3294cSantirez }
94*21d3294cSantirez 
95*21d3294cSantirez 
luaS_newudata(lua_State * L,size_t s,Table * e)96*21d3294cSantirez Udata *luaS_newudata (lua_State *L, size_t s, Table *e) {
97*21d3294cSantirez   Udata *u;
98*21d3294cSantirez   if (s > MAX_SIZET - sizeof(Udata))
99*21d3294cSantirez     luaM_toobig(L);
100*21d3294cSantirez   u = cast(Udata *, luaM_malloc(L, s + sizeof(Udata)));
101*21d3294cSantirez   u->uv.marked = luaC_white(G(L));  /* is not finalized */
102*21d3294cSantirez   u->uv.tt = LUA_TUSERDATA;
103*21d3294cSantirez   u->uv.len = s;
104*21d3294cSantirez   u->uv.metatable = NULL;
105*21d3294cSantirez   u->uv.env = e;
106*21d3294cSantirez   /* chain it on udata list (after main thread) */
107*21d3294cSantirez   u->uv.next = G(L)->mainthread->next;
108*21d3294cSantirez   G(L)->mainthread->next = obj2gco(u);
109*21d3294cSantirez   return u;
110*21d3294cSantirez }
111*21d3294cSantirez 
112