lstring.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. ** $Id: lstring.c,v 2.58 2017/12/01 16:40:29 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. ** Resizes the string table.
  55. */
  56. void luaS_resize (lua_State *L, int newsize) {
  57. int i;
  58. TString **newhash = luaM_newvector(L, newsize, TString *);
  59. stringtable *tb = &G(L)->strt;
  60. for (i = 0; i < newsize; i++) /* initialize new hash array */
  61. newhash[i] = NULL;
  62. for (i = 0; i < tb->size; i++) { /* rehash all elements into new array */
  63. TString *p = tb->hash[i];
  64. while (p) { /* for each string in the list */
  65. TString *hnext = p->u.hnext; /* save next */
  66. unsigned int h = lmod(p->hash, newsize); /* new position */
  67. p->u.hnext = newhash[h]; /* chain it into new array */
  68. newhash[h] = p;
  69. p = hnext;
  70. }
  71. }
  72. luaM_freearray(L, tb->hash, tb->size); /* free old array */
  73. tb->hash = newhash;
  74. tb->size = newsize;
  75. }
  76. /*
  77. ** Clear API string cache. (Entries cannot be empty, so fill them with
  78. ** a non-collectable string.)
  79. */
  80. void luaS_clearcache (global_State *g) {
  81. int i, j;
  82. for (i = 0; i < STRCACHE_N; i++)
  83. for (j = 0; j < STRCACHE_M; j++) {
  84. if (iswhite(g->strcache[i][j])) /* will entry be collected? */
  85. g->strcache[i][j] = g->nfield; /* replace it with something fixed */
  86. }
  87. }
  88. /*
  89. ** Initialize the string table and the string cache
  90. */
  91. void luaS_init (lua_State *L) {
  92. global_State *g = G(L);
  93. int i, j;
  94. TString *memerrmsg;
  95. luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */
  96. /* pre-create memory-error message */
  97. memerrmsg = luaS_newliteral(L, MEMERRMSG);
  98. luaC_fix(L, obj2gco(memerrmsg)); /* it should never be collected */
  99. g->nfield = luaS_newliteral(L, "n"); /* pre-create "n" field name */
  100. luaC_fix(L, obj2gco(g->nfield)); /* it also should never be collected */
  101. for (i = 0; i < STRCACHE_N; i++) /* fill cache with valid strings */
  102. for (j = 0; j < STRCACHE_M; j++)
  103. g->strcache[i][j] = g->nfield;
  104. }
  105. /*
  106. ** creates a new string object
  107. */
  108. static TString *createstrobj (lua_State *L, size_t l, int tag, unsigned int h) {
  109. TString *ts;
  110. GCObject *o;
  111. size_t totalsize; /* total size of TString object */
  112. totalsize = sizelstring(l);
  113. o = luaC_newobj(L, tag, totalsize);
  114. ts = gco2ts(o);
  115. ts->hash = h;
  116. ts->extra = 0;
  117. getstr(ts)[l] = '\0'; /* ending 0 */
  118. return ts;
  119. }
  120. TString *luaS_createlngstrobj (lua_State *L, size_t l) {
  121. TString *ts = createstrobj(L, l, LUA_TLNGSTR, G(L)->seed);
  122. ts->u.lnglen = l;
  123. return ts;
  124. }
  125. void luaS_remove (lua_State *L, TString *ts) {
  126. stringtable *tb = &G(L)->strt;
  127. TString **p = &tb->hash[lmod(ts->hash, tb->size)];
  128. while (*p != ts) /* find previous element */
  129. p = &(*p)->u.hnext;
  130. *p = (*p)->u.hnext; /* remove element from its list */
  131. tb->nuse--;
  132. }
  133. /*
  134. ** checks whether short string exists and reuses it or creates a new one
  135. */
  136. static TString *internshrstr (lua_State *L, const char *str, size_t l) {
  137. TString *ts;
  138. global_State *g = G(L);
  139. unsigned int h = luaS_hash(str, l, g->seed);
  140. TString **list = &g->strt.hash[lmod(h, g->strt.size)];
  141. lua_assert(str != NULL); /* otherwise 'memcmp'/'memcpy' are undefined */
  142. for (ts = *list; ts != NULL; ts = ts->u.hnext) {
  143. if (l == ts->shrlen &&
  144. (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) {
  145. /* found! */
  146. if (isdead(g, ts)) /* dead (but not collected yet)? */
  147. changewhite(ts); /* resurrect it */
  148. return ts;
  149. }
  150. }
  151. if (g->strt.nuse >= g->strt.size &&
  152. g->strt.size <= MAXSTRTB / 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. }