lstring.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. ** $Id: lstring.c,v 1.31 1999/12/14 18:42:57 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(TaggedString)+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, TaggedString *);;
  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. while (l--)
  36. h = h ^ ((h<<5)+(h>>2)+(unsigned char)*(s++));
  37. return h;
  38. }
  39. void luaS_resize (lua_State *L, stringtable *tb, int newsize) {
  40. TaggedString **newhash = luaM_newvector(L, newsize, TaggedString *);
  41. int i;
  42. for (i=0; i<newsize; i++) newhash[i] = NULL;
  43. /* rehash */
  44. for (i=0; i<tb->size; i++) {
  45. TaggedString *p = tb->hash[i];
  46. while (p) { /* for each node in the list */
  47. TaggedString *next = p->nexthash; /* save next */
  48. int h = p->hash&(newsize-1); /* new position */
  49. LUA_ASSERT(L, p->hash%newsize == (p->hash&(newsize-1)),
  50. "a&(x-1) == a%x, for x power of 2");
  51. p->nexthash = newhash[h]; /* chain it in new position */
  52. newhash[h] = p;
  53. p = next;
  54. }
  55. }
  56. luaM_free(L, tb->hash);
  57. tb->size = newsize;
  58. tb->hash = newhash;
  59. }
  60. static TaggedString *newone (lua_State *L, long l, unsigned long h) {
  61. TaggedString *ts = (TaggedString *)luaM_malloc(L,
  62. sizeof(TaggedString)+l*sizeof(char));
  63. ts->marked = 0;
  64. ts->nexthash = NULL;
  65. ts->hash = h;
  66. return ts;
  67. }
  68. static TaggedString *newone_s (lua_State *L, const char *str,
  69. long l, unsigned long h) {
  70. TaggedString *ts = newone(L, l, h);
  71. memcpy(ts->str, str, l);
  72. ts->str[l] = 0; /* ending 0 */
  73. ts->u.s.gv = NULL; /* no global value */
  74. ts->u.s.len = l;
  75. ts->constindex = 0;
  76. L->nblocks += gcsizestring(L, l);
  77. return ts;
  78. }
  79. static TaggedString *newone_u (lua_State *L, void *buff,
  80. int tag, unsigned long h) {
  81. TaggedString *ts = newone(L, 0, h);
  82. ts->u.d.value = buff;
  83. ts->u.d.tag = (tag == LUA_ANYTAG) ? 0 : tag;
  84. ts->constindex = -1; /* tag -> this is a userdata */
  85. L->nblocks += gcsizeudata;
  86. return ts;
  87. }
  88. static void newentry (lua_State *L, stringtable *tb, TaggedString *ts, int h) {
  89. ts->nexthash = tb->hash[h]; /* chain new entry */
  90. tb->hash[h] = ts;
  91. tb->nuse++;
  92. if (tb->nuse > tb->size) /* too crowded? */
  93. luaS_resize(L, tb, tb->size*2);
  94. }
  95. TaggedString *luaS_newlstr (lua_State *L, const char *str, long l) {
  96. unsigned long h = hash_s(str, l);
  97. stringtable *tb = &L->string_root[h%NUM_HASHSTR];
  98. int h1 = h&(tb->size-1);
  99. TaggedString *ts;
  100. for (ts = tb->hash[h1]; ts; ts = ts->nexthash) {
  101. if (ts->u.s.len == l && (memcmp(str, ts->str, l) == 0))
  102. return ts;
  103. }
  104. /* not found */
  105. ts = newone_s(L, str, l, h); /* create new entry */
  106. newentry(L, tb, ts, h1); /* insert it on table */
  107. return ts;
  108. }
  109. TaggedString *luaS_createudata (lua_State *L, void *udata, int tag) {
  110. unsigned long h = IntPoint(L, udata);
  111. stringtable *tb = &L->string_root[(h%NUM_HASHUDATA)+NUM_HASHSTR];
  112. int h1 = h&(tb->size-1);
  113. TaggedString *ts;
  114. for (ts = tb->hash[h1]; ts; ts = ts->nexthash) {
  115. if (udata == ts->u.d.value && (tag == ts->u.d.tag || tag == LUA_ANYTAG))
  116. return ts;
  117. }
  118. /* not found */
  119. ts = newone_u(L, udata, tag, h);
  120. newentry(L, tb, ts, h1);
  121. return ts;
  122. }
  123. TaggedString *luaS_new (lua_State *L, const char *str) {
  124. return luaS_newlstr(L, str, strlen(str));
  125. }
  126. TaggedString *luaS_newfixed (lua_State *L, const char *str) {
  127. TaggedString *ts = luaS_new(L, str);
  128. if (ts->marked == 0) ts->marked = FIXMARK; /* avoid GC */
  129. return ts;
  130. }
  131. void luaS_free (lua_State *L, TaggedString *t) {
  132. if (t->constindex == -1) /* is userdata? */
  133. L->nblocks -= gcsizeudata;
  134. else { /* is string */
  135. L->nblocks -= gcsizestring(L, t->u.s.len);
  136. luaM_free(L, t->u.s.gv);
  137. }
  138. luaM_free(L, t);
  139. }
  140. GlobalVar *luaS_assertglobal (lua_State *L, TaggedString *ts) {
  141. GlobalVar *gv = ts->u.s.gv;
  142. if (!gv) { /* no global value yet? */
  143. gv = luaM_new(L, GlobalVar);
  144. gv->value.ttype = LUA_T_NIL; /* initial value */
  145. gv->name = ts;
  146. gv->next = L->rootglobal; /* chain in global list */
  147. L->rootglobal = gv;
  148. ts->u.s.gv = gv;
  149. }
  150. return gv;
  151. }
  152. GlobalVar *luaS_assertglobalbyname (lua_State *L, const char *name) {
  153. return luaS_assertglobal(L, luaS_new(L, name));
  154. }
  155. int luaS_globaldefined (lua_State *L, const char *name) {
  156. TaggedString *ts = luaS_new(L, name);
  157. return ts->u.s.gv && ts->u.s.gv->value.ttype != LUA_T_NIL;
  158. }