lstring.c 6.4 KB

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