lstring.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. ** $Id: lstring.c,v 1.18 1999/02/08 16:28:48 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_HASHSTR 31
  13. #define NUM_HASHUDATA 31
  14. #define NUM_HASHS (NUM_HASHSTR+NUM_HASHUDATA)
  15. #define gcsizestring(l) (1+(l/64)) /* "weight" for a string with length 'l' */
  16. static TaggedString EMPTY = {{NULL, 2}, 0L, 0,
  17. {{{LUA_T_NIL, {NULL}}, 0L}}, {0}};
  18. void luaS_init (void) {
  19. int i;
  20. L->string_root = luaM_newvector(NUM_HASHS, stringtable);
  21. for (i=0; i<NUM_HASHS; i++) {
  22. L->string_root[i].size = 0;
  23. L->string_root[i].nuse = 0;
  24. L->string_root[i].hash = NULL;
  25. }
  26. }
  27. static unsigned long hash_s (char *s, long l) {
  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. int ns = newsize(tb);
  45. TaggedString **newhash = luaM_newvector(ns, TaggedString *);
  46. int i;
  47. for (i=0; i<ns; i++)
  48. newhash[i] = NULL;
  49. /* rehash */
  50. tb->nuse = 0;
  51. for (i=0; i<tb->size; i++) {
  52. if (tb->hash[i] != NULL && tb->hash[i] != &EMPTY) {
  53. unsigned long h = tb->hash[i]->hash;
  54. int h1 = h%ns;
  55. while (newhash[h1]) {
  56. h1 += (h&(ns-2)) + 1; /* double hashing */
  57. if (h1 >= ns) h1 -= ns;
  58. }
  59. newhash[h1] = tb->hash[i];
  60. tb->nuse++;
  61. }
  62. }
  63. luaM_free(tb->hash);
  64. tb->size = ns;
  65. tb->hash = newhash;
  66. }
  67. static TaggedString *newone_s (char *str, long l, unsigned long h) {
  68. TaggedString *ts = (TaggedString *)luaM_malloc(sizeof(TaggedString)+l);
  69. memcpy(ts->str, str, l);
  70. ts->str[l] = 0; /* ending 0 */
  71. ts->u.s.globalval.ttype = LUA_T_NIL; /* initialize global value */
  72. ts->u.s.len = l;
  73. ts->constindex = 0;
  74. L->nblocks += gcsizestring(l);
  75. ts->head.marked = 0;
  76. ts->head.next = (GCnode *)ts; /* signal it is in no list */
  77. ts->hash = h;
  78. return ts;
  79. }
  80. static TaggedString *newone_u (char *buff, int tag, unsigned long h) {
  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. TaggedString *ts;
  93. unsigned long h = hash_s(str, l);
  94. int size = tb->size;
  95. int j = -1;
  96. int h1;
  97. if ((long)tb->nuse*3 >= (long)size*2) {
  98. grow(tb);
  99. size = tb->size;
  100. }
  101. h1 = h%size;
  102. while ((ts = tb->hash[h1]) != NULL) {
  103. if (ts == &EMPTY)
  104. j = h1;
  105. else if (ts->u.s.len == l && (memcmp(str, ts->str, l) == 0))
  106. return ts;
  107. h1 += (h&(size-2)) + 1; /* double hashing */
  108. if (h1 >= size) h1 -= size;
  109. }
  110. /* not found */
  111. if (j != -1) /* is there an EMPTY space? */
  112. h1 = j;
  113. else
  114. tb->nuse++;
  115. ts = tb->hash[h1] = newone_s(str, l, h);
  116. return ts;
  117. }
  118. static TaggedString *insert_u (void *buff, int tag, stringtable *tb) {
  119. TaggedString *ts;
  120. unsigned long h = (unsigned long)buff;
  121. int size = tb->size;
  122. int j = -1;
  123. int h1;
  124. if ((long)tb->nuse*3 >= (long)size*2) {
  125. grow(tb);
  126. size = tb->size;
  127. }
  128. h1 = h%size;
  129. while ((ts = tb->hash[h1]) != NULL) {
  130. if (ts == &EMPTY)
  131. j = h1;
  132. else if ((tag == ts->u.d.tag || tag == LUA_ANYTAG) && buff == ts->u.d.v)
  133. return ts;
  134. h1 += (h&(size-2)) + 1; /* double hashing */
  135. if (h1 >= size) h1 -= size;
  136. }
  137. /* not found */
  138. if (j != -1) /* is there an EMPTY space? */
  139. h1 = j;
  140. else
  141. tb->nuse++;
  142. ts = tb->hash[h1] = newone_u(buff, tag, h);
  143. return ts;
  144. }
  145. TaggedString *luaS_createudata (void *udata, int tag) {
  146. int t = ((unsigned)udata%NUM_HASHUDATA)+NUM_HASHSTR;
  147. return insert_u(udata, tag, &L->string_root[t]);
  148. }
  149. TaggedString *luaS_newlstr (char *str, long l) {
  150. int t = (l==0) ? 0 : ((int)((unsigned char)str[0]*l))%NUM_HASHSTR;
  151. return insert_s(str, l, &L->string_root[t]);
  152. }
  153. TaggedString *luaS_new (char *str) {
  154. return luaS_newlstr(str, strlen(str));
  155. }
  156. TaggedString *luaS_newfixedstring (char *str) {
  157. TaggedString *ts = luaS_new(str);
  158. if (ts->head.marked == 0)
  159. ts->head.marked = 2; /* avoid GC */
  160. return ts;
  161. }
  162. void luaS_free (TaggedString *l) {
  163. while (l) {
  164. TaggedString *next = (TaggedString *)l->head.next;
  165. L->nblocks -= (l->constindex == -1) ? 1 : gcsizestring(l->u.s.len);
  166. luaM_free(l);
  167. l = next;
  168. }
  169. }
  170. /*
  171. ** Garbage collection functions.
  172. */
  173. static void remove_from_list (GCnode *l) {
  174. while (l) {
  175. GCnode *next = l->next;
  176. while (next && !next->marked)
  177. next = l->next = next->next;
  178. l = next;
  179. }
  180. }
  181. TaggedString *luaS_collector (void) {
  182. TaggedString *frees = NULL;
  183. int i;
  184. remove_from_list(&(L->rootglobal));
  185. for (i=0; i<NUM_HASHS; i++) {
  186. stringtable *tb = &L->string_root[i];
  187. int j;
  188. for (j=0; j<tb->size; j++) {
  189. TaggedString *t = tb->hash[j];
  190. if (t == NULL) continue;
  191. if (t->head.marked == 1)
  192. t->head.marked = 0;
  193. else if (!t->head.marked) {
  194. t->head.next = (GCnode *)frees;
  195. frees = t;
  196. tb->hash[j] = &EMPTY;
  197. }
  198. }
  199. }
  200. return frees;
  201. }
  202. TaggedString *luaS_collectudata (void) {
  203. TaggedString *frees = NULL;
  204. int i;
  205. L->rootglobal.next = NULL; /* empty list of globals */
  206. for (i=NUM_HASHSTR; i<NUM_HASHS; i++) {
  207. stringtable *tb = &L->string_root[i];
  208. int j;
  209. for (j=0; j<tb->size; j++) {
  210. TaggedString *t = tb->hash[j];
  211. if (t == NULL || t == &EMPTY)
  212. continue;
  213. LUA_ASSERT(t->constindex == -1, "must be userdata");
  214. t->head.next = (GCnode *)frees;
  215. frees = t;
  216. tb->hash[j] = &EMPTY;
  217. }
  218. }
  219. return frees;
  220. }
  221. void luaS_freeall (void) {
  222. int i;
  223. for (i=0; i<NUM_HASHS; i++) {
  224. stringtable *tb = &L->string_root[i];
  225. int j;
  226. for (j=0; j<tb->size; j++) {
  227. TaggedString *t = tb->hash[j];
  228. if (t == &EMPTY) continue;
  229. luaM_free(t);
  230. }
  231. luaM_free(tb->hash);
  232. }
  233. luaM_free(L->string_root);
  234. }
  235. void luaS_rawsetglobal (TaggedString *ts, TObject *newval) {
  236. ts->u.s.globalval = *newval;
  237. if (ts->head.next == (GCnode *)ts) { /* is not in list? */
  238. ts->head.next = L->rootglobal.next;
  239. L->rootglobal.next = (GCnode *)ts;
  240. }
  241. }
  242. char *luaS_travsymbol (int (*fn)(TObject *)) {
  243. TaggedString *g;
  244. for (g=(TaggedString *)L->rootglobal.next; g; g=(TaggedString *)g->head.next)
  245. if (fn(&g->u.s.globalval))
  246. return g->str;
  247. return NULL;
  248. }
  249. int luaS_globaldefined (char *name) {
  250. TaggedString *ts = luaS_new(name);
  251. return ts->u.s.globalval.ttype != LUA_T_NIL;
  252. }