lstring.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. ** $Id: lstring.c,v 1.2 1997/09/26 15:02:26 roberto Exp roberto $
  3. ** String table (keep 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 "lstring.h"
  10. #include "lua.h"
  11. #define NUM_HASHS 61
  12. #define gcsizestring(l) (1+(l/64))
  13. GCnode luaS_root = {NULL, 0}; /* list of global variables */
  14. typedef struct {
  15. int size;
  16. int nuse; /* number of elements (including EMPTYs) */
  17. TaggedString **hash;
  18. } stringtable;
  19. static stringtable string_root[NUM_HASHS] = {
  20. {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL},
  21. {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL},
  22. {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL},
  23. {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL},
  24. {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL},
  25. {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL},
  26. {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL},
  27. {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL},
  28. {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL},
  29. {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL},
  30. {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL},
  31. {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL}, {0, 0, NULL},
  32. {0, 0, NULL}
  33. };
  34. static TaggedString EMPTY = {{NULL, 2}, 0, 0L, {{LUA_T_NIL, {NULL}}}, {0}};
  35. static unsigned long hash (char *s, int tag)
  36. {
  37. unsigned long h;
  38. if (tag != LUA_T_STRING)
  39. h = (unsigned long)s;
  40. else {
  41. h = 0;
  42. while (*s)
  43. h = ((h<<5)-h)^(unsigned char)*(s++);
  44. }
  45. return h;
  46. }
  47. static void grow (stringtable *tb)
  48. {
  49. int newsize = luaO_redimension(tb->size);
  50. TaggedString **newhash = luaM_newvector(newsize, TaggedString *);
  51. int i;
  52. for (i=0; i<newsize; 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%newsize;
  59. while (newhash[h])
  60. h = (h+1)%newsize;
  61. newhash[h] = tb->hash[i];
  62. tb->nuse++;
  63. }
  64. }
  65. luaM_free(tb->hash);
  66. tb->size = newsize;
  67. tb->hash = newhash;
  68. }
  69. static TaggedString *newone(char *buff, int tag, unsigned long h)
  70. {
  71. TaggedString *ts;
  72. if (tag == LUA_T_STRING) {
  73. long l = strlen(buff);
  74. ts = (TaggedString *)luaM_malloc(sizeof(TaggedString)+l);
  75. strcpy(ts->str, buff);
  76. ts->u.globalval.ttype = LUA_T_NIL; /* initialize global value */
  77. ts->constindex = 0;
  78. luaO_nblocks += gcsizestring(l);
  79. }
  80. else {
  81. ts = (TaggedString *)luaM_malloc(sizeof(TaggedString));
  82. ts->u.d.v = buff;
  83. ts->u.d.tag = tag == LUA_ANYTAG ? 0 : tag;
  84. ts->constindex = -1; /* tag -> this is a userdata */
  85. luaO_nblocks++;
  86. }
  87. ts->head.marked = 0;
  88. ts->head.next = (GCnode *)ts; /* signal it is in no list */
  89. ts->hash = h;
  90. return ts;
  91. }
  92. static TaggedString *insert (char *buff, int tag, stringtable *tb)
  93. {
  94. TaggedString *ts;
  95. unsigned long h = hash(buff, tag);
  96. int i;
  97. int j = -1;
  98. if ((long)tb->nuse*3 >= (long)tb->size*2)
  99. grow(tb);
  100. i = h%tb->size;
  101. while ((ts = tb->hash[i]) != NULL)
  102. {
  103. if (ts == &EMPTY)
  104. j = i;
  105. else if ((ts->constindex >= 0) ? /* is a string? */
  106. (tag == LUA_T_STRING && (strcmp(buff, ts->str) == 0)) :
  107. ((tag == ts->u.d.tag || tag == LUA_ANYTAG) && buff == ts->u.d.v))
  108. return ts;
  109. i = (i+1)%tb->size;
  110. }
  111. /* not found */
  112. if (j != -1) /* is there an EMPTY space? */
  113. i = j;
  114. else
  115. tb->nuse++;
  116. ts = tb->hash[i] = newone(buff, tag, h);
  117. return ts;
  118. }
  119. TaggedString *luaS_createudata (void *udata, int tag)
  120. {
  121. return insert(udata, tag, &string_root[(unsigned)udata%NUM_HASHS]);
  122. }
  123. TaggedString *luaS_new (char *str)
  124. {
  125. return insert(str, LUA_T_STRING, &string_root[(unsigned)str[0]%NUM_HASHS]);
  126. }
  127. TaggedString *luaS_newfixedstring (char *str)
  128. {
  129. TaggedString *ts = luaS_new(str);
  130. if (ts->head.marked == 0)
  131. ts->head.marked = 2; /* avoid GC */
  132. return ts;
  133. }
  134. void luaS_free (TaggedString *l)
  135. {
  136. while (l) {
  137. TaggedString *next = (TaggedString *)l->head.next;
  138. luaO_nblocks -= (l->constindex == -1) ? 1 : gcsizestring(strlen(l->str));
  139. luaM_free(l);
  140. l = next;
  141. }
  142. }
  143. /*
  144. ** Garbage collection functions.
  145. */
  146. static void remove_from_list (GCnode *l)
  147. {
  148. while (l) {
  149. GCnode *next = l->next;
  150. while (next && !next->marked)
  151. next = l->next = next->next;
  152. l = next;
  153. }
  154. }
  155. TaggedString *luaS_collector (void)
  156. {
  157. TaggedString *frees = NULL;
  158. int i;
  159. remove_from_list(&luaS_root);
  160. for (i=0; i<NUM_HASHS; i++) {
  161. stringtable *tb = &string_root[i];
  162. int j;
  163. for (j=0; j<tb->size; j++) {
  164. TaggedString *t = tb->hash[j];
  165. if (t == NULL) continue;
  166. if (t->head.marked == 1)
  167. t->head.marked = 0;
  168. else if (!t->head.marked) {
  169. t->head.next = (GCnode *)frees;
  170. frees = t;
  171. tb->hash[j] = &EMPTY;
  172. }
  173. }
  174. }
  175. return frees;
  176. }
  177. void luaS_rawsetglobal (TaggedString *ts, TObject *newval)
  178. {
  179. ts->u.globalval = *newval;
  180. if (ts->head.next == (GCnode *)ts) { /* is not in list? */
  181. ts->head.next = luaS_root.next;
  182. luaS_root.next = (GCnode *)ts;
  183. }
  184. }
  185. char *luaS_travsymbol (int (*fn)(TObject *))
  186. {
  187. TaggedString *g;
  188. for (g=(TaggedString *)luaS_root.next; g; g=(TaggedString *)g->head.next)
  189. if (fn(&g->u.globalval))
  190. return g->str;
  191. return NULL;
  192. }
  193. int luaS_globaldefined (char *name)
  194. {
  195. TaggedString *ts = luaS_new(name);
  196. return ts->u.globalval.ttype != LUA_T_NIL;
  197. }