lstring.c 5.5 KB

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