lstring.c 5.7 KB

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