lstring.c 6.7 KB

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