lstring.c 5.3 KB

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