lstring.c 6.5 KB

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