lstring.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. ** $Id: lstring.c,v 1.15 1998/08/10 21:36:32 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+6); /* +1 is the new element, +6 to
  40. ensure minimum size of 8 (for double hashing) */
  41. }
  42. static void grow (stringtable *tb) {
  43. int ns = newsize(tb);
  44. TaggedString **newhash = luaM_newvector(ns, TaggedString *);
  45. int i;
  46. for (i=0; i<ns; i++)
  47. newhash[i] = NULL;
  48. /* rehash */
  49. tb->nuse = 0;
  50. for (i=0; i<tb->size; i++) {
  51. if (tb->hash[i] != NULL && tb->hash[i] != &EMPTY) {
  52. unsigned long h = tb->hash[i]->hash;
  53. int h1 = h%ns;
  54. int h2 = 8-(h&7);
  55. while (newhash[h1])
  56. h1 = (h1+h2)%ns;
  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. int h2 = 8-(h&7); /* for double hashing */
  96. if ((long)tb->nuse*3 >= (long)size*2) {
  97. grow(tb);
  98. size = tb->size;
  99. }
  100. for (h1 = h%size; (ts = tb->hash[h1]) != NULL; h1 = (h1+h2)%size) {
  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. }
  108. /* not found */
  109. if (j != -1) /* is there an EMPTY space? */
  110. h1 = j;
  111. else
  112. tb->nuse++;
  113. ts = tb->hash[h1] = newone_s(str, l, h);
  114. return ts;
  115. }
  116. static TaggedString *insert_u (void *buff, int tag, stringtable *tb) {
  117. TaggedString *ts;
  118. unsigned long h = (unsigned long)buff;
  119. int size = tb->size;
  120. int j = -1;
  121. int h1;
  122. int h2 = 8-(h&7); /* for double hashing */
  123. if ((long)tb->nuse*3 >= (long)size*2) {
  124. grow(tb);
  125. size = tb->size;
  126. }
  127. for (h1 = h%size; (ts = tb->hash[h1]) != NULL; h1 = (h1+h2)%size) {
  128. if (ts == &EMPTY)
  129. j = h1;
  130. else if (ts->constindex < 0 && /* is a udata? */
  131. (tag == ts->u.d.tag || tag == LUA_ANYTAG) &&
  132. buff == ts->u.d.v)
  133. return ts;
  134. }
  135. /* not found */
  136. if (j != -1) /* is there an EMPTY space? */
  137. h1 = j;
  138. else
  139. tb->nuse++;
  140. ts = tb->hash[h1] = newone_u(buff, tag, h);
  141. return ts;
  142. }
  143. TaggedString *luaS_createudata (void *udata, int tag) {
  144. return insert_u(udata, tag, &L->string_root[(unsigned)udata%NUM_HASHS]);
  145. }
  146. TaggedString *luaS_newlstr (char *str, long l) {
  147. int i = (l==0)?0:(unsigned char)str[0];
  148. return insert_s(str, l, &L->string_root[i%NUM_HASHS]);
  149. }
  150. TaggedString *luaS_new (char *str) {
  151. return luaS_newlstr(str, strlen(str));
  152. }
  153. TaggedString *luaS_newfixedstring (char *str) {
  154. TaggedString *ts = luaS_new(str);
  155. if (ts->head.marked == 0)
  156. ts->head.marked = 2; /* avoid GC */
  157. return ts;
  158. }
  159. void luaS_free (TaggedString *l) {
  160. while (l) {
  161. TaggedString *next = (TaggedString *)l->head.next;
  162. L->nblocks -= (l->constindex == -1) ? 1 : gcsizestring(l->u.s.len);
  163. luaM_free(l);
  164. l = next;
  165. }
  166. }
  167. /*
  168. ** Garbage collection functions.
  169. */
  170. static void remove_from_list (GCnode *l) {
  171. while (l) {
  172. GCnode *next = l->next;
  173. while (next && !next->marked)
  174. next = l->next = next->next;
  175. l = next;
  176. }
  177. }
  178. TaggedString *luaS_collector (void) {
  179. TaggedString *frees = NULL;
  180. int i;
  181. remove_from_list(&(L->rootglobal));
  182. for (i=0; i<NUM_HASHS; i++) {
  183. stringtable *tb = &L->string_root[i];
  184. int j;
  185. for (j=0; j<tb->size; j++) {
  186. TaggedString *t = tb->hash[j];
  187. if (t == NULL) continue;
  188. if (t->head.marked == 1)
  189. t->head.marked = 0;
  190. else if (!t->head.marked) {
  191. t->head.next = (GCnode *)frees;
  192. frees = t;
  193. tb->hash[j] = &EMPTY;
  194. }
  195. }
  196. }
  197. return frees;
  198. }
  199. TaggedString *luaS_collectudata (void) {
  200. TaggedString *frees = NULL;
  201. int i;
  202. L->rootglobal.next = NULL; /* empty list of globals */
  203. for (i=0; i<NUM_HASHS; i++) {
  204. stringtable *tb = &L->string_root[i];
  205. int j;
  206. for (j=0; j<tb->size; j++) {
  207. TaggedString *t = tb->hash[j];
  208. if (t == NULL || t == &EMPTY || t->constindex != -1)
  209. continue; /* get only user data */
  210. t->head.next = (GCnode *)frees;
  211. frees = t;
  212. tb->hash[j] = &EMPTY;
  213. }
  214. }
  215. return frees;
  216. }
  217. void luaS_freeall (void) {
  218. int i;
  219. for (i=0; i<NUM_HASHS; i++) {
  220. stringtable *tb = &L->string_root[i];
  221. int j;
  222. for (j=0; j<tb->size; j++) {
  223. TaggedString *t = tb->hash[j];
  224. if (t == &EMPTY) continue;
  225. luaM_free(t);
  226. }
  227. luaM_free(tb->hash);
  228. }
  229. luaM_free(L->string_root);
  230. }
  231. void luaS_rawsetglobal (TaggedString *ts, TObject *newval) {
  232. ts->u.s.globalval = *newval;
  233. if (ts->head.next == (GCnode *)ts) { /* is not in list? */
  234. ts->head.next = L->rootglobal.next;
  235. L->rootglobal.next = (GCnode *)ts;
  236. }
  237. }
  238. char *luaS_travsymbol (int (*fn)(TObject *)) {
  239. TaggedString *g;
  240. for (g=(TaggedString *)L->rootglobal.next; g; g=(TaggedString *)g->head.next)
  241. if (fn(&g->u.s.globalval))
  242. return g->str;
  243. return NULL;
  244. }
  245. int luaS_globaldefined (char *name) {
  246. TaggedString *ts = luaS_new(name);
  247. return ts->u.s.globalval.ttype != LUA_T_NIL;
  248. }