diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-11-19 15:29:23 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-11-19 15:29:23 -0200 |
| commit | 592a3f289b428e3ee5cc595a266607ad7f5d94ff (patch) | |
| tree | 19a371157be240f7e0f579117d04d466e911afcd /lstring.c | |
| parent | 9cdeb275e7c93007b2ece6f81aaeafe530076805 (diff) | |
| download | lua-592a3f289b428e3ee5cc595a266607ad7f5d94ff.tar.gz lua-592a3f289b428e3ee5cc595a266607ad7f5d94ff.tar.bz2 lua-592a3f289b428e3ee5cc595a266607ad7f5d94ff.zip | |
first implementation of centralized global state.
Diffstat (limited to 'lstring.c')
| -rw-r--r-- | lstring.c | 42 |
1 files changed, 16 insertions, 26 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lstring.c,v 1.3 1997/10/23 16:26:37 roberto Exp roberto $ | 2 | ** $Id: lstring.c,v 1.4 1997/11/04 15:27:53 roberto Exp roberto $ |
| 3 | ** String table (keep all strings handled by Lua) | 3 | ** String table (keep all strings handled by Lua) |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -9,6 +9,7 @@ | |||
| 9 | 9 | ||
| 10 | #include "lmem.h" | 10 | #include "lmem.h" |
| 11 | #include "lobject.h" | 11 | #include "lobject.h" |
| 12 | #include "lstate.h" | ||
| 12 | #include "lstring.h" | 13 | #include "lstring.h" |
| 13 | #include "lua.h" | 14 | #include "lua.h" |
| 14 | 15 | ||
| @@ -19,18 +20,6 @@ | |||
| 19 | #define gcsizestring(l) (1+(l/64)) | 20 | #define gcsizestring(l) (1+(l/64)) |
| 20 | 21 | ||
| 21 | 22 | ||
| 22 | GCnode luaS_root = {NULL, 0}; /* list of global variables */ | ||
| 23 | |||
| 24 | |||
| 25 | typedef struct { | ||
| 26 | int size; | ||
| 27 | int nuse; /* number of elements (including EMPTYs) */ | ||
| 28 | TaggedString **hash; | ||
| 29 | } stringtable; | ||
| 30 | |||
| 31 | |||
| 32 | static stringtable string_root[NUM_HASHS]; | ||
| 33 | |||
| 34 | 23 | ||
| 35 | static TaggedString EMPTY = {{NULL, 2}, 0, 0L, {{LUA_T_NIL, {NULL}}}, {0}}; | 24 | static TaggedString EMPTY = {{NULL, 2}, 0, 0L, {{LUA_T_NIL, {NULL}}}, {0}}; |
| 36 | 25 | ||
| @@ -38,10 +27,11 @@ static TaggedString EMPTY = {{NULL, 2}, 0, 0L, {{LUA_T_NIL, {NULL}}}, {0}}; | |||
| 38 | void luaS_init (void) | 27 | void luaS_init (void) |
| 39 | { | 28 | { |
| 40 | int i; | 29 | int i; |
| 30 | L->string_root = luaM_newvector(NUM_HASHS, stringtable); | ||
| 41 | for (i=0; i<NUM_HASHS; i++) { | 31 | for (i=0; i<NUM_HASHS; i++) { |
| 42 | string_root[i].size = 0; | 32 | L->string_root[i].size = 0; |
| 43 | string_root[i].nuse = 0; | 33 | L->string_root[i].nuse = 0; |
| 44 | string_root[i].hash = NULL; | 34 | L->string_root[i].hash = NULL; |
| 45 | } | 35 | } |
| 46 | } | 36 | } |
| 47 | 37 | ||
| @@ -93,14 +83,14 @@ static TaggedString *newone(char *buff, int tag, unsigned long h) | |||
| 93 | strcpy(ts->str, buff); | 83 | strcpy(ts->str, buff); |
| 94 | ts->u.globalval.ttype = LUA_T_NIL; /* initialize global value */ | 84 | ts->u.globalval.ttype = LUA_T_NIL; /* initialize global value */ |
| 95 | ts->constindex = 0; | 85 | ts->constindex = 0; |
| 96 | luaO_nblocks += gcsizestring(l); | 86 | L->nblocks += gcsizestring(l); |
| 97 | } | 87 | } |
| 98 | else { | 88 | else { |
| 99 | ts = (TaggedString *)luaM_malloc(sizeof(TaggedString)); | 89 | ts = (TaggedString *)luaM_malloc(sizeof(TaggedString)); |
| 100 | ts->u.d.v = buff; | 90 | ts->u.d.v = buff; |
| 101 | ts->u.d.tag = tag == LUA_ANYTAG ? 0 : tag; | 91 | ts->u.d.tag = tag == LUA_ANYTAG ? 0 : tag; |
| 102 | ts->constindex = -1; /* tag -> this is a userdata */ | 92 | ts->constindex = -1; /* tag -> this is a userdata */ |
| 103 | luaO_nblocks++; | 93 | L->nblocks++; |
| 104 | } | 94 | } |
| 105 | ts->head.marked = 0; | 95 | ts->head.marked = 0; |
| 106 | ts->head.next = (GCnode *)ts; /* signal it is in no list */ | 96 | ts->head.next = (GCnode *)ts; /* signal it is in no list */ |
| @@ -138,12 +128,12 @@ static TaggedString *insert (char *buff, int tag, stringtable *tb) | |||
| 138 | 128 | ||
| 139 | TaggedString *luaS_createudata (void *udata, int tag) | 129 | TaggedString *luaS_createudata (void *udata, int tag) |
| 140 | { | 130 | { |
| 141 | return insert(udata, tag, &string_root[(unsigned)udata%NUM_HASHS]); | 131 | return insert(udata, tag, &L->string_root[(unsigned)udata%NUM_HASHS]); |
| 142 | } | 132 | } |
| 143 | 133 | ||
| 144 | TaggedString *luaS_new (char *str) | 134 | TaggedString *luaS_new (char *str) |
| 145 | { | 135 | { |
| 146 | return insert(str, LUA_T_STRING, &string_root[(unsigned)str[0]%NUM_HASHS]); | 136 | return insert(str, LUA_T_STRING, &L->string_root[(unsigned)str[0]%NUM_HASHS]); |
| 147 | } | 137 | } |
| 148 | 138 | ||
| 149 | TaggedString *luaS_newfixedstring (char *str) | 139 | TaggedString *luaS_newfixedstring (char *str) |
| @@ -159,7 +149,7 @@ void luaS_free (TaggedString *l) | |||
| 159 | { | 149 | { |
| 160 | while (l) { | 150 | while (l) { |
| 161 | TaggedString *next = (TaggedString *)l->head.next; | 151 | TaggedString *next = (TaggedString *)l->head.next; |
| 162 | luaO_nblocks -= (l->constindex == -1) ? 1 : gcsizestring(strlen(l->str)); | 152 | L->nblocks -= (l->constindex == -1) ? 1 : gcsizestring(strlen(l->str)); |
| 163 | luaM_free(l); | 153 | luaM_free(l); |
| 164 | l = next; | 154 | l = next; |
| 165 | } | 155 | } |
| @@ -185,9 +175,9 @@ TaggedString *luaS_collector (void) | |||
| 185 | { | 175 | { |
| 186 | TaggedString *frees = NULL; | 176 | TaggedString *frees = NULL; |
| 187 | int i; | 177 | int i; |
| 188 | remove_from_list(&luaS_root); | 178 | remove_from_list(&(L->rootglobal)); |
| 189 | for (i=0; i<NUM_HASHS; i++) { | 179 | for (i=0; i<NUM_HASHS; i++) { |
| 190 | stringtable *tb = &string_root[i]; | 180 | stringtable *tb = &L->string_root[i]; |
| 191 | int j; | 181 | int j; |
| 192 | for (j=0; j<tb->size; j++) { | 182 | for (j=0; j<tb->size; j++) { |
| 193 | TaggedString *t = tb->hash[j]; | 183 | TaggedString *t = tb->hash[j]; |
| @@ -209,8 +199,8 @@ void luaS_rawsetglobal (TaggedString *ts, TObject *newval) | |||
| 209 | { | 199 | { |
| 210 | ts->u.globalval = *newval; | 200 | ts->u.globalval = *newval; |
| 211 | if (ts->head.next == (GCnode *)ts) { /* is not in list? */ | 201 | if (ts->head.next == (GCnode *)ts) { /* is not in list? */ |
| 212 | ts->head.next = luaS_root.next; | 202 | ts->head.next = L->rootglobal.next; |
| 213 | luaS_root.next = (GCnode *)ts; | 203 | L->rootglobal.next = (GCnode *)ts; |
| 214 | } | 204 | } |
| 215 | } | 205 | } |
| 216 | 206 | ||
| @@ -218,7 +208,7 @@ void luaS_rawsetglobal (TaggedString *ts, TObject *newval) | |||
| 218 | char *luaS_travsymbol (int (*fn)(TObject *)) | 208 | char *luaS_travsymbol (int (*fn)(TObject *)) |
| 219 | { | 209 | { |
| 220 | TaggedString *g; | 210 | TaggedString *g; |
| 221 | for (g=(TaggedString *)luaS_root.next; g; g=(TaggedString *)g->head.next) | 211 | for (g=(TaggedString *)L->rootglobal.next; g; g=(TaggedString *)g->head.next) |
| 222 | if (fn(&g->u.globalval)) | 212 | if (fn(&g->u.globalval)) |
| 223 | return g->str; | 213 | return g->str; |
| 224 | return NULL; | 214 | return NULL; |
