lstring.c 4.8 KB

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