lstring.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. ** $Id: lstring.c,v 1.25 1999/10/19 13:33:22 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. /*
  14. ** to avoid hash tables with size = 0 (cannot hash with size=0), all
  15. ** hash tables are initialized with this `array'. Elements are never
  16. ** written here, because this table (with size=1) must grow to get an
  17. ** element, and before it grows we replace it for a `real' table.
  18. **
  19. */
  20. static TaggedString *init_hash[1] = {NULL};
  21. void luaS_init (void) {
  22. int i;
  23. L->string_root = luaM_newvector(NUM_HASHS, stringtable);
  24. for (i=0; i<NUM_HASHS; i++) {
  25. L->string_root[i].size = 1;
  26. L->string_root[i].nuse = 0;
  27. L->string_root[i].hash = init_hash;
  28. }
  29. }
  30. void luaS_freeall (void) {
  31. int i;
  32. for (i=0; i<NUM_HASHS; i++) {
  33. LUA_ASSERT(L->string_root[i].nuse==0, "non-empty string table");
  34. if (L->string_root[i].hash != init_hash)
  35. luaM_free(L->string_root[i].hash);
  36. }
  37. luaM_free(L->string_root);
  38. LUA_ASSERT(init_hash[0] == NULL, "init_hash corrupted");
  39. }
  40. static unsigned long hash_s (const char *s, long l) {
  41. unsigned long h = l; /* seed */
  42. while (l--)
  43. h = h ^ ((h<<5)+(h>>2)+(unsigned char)*(s++));
  44. return h;
  45. }
  46. void luaS_grow (stringtable *tb) {
  47. int ns = luaO_redimension(tb->nuse*2); /* new size */
  48. TaggedString **newhash = luaM_newvector(ns, TaggedString *);
  49. int i;
  50. for (i=0; i<ns; i++) newhash[i] = NULL;
  51. /* rehash */
  52. for (i=0; i<tb->size; i++) {
  53. TaggedString *p = tb->hash[i];
  54. while (p) { /* for each node in the list */
  55. TaggedString *next = p->nexthash; /* save next */
  56. int h = p->hash%ns; /* new position */
  57. p->nexthash = newhash[h]; /* chain it in new position */
  58. newhash[h] = p;
  59. p = next;
  60. }
  61. }
  62. luaM_free(tb->hash);
  63. tb->size = ns;
  64. tb->hash = newhash;
  65. }
  66. static TaggedString *newone (long l, unsigned long h) {
  67. TaggedString *ts = (TaggedString *)luaM_malloc(
  68. sizeof(TaggedString)+l*sizeof(char));
  69. ts->marked = 0;
  70. ts->nexthash = NULL;
  71. ts->hash = h;
  72. return ts;
  73. }
  74. static TaggedString *newone_s (const char *str, long l, unsigned long h) {
  75. TaggedString *ts = newone(l, h);
  76. memcpy(ts->str, str, l);
  77. ts->str[l] = 0; /* ending 0 */
  78. ts->u.s.gv = NULL; /* no global value */
  79. ts->u.s.len = l;
  80. ts->constindex = 0;
  81. L->nblocks += gcsizestring(l);
  82. return ts;
  83. }
  84. static TaggedString *newone_u (void *buff, int tag, unsigned long h) {
  85. TaggedString *ts = newone(0, h);
  86. ts->u.d.value = buff;
  87. ts->u.d.tag = (tag == LUA_ANYTAG) ? 0 : tag;
  88. ts->constindex = -1; /* tag -> this is a userdata */
  89. L->nblocks++;
  90. return ts;
  91. }
  92. static void newentry (stringtable *tb, TaggedString *ts, int h) {
  93. tb->nuse++;
  94. if (tb->nuse >= tb->size) { /* no more room? */
  95. if (tb->hash == init_hash) { /* cannot change init_hash */
  96. LUA_ASSERT(h==0, "`init_hash' has size 1");
  97. tb->hash = luaM_newvector(1, TaggedString *); /* so, `clone' it */
  98. tb->hash[0] = NULL;
  99. }
  100. luaS_grow(tb);
  101. h = ts->hash%tb->size; /* new hash position */
  102. }
  103. ts->nexthash = tb->hash[h]; /* chain new entry */
  104. tb->hash[h] = ts;
  105. }
  106. TaggedString *luaS_newlstr (const char *str, long l) {
  107. unsigned long h = hash_s(str, l);
  108. stringtable *tb = &L->string_root[h%NUM_HASHSTR];
  109. int h1 = h%tb->size;
  110. TaggedString *ts;
  111. for (ts = tb->hash[h1]; ts; ts = ts->nexthash) {
  112. if (ts->u.s.len == l && (memcmp(str, ts->str, l) == 0))
  113. return ts;
  114. }
  115. /* not found */
  116. ts = newone_s(str, l, h); /* create new entry */
  117. newentry(tb, ts, h1); /* insert it on table */
  118. return ts;
  119. }
  120. TaggedString *luaS_createudata (void *udata, int tag) {
  121. unsigned long h = (IntPoint)udata;
  122. stringtable *tb = &L->string_root[(h%NUM_HASHUDATA)+NUM_HASHSTR];
  123. int h1 = h%tb->size;
  124. TaggedString *ts;
  125. for (ts = tb->hash[h1]; ts; ts = ts->nexthash) {
  126. if (udata == ts->u.d.value && (tag == ts->u.d.tag || tag == LUA_ANYTAG))
  127. return ts;
  128. }
  129. /* not found */
  130. ts = newone_u(udata, tag, h);
  131. newentry(tb, ts, h1);
  132. return ts;
  133. }
  134. TaggedString *luaS_new (const char *str) {
  135. return luaS_newlstr(str, strlen(str));
  136. }
  137. TaggedString *luaS_newfixedstring (const char *str) {
  138. TaggedString *ts = luaS_new(str);
  139. if (ts->marked == 0) ts->marked = FIXMARK; /* avoid GC */
  140. return ts;
  141. }
  142. void luaS_free (TaggedString *t) {
  143. if (t->constindex == -1) /* is userdata? */
  144. L->nblocks--;
  145. else { /* is string */
  146. L->nblocks -= gcsizestring(t->u.s.len);
  147. luaM_free(t->u.s.gv);
  148. }
  149. luaM_free(t);
  150. }
  151. GlobalVar *luaS_assertglobal (TaggedString *ts) {
  152. GlobalVar *gv = ts->u.s.gv;
  153. if (!gv) { /* no global value yet? */
  154. gv = luaM_new(GlobalVar);
  155. gv->value.ttype = LUA_T_NIL; /* initial value */
  156. gv->name = ts;
  157. gv->next = L->rootglobal; /* chain in global list */
  158. L->rootglobal = gv;
  159. ts->u.s.gv = gv;
  160. }
  161. return gv;
  162. }
  163. GlobalVar *luaS_assertglobalbyname (const char *name) {
  164. return luaS_assertglobal(luaS_new(name));
  165. }
  166. int luaS_globaldefined (const char *name) {
  167. TaggedString *ts = luaS_new(name);
  168. return ts->u.s.gv && ts->u.s.gv->value.ttype != LUA_T_NIL;
  169. }