lstring.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. ** $Id: lstring.c,v 1.14 1998/07/27 17:06:17 roberto Exp roberto $
  3. ** String table (keeps all strings handled by Lua)
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <string.h>
  7. #include "lmem.h"
  8. #include "lobject.h"
  9. #include "lstate.h"
  10. #include "lstring.h"
  11. #include "lua.h"
  12. #define NUM_HASHS 61
  13. #define gcsizestring(l) (1+(l/64)) /* "weight" for a string with length 'l' */
  14. static TaggedString EMPTY = {{NULL, 2}, 0L, 0,
  15. {{{LUA_T_NIL, {NULL}}, 0L}}, {0}};
  16. void luaS_init (void)
  17. {
  18. int i;
  19. L->string_root = luaM_newvector(NUM_HASHS, stringtable);
  20. for (i=0; i<NUM_HASHS; i++) {
  21. L->string_root[i].size = 0;
  22. L->string_root[i].nuse = 0;
  23. L->string_root[i].hash = NULL;
  24. }
  25. }
  26. static unsigned long hash_s (char *s, long l)
  27. {
  28. unsigned long h = 0; /* seed */
  29. while (l--)
  30. h = h ^ ((h<<5)+(h>>2)+(unsigned char)*(s++));
  31. return h;
  32. }
  33. static int newsize (stringtable *tb) {
  34. int size = tb->size;
  35. int realuse = 0;
  36. int i;
  37. /* count how many entries are really in use */
  38. for (i=0; i<size; i++)
  39. if (tb->hash[i] != NULL && tb->hash[i] != &EMPTY)
  40. realuse++;
  41. return luaO_redimension((realuse+1)*2); /* +1 is the new element */
  42. }
  43. static void grow (stringtable *tb)
  44. {
  45. int ns = newsize(tb);
  46. TaggedString **newhash = luaM_newvector(ns, TaggedString *);
  47. int i;
  48. for (i=0; i<ns; i++)
  49. newhash[i] = NULL;
  50. /* rehash */
  51. tb->nuse = 0;
  52. for (i=0; i<tb->size; i++) {
  53. if (tb->hash[i] != NULL && tb->hash[i] != &EMPTY) {
  54. int h = tb->hash[i]->hash%ns;
  55. while (newhash[h])
  56. h = (h+1)%ns;
  57. newhash[h] = tb->hash[i];
  58. tb->nuse++;
  59. }
  60. }
  61. luaM_free(tb->hash);
  62. tb->size = ns;
  63. tb->hash = newhash;
  64. }
  65. static TaggedString *newone_s (char *str, long l, unsigned long h)
  66. {
  67. TaggedString *ts = (TaggedString *)luaM_malloc(sizeof(TaggedString)+l);
  68. memcpy(ts->str, str, l);
  69. ts->str[l] = 0; /* ending 0 */
  70. ts->u.s.globalval.ttype = LUA_T_NIL; /* initialize global value */
  71. ts->u.s.len = l;
  72. ts->constindex = 0;
  73. L->nblocks += gcsizestring(l);
  74. ts->head.marked = 0;
  75. ts->head.next = (GCnode *)ts; /* signal it is in no list */
  76. ts->hash = h;
  77. return ts;
  78. }
  79. static TaggedString *newone_u (char *buff, int tag, unsigned long h)
  80. {
  81. TaggedString *ts = luaM_new(TaggedString);
  82. ts->u.d.v = buff;
  83. ts->u.d.tag = (tag == LUA_ANYTAG) ? 0 : tag;
  84. ts->constindex = -1; /* tag -> this is a userdata */
  85. L->nblocks++;
  86. ts->head.marked = 0;
  87. ts->head.next = (GCnode *)ts; /* signal it is in no list */
  88. ts->hash = h;
  89. return ts;
  90. }
  91. static TaggedString *insert_s (char *str, long l, stringtable *tb)
  92. {
  93. TaggedString *ts;
  94. unsigned long h = hash_s(str, l);
  95. int size = tb->size;
  96. int i;
  97. int j = -1;
  98. if ((long)tb->nuse*3 >= (long)size*2) {
  99. grow(tb);
  100. size = tb->size;
  101. }
  102. for (i = h%size; (ts = tb->hash[i]) != NULL; ) {
  103. if (ts == &EMPTY)
  104. j = i;
  105. else if (ts->constindex >= 0 &&
  106. ts->u.s.len == l &&
  107. (memcmp(str, ts->str, l) == 0))
  108. return ts;
  109. if (++i == size) i=0;
  110. }
  111. /* not found */
  112. if (j != -1) /* is there an EMPTY space? */
  113. i = j;
  114. else
  115. tb->nuse++;
  116. ts = tb->hash[i] = newone_s(str, l, h);
  117. return ts;
  118. }
  119. static TaggedString *insert_u (void *buff, int tag, stringtable *tb)
  120. {
  121. TaggedString *ts;
  122. unsigned long h = (unsigned long)buff;
  123. int size = tb->size;
  124. int i;
  125. int j = -1;
  126. if ((long)tb->nuse*3 >= (long)size*2) {
  127. grow(tb);
  128. size = tb->size;
  129. }
  130. for (i = h%size; (ts = tb->hash[i]) != NULL; ) {
  131. if (ts == &EMPTY)
  132. j = i;
  133. else if (ts->constindex < 0 && /* is a udata? */
  134. (tag == ts->u.d.tag || tag == LUA_ANYTAG) &&
  135. buff == ts->u.d.v)
  136. return ts;
  137. if (++i == size) i=0;
  138. }
  139. /* not found */
  140. if (j != -1) /* is there an EMPTY space? */
  141. i = j;
  142. else
  143. tb->nuse++;
  144. ts = tb->hash[i] = newone_u(buff, tag, h);
  145. return ts;
  146. }
  147. TaggedString *luaS_createudata (void *udata, int tag)
  148. {
  149. return insert_u(udata, tag, &L->string_root[(unsigned)udata%NUM_HASHS]);
  150. }
  151. TaggedString *luaS_newlstr (char *str, long l)
  152. {
  153. int i = (l==0)?0:(unsigned char)str[0];
  154. return insert_s(str, l, &L->string_root[i%NUM_HASHS]);
  155. }
  156. TaggedString *luaS_new (char *str)
  157. {
  158. return luaS_newlstr(str, strlen(str));
  159. }
  160. TaggedString *luaS_newfixedstring (char *str)
  161. {
  162. TaggedString *ts = luaS_new(str);
  163. if (ts->head.marked == 0)
  164. ts->head.marked = 2; /* avoid GC */
  165. return ts;
  166. }
  167. void luaS_free (TaggedString *l)
  168. {
  169. while (l) {
  170. TaggedString *next = (TaggedString *)l->head.next;
  171. L->nblocks -= (l->constindex == -1) ? 1 : gcsizestring(l->u.s.len);
  172. luaM_free(l);
  173. l = next;
  174. }
  175. }
  176. /*
  177. ** Garbage collection functions.
  178. */
  179. static void remove_from_list (GCnode *l)
  180. {
  181. while (l) {
  182. GCnode *next = l->next;
  183. while (next && !next->marked)
  184. next = l->next = next->next;
  185. l = next;
  186. }
  187. }
  188. TaggedString *luaS_collector (void)
  189. {
  190. TaggedString *frees = NULL;
  191. int i;
  192. remove_from_list(&(L->rootglobal));
  193. for (i=0; i<NUM_HASHS; i++) {
  194. stringtable *tb = &L->string_root[i];
  195. int j;
  196. for (j=0; j<tb->size; j++) {
  197. TaggedString *t = tb->hash[j];
  198. if (t == NULL) continue;
  199. if (t->head.marked == 1)
  200. t->head.marked = 0;
  201. else if (!t->head.marked) {
  202. t->head.next = (GCnode *)frees;
  203. frees = t;
  204. tb->hash[j] = &EMPTY;
  205. }
  206. }
  207. }
  208. return frees;
  209. }
  210. TaggedString *luaS_collectudata (void)
  211. {
  212. TaggedString *frees = NULL;
  213. int i;
  214. L->rootglobal.next = NULL; /* empty list of globals */
  215. for (i=0; i<NUM_HASHS; i++) {
  216. stringtable *tb = &L->string_root[i];
  217. int j;
  218. for (j=0; j<tb->size; j++) {
  219. TaggedString *t = tb->hash[j];
  220. if (t == NULL || t == &EMPTY || t->constindex != -1)
  221. continue; /* get only user data */
  222. t->head.next = (GCnode *)frees;
  223. frees = t;
  224. tb->hash[j] = &EMPTY;
  225. }
  226. }
  227. return frees;
  228. }
  229. void luaS_freeall (void)
  230. {
  231. int i;
  232. for (i=0; i<NUM_HASHS; i++) {
  233. stringtable *tb = &L->string_root[i];
  234. int j;
  235. for (j=0; j<tb->size; j++) {
  236. TaggedString *t = tb->hash[j];
  237. if (t == &EMPTY) continue;
  238. luaM_free(t);
  239. }
  240. luaM_free(tb->hash);
  241. }
  242. luaM_free(L->string_root);
  243. }
  244. void luaS_rawsetglobal (TaggedString *ts, TObject *newval)
  245. {
  246. ts->u.s.globalval = *newval;
  247. if (ts->head.next == (GCnode *)ts) { /* is not in list? */
  248. ts->head.next = L->rootglobal.next;
  249. L->rootglobal.next = (GCnode *)ts;
  250. }
  251. }
  252. char *luaS_travsymbol (int (*fn)(TObject *))
  253. {
  254. TaggedString *g;
  255. for (g=(TaggedString *)L->rootglobal.next; g; g=(TaggedString *)g->head.next)
  256. if (fn(&g->u.s.globalval))
  257. return g->str;
  258. return NULL;
  259. }
  260. int luaS_globaldefined (char *name)
  261. {
  262. TaggedString *ts = luaS_new(name);
  263. return ts->u.s.globalval.ttype != LUA_T_NIL;
  264. }