diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-09-16 16:25:59 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-09-16 16:25:59 -0300 |
commit | daa858ef278ad136afcb62d7f625b491ffbd5fa3 (patch) | |
tree | 1b3ce52cd504f66b9bd00b301544ed9308ba85c8 /lstring.c | |
parent | ea169d20835aa7d2a42641decc5b5d802047afca (diff) | |
download | lua-daa858ef278ad136afcb62d7f625b491ffbd5fa3.tar.gz lua-daa858ef278ad136afcb62d7f625b491ffbd5fa3.tar.bz2 lua-daa858ef278ad136afcb62d7f625b491ffbd5fa3.zip |
String table (keep all strings handled by Lua)
Diffstat (limited to 'lstring.c')
-rw-r--r-- | lstring.c | 186 |
1 files changed, 186 insertions, 0 deletions
diff --git a/lstring.c b/lstring.c new file mode 100644 index 00000000..c6c77aa6 --- /dev/null +++ b/lstring.c | |||
@@ -0,0 +1,186 @@ | |||
1 | /* | ||
2 | ** $Id: lstring.c,v 1.1 1997/08/14 20:23:30 roberto Exp $ | ||
3 | ** String table (keep all strings handled by Lua) | ||
4 | ** See Copyright Notice in lua.h | ||
5 | */ | ||
6 | |||
7 | |||
8 | #include <string.h> | ||
9 | |||
10 | #include "lmem.h" | ||
11 | #include "lobject.h" | ||
12 | #include "lstring.h" | ||
13 | #include "lua.h" | ||
14 | |||
15 | |||
16 | #define NUM_HASHS 61 | ||
17 | |||
18 | typedef struct { | ||
19 | int size; | ||
20 | int nuse; /* number of elements (including EMPTYs) */ | ||
21 | TaggedString **hash; | ||
22 | } stringtable; | ||
23 | |||
24 | |||
25 | static stringtable string_root[NUM_HASHS] = { | ||
26 | {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, | ||
27 | {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, | ||
28 | {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, | ||
29 | {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, | ||
30 | {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, | ||
31 | {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, | ||
32 | {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, | ||
33 | {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, | ||
34 | {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, | ||
35 | {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, | ||
36 | {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, | ||
37 | {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, | ||
38 | {0, 0, NULL} | ||
39 | }; | ||
40 | |||
41 | |||
42 | static TaggedString EMPTY = {LUA_T_STRING, {0}, {{NOT_USED, NOT_USED}}, 2, {0}}; | ||
43 | |||
44 | |||
45 | |||
46 | static unsigned long hash (char *s, int tag) | ||
47 | { | ||
48 | unsigned long h; | ||
49 | if (tag != LUA_T_STRING) | ||
50 | h = (unsigned long)s; | ||
51 | else { | ||
52 | h = 0; | ||
53 | while (*s) | ||
54 | h = ((h<<5)-h)^(unsigned char)*(s++); | ||
55 | } | ||
56 | return h; | ||
57 | } | ||
58 | |||
59 | |||
60 | static void grow (stringtable *tb) | ||
61 | { | ||
62 | int newsize = luaO_redimension(tb->size); | ||
63 | TaggedString **newhash = luaM_newvector(newsize, TaggedString *); | ||
64 | int i; | ||
65 | for (i=0; i<newsize; i++) | ||
66 | newhash[i] = NULL; | ||
67 | /* rehash */ | ||
68 | tb->nuse = 0; | ||
69 | for (i=0; i<tb->size; i++) { | ||
70 | if (tb->hash[i] != NULL && tb->hash[i] != &EMPTY) { | ||
71 | int h = tb->hash[i]->uu.hash%newsize; | ||
72 | while (newhash[h]) | ||
73 | h = (h+1)%newsize; | ||
74 | newhash[h] = tb->hash[i]; | ||
75 | tb->nuse++; | ||
76 | } | ||
77 | } | ||
78 | luaM_free(tb->hash); | ||
79 | tb->size = newsize; | ||
80 | tb->hash = newhash; | ||
81 | } | ||
82 | |||
83 | |||
84 | static TaggedString *newone(char *buff, int tag, unsigned long h) | ||
85 | { | ||
86 | TaggedString *ts; | ||
87 | if (tag == LUA_T_STRING) { | ||
88 | ts = (TaggedString *)luaM_malloc(sizeof(TaggedString)+strlen(buff)); | ||
89 | strcpy(ts->str, buff); | ||
90 | ts->u.s.varindex = ts->u.s.constindex = NOT_USED; | ||
91 | ts->tag = LUA_T_STRING; | ||
92 | } | ||
93 | else { | ||
94 | ts = (TaggedString *)luaM_malloc(sizeof(TaggedString)); | ||
95 | ts->u.v = buff; | ||
96 | ts->tag = tag == LUA_ANYTAG ? 0 : tag; | ||
97 | } | ||
98 | ts->marked = 0; | ||
99 | ts->uu.hash = h; | ||
100 | return ts; | ||
101 | } | ||
102 | |||
103 | static TaggedString *insert (char *buff, int tag, stringtable *tb) | ||
104 | { | ||
105 | TaggedString *ts; | ||
106 | unsigned long h = hash(buff, tag); | ||
107 | int i; | ||
108 | int j = -1; | ||
109 | if ((long)tb->nuse*3 >= (long)tb->size*2) | ||
110 | grow(tb); | ||
111 | i = h%tb->size; | ||
112 | while ((ts = tb->hash[i]) != NULL) | ||
113 | { | ||
114 | if (ts == &EMPTY) | ||
115 | j = i; | ||
116 | else if ((ts->tag == LUA_T_STRING) ? | ||
117 | (tag == LUA_T_STRING && (strcmp(buff, ts->str) == 0)) : | ||
118 | ((tag == ts->tag || tag == LUA_ANYTAG) && buff == ts->u.v)) | ||
119 | return ts; | ||
120 | i = (i+1)%tb->size; | ||
121 | } | ||
122 | /* not found */ | ||
123 | ++luaO_nentities; | ||
124 | if (j != -1) /* is there an EMPTY space? */ | ||
125 | i = j; | ||
126 | else | ||
127 | tb->nuse++; | ||
128 | ts = tb->hash[i] = newone(buff, tag, h); | ||
129 | return ts; | ||
130 | } | ||
131 | |||
132 | TaggedString *luaS_createudata (void *udata, int tag) | ||
133 | { | ||
134 | return insert(udata, tag, &string_root[(unsigned)udata%NUM_HASHS]); | ||
135 | } | ||
136 | |||
137 | TaggedString *luaS_new (char *str) | ||
138 | { | ||
139 | return insert(str, LUA_T_STRING, &string_root[(unsigned)str[0]%NUM_HASHS]); | ||
140 | } | ||
141 | |||
142 | TaggedString *luaS_newfixedstring (char *str) | ||
143 | { | ||
144 | TaggedString *ts = luaS_new(str); | ||
145 | if (ts->marked == 0) | ||
146 | ts->marked = 2; /* avoid GC */ | ||
147 | return ts; | ||
148 | } | ||
149 | |||
150 | |||
151 | void luaS_free (TaggedString *l) | ||
152 | { | ||
153 | while (l) { | ||
154 | TaggedString *next = l->uu.next; | ||
155 | luaM_free(l); | ||
156 | l = next; | ||
157 | } | ||
158 | } | ||
159 | |||
160 | |||
161 | /* | ||
162 | ** Garbage collection function. | ||
163 | */ | ||
164 | TaggedString *luaS_collector (void) | ||
165 | { | ||
166 | TaggedString *frees = NULL; | ||
167 | int i; | ||
168 | for (i=0; i<NUM_HASHS; i++) { | ||
169 | stringtable *tb = &string_root[i]; | ||
170 | int j; | ||
171 | for (j=0; j<tb->size; j++) { | ||
172 | TaggedString *t = tb->hash[j]; | ||
173 | if (t == NULL) continue; | ||
174 | if (t->marked == 1) | ||
175 | t->marked = 0; | ||
176 | else if (!t->marked) { | ||
177 | t->uu.next = frees; | ||
178 | frees = t; | ||
179 | tb->hash[j] = &EMPTY; | ||
180 | --luaO_nentities; | ||
181 | } | ||
182 | } | ||
183 | } | ||
184 | return frees; | ||
185 | } | ||
186 | |||