You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

252 lines
6.5 KiB

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