diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-06-15 17:36:57 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-06-15 17:36:57 -0300 |
commit | 8e586c13fcf3066886a7edd69011304eaad57a2b (patch) | |
tree | 417c2102ba8c4d693c49a2df839612d371eded50 /lstring.c | |
parent | eadf2aaaffa7a35e7f67b150ce0d57f2c17b9231 (diff) | |
download | lua-8e586c13fcf3066886a7edd69011304eaad57a2b.tar.gz lua-8e586c13fcf3066886a7edd69011304eaad57a2b.tar.bz2 lua-8e586c13fcf3066886a7edd69011304eaad57a2b.zip |
cleaner way to ensure alignment for strings and userdata
Diffstat (limited to 'lstring.c')
-rw-r--r-- | lstring.c | 34 |
1 files changed, 18 insertions, 16 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstring.c,v 1.63 2001/06/06 18:00:19 roberto Exp roberto $ | 2 | ** $Id: lstring.c,v 1.64 2001/06/07 15:01:21 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 | */ |
@@ -32,11 +32,11 @@ void luaS_resize (lua_State *L, int newsize) { | |||
32 | for (i=0; i<tb->size; i++) { | 32 | for (i=0; i<tb->size; i++) { |
33 | TString *p = tb->hash[i]; | 33 | TString *p = tb->hash[i]; |
34 | while (p) { /* for each node in the list */ | 34 | while (p) { /* for each node in the list */ |
35 | TString *next = p->nexthash; /* save next */ | 35 | TString *next = p->tsv.nexthash; /* save next */ |
36 | lu_hash h = p->hash; | 36 | lu_hash h = p->tsv.hash; |
37 | int h1 = lmod(h, newsize); /* new position */ | 37 | int h1 = lmod(h, newsize); /* new position */ |
38 | lua_assert((int)(h%newsize) == lmod(h, newsize)); | 38 | lua_assert((int)(h%newsize) == lmod(h, newsize)); |
39 | p->nexthash = newhash[h1]; /* chain it in new position */ | 39 | p->tsv.nexthash = newhash[h1]; /* chain it in new position */ |
40 | newhash[h1] = p; | 40 | newhash[h1] = p; |
41 | p = next; | 41 | p = next; |
42 | } | 42 | } |
@@ -50,16 +50,16 @@ void luaS_resize (lua_State *L, int newsize) { | |||
50 | static TString *newlstr (lua_State *L, const l_char *str, size_t l, lu_hash h) { | 50 | static TString *newlstr (lua_State *L, const l_char *str, size_t l, lu_hash h) { |
51 | TString *ts = (TString *)luaM_malloc(L, sizestring(l)); | 51 | TString *ts = (TString *)luaM_malloc(L, sizestring(l)); |
52 | stringtable *tb; | 52 | stringtable *tb; |
53 | ts->nexthash = NULL; | 53 | ts->tsv.nexthash = NULL; |
54 | ts->len = l; | 54 | ts->tsv.len = l; |
55 | ts->hash = h; | 55 | ts->tsv.hash = h; |
56 | ts->marked = 0; | 56 | ts->tsv.marked = 0; |
57 | ts->constindex = 0; | 57 | ts->tsv.constindex = 0; |
58 | memcpy(getstr(ts), str, l*sizeof(l_char)); | 58 | memcpy(getstr(ts), str, l*sizeof(l_char)); |
59 | getstr(ts)[l] = l_c('\0'); /* ending 0 */ | 59 | getstr(ts)[l] = l_c('\0'); /* ending 0 */ |
60 | tb = &G(L)->strt; | 60 | tb = &G(L)->strt; |
61 | h = lmod(h, tb->size); | 61 | h = lmod(h, tb->size); |
62 | ts->nexthash = tb->hash[h]; /* chain new entry */ | 62 | ts->tsv.nexthash = tb->hash[h]; /* chain new entry */ |
63 | tb->hash[h] = ts; | 63 | tb->hash[h] = ts; |
64 | tb->nuse++; | 64 | tb->nuse++; |
65 | if (tb->nuse > (ls_nstr)tb->size && tb->size <= MAX_INT/2) | 65 | if (tb->nuse > (ls_nstr)tb->size && tb->size <= MAX_INT/2) |
@@ -75,8 +75,10 @@ TString *luaS_newlstr (lua_State *L, const l_char *str, size_t l) { | |||
75 | size_t l1; | 75 | size_t l1; |
76 | for (l1=l; l1>=step; l1-=step) /* compute hash */ | 76 | for (l1=l; l1>=step; l1-=step) /* compute hash */ |
77 | h = h ^ ((h<<5)+(h>>2)+uchar(str[l1-1])); | 77 | h = h ^ ((h<<5)+(h>>2)+uchar(str[l1-1])); |
78 | for (ts = G(L)->strt.hash[lmod(h, G(L)->strt.size)]; ts; ts = ts->nexthash) { | 78 | for (ts = G(L)->strt.hash[lmod(h, G(L)->strt.size)]; |
79 | if (ts->len == l && (memcmp(str, getstr(ts), l) == 0)) | 79 | ts != NULL; |
80 | ts = ts->tsv.nexthash) { | ||
81 | if (ts->tsv.len == l && (memcmp(str, getstr(ts), l) == 0)) | ||
80 | return ts; | 82 | return ts; |
81 | } | 83 | } |
82 | return newlstr(L, str, l, h); /* not found */ | 84 | return newlstr(L, str, l, h); /* not found */ |
@@ -85,11 +87,11 @@ TString *luaS_newlstr (lua_State *L, const l_char *str, size_t l) { | |||
85 | 87 | ||
86 | Udata *luaS_newudata (lua_State *L, size_t s) { | 88 | Udata *luaS_newudata (lua_State *L, size_t s) { |
87 | Udata *u = (Udata *)luaM_malloc(L, sizeudata(s)); | 89 | Udata *u = (Udata *)luaM_malloc(L, sizeudata(s)); |
88 | u->len = s; | 90 | u->uv.len = s; |
89 | u->tag = 0; | 91 | u->uv.tag = 0; |
90 | u->value = ((union L_UUdata *)(u) + 1); | 92 | u->uv.value = u + 1; |
91 | /* chain it on udata list */ | 93 | /* chain it on udata list */ |
92 | u->next = G(L)->rootudata; | 94 | u->uv.next = G(L)->rootudata; |
93 | G(L)->rootudata = u; | 95 | G(L)->rootudata = u; |
94 | return u; | 96 | return u; |
95 | } | 97 | } |