aboutsummaryrefslogtreecommitdiff
path: root/lstring.c
diff options
context:
space:
mode:
Diffstat (limited to 'lstring.c')
-rw-r--r--lstring.c94
1 files changed, 48 insertions, 46 deletions
diff --git a/lstring.c b/lstring.c
index f1353033..e5edc12c 100644
--- a/lstring.c
+++ b/lstring.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstring.c,v 1.26 1999/11/04 17:22:26 roberto Exp roberto $ 2** $Id: lstring.c,v 1.27 1999/11/10 15:39:35 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*/
@@ -7,6 +7,8 @@
7 7
8#include <string.h> 8#include <string.h>
9 9
10#define LUA_REENTRANT
11
10#include "lmem.h" 12#include "lmem.h"
11#include "lobject.h" 13#include "lobject.h"
12#include "lstate.h" 14#include "lstate.h"
@@ -15,8 +17,8 @@
15 17
16 18
17 19
18#define gcsizestring(l) numblocks(0, sizeof(TaggedString)+l) 20#define gcsizestring(L, l) numblocks(L, 0, sizeof(TaggedString)+l)
19#define gcsizeudata gcsizestring(0) 21#define gcsizeudata gcsizestring(L, 0)
20 22
21 23
22 24
@@ -30,9 +32,9 @@
30static TaggedString *init_hash[1] = {NULL}; 32static TaggedString *init_hash[1] = {NULL};
31 33
32 34
33void luaS_init (void) { 35void luaS_init (lua_State *L) {
34 int i; 36 int i;
35 L->string_root = luaM_newvector(NUM_HASHS, stringtable); 37 L->string_root = luaM_newvector(L, NUM_HASHS, stringtable);
36 for (i=0; i<NUM_HASHS; i++) { 38 for (i=0; i<NUM_HASHS; i++) {
37 L->string_root[i].size = 1; 39 L->string_root[i].size = 1;
38 L->string_root[i].nuse = 0; 40 L->string_root[i].nuse = 0;
@@ -41,15 +43,15 @@ void luaS_init (void) {
41} 43}
42 44
43 45
44void luaS_freeall (void) { 46void luaS_freeall (lua_State *L) {
45 int i; 47 int i;
46 for (i=0; i<NUM_HASHS; i++) { 48 for (i=0; i<NUM_HASHS; i++) {
47 LUA_ASSERT(L->string_root[i].nuse==0, "non-empty string table"); 49 LUA_ASSERT(L, L->string_root[i].nuse==0, "non-empty string table");
48 if (L->string_root[i].hash != init_hash) 50 if (L->string_root[i].hash != init_hash)
49 luaM_free(L->string_root[i].hash); 51 luaM_free(L, L->string_root[i].hash);
50 } 52 }
51 luaM_free(L->string_root); 53 luaM_free(L, L->string_root);
52 LUA_ASSERT(init_hash[0] == NULL, "init_hash corrupted"); 54 LUA_ASSERT(L, init_hash[0] == NULL, "init_hash corrupted");
53} 55}
54 56
55 57
@@ -61,9 +63,9 @@ static unsigned long hash_s (const char *s, long l) {
61} 63}
62 64
63 65
64void luaS_grow (stringtable *tb) { 66void luaS_grow (lua_State *L, stringtable *tb) {
65 int ns = luaO_redimension(tb->nuse*2); /* new size */ 67 int ns = luaO_redimension(L, tb->nuse*2); /* new size */
66 TaggedString **newhash = luaM_newvector(ns, TaggedString *); 68 TaggedString **newhash = luaM_newvector(L, ns, TaggedString *);
67 int i; 69 int i;
68 for (i=0; i<ns; i++) newhash[i] = NULL; 70 for (i=0; i<ns; i++) newhash[i] = NULL;
69 /* rehash */ 71 /* rehash */
@@ -77,14 +79,14 @@ void luaS_grow (stringtable *tb) {
77 p = next; 79 p = next;
78 } 80 }
79 } 81 }
80 luaM_free(tb->hash); 82 luaM_free(L, tb->hash);
81 tb->size = ns; 83 tb->size = ns;
82 tb->hash = newhash; 84 tb->hash = newhash;
83} 85}
84 86
85 87
86static TaggedString *newone (long l, unsigned long h) { 88static TaggedString *newone (lua_State *L, long l, unsigned long h) {
87 TaggedString *ts = (TaggedString *)luaM_malloc( 89 TaggedString *ts = (TaggedString *)luaM_malloc(L,
88 sizeof(TaggedString)+l*sizeof(char)); 90 sizeof(TaggedString)+l*sizeof(char));
89 ts->marked = 0; 91 ts->marked = 0;
90 ts->nexthash = NULL; 92 ts->nexthash = NULL;
@@ -93,20 +95,20 @@ static TaggedString *newone (long l, unsigned long h) {
93} 95}
94 96
95 97
96static TaggedString *newone_s (const char *str, long l, unsigned long h) { 98static TaggedString *newone_s (lua_State *L, const char *str, long l, unsigned long h) {
97 TaggedString *ts = newone(l, h); 99 TaggedString *ts = newone(L, l, h);
98 memcpy(ts->str, str, l); 100 memcpy(ts->str, str, l);
99 ts->str[l] = 0; /* ending 0 */ 101 ts->str[l] = 0; /* ending 0 */
100 ts->u.s.gv = NULL; /* no global value */ 102 ts->u.s.gv = NULL; /* no global value */
101 ts->u.s.len = l; 103 ts->u.s.len = l;
102 ts->constindex = 0; 104 ts->constindex = 0;
103 L->nblocks += gcsizestring(l); 105 L->nblocks += gcsizestring(L, l);
104 return ts; 106 return ts;
105} 107}
106 108
107 109
108static TaggedString *newone_u (void *buff, int tag, unsigned long h) { 110static TaggedString *newone_u (lua_State *L, void *buff, int tag, unsigned long h) {
109 TaggedString *ts = newone(0, h); 111 TaggedString *ts = newone(L, 0, h);
110 ts->u.d.value = buff; 112 ts->u.d.value = buff;
111 ts->u.d.tag = (tag == LUA_ANYTAG) ? 0 : tag; 113 ts->u.d.tag = (tag == LUA_ANYTAG) ? 0 : tag;
112 ts->constindex = -1; /* tag -> this is a userdata */ 114 ts->constindex = -1; /* tag -> this is a userdata */
@@ -115,15 +117,15 @@ static TaggedString *newone_u (void *buff, int tag, unsigned long h) {
115} 117}
116 118
117 119
118static void newentry (stringtable *tb, TaggedString *ts, int h) { 120static void newentry (lua_State *L, stringtable *tb, TaggedString *ts, int h) {
119 tb->nuse++; 121 tb->nuse++;
120 if (tb->nuse >= tb->size) { /* no more room? */ 122 if (tb->nuse >= tb->size) { /* no more room? */
121 if (tb->hash == init_hash) { /* cannot change init_hash */ 123 if (tb->hash == init_hash) { /* cannot change init_hash */
122 LUA_ASSERT(h==0, "`init_hash' has size 1"); 124 LUA_ASSERT(L, h==0, "`init_hash' has size 1");
123 tb->hash = luaM_newvector(1, TaggedString *); /* so, `clone' it */ 125 tb->hash = luaM_newvector(L, 1, TaggedString *); /* so, `clone' it */
124 tb->hash[0] = NULL; 126 tb->hash[0] = NULL;
125 } 127 }
126 luaS_grow(tb); 128 luaS_grow(L, tb);
127 h = ts->hash%tb->size; /* new hash position */ 129 h = ts->hash%tb->size; /* new hash position */
128 } 130 }
129 ts->nexthash = tb->hash[h]; /* chain new entry */ 131 ts->nexthash = tb->hash[h]; /* chain new entry */
@@ -131,7 +133,7 @@ static void newentry (stringtable *tb, TaggedString *ts, int h) {
131} 133}
132 134
133 135
134TaggedString *luaS_newlstr (const char *str, long l) { 136TaggedString *luaS_newlstr (lua_State *L, const char *str, long l) {
135 unsigned long h = hash_s(str, l); 137 unsigned long h = hash_s(str, l);
136 stringtable *tb = &L->string_root[h%NUM_HASHSTR]; 138 stringtable *tb = &L->string_root[h%NUM_HASHSTR];
137 int h1 = h%tb->size; 139 int h1 = h%tb->size;
@@ -141,14 +143,14 @@ TaggedString *luaS_newlstr (const char *str, long l) {
141 return ts; 143 return ts;
142 } 144 }
143 /* not found */ 145 /* not found */
144 ts = newone_s(str, l, h); /* create new entry */ 146 ts = newone_s(L, str, l, h); /* create new entry */
145 newentry(tb, ts, h1); /* insert it on table */ 147 newentry(L, tb, ts, h1); /* insert it on table */
146 return ts; 148 return ts;
147} 149}
148 150
149 151
150TaggedString *luaS_createudata (void *udata, int tag) { 152TaggedString *luaS_createudata (lua_State *L, void *udata, int tag) {
151 unsigned long h = IntPoint(udata); 153 unsigned long h = IntPoint(L, udata);
152 stringtable *tb = &L->string_root[(h%NUM_HASHUDATA)+NUM_HASHSTR]; 154 stringtable *tb = &L->string_root[(h%NUM_HASHUDATA)+NUM_HASHSTR];
153 int h1 = h%tb->size; 155 int h1 = h%tb->size;
154 TaggedString *ts; 156 TaggedString *ts;
@@ -157,38 +159,38 @@ TaggedString *luaS_createudata (void *udata, int tag) {
157 return ts; 159 return ts;
158 } 160 }
159 /* not found */ 161 /* not found */
160 ts = newone_u(udata, tag, h); 162 ts = newone_u(L, udata, tag, h);
161 newentry(tb, ts, h1); 163 newentry(L, tb, ts, h1);
162 return ts; 164 return ts;
163} 165}
164 166
165 167
166TaggedString *luaS_new (const char *str) { 168TaggedString *luaS_new (lua_State *L, const char *str) {
167 return luaS_newlstr(str, strlen(str)); 169 return luaS_newlstr(L, str, strlen(str));
168} 170}
169 171
170TaggedString *luaS_newfixedstring (const char *str) { 172TaggedString *luaS_newfixedstring (lua_State *L, const char *str) {
171 TaggedString *ts = luaS_new(str); 173 TaggedString *ts = luaS_new(L, str);
172 if (ts->marked == 0) ts->marked = FIXMARK; /* avoid GC */ 174 if (ts->marked == 0) ts->marked = FIXMARK; /* avoid GC */
173 return ts; 175 return ts;
174} 176}
175 177
176 178
177void luaS_free (TaggedString *t) { 179void luaS_free (lua_State *L, TaggedString *t) {
178 if (t->constindex == -1) /* is userdata? */ 180 if (t->constindex == -1) /* is userdata? */
179 L->nblocks -= gcsizeudata; 181 L->nblocks -= gcsizeudata;
180 else { /* is string */ 182 else { /* is string */
181 L->nblocks -= gcsizestring(t->u.s.len); 183 L->nblocks -= gcsizestring(L, t->u.s.len);
182 luaM_free(t->u.s.gv); 184 luaM_free(L, t->u.s.gv);
183 } 185 }
184 luaM_free(t); 186 luaM_free(L, t);
185} 187}
186 188
187 189
188GlobalVar *luaS_assertglobal (TaggedString *ts) { 190GlobalVar *luaS_assertglobal (lua_State *L, TaggedString *ts) {
189 GlobalVar *gv = ts->u.s.gv; 191 GlobalVar *gv = ts->u.s.gv;
190 if (!gv) { /* no global value yet? */ 192 if (!gv) { /* no global value yet? */
191 gv = luaM_new(GlobalVar); 193 gv = luaM_new(L, GlobalVar);
192 gv->value.ttype = LUA_T_NIL; /* initial value */ 194 gv->value.ttype = LUA_T_NIL; /* initial value */
193 gv->name = ts; 195 gv->name = ts;
194 gv->next = L->rootglobal; /* chain in global list */ 196 gv->next = L->rootglobal; /* chain in global list */
@@ -199,13 +201,13 @@ GlobalVar *luaS_assertglobal (TaggedString *ts) {
199} 201}
200 202
201 203
202GlobalVar *luaS_assertglobalbyname (const char *name) { 204GlobalVar *luaS_assertglobalbyname (lua_State *L, const char *name) {
203 return luaS_assertglobal(luaS_new(name)); 205 return luaS_assertglobal(L, luaS_new(L, name));
204} 206}
205 207
206 208
207int luaS_globaldefined (const char *name) { 209int luaS_globaldefined (lua_State *L, const char *name) {
208 TaggedString *ts = luaS_new(name); 210 TaggedString *ts = luaS_new(L, name);
209 return ts->u.s.gv && ts->u.s.gv->value.ttype != LUA_T_NIL; 211 return ts->u.s.gv && ts->u.s.gv->value.ttype != LUA_T_NIL;
210} 212}
211 213