lstring.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. ** $Id: lstring.c,v 1.8 1997/12/09 13:35:19 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}, 0, 0L, {{LUA_T_NIL, {NULL}}}, {0}};
  15. void luaS_init (void)
  16. {
  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 (char *s, int tag)
  26. {
  27. unsigned long h;
  28. if (tag != LUA_T_STRING)
  29. h = (unsigned long)s;
  30. else {
  31. h = 0;
  32. while (*s)
  33. h = ((h<<5)-h)^(unsigned char)*(s++);
  34. }
  35. return h;
  36. }
  37. static void grow (stringtable *tb)
  38. {
  39. int newsize = luaO_redimension(tb->size);
  40. TaggedString **newhash = luaM_newvector(newsize, TaggedString *);
  41. int i;
  42. for (i=0; i<newsize; i++)
  43. newhash[i] = NULL;
  44. /* rehash */
  45. tb->nuse = 0;
  46. for (i=0; i<tb->size; i++) {
  47. if (tb->hash[i] != NULL && tb->hash[i] != &EMPTY) {
  48. int h = tb->hash[i]->hash%newsize;
  49. while (newhash[h])
  50. h = (h+1)%newsize;
  51. newhash[h] = tb->hash[i];
  52. tb->nuse++;
  53. }
  54. }
  55. luaM_free(tb->hash);
  56. tb->size = newsize;
  57. tb->hash = newhash;
  58. }
  59. static TaggedString *newone (char *buff, int tag, unsigned long h)
  60. {
  61. TaggedString *ts;
  62. if (tag == LUA_T_STRING) {
  63. long l = strlen(buff);
  64. ts = (TaggedString *)luaM_malloc(sizeof(TaggedString)+l);
  65. strcpy(ts->str, buff);
  66. ts->u.globalval.ttype = LUA_T_NIL; /* initialize global value */
  67. ts->constindex = 0;
  68. L->nblocks += gcsizestring(l);
  69. }
  70. else {
  71. ts = (TaggedString *)luaM_malloc(sizeof(TaggedString));
  72. ts->u.d.v = buff;
  73. ts->u.d.tag = tag == LUA_ANYTAG ? 0 : tag;
  74. ts->constindex = -1; /* tag -> this is a userdata */
  75. L->nblocks++;
  76. }
  77. ts->head.marked = 0;
  78. ts->head.next = (GCnode *)ts; /* signal it is in no list */
  79. ts->hash = h;
  80. return ts;
  81. }
  82. static TaggedString *insert (char *buff, int tag, stringtable *tb)
  83. {
  84. TaggedString *ts;
  85. unsigned long h = hash(buff, tag);
  86. int i;
  87. int j = -1;
  88. if ((long)tb->nuse*3 >= (long)tb->size*2)
  89. grow(tb);
  90. for (i = h%tb->size; (ts = tb->hash[i]) != NULL; i = (i+1)%tb->size) {
  91. if (ts == &EMPTY)
  92. j = i;
  93. else if ((ts->constindex >= 0) ? /* is a string? */
  94. (tag == LUA_T_STRING && (strcmp(buff, ts->str) == 0)) :
  95. ((tag == ts->u.d.tag || tag == LUA_ANYTAG) && buff == ts->u.d.v))
  96. return ts;
  97. }
  98. /* not found */
  99. if (j != -1) /* is there an EMPTY space? */
  100. i = j;
  101. else
  102. tb->nuse++;
  103. ts = tb->hash[i] = newone(buff, tag, h);
  104. return ts;
  105. }
  106. TaggedString *luaS_createudata (void *udata, int tag)
  107. {
  108. return insert(udata, tag, &L->string_root[(unsigned)udata%NUM_HASHS]);
  109. }
  110. TaggedString *luaS_new (char *str)
  111. {
  112. return insert(str, LUA_T_STRING, &L->string_root[(unsigned)str[0]%NUM_HASHS]);
  113. }
  114. TaggedString *luaS_newfixedstring (char *str)
  115. {
  116. TaggedString *ts = luaS_new(str);
  117. if (ts->head.marked == 0)
  118. ts->head.marked = 2; /* avoid GC */
  119. return ts;
  120. }
  121. void luaS_free (TaggedString *l)
  122. {
  123. while (l) {
  124. TaggedString *next = (TaggedString *)l->head.next;
  125. L->nblocks -= (l->constindex == -1) ? 1 : gcsizestring(strlen(l->str));
  126. luaM_free(l);
  127. l = next;
  128. }
  129. }
  130. /*
  131. ** Garbage collection functions.
  132. */
  133. static void remove_from_list (GCnode *l)
  134. {
  135. while (l) {
  136. GCnode *next = l->next;
  137. while (next && !next->marked)
  138. next = l->next = next->next;
  139. l = next;
  140. }
  141. }
  142. TaggedString *luaS_collector (void)
  143. {
  144. TaggedString *frees = NULL;
  145. int i;
  146. remove_from_list(&(L->rootglobal));
  147. for (i=0; i<NUM_HASHS; i++) {
  148. stringtable *tb = &L->string_root[i];
  149. int j;
  150. for (j=0; j<tb->size; j++) {
  151. TaggedString *t = tb->hash[j];
  152. if (t == NULL) continue;
  153. if (t->head.marked == 1)
  154. t->head.marked = 0;
  155. else if (!t->head.marked) {
  156. t->head.next = (GCnode *)frees;
  157. frees = t;
  158. tb->hash[j] = &EMPTY;
  159. }
  160. }
  161. }
  162. return frees;
  163. }
  164. TaggedString *luaS_collectudata (void)
  165. {
  166. TaggedString *frees = NULL;
  167. int i;
  168. L->rootglobal.next = NULL; /* empty list of globals */
  169. for (i=0; i<NUM_HASHS; i++) {
  170. stringtable *tb = &L->string_root[i];
  171. int j;
  172. for (j=0; j<tb->size; j++) {
  173. TaggedString *t = tb->hash[j];
  174. if (t == NULL || t == &EMPTY || t->constindex != -1)
  175. continue; /* get only user datas */
  176. t->head.next = (GCnode *)frees;
  177. frees = t;
  178. tb->hash[j] = &EMPTY;
  179. }
  180. }
  181. return frees;
  182. }
  183. void luaS_freeall (void)
  184. {
  185. int i;
  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 == &EMPTY) continue;
  192. luaM_free(t);
  193. }
  194. luaM_free(tb->hash);
  195. }
  196. luaM_free(L->string_root);
  197. }
  198. void luaS_rawsetglobal (TaggedString *ts, TObject *newval)
  199. {
  200. ts->u.globalval = *newval;
  201. if (ts->head.next == (GCnode *)ts) { /* is not in list? */
  202. ts->head.next = L->rootglobal.next;
  203. L->rootglobal.next = (GCnode *)ts;
  204. }
  205. }
  206. char *luaS_travsymbol (int (*fn)(TObject *))
  207. {
  208. TaggedString *g;
  209. for (g=(TaggedString *)L->rootglobal.next; g; g=(TaggedString *)g->head.next)
  210. if (fn(&g->u.globalval))
  211. return g->str;
  212. return NULL;
  213. }
  214. int luaS_globaldefined (char *name)
  215. {
  216. TaggedString *ts = luaS_new(name);
  217. return ts->u.globalval.ttype != LUA_T_NIL;
  218. }