lstring.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. ** $Id: lstring.c,v 1.16 1999/01/04 13:37:29 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. int i;
  18. L->string_root = luaM_newvector(NUM_HASHS, stringtable);
  19. for (i=0; i<NUM_HASHS; i++) {
  20. L->string_root[i].size = 0;
  21. L->string_root[i].nuse = 0;
  22. L->string_root[i].hash = NULL;
  23. }
  24. }
  25. static unsigned long hash_s (char *s, long l) {
  26. unsigned long h = 0; /* seed */
  27. while (l--)
  28. h = h ^ ((h<<5)+(h>>2)+(unsigned char)*(s++));
  29. return h;
  30. }
  31. static int newsize (stringtable *tb) {
  32. int size = tb->size;
  33. int realuse = 0;
  34. int i;
  35. /* count how many entries are really in use */
  36. for (i=0; i<size; i++)
  37. if (tb->hash[i] != NULL && tb->hash[i] != &EMPTY)
  38. realuse++;
  39. return luaO_redimension((realuse+1)*2); /* +1 is the new element */
  40. }
  41. static void grow (stringtable *tb) {
  42. int ns = newsize(tb);
  43. TaggedString **newhash = luaM_newvector(ns, TaggedString *);
  44. int i;
  45. for (i=0; i<ns; i++)
  46. newhash[i] = NULL;
  47. /* rehash */
  48. tb->nuse = 0;
  49. for (i=0; i<tb->size; i++) {
  50. if (tb->hash[i] != NULL && tb->hash[i] != &EMPTY) {
  51. unsigned long h = tb->hash[i]->hash;
  52. int h1 = h%ns;
  53. while (newhash[h1]) {
  54. h1 += (h&(ns-2)) + 1; /* double hashing */
  55. if (h1 >= ns) h1 -= ns;
  56. }
  57. newhash[h1] = 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. TaggedString *ts = (TaggedString *)luaM_malloc(sizeof(TaggedString)+l);
  67. memcpy(ts->str, str, l);
  68. ts->str[l] = 0; /* ending 0 */
  69. ts->u.s.globalval.ttype = LUA_T_NIL; /* initialize global value */
  70. ts->u.s.len = l;
  71. ts->constindex = 0;
  72. L->nblocks += gcsizestring(l);
  73. ts->head.marked = 0;
  74. ts->head.next = (GCnode *)ts; /* signal it is in no list */
  75. ts->hash = h;
  76. return ts;
  77. }
  78. static TaggedString *newone_u (char *buff, int tag, unsigned long h) {
  79. TaggedString *ts = luaM_new(TaggedString);
  80. ts->u.d.v = buff;
  81. ts->u.d.tag = (tag == LUA_ANYTAG) ? 0 : tag;
  82. ts->constindex = -1; /* tag -> this is a userdata */
  83. L->nblocks++;
  84. ts->head.marked = 0;
  85. ts->head.next = (GCnode *)ts; /* signal it is in no list */
  86. ts->hash = h;
  87. return ts;
  88. }
  89. static TaggedString *insert_s (char *str, long l, stringtable *tb) {
  90. TaggedString *ts;
  91. unsigned long h = hash_s(str, l);
  92. int size = tb->size;
  93. int j = -1;
  94. int h1;
  95. if ((long)tb->nuse*3 >= (long)size*2) {
  96. grow(tb);
  97. size = tb->size;
  98. }
  99. h1 = h%size;
  100. while ((ts = tb->hash[h1]) != NULL) {
  101. if (ts == &EMPTY)
  102. j = h1;
  103. else if (ts->constindex >= 0 &&
  104. ts->u.s.len == l &&
  105. (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 (ts->constindex < 0 && /* is a udata? */
  133. (tag == ts->u.d.tag || tag == LUA_ANYTAG) &&
  134. buff == ts->u.d.v)
  135. return ts;
  136. h1 += (h&(size-2)) + 1; /* double hashing */
  137. if (h1 >= size) h1 -= size;
  138. }
  139. /* not found */
  140. if (j != -1) /* is there an EMPTY space? */
  141. h1 = j;
  142. else
  143. tb->nuse++;
  144. ts = tb->hash[h1] = newone_u(buff, tag, h);
  145. return ts;
  146. }
  147. TaggedString *luaS_createudata (void *udata, int tag) {
  148. return insert_u(udata, tag, &L->string_root[(unsigned)udata%NUM_HASHS]);
  149. }
  150. TaggedString *luaS_newlstr (char *str, long l) {
  151. int i = (l==0)?0:(unsigned char)str[0];
  152. return insert_s(str, l, &L->string_root[i%NUM_HASHS]);
  153. }
  154. TaggedString *luaS_new (char *str) {
  155. return luaS_newlstr(str, strlen(str));
  156. }
  157. TaggedString *luaS_newfixedstring (char *str) {
  158. TaggedString *ts = luaS_new(str);
  159. if (ts->head.marked == 0)
  160. ts->head.marked = 2; /* avoid GC */
  161. return ts;
  162. }
  163. void luaS_free (TaggedString *l) {
  164. while (l) {
  165. TaggedString *next = (TaggedString *)l->head.next;
  166. L->nblocks -= (l->constindex == -1) ? 1 : gcsizestring(l->u.s.len);
  167. luaM_free(l);
  168. l = next;
  169. }
  170. }
  171. /*
  172. ** Garbage collection functions.
  173. */
  174. static void remove_from_list (GCnode *l) {
  175. while (l) {
  176. GCnode *next = l->next;
  177. while (next && !next->marked)
  178. next = l->next = next->next;
  179. l = next;
  180. }
  181. }
  182. TaggedString *luaS_collector (void) {
  183. TaggedString *frees = NULL;
  184. int i;
  185. remove_from_list(&(L->rootglobal));
  186. for (i=0; i<NUM_HASHS; i++) {
  187. stringtable *tb = &L->string_root[i];
  188. int j;
  189. for (j=0; j<tb->size; j++) {
  190. TaggedString *t = tb->hash[j];
  191. if (t == NULL) continue;
  192. if (t->head.marked == 1)
  193. t->head.marked = 0;
  194. else if (!t->head.marked) {
  195. t->head.next = (GCnode *)frees;
  196. frees = t;
  197. tb->hash[j] = &EMPTY;
  198. }
  199. }
  200. }
  201. return frees;
  202. }
  203. TaggedString *luaS_collectudata (void) {
  204. TaggedString *frees = NULL;
  205. int i;
  206. L->rootglobal.next = NULL; /* empty list of globals */
  207. for (i=0; i<NUM_HASHS; i++) {
  208. stringtable *tb = &L->string_root[i];
  209. int j;
  210. for (j=0; j<tb->size; j++) {
  211. TaggedString *t = tb->hash[j];
  212. if (t == NULL || t == &EMPTY || t->constindex != -1)
  213. continue; /* get only user data */
  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. }