lstring.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. ** $Id: lstring.c,v 1.9 1997/12/30 19:15:52 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 size = tb->size;
  87. int i;
  88. int j = -1;
  89. if ((long)tb->nuse*3 >= (long)size*2) {
  90. grow(tb);
  91. size = tb->size;
  92. }
  93. for (i = h%size; (ts = tb->hash[i]) != NULL; ) {
  94. if (ts == &EMPTY)
  95. j = i;
  96. else if ((ts->constindex >= 0) ? /* is a string? */
  97. (tag == LUA_T_STRING && (strcmp(buff, ts->str) == 0)) :
  98. ((tag == ts->u.d.tag || tag == LUA_ANYTAG) && buff == ts->u.d.v))
  99. return ts;
  100. if (++i == size) i=0;
  101. }
  102. /* not found */
  103. if (j != -1) /* is there an EMPTY space? */
  104. i = j;
  105. else
  106. tb->nuse++;
  107. ts = tb->hash[i] = newone(buff, tag, h);
  108. return ts;
  109. }
  110. TaggedString *luaS_createudata (void *udata, int tag)
  111. {
  112. return insert(udata, tag, &L->string_root[(unsigned)udata%NUM_HASHS]);
  113. }
  114. TaggedString *luaS_new (char *str)
  115. {
  116. return insert(str, LUA_T_STRING, &L->string_root[(unsigned)str[0]%NUM_HASHS]);
  117. }
  118. TaggedString *luaS_newfixedstring (char *str)
  119. {
  120. TaggedString *ts = luaS_new(str);
  121. if (ts->head.marked == 0)
  122. ts->head.marked = 2; /* avoid GC */
  123. return ts;
  124. }
  125. void luaS_free (TaggedString *l)
  126. {
  127. while (l) {
  128. TaggedString *next = (TaggedString *)l->head.next;
  129. L->nblocks -= (l->constindex == -1) ? 1 : gcsizestring(strlen(l->str));
  130. luaM_free(l);
  131. l = next;
  132. }
  133. }
  134. /*
  135. ** Garbage collection functions.
  136. */
  137. static void remove_from_list (GCnode *l)
  138. {
  139. while (l) {
  140. GCnode *next = l->next;
  141. while (next && !next->marked)
  142. next = l->next = next->next;
  143. l = next;
  144. }
  145. }
  146. TaggedString *luaS_collector (void)
  147. {
  148. TaggedString *frees = NULL;
  149. int i;
  150. remove_from_list(&(L->rootglobal));
  151. for (i=0; i<NUM_HASHS; i++) {
  152. stringtable *tb = &L->string_root[i];
  153. int j;
  154. for (j=0; j<tb->size; j++) {
  155. TaggedString *t = tb->hash[j];
  156. if (t == NULL) continue;
  157. if (t->head.marked == 1)
  158. t->head.marked = 0;
  159. else if (!t->head.marked) {
  160. t->head.next = (GCnode *)frees;
  161. frees = t;
  162. tb->hash[j] = &EMPTY;
  163. }
  164. }
  165. }
  166. return frees;
  167. }
  168. TaggedString *luaS_collectudata (void)
  169. {
  170. TaggedString *frees = NULL;
  171. int i;
  172. L->rootglobal.next = NULL; /* empty list of globals */
  173. for (i=0; i<NUM_HASHS; i++) {
  174. stringtable *tb = &L->string_root[i];
  175. int j;
  176. for (j=0; j<tb->size; j++) {
  177. TaggedString *t = tb->hash[j];
  178. if (t == NULL || t == &EMPTY || t->constindex != -1)
  179. continue; /* get only user datas */
  180. t->head.next = (GCnode *)frees;
  181. frees = t;
  182. tb->hash[j] = &EMPTY;
  183. }
  184. }
  185. return frees;
  186. }
  187. void luaS_freeall (void)
  188. {
  189. int i;
  190. for (i=0; i<NUM_HASHS; i++) {
  191. stringtable *tb = &L->string_root[i];
  192. int j;
  193. for (j=0; j<tb->size; j++) {
  194. TaggedString *t = tb->hash[j];
  195. if (t == &EMPTY) continue;
  196. luaM_free(t);
  197. }
  198. luaM_free(tb->hash);
  199. }
  200. luaM_free(L->string_root);
  201. }
  202. void luaS_rawsetglobal (TaggedString *ts, TObject *newval)
  203. {
  204. ts->u.globalval = *newval;
  205. if (ts->head.next == (GCnode *)ts) { /* is not in list? */
  206. ts->head.next = L->rootglobal.next;
  207. L->rootglobal.next = (GCnode *)ts;
  208. }
  209. }
  210. char *luaS_travsymbol (int (*fn)(TObject *))
  211. {
  212. TaggedString *g;
  213. for (g=(TaggedString *)L->rootglobal.next; g; g=(TaggedString *)g->head.next)
  214. if (fn(&g->u.globalval))
  215. return g->str;
  216. return NULL;
  217. }
  218. int luaS_globaldefined (char *name)
  219. {
  220. TaggedString *ts = luaS_new(name);
  221. return ts->u.globalval.ttype != LUA_T_NIL;
  222. }