diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-08-30 16:09:21 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-08-30 16:09:21 -0300 |
commit | fdafd4f4a88d1584dea900517445a17d87f8b82f (patch) | |
tree | 52e38e886d569e77c1df82ebe2bcfbaafd4c6f75 /lstring.c | |
parent | beeff4ccafe5877d00119cb3d93f1f937d46dcfb (diff) | |
download | lua-fdafd4f4a88d1584dea900517445a17d87f8b82f.tar.gz lua-fdafd4f4a88d1584dea900517445a17d87f8b82f.tar.bz2 lua-fdafd4f4a88d1584dea900517445a17d87f8b82f.zip |
new structure for collectable objects, sharing a common header
Diffstat (limited to 'lstring.c')
-rw-r--r-- | lstring.c | 31 |
1 files changed, 16 insertions, 15 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstring.c,v 1.74 2002/04/05 18:54:31 roberto Exp roberto $ | 2 | ** $Id: lstring.c,v 1.75 2002/08/16 14:45:55 roberto Exp roberto $ |
3 | ** String table (keeps all strings handled by Lua) | 3 | ** String table (keeps all strings handled by Lua) |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -23,19 +23,19 @@ void luaS_freeall (lua_State *L) { | |||
23 | 23 | ||
24 | 24 | ||
25 | void luaS_resize (lua_State *L, int newsize) { | 25 | void luaS_resize (lua_State *L, int newsize) { |
26 | TString **newhash = luaM_newvector(L, newsize, TString *); | 26 | GCObject **newhash = luaM_newvector(L, newsize, GCObject *); |
27 | stringtable *tb = &G(L)->strt; | 27 | stringtable *tb = &G(L)->strt; |
28 | int i; | 28 | int i; |
29 | for (i=0; i<newsize; i++) newhash[i] = NULL; | 29 | for (i=0; i<newsize; i++) newhash[i] = NULL; |
30 | /* rehash */ | 30 | /* rehash */ |
31 | for (i=0; i<tb->size; i++) { | 31 | for (i=0; i<tb->size; i++) { |
32 | TString *p = tb->hash[i]; | 32 | GCObject *p = tb->hash[i]; |
33 | while (p) { /* for each node in the list */ | 33 | while (p) { /* for each node in the list */ |
34 | TString *next = p->tsv.nexthash; /* save next */ | 34 | GCObject *next = p->gch.next; /* save next */ |
35 | lu_hash h = p->tsv.hash; | 35 | lu_hash h = (&p->ts)->tsv.hash; |
36 | int h1 = lmod(h, newsize); /* new position */ | 36 | int h1 = lmod(h, newsize); /* new position */ |
37 | lua_assert(cast(int, h%newsize) == lmod(h, newsize)); | 37 | lua_assert(cast(int, h%newsize) == lmod(h, newsize)); |
38 | p->tsv.nexthash = newhash[h1]; /* chain it in new position */ | 38 | p->gch.next = newhash[h1]; /* chain it */ |
39 | newhash[h1] = p; | 39 | newhash[h1] = p; |
40 | p = next; | 40 | p = next; |
41 | } | 41 | } |
@@ -49,17 +49,17 @@ void luaS_resize (lua_State *L, int newsize) { | |||
49 | static TString *newlstr (lua_State *L, const char *str, size_t l, lu_hash h) { | 49 | static TString *newlstr (lua_State *L, const char *str, size_t l, lu_hash h) { |
50 | TString *ts = cast(TString *, luaM_malloc(L, sizestring(l))); | 50 | TString *ts = cast(TString *, luaM_malloc(L, sizestring(l))); |
51 | stringtable *tb; | 51 | stringtable *tb; |
52 | ts->tsv.nexthash = NULL; | ||
53 | ts->tsv.len = l; | 52 | ts->tsv.len = l; |
54 | ts->tsv.hash = h; | 53 | ts->tsv.hash = h; |
55 | ts->tsv.marked = 0; | 54 | ts->tsv.marked = 0; |
55 | ts->tsv.tt = LUA_TSTRING; | ||
56 | ts->tsv.reserved = 0; | 56 | ts->tsv.reserved = 0; |
57 | memcpy(ts+1, str, l*sizeof(char)); | 57 | memcpy(ts+1, str, l*sizeof(char)); |
58 | ((char *)(ts+1))[l] = '\0'; /* ending 0 */ | 58 | ((char *)(ts+1))[l] = '\0'; /* ending 0 */ |
59 | tb = &G(L)->strt; | 59 | tb = &G(L)->strt; |
60 | h = lmod(h, tb->size); | 60 | h = lmod(h, tb->size); |
61 | ts->tsv.nexthash = tb->hash[h]; /* chain new entry */ | 61 | ts->tsv.next = tb->hash[h]; /* chain new entry */ |
62 | tb->hash[h] = ts; | 62 | tb->hash[h] = cast(GCObject *, ts); |
63 | tb->nuse++; | 63 | tb->nuse++; |
64 | if (tb->nuse > cast(ls_nstr, tb->size) && tb->size <= MAX_INT/2) | 64 | if (tb->nuse > cast(ls_nstr, tb->size) && tb->size <= MAX_INT/2) |
65 | luaS_resize(L, tb->size*2); /* too crowded */ | 65 | luaS_resize(L, tb->size*2); /* too crowded */ |
@@ -68,7 +68,7 @@ static TString *newlstr (lua_State *L, const char *str, size_t l, lu_hash h) { | |||
68 | 68 | ||
69 | 69 | ||
70 | TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { | 70 | TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { |
71 | TString *ts; | 71 | GCObject *ts; |
72 | lu_hash h = (lu_hash)l; /* seed */ | 72 | lu_hash h = (lu_hash)l; /* seed */ |
73 | size_t step = (l>>5)+1; /* if string is too long, don't hash all its chars */ | 73 | size_t step = (l>>5)+1; /* if string is too long, don't hash all its chars */ |
74 | size_t l1; | 74 | size_t l1; |
@@ -76,9 +76,9 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { | |||
76 | h = h ^ ((h<<5)+(h>>2)+(unsigned char)(str[l1-1])); | 76 | h = h ^ ((h<<5)+(h>>2)+(unsigned char)(str[l1-1])); |
77 | for (ts = G(L)->strt.hash[lmod(h, G(L)->strt.size)]; | 77 | for (ts = G(L)->strt.hash[lmod(h, G(L)->strt.size)]; |
78 | ts != NULL; | 78 | ts != NULL; |
79 | ts = ts->tsv.nexthash) { | 79 | ts = ts->gch.next) { |
80 | if (ts->tsv.len == l && (memcmp(str, getstr(ts), l) == 0)) | 80 | if ((&ts->ts)->tsv.len == l && (memcmp(str, getstr(&ts->ts), l) == 0)) |
81 | return ts; | 81 | return &ts->ts; |
82 | } | 82 | } |
83 | return newlstr(L, str, l, h); /* not found */ | 83 | return newlstr(L, str, l, h); /* not found */ |
84 | } | 84 | } |
@@ -87,12 +87,13 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { | |||
87 | Udata *luaS_newudata (lua_State *L, size_t s) { | 87 | Udata *luaS_newudata (lua_State *L, size_t s) { |
88 | Udata *u; | 88 | Udata *u; |
89 | u = cast(Udata *, luaM_malloc(L, sizeudata(s))); | 89 | u = cast(Udata *, luaM_malloc(L, sizeudata(s))); |
90 | u->uv.marked = 0; | 90 | u->uv.marked = (1<<1); /* is not finalized */ |
91 | u->uv.tt = LUA_TUSERDATA; | ||
91 | u->uv.len = s; | 92 | u->uv.len = s; |
92 | u->uv.metatable = hvalue(defaultmeta(L)); | 93 | u->uv.metatable = hvalue(defaultmeta(L)); |
93 | /* chain it on udata list */ | 94 | /* chain it on udata list */ |
94 | u->uv.next = G(L)->rootudata; | 95 | u->uv.next = G(L)->rootudata; |
95 | G(L)->rootudata = u; | 96 | G(L)->rootudata = cast(GCObject *, u); |
96 | return u; | 97 | return u; |
97 | } | 98 | } |
98 | 99 | ||