lstring.c 6.9 KB

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