lstring.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. ** $Id: lstring.c,v 1.21 1999/09/28 12:27:06 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 gcsizestring(l) (1+(l/64)) /* "weight" for a string with length 'l' */
  13. TaggedString luaS_EMPTY = {NULL, MAX_INT, 0L, 0,
  14. {{{LUA_T_NIL, {NULL}}, 0L}}, {0}};
  15. /*
  16. ** to avoid hash tables with size = 0 (cannot hash with size=0), all
  17. ** hash tables are initialized with this `array'. Elements are never
  18. ** written here, because this table (with size=1) must grow to get an
  19. ** element, and before it grows we replace it for a `real' table.
  20. **
  21. */
  22. static TaggedString *init_hash[1] = {NULL};
  23. void luaS_init (void) {
  24. int i;
  25. L->string_root = luaM_newvector(NUM_HASHS, stringtable);
  26. for (i=0; i<NUM_HASHS; i++) {
  27. L->string_root[i].size = 1;
  28. L->string_root[i].nuse = 0;
  29. L->string_root[i].hash = init_hash;
  30. }
  31. }
  32. void luaS_freeall (void) {
  33. int i;
  34. for (i=0; i<NUM_HASHS; i++) {
  35. if (L->string_root[i].hash != init_hash)
  36. luaM_free(L->string_root[i].hash);
  37. }
  38. luaM_free(L->string_root);
  39. }
  40. static unsigned long hash_s (const char *s, long l) {
  41. unsigned long h = 0; /* seed */
  42. while (l--)
  43. h = h ^ ((h<<5)+(h>>2)+(unsigned char)*(s++));
  44. return h;
  45. }
  46. static int newsize (const stringtable *tb) {
  47. int realuse = 0;
  48. int i;
  49. /* count how many entries are really in use */
  50. for (i=0; i<tb->size; i++) {
  51. if (tb->hash[i] != NULL && tb->hash[i] != &luaS_EMPTY)
  52. realuse++;
  53. }
  54. return luaO_redimension(realuse*2);
  55. }
  56. static void grow (stringtable *tb) {
  57. int ns = newsize(tb);
  58. TaggedString **newhash = luaM_newvector(ns, TaggedString *);
  59. int i;
  60. for (i=0; i<ns; i++)
  61. newhash[i] = NULL;
  62. /* rehash */
  63. tb->nuse = 0;
  64. for (i=0; i<tb->size; i++) {
  65. if (tb->hash[i] != NULL && tb->hash[i] != &luaS_EMPTY) {
  66. unsigned long h = tb->hash[i]->hash;
  67. int h1 = h%ns;
  68. while (newhash[h1]) {
  69. h1 += (h&(ns-2)) + 1; /* double hashing */
  70. if (h1 >= ns) h1 -= ns;
  71. }
  72. newhash[h1] = tb->hash[i];
  73. tb->nuse++;
  74. }
  75. }
  76. luaM_free(tb->hash);
  77. tb->size = ns;
  78. tb->hash = newhash;
  79. }
  80. static TaggedString *newone_s (const char *str, long l, unsigned long h) {
  81. TaggedString *ts = (TaggedString *)luaM_malloc(sizeof(TaggedString)+l);
  82. memcpy(ts->str, str, l);
  83. ts->str[l] = 0; /* ending 0 */
  84. ts->u.s.globalval.ttype = LUA_T_NIL; /* initialize global value */
  85. ts->u.s.len = l;
  86. ts->constindex = 0;
  87. L->nblocks += gcsizestring(l);
  88. ts->marked = 0;
  89. ts->next = ts; /* signal it is in no list */
  90. ts->hash = h;
  91. return ts;
  92. }
  93. static TaggedString *newone_u (void *buff, int tag, unsigned long h) {
  94. TaggedString *ts = luaM_new(TaggedString);
  95. ts->u.d.v = buff;
  96. ts->u.d.tag = (tag == LUA_ANYTAG) ? 0 : tag;
  97. ts->constindex = -1; /* tag -> this is a userdata */
  98. L->nblocks++;
  99. ts->marked = 0;
  100. ts->next = ts; /* signal it is in no list */
  101. ts->hash = h;
  102. return ts;
  103. }
  104. static void newentry (stringtable *tb, TaggedString *ts, int h1) {
  105. tb->nuse++;
  106. if ((long)tb->nuse*3 < (long)tb->size*2) /* still have room? */
  107. tb->hash[h1] = ts;
  108. else { /* must grow */
  109. if (tb->hash == init_hash) { /* cannot change init_hash */
  110. LUA_ASSERT(h1==0, "`init_hash' has size 1");
  111. tb->hash = luaM_newvector(1, TaggedString *); /* so, `clone' it */
  112. }
  113. tb->hash[h1] = ts;
  114. grow(tb);
  115. }
  116. }
  117. static TaggedString *insert_s (const char *str, long l, stringtable *tb) {
  118. TaggedString *ts;
  119. unsigned long h = hash_s(str, l);
  120. int size = tb->size;
  121. int j = -1; /* last empty place found (or -1) */
  122. int h1 = h%size;
  123. while ((ts = tb->hash[h1]) != NULL) {
  124. if (ts == &luaS_EMPTY)
  125. j = h1;
  126. else if (ts->u.s.len == l && (memcmp(str, ts->str, l) == 0))
  127. return ts;
  128. h1 += (h&(size-2)) + 1; /* double hashing */
  129. if (h1 >= size) h1 -= size;
  130. }
  131. /* not found */
  132. ts = newone_s(str, l, h); /* create new entry */
  133. if (j != -1) /* is there an EMPTY space? */
  134. tb->hash[j] = ts; /* use it for new element */
  135. else
  136. newentry(tb, ts, h1); /* no EMPTY places; must use a virgin one */
  137. return ts;
  138. }
  139. static TaggedString *insert_u (void *buff, int tag, stringtable *tb) {
  140. TaggedString *ts;
  141. unsigned long h = (unsigned long)buff;
  142. int size = tb->size;
  143. int j = -1;
  144. int h1 = h%size;
  145. while ((ts = tb->hash[h1]) != NULL) {
  146. if (ts == &luaS_EMPTY)
  147. j = h1;
  148. else if ((tag == ts->u.d.tag || tag == LUA_ANYTAG) && buff == ts->u.d.v)
  149. return ts;
  150. h1 += (h&(size-2)) + 1;
  151. if (h1 >= size) h1 -= size;
  152. }
  153. ts = newone_u(buff, tag, h);
  154. if (j != -1)
  155. tb->hash[j] = ts;
  156. else
  157. newentry(tb, ts, h1);
  158. return ts;
  159. }
  160. TaggedString *luaS_createudata (void *udata, int tag) {
  161. int t = ((unsigned int)udata%NUM_HASHUDATA)+NUM_HASHSTR;
  162. return insert_u(udata, tag, &L->string_root[t]);
  163. }
  164. TaggedString *luaS_newlstr (const char *str, long l) {
  165. int t = (l==0) ? 0 : ((int)((unsigned char)str[0]+l))%NUM_HASHSTR;
  166. return insert_s(str, l, &L->string_root[t]);
  167. }
  168. TaggedString *luaS_new (const char *str) {
  169. return luaS_newlstr(str, strlen(str));
  170. }
  171. TaggedString *luaS_newfixedstring (const char *str) {
  172. TaggedString *ts = luaS_new(str);
  173. if (ts->marked == 0) ts->marked = 2; /* avoid GC */
  174. return ts;
  175. }
  176. void luaS_free (TaggedString *t) {
  177. L->nblocks -= (t->constindex == -1) ? 1 : gcsizestring(t->u.s.len);
  178. luaM_free(t);
  179. }
  180. void luaS_rawsetglobal (TaggedString *ts, const TObject *newval) {
  181. ts->u.s.globalval = *newval;
  182. if (ts->next == ts) { /* is not in list? */
  183. ts->next = L->rootglobal;
  184. L->rootglobal = ts;
  185. }
  186. }
  187. int luaS_globaldefined (const char *name) {
  188. TaggedString *ts = luaS_new(name);
  189. return ts->u.s.globalval.ttype != LUA_T_NIL;
  190. }