lstring.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. ** $Id: lstring.c,v 2.28 2013/08/05 16:58:28 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 lstring_c
  8. #define LUA_CORE
  9. #include "lua.h"
  10. #include "ldebug.h"
  11. #include "lmem.h"
  12. #include "lobject.h"
  13. #include "lstate.h"
  14. #include "lstring.h"
  15. /* mark for vacant places in hash table */
  16. #define VACANTK cast(TString *, cast(size_t, -1))
  17. /* second hash (for double hash) */
  18. #define h2(h1,hash,size) lmod(h1 + ((hash % 31) | 1), size)
  19. /*
  20. ** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to
  21. ** compute its hash
  22. */
  23. #if !defined(LUAI_HASHLIMIT)
  24. #define LUAI_HASHLIMIT 5
  25. #endif
  26. /*
  27. ** equality for long strings
  28. */
  29. int luaS_eqlngstr (TString *a, TString *b) {
  30. size_t len = a->tsv.len;
  31. lua_assert(a->tsv.tt == LUA_TLNGSTR && b->tsv.tt == LUA_TLNGSTR);
  32. return (a == b) || /* same instance or... */
  33. ((len == b->tsv.len) && /* equal length and ... */
  34. (memcmp(getstr(a), getstr(b), len) == 0)); /* equal contents */
  35. }
  36. /*
  37. ** equality for strings
  38. */
  39. int luaS_eqstr (TString *a, TString *b) {
  40. return (a->tsv.tt == b->tsv.tt) &&
  41. (a->tsv.tt == LUA_TSHRSTR ? eqshrstr(a, b) : luaS_eqlngstr(a, b));
  42. }
  43. unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) {
  44. unsigned int h = seed ^ cast(unsigned int, l);
  45. size_t l1;
  46. size_t step = (l >> LUAI_HASHLIMIT) + 1;
  47. for (l1 = l; l1 >= step; l1 -= step)
  48. h = h ^ ((h<<5) + (h>>2) + cast_byte(str[l1 - 1]));
  49. return h;
  50. }
  51. /*
  52. ** resizes the string table
  53. */
  54. void luaS_resize (lua_State *L, int newsize) {
  55. int i;
  56. stringtable *tb = &G(L)->strt;
  57. TString **oldhash = tb->hash;
  58. int oldsize = tb->size;
  59. tb->hash = luaM_newvector(L, newsize, TString *);
  60. tb->size = newsize;
  61. /* keep load factor below 75% */
  62. tb->empty = newsize/2 + newsize/4 - tb->nuse;
  63. for (i = 0; i < newsize; i++) tb->hash[i] = NULL;
  64. tb->nuse = 0;
  65. /* rehash */
  66. for (i = 0; i < oldsize; i++) {
  67. TString *ts = oldhash[i];
  68. if (ts != NULL && ts != VACANTK) {
  69. unsigned int hash = ts->tsv.hash;
  70. int h1 = lmod(hash, tb->size);
  71. while (tb->hash[h1] != NULL)
  72. h1 = h2(h1, hash, tb->size);
  73. tb->hash[h1] = ts;
  74. tb->nuse++;
  75. }
  76. }
  77. luaM_freearray(L, oldhash, oldsize);
  78. }
  79. /*
  80. ** creates a new string object
  81. */
  82. static TString *createstrobj (lua_State *L, const char *str, size_t l,
  83. int tag, unsigned int h) {
  84. TString *ts;
  85. size_t totalsize; /* total size of TString object */
  86. totalsize = sizeof(TString) + ((l + 1) * sizeof(char));
  87. ts = &luaC_newobj(L, tag, totalsize, NULL, 0)->ts;
  88. ts->tsv.len = l;
  89. ts->tsv.hash = h;
  90. ts->tsv.extra = 0;
  91. memcpy(ts+1, str, l*sizeof(char));
  92. ((char *)(ts+1))[l] = '\0'; /* ending 0 */
  93. return ts;
  94. }
  95. static void rehash (lua_State *L, stringtable *tb) {
  96. int newsize;
  97. if (tb->nuse < tb->size / 2) { /* using less than half the size? */
  98. if (tb->nuse < tb->size / 4) /* using less than half of that? */
  99. newsize = tb->size / 2; /* shrink table */
  100. else
  101. newsize = tb->size; /* keep size (but reorganize table) */
  102. }
  103. else { /* table must grow */
  104. if (tb->size >= MAX_INT/2)
  105. luaG_runerror(L, "string-table overflow: too many strings");
  106. newsize = tb->size * 2;
  107. }
  108. luaS_resize(L, newsize);
  109. }
  110. LUAI_FUNC void luaS_remove (lua_State *L, TString *ts) {
  111. stringtable *tb = &G(L)->strt;
  112. unsigned int hash = ts->tsv.hash;
  113. int h1 = lmod(hash, tb->size);
  114. while (tb->hash[h1] != ts) {
  115. lua_assert(tb->hash[h1] != NULL);
  116. h1 = h2(h1, hash, tb->size);
  117. }
  118. tb->hash[h1] = VACANTK;
  119. tb->nuse--;
  120. }
  121. /*
  122. ** checks whether short string exists and reuses it or creates a new one
  123. */
  124. static TString *internshrstr (lua_State *L, const char *str, size_t l) {
  125. TString *ts;
  126. unsigned int hash = luaS_hash(str, l, G(L)->seed);
  127. stringtable *tb = &G(L)->strt;
  128. int vacant = -1;
  129. int h1;
  130. if (tb->empty <= 0)
  131. rehash(L, tb);
  132. h1 = lmod(hash, tb->size); /* previous call can changed 'size' */
  133. while ((ts = tb->hash[h1]) != NULL) { /* search the string in hash table */
  134. if (ts == VACANTK) {
  135. if (vacant < 0) vacant = h1; /* keep track of a vacant place */
  136. }
  137. else if (l == ts->tsv.len &&
  138. (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) {
  139. /* found! */
  140. if (isdead(G(L), obj2gco(ts))) /* dead (but was not collected yet)? */
  141. changewhite(obj2gco(ts)); /* resurrect it */
  142. if (vacant >= 0) { /* is there a better place for this string? */
  143. tb->hash[vacant] = ts; /* move it up the line */
  144. tb->hash[h1] = VACANTK;
  145. }
  146. return ts; /* found */
  147. }
  148. h1 = h2(h1, hash, tb->size);
  149. }
  150. ts = createstrobj(L, str, l, LUA_TSHRSTR, hash);
  151. tb->nuse++;
  152. if (vacant < 0) /* found no vacant place? */
  153. tb->empty--; /* will have to use the empty place */
  154. else
  155. h1 = vacant; /* insert string into vacant place */
  156. tb->hash[h1] = ts;
  157. return ts;
  158. }
  159. /*
  160. ** new string (with explicit length)
  161. */
  162. TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
  163. if (l <= LUAI_MAXSHORTLEN) /* short string? */
  164. return internshrstr(L, str, l);
  165. else {
  166. if (l + 1 > (MAX_SIZE - sizeof(TString))/sizeof(char))
  167. luaM_toobig(L);
  168. return createstrobj(L, str, l, LUA_TLNGSTR, G(L)->seed);
  169. }
  170. }
  171. /*
  172. ** new zero-terminated string
  173. */
  174. TString *luaS_new (lua_State *L, const char *str) {
  175. return luaS_newlstr(L, str, strlen(str));
  176. }
  177. Udata *luaS_newudata (lua_State *L, size_t s, Table *e) {
  178. Udata *u;
  179. if (s > MAX_SIZE - sizeof(Udata))
  180. luaM_toobig(L);
  181. u = &luaC_newobj(L, LUA_TUSERDATA, sizeof(Udata) + s, NULL, 0)->u;
  182. u->uv.len = s;
  183. u->uv.metatable = NULL;
  184. u->uv.env = e;
  185. return u;
  186. }