lstring.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. ** $Id: lstring.c,v 2.59 2017/12/07 18:59:52 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. ** Maximum size for string table.
  26. */
  27. #define MAXSTRTB cast_int(luaM_limitN(MAX_INT, TString*))
  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. ** Resize the string table. If allocation fails, keep the current size.
  55. ** (This can degrade performance, but any size should work correctly.)
  56. */
  57. void luaS_resize (lua_State *L, int newsize) {
  58. int i;
  59. TString **newhash = luaM_newvector(L, newsize, TString *);
  60. stringtable *tb = &G(L)->strt;
  61. if (newhash == NULL) /* allocation failed? */
  62. return; /* leave hash as it is */
  63. for (i = 0; i < newsize; i++) /* initialize new hash array */
  64. newhash[i] = NULL;
  65. for (i = 0; i < tb->size; i++) { /* rehash all elements into new array */
  66. TString *p = tb->hash[i];
  67. while (p) { /* for each string 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 = newhash[h]; /* chain it into new array */
  71. newhash[h] = p;
  72. p = hnext;
  73. }
  74. }
  75. luaM_freearray(L, tb->hash, tb->size); /* free old array */
  76. tb->hash = newhash;
  77. tb->size = newsize;
  78. }
  79. /*
  80. ** Clear API string cache. (Entries cannot be empty, so fill them with
  81. ** a non-collectable string.)
  82. */
  83. void luaS_clearcache (global_State *g) {
  84. int i, j;
  85. for (i = 0; i < STRCACHE_N; i++)
  86. for (j = 0; j < STRCACHE_M; j++) {
  87. if (iswhite(g->strcache[i][j])) /* will entry be collected? */
  88. g->strcache[i][j] = g->nfield; /* replace it with something fixed */
  89. }
  90. }
  91. /*
  92. ** Initialize the string table and the string cache
  93. */
  94. void luaS_init (lua_State *L) {
  95. global_State *g = G(L);
  96. int i, j;
  97. TString *memerrmsg;
  98. luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */
  99. /* pre-create memory-error message */
  100. memerrmsg = luaS_newliteral(L, MEMERRMSG);
  101. luaC_fix(L, obj2gco(memerrmsg)); /* it should never be collected */
  102. g->nfield = luaS_newliteral(L, "n"); /* pre-create "n" field name */
  103. luaC_fix(L, obj2gco(g->nfield)); /* it also 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->nfield;
  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 &&
  155. g->strt.size <= MAXSTRTB / 2) {
  156. luaS_resize(L, g->strt.size * 2);
  157. list = &g->strt.hash[lmod(h, g->strt.size)]; /* recompute with new size */
  158. }
  159. ts = createstrobj(L, l, LUA_TSHRSTR, h);
  160. memcpy(getstr(ts), str, l * sizeof(char));
  161. ts->shrlen = cast_byte(l);
  162. ts->u.hnext = *list;
  163. *list = ts;
  164. g->strt.nuse++;
  165. return ts;
  166. }
  167. /*
  168. ** new string (with explicit length)
  169. */
  170. TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
  171. if (l <= LUAI_MAXSHORTLEN) /* short string? */
  172. return internshrstr(L, str, l);
  173. else {
  174. TString *ts;
  175. if (l >= (MAX_SIZE - sizeof(TString))/sizeof(char))
  176. luaM_toobig(L);
  177. ts = luaS_createlngstrobj(L, l);
  178. memcpy(getstr(ts), str, l * sizeof(char));
  179. return ts;
  180. }
  181. }
  182. /*
  183. ** Create or reuse a zero-terminated string, first checking in the
  184. ** cache (using the string address as a key). The cache can contain
  185. ** only zero-terminated strings, so it is safe to use 'strcmp' to
  186. ** check hits.
  187. */
  188. TString *luaS_new (lua_State *L, const char *str) {
  189. unsigned int i = point2uint(str) % STRCACHE_N; /* hash */
  190. int j;
  191. TString **p = G(L)->strcache[i];
  192. for (j = 0; j < STRCACHE_M; j++) {
  193. if (strcmp(str, getstr(p[j])) == 0) /* hit? */
  194. return p[j]; /* that is it */
  195. }
  196. /* normal route */
  197. for (j = STRCACHE_M - 1; j > 0; j--)
  198. p[j] = p[j - 1]; /* move out last element */
  199. /* new element is first in the list */
  200. p[0] = luaS_newlstr(L, str, strlen(str));
  201. return p[0];
  202. }
  203. Udata *luaS_newudata (lua_State *L, size_t s) {
  204. Udata *u;
  205. GCObject *o;
  206. if (s > MAX_SIZE - sizeof(Udata))
  207. luaM_toobig(L);
  208. o = luaC_newobj(L, LUA_TUSERDATA, sizeludata(s));
  209. u = gco2u(o);
  210. u->len = s;
  211. u->metatable = NULL;
  212. setuservalue(L, u, luaO_nilobject);
  213. return u;
  214. }