lstring.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. ** $Id: lstring.c,v 1.33 2000/03/10 14:38:10 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. #define LUA_REENTRANT
  8. #include "lmem.h"
  9. #include "lobject.h"
  10. #include "lstate.h"
  11. #include "lstring.h"
  12. #include "lua.h"
  13. #define gcsizestring(L, l) numblocks(L, 0, sizeof(TString)+l)
  14. #define gcsizeudata gcsizestring(L, 0)
  15. void luaS_init (lua_State *L) {
  16. int i;
  17. L->string_root = luaM_newvector(L, NUM_HASHS, stringtable);
  18. for (i=0; i<NUM_HASHS; i++) {
  19. L->string_root[i].size = 1;
  20. L->string_root[i].nuse = 0;
  21. L->string_root[i].hash = luaM_newvector(L, 1, TString *);;
  22. L->string_root[i].hash[0] = NULL;
  23. }
  24. }
  25. void luaS_freeall (lua_State *L) {
  26. int i;
  27. for (i=0; i<NUM_HASHS; i++) {
  28. LUA_ASSERT(L, L->string_root[i].nuse==0, "non-empty string table");
  29. luaM_free(L, L->string_root[i].hash);
  30. }
  31. luaM_free(L, L->string_root);
  32. }
  33. static unsigned long hash_s (const char *s, long l) {
  34. unsigned long h = l; /* seed */
  35. long step = (l>>6)+1; /* if string is too long, don't hash all its chars */
  36. for (; l>0; l-=step)
  37. h = h ^ ((h<<5)+(h>>2)+(unsigned char)*(s++));
  38. return h;
  39. }
  40. void luaS_resize (lua_State *L, stringtable *tb, int newsize) {
  41. TString **newhash = luaM_newvector(L, newsize, TString *);
  42. int i;
  43. for (i=0; i<newsize; i++) newhash[i] = NULL;
  44. /* rehash */
  45. for (i=0; i<tb->size; i++) {
  46. TString *p = tb->hash[i];
  47. while (p) { /* for each node in the list */
  48. TString *next = p->nexthash; /* save next */
  49. int h = p->hash&(newsize-1); /* new position */
  50. LUA_ASSERT(L, p->hash%newsize == (p->hash&(newsize-1)),
  51. "a&(x-1) == a%x, for x power of 2");
  52. p->nexthash = newhash[h]; /* chain it in new position */
  53. newhash[h] = p;
  54. p = next;
  55. }
  56. }
  57. luaM_free(L, tb->hash);
  58. tb->size = newsize;
  59. tb->hash = newhash;
  60. }
  61. static TString *newone (lua_State *L, long l, unsigned long h) {
  62. TString *ts = (TString *)luaM_malloc(L,
  63. sizeof(TString)+l*sizeof(char));
  64. ts->marked = 0;
  65. ts->nexthash = NULL;
  66. ts->hash = h;
  67. return ts;
  68. }
  69. static TString *newone_s (lua_State *L, const char *str,
  70. long l, unsigned long h) {
  71. TString *ts = newone(L, l, h);
  72. memcpy(ts->str, str, l);
  73. ts->str[l] = 0; /* ending 0 */
  74. ts->u.s.gv = NULL; /* no global value */
  75. ts->u.s.len = l;
  76. ts->constindex = 0;
  77. L->nblocks += gcsizestring(L, l);
  78. return ts;
  79. }
  80. static TString *newone_u (lua_State *L, void *buff,
  81. int tag, unsigned long h) {
  82. TString *ts = newone(L, 0, h);
  83. ts->u.d.value = buff;
  84. ts->u.d.tag = (tag == LUA_ANYTAG) ? 0 : tag;
  85. ts->constindex = -1; /* tag -> this is a userdata */
  86. L->nblocks += gcsizeudata;
  87. return ts;
  88. }
  89. static void newentry (lua_State *L, stringtable *tb, TString *ts, int h) {
  90. ts->nexthash = tb->hash[h]; /* chain new entry */
  91. tb->hash[h] = ts;
  92. tb->nuse++;
  93. if (tb->nuse > tb->size) /* too crowded? */
  94. luaS_resize(L, tb, tb->size*2);
  95. }
  96. TString *luaS_newlstr (lua_State *L, const char *str, long l) {
  97. unsigned long h = hash_s(str, l);
  98. stringtable *tb = &L->string_root[(l==0) ? 0 :
  99. ((unsigned int)(str[0]+str[l-1]))&(NUM_HASHSTR-1)];
  100. int h1 = h&(tb->size-1);
  101. TString *ts;
  102. for (ts = tb->hash[h1]; ts; ts = ts->nexthash) {
  103. if (ts->u.s.len == l && (memcmp(str, ts->str, l) == 0))
  104. return ts;
  105. }
  106. /* not found */
  107. ts = newone_s(L, str, l, h); /* create new entry */
  108. newentry(L, tb, ts, h1); /* insert it on table */
  109. return ts;
  110. }
  111. /*
  112. ** uses '%' for one hashing with userdata because addresses are too regular,
  113. ** so two '&' operations would be highly correlated
  114. */
  115. TString *luaS_createudata (lua_State *L, void *udata, int tag) {
  116. unsigned long h = IntPoint(L, udata);
  117. stringtable *tb = &L->string_root[(h%NUM_HASHUDATA)+NUM_HASHSTR];
  118. int h1 = h&(tb->size-1);
  119. TString *ts;
  120. for (ts = tb->hash[h1]; ts; ts = ts->nexthash) {
  121. if (udata == ts->u.d.value && (tag == ts->u.d.tag || tag == LUA_ANYTAG))
  122. return ts;
  123. }
  124. /* not found */
  125. ts = newone_u(L, udata, tag, h);
  126. newentry(L, tb, ts, h1);
  127. return ts;
  128. }
  129. TString *luaS_new (lua_State *L, const char *str) {
  130. return luaS_newlstr(L, str, strlen(str));
  131. }
  132. TString *luaS_newfixed (lua_State *L, const char *str) {
  133. TString *ts = luaS_new(L, str);
  134. if (ts->marked == 0) ts->marked = FIXMARK; /* avoid GC */
  135. return ts;
  136. }
  137. void luaS_free (lua_State *L, TString *t) {
  138. if (t->constindex == -1) /* is userdata? */
  139. L->nblocks -= gcsizeudata;
  140. else { /* is string */
  141. L->nblocks -= gcsizestring(L, t->u.s.len);
  142. luaM_free(L, t->u.s.gv);
  143. }
  144. luaM_free(L, t);
  145. }
  146. GlobalVar *luaS_assertglobal (lua_State *L, TString *ts) {
  147. GlobalVar *gv = ts->u.s.gv;
  148. if (!gv) { /* no global value yet? */
  149. gv = luaM_new(L, GlobalVar);
  150. gv->value.ttype = TAG_NIL; /* initial value */
  151. gv->name = ts;
  152. gv->next = L->rootglobal; /* chain in global list */
  153. L->rootglobal = gv;
  154. ts->u.s.gv = gv;
  155. }
  156. return gv;
  157. }
  158. GlobalVar *luaS_assertglobalbyname (lua_State *L, const char *name) {
  159. return luaS_assertglobal(L, luaS_new(L, name));
  160. }
  161. int luaS_globaldefined (lua_State *L, const char *name) {
  162. TString *ts = luaS_new(L, name);
  163. return ts->u.s.gv && ts->u.s.gv->value.ttype != TAG_NIL;
  164. }