Imported from ../lua-3.0.tar.gz.
[lua.git] / src / tree.c
blob6f3f2d44c47f52ad82c8823b1eb2d536585a4e83
1 /*
2 ** tree.c
3 ** TecCGraf - PUC-Rio
4 */
6 char *rcs_tree="$Id: tree.c,v 1.28 1997/06/11 14:24:40 roberto Exp $";
9 #include <string.h>
11 #include "luamem.h"
12 #include "lua.h"
13 #include "tree.h"
14 #include "lex.h"
15 #include "hash.h"
16 #include "table.h"
17 #include "fallback.h"
20 #define NUM_HASHS 64
22 typedef struct {
23 int size;
24 int nuse; /* number of elements (including EMPTYs) */
25 TaggedString **hash;
26 } stringtable;
28 static int initialized = 0;
30 static stringtable string_root[NUM_HASHS];
32 static TaggedString EMPTY = {LUA_T_STRING, NULL, {{NOT_USED, NOT_USED}},
33 0, 2, {0}};
36 static unsigned long hash (char *s, int tag)
38 unsigned long h;
39 if (tag != LUA_T_STRING)
40 h = (unsigned long)s;
41 else {
42 h = 0;
43 while (*s)
44 h = ((h<<5)-h)^(unsigned char)*(s++);
46 return h;
50 static void luaI_inittree (void)
52 int i;
53 for (i=0; i<NUM_HASHS; i++) {
54 string_root[i].size = 0;
55 string_root[i].nuse = 0;
56 string_root[i].hash = NULL;
61 static void initialize (void)
63 initialized = 1;
64 luaI_inittree();
65 luaI_addReserved();
66 luaI_initsymbol();
67 luaI_initconstant();
68 luaI_initfallbacks();
72 static void grow (stringtable *tb)
74 int newsize = luaI_redimension(tb->size);
75 TaggedString **newhash = newvector(newsize, TaggedString *);
76 int i;
77 for (i=0; i<newsize; i++)
78 newhash[i] = NULL;
79 /* rehash */
80 tb->nuse = 0;
81 for (i=0; i<tb->size; i++)
82 if (tb->hash[i] != NULL && tb->hash[i] != &EMPTY) {
83 int h = tb->hash[i]->hash%newsize;
84 while (newhash[h])
85 h = (h+1)%newsize;
86 newhash[h] = tb->hash[i];
87 tb->nuse++;
89 luaI_free(tb->hash);
90 tb->size = newsize;
91 tb->hash = newhash;
95 static TaggedString *newone(char *buff, int tag, unsigned long h)
97 TaggedString *ts;
98 if (tag == LUA_T_STRING) {
99 ts = (TaggedString *)luaI_malloc(sizeof(TaggedString)+strlen(buff));
100 strcpy(ts->str, buff);
101 ts->u.s.varindex = ts->u.s.constindex = NOT_USED;
102 ts->tag = LUA_T_STRING;
104 else {
105 ts = (TaggedString *)luaI_malloc(sizeof(TaggedString));
106 ts->u.v = buff;
107 ts->tag = tag == LUA_ANYTAG ? 0 : tag;
109 ts->marked = 0;
110 ts->hash = h;
111 return ts;
114 static TaggedString *insert (char *buff, int tag, stringtable *tb)
116 TaggedString *ts;
117 unsigned long h = hash(buff, tag);
118 int i;
119 int j = -1;
120 if ((Long)tb->nuse*3 >= (Long)tb->size*2)
122 if (!initialized)
123 initialize();
124 grow(tb);
126 i = h%tb->size;
127 while ((ts = tb->hash[i]) != NULL)
129 if (ts == &EMPTY)
130 j = i;
131 else if ((ts->tag == LUA_T_STRING) ?
132 (tag == LUA_T_STRING && (strcmp(buff, ts->str) == 0)) :
133 ((tag == ts->tag || tag == LUA_ANYTAG) && buff == ts->u.v))
134 return ts;
135 i = (i+1)%tb->size;
137 /* not found */
138 lua_pack();
139 if (j != -1) /* is there an EMPTY space? */
140 i = j;
141 else
142 tb->nuse++;
143 ts = tb->hash[i] = newone(buff, tag, h);
144 return ts;
147 TaggedString *luaI_createudata (void *udata, int tag)
149 return insert(udata, tag, &string_root[(unsigned)udata%NUM_HASHS]);
152 TaggedString *lua_createstring (char *str)
154 return insert(str, LUA_T_STRING, &string_root[(unsigned)str[0]%NUM_HASHS]);
158 void luaI_strcallIM (TaggedString *l)
160 TObject o;
161 ttype(&o) = LUA_T_USERDATA;
162 for (; l; l=l->next) {
163 tsvalue(&o) = l;
164 luaI_gcIM(&o);
169 void luaI_strfree (TaggedString *l)
171 while (l) {
172 TaggedString *next = l->next;
173 luaI_free(l);
174 l = next;
180 ** Garbage collection function.
182 TaggedString *luaI_strcollector (long *acum)
184 Long counter = 0;
185 TaggedString *frees = NULL;
186 int i;
187 for (i=0; i<NUM_HASHS; i++)
189 stringtable *tb = &string_root[i];
190 int j;
191 for (j=0; j<tb->size; j++)
193 TaggedString *t = tb->hash[j];
194 if (t != NULL && t->marked <= 1)
196 if (t->marked)
197 t->marked = 0;
198 else
200 t->next = frees;
201 frees = t;
202 tb->hash[j] = &EMPTY;
203 counter++;
208 *acum += counter;
209 return frees;