lstring.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. ** $Id: lstring.c,v 2.53 2015/09/22 14:18:24 roberto Exp roberto $
  3. ** String table (keeps all strings handled by Lua)
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define lstring_c
  7. #define LUA_CORE
  8. #include "lprefix.h"
  9. #include <string.h>
  10. #include "lua.h"
  11. #include "ldebug.h"
  12. #include "ldo.h"
  13. #include "lmem.h"
  14. #include "lobject.h"
  15. #include "lstate.h"
  16. #include "lstring.h"
  17. #define MEMERRMSG "not enough memory"
  18. /*
  19. ** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to
  20. ** compute its hash
  21. */
  22. #if !defined(LUAI_HASHLIMIT)
  23. #define LUAI_HASHLIMIT 5
  24. #endif
  25. /*
  26. ** equality for long strings
  27. */
  28. int luaS_eqlngstr (TString *a, TString *b) {
  29. size_t len = a->u.lnglen;
  30. lua_assert(a->tt == LUA_TLNGSTR && b->tt == LUA_TLNGSTR);
  31. return (a == b) || /* same instance or... */
  32. ((len == b->u.lnglen) && /* equal length and ... */
  33. (memcmp(getstr(a), getstr(b), len) == 0)); /* equal contents */
  34. }
  35. unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) {
  36. unsigned int h = seed ^ cast(unsigned int, l);
  37. size_t l1;
  38. size_t step = (l >> LUAI_HASHLIMIT) + 1;
  39. for (l1 = l; l1 >= step; l1 -= step)
  40. h = h ^ ((h<<5) + (h>>2) + cast_byte(str[l1 - 1]));
  41. return h;
  42. }
  43. /*
  44. ** resizes the string table
  45. */
  46. void luaS_resize (lua_State *L, int newsize) {
  47. int i;
  48. stringtable *tb = &G(L)->strt;
  49. if (newsize > tb->size) { /* grow table if needed */
  50. luaM_reallocvector(L, tb->hash, tb->size, newsize, TString *);
  51. for (i = tb->size; i < newsize; i++)
  52. tb->hash[i] = NULL;
  53. }
  54. for (i = 0; i < tb->size; i++) { /* rehash */
  55. TString *p = tb->hash[i];
  56. tb->hash[i] = NULL;
  57. while (p) { /* for each node in the list */
  58. TString *hnext = p->u.hnext; /* save next */
  59. unsigned int h = lmod(p->hash, newsize); /* new position */
  60. p->u.hnext = tb->hash[h]; /* chain it */
  61. tb->hash[h] = p;
  62. p = hnext;
  63. }
  64. }
  65. if (newsize < tb->size) { /* shrink table if needed */
  66. /* vanishing slice should be empty */
  67. lua_assert(tb->hash[newsize] == NULL && tb->hash[tb->size - 1] == NULL);
  68. luaM_reallocvector(L, tb->hash, tb->size, newsize, TString *);
  69. }
  70. tb->size = newsize;
  71. }
  72. /*
  73. ** Clear API string cache. (Entries cannot be empty, so fill them with
  74. ** a non-collectable string.)
  75. */
  76. void luaS_clearcache (global_State *g) {
  77. int i, j;
  78. for (i = 0; i < STRCACHE_N; i++)
  79. for (j = 0; j < STRCACHE_M; j++) {
  80. if (iswhite(g->strcache[i][j])) /* will entry be collected? */
  81. g->strcache[i][j] = g->memerrmsg; /* replace it with something fixed */
  82. }
  83. }
  84. /*
  85. ** Initialize the string table and the string cache
  86. */
  87. void luaS_init (lua_State *L) {
  88. global_State *g = G(L);
  89. int i, j;
  90. luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */
  91. /* pre-create memory-error message */
  92. g->memerrmsg = luaS_newliteral(L, MEMERRMSG);
  93. luaC_fix(L, obj2gco(g->memerrmsg)); /* it should never be collected */
  94. for (i = 0; i < STRCACHE_N; i++) /* fill cache with valid strings */
  95. for (j = 0; j < STRCACHE_M; j++)
  96. g->strcache[i][j] = g->memerrmsg;
  97. }
  98. /*
  99. ** creates a new string object
  100. */
  101. static TString *createstrobj (lua_State *L, size_t l, int tag, unsigned int h) {
  102. TString *ts;
  103. GCObject *o;
  104. size_t totalsize; /* total size of TString object */
  105. totalsize = sizelstring(l);
  106. o = luaC_newobj(L, tag, totalsize);
  107. ts = gco2ts(o);
  108. ts->hash = h;
  109. ts->extra = 0;
  110. getstr(ts)[l] = '\0'; /* ending 0 */
  111. return ts;
  112. }
  113. TString *luaS_createlngstrobj (lua_State *L, size_t l) {
  114. TString *ts = createstrobj(L, l, LUA_TLNGSTR, G(L)->seed);
  115. ts->u.lnglen = l;
  116. return ts;
  117. }
  118. void luaS_remove (lua_State *L, TString *ts) {
  119. stringtable *tb = &G(L)->strt;
  120. TString **p = &tb->hash[lmod(ts->hash, tb->size)];
  121. while (*p != ts) /* find previous element */
  122. p = &(*p)->u.hnext;
  123. *p = (*p)->u.hnext; /* remove element from its list */
  124. tb->nuse--;
  125. }
  126. /*
  127. ** checks whether short string exists and reuses it or creates a new one
  128. */
  129. static TString *internshrstr (lua_State *L, const char *str, size_t l) {
  130. TString *ts;
  131. global_State *g = G(L);
  132. unsigned int h = luaS_hash(str, l, g->seed);
  133. TString **list = &g->strt.hash[lmod(h, g->strt.size)];
  134. lua_assert(str != NULL); /* otherwise 'memcmp'/'memcpy' are undefined */
  135. for (ts = *list; ts != NULL; ts = ts->u.hnext) {
  136. if (l == ts->shrlen &&
  137. (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) {
  138. /* found! */
  139. if (isdead(g, ts)) /* dead (but not collected yet)? */
  140. changewhite(ts); /* resurrect it */
  141. return ts;
  142. }
  143. }
  144. if (g->strt.nuse >= g->strt.size && g->strt.size <= MAX_INT/2) {
  145. luaS_resize(L, g->strt.size * 2);
  146. list = &g->strt.hash[lmod(h, g->strt.size)]; /* recompute with new size */
  147. }
  148. ts = createstrobj(L, l, LUA_TSHRSTR, h);
  149. memcpy(getstr(ts), str, l * sizeof(char));
  150. ts->shrlen = cast_byte(l);
  151. ts->u.hnext = *list;
  152. *list = ts;
  153. g->strt.nuse++;
  154. return ts;
  155. }
  156. /*
  157. ** new string (with explicit length)
  158. */
  159. TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
  160. if (l <= LUAI_MAXSHORTLEN) /* short string? */
  161. return internshrstr(L, str, l);
  162. else {
  163. TString *ts;
  164. if (l >= (MAX_SIZE - sizeof(TString))/sizeof(char))
  165. luaM_toobig(L);
  166. ts = luaS_createlngstrobj(L, l);
  167. memcpy(getstr(ts), str, l * sizeof(char));
  168. return ts;
  169. }
  170. }
  171. /*
  172. ** Create or reuse a zero-terminated string, first checking in the
  173. ** cache (using the string address as a key). The cache can contain
  174. ** only zero-terminated strings, so it is safe to use 'strcmp' to
  175. ** check hits.
  176. */
  177. TString *luaS_new (lua_State *L, const char *str) {
  178. unsigned int i = point2uint(str) % STRCACHE_N; /* hash */
  179. int j;
  180. TString **p = G(L)->strcache[i];
  181. for (j = 0; j < STRCACHE_M; j++) {
  182. if (strcmp(str, getstr(p[j])) == 0) /* hit? */
  183. return p[j]; /* that is it */
  184. }
  185. /* normal route */
  186. for (j = STRCACHE_M - 1; j > 0; j--)
  187. p[j] = p[j - 1]; /* move out last element */
  188. /* new element is first in the list */
  189. p[0] = luaS_newlstr(L, str, strlen(str));
  190. return p[0];
  191. }
  192. Udata *luaS_newudata (lua_State *L, size_t s) {
  193. Udata *u;
  194. GCObject *o;
  195. if (s > MAX_SIZE - sizeof(Udata))
  196. luaM_toobig(L);
  197. o = luaC_newobj(L, LUA_TUSERDATA, sizeludata(s));
  198. u = gco2u(o);
  199. u->len = s;
  200. u->metatable = NULL;
  201. setuservalue(L, u, luaO_nilobject);
  202. return u;
  203. }