lstring.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. ** $Id: lstring.c $
  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 long 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_VLNGSTR && b->tt == LUA_VLNGSTR);
  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. size_t step) {
  40. unsigned int h = seed ^ cast_uint(l);
  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_VLNGSTR);
  47. if (ts->extra == 0) { /* no hash? */
  48. size_t len = ts->u.lnglen;
  49. size_t step = (len >> LUAI_HASHLIMIT) + 1;
  50. ts->hash = luaS_hash(getstr(ts), len, ts->hash, step);
  51. ts->extra = 1; /* now it has its hash */
  52. }
  53. return ts->hash;
  54. }
  55. static void tablerehash (TString **vect, int osize, int nsize) {
  56. int i;
  57. for (i = osize; i < nsize; i++) /* clear new elements */
  58. vect[i] = NULL;
  59. for (i = 0; i < osize; i++) { /* rehash old part of the array */
  60. TString *p = vect[i];
  61. vect[i] = NULL;
  62. while (p) { /* for each string in the list */
  63. TString *hnext = p->u.hnext; /* save next */
  64. unsigned int h = lmod(p->hash, nsize); /* new position */
  65. p->u.hnext = vect[h]; /* chain it into array */
  66. vect[h] = p;
  67. p = hnext;
  68. }
  69. }
  70. }
  71. /*
  72. ** Resize the string table. If allocation fails, keep the current size.
  73. ** (This can degrade performance, but any non-zero size should work
  74. ** correctly.)
  75. */
  76. void luaS_resize (lua_State *L, int nsize) {
  77. stringtable *tb = &G(L)->strt;
  78. int osize = tb->size;
  79. TString **newvect;
  80. if (nsize < osize) /* shrinking table? */
  81. tablerehash(tb->hash, osize, nsize); /* depopulate shrinking part */
  82. newvect = luaM_reallocvector(L, tb->hash, osize, nsize, TString*);
  83. if (unlikely(newvect == NULL)) { /* reallocation failed? */
  84. if (nsize < osize) /* was it shrinking table? */
  85. tablerehash(tb->hash, nsize, osize); /* restore to original size */
  86. /* leave table as it was */
  87. }
  88. else { /* allocation succeeded */
  89. tb->hash = newvect;
  90. tb->size = nsize;
  91. if (nsize > osize)
  92. tablerehash(newvect, osize, nsize); /* rehash for new size */
  93. }
  94. }
  95. /*
  96. ** Clear API string cache. (Entries cannot be empty, so fill them with
  97. ** a non-collectable string.)
  98. */
  99. void luaS_clearcache (global_State *g) {
  100. int i, j;
  101. for (i = 0; i < STRCACHE_N; i++)
  102. for (j = 0; j < STRCACHE_M; j++) {
  103. if (iswhite(g->strcache[i][j])) /* will entry be collected? */
  104. g->strcache[i][j] = g->memerrmsg; /* replace it with something fixed */
  105. }
  106. }
  107. /*
  108. ** Initialize the string table and the string cache
  109. */
  110. void luaS_init (lua_State *L) {
  111. global_State *g = G(L);
  112. int i, j;
  113. stringtable *tb = &G(L)->strt;
  114. tb->hash = luaM_newvector(L, MINSTRTABSIZE, TString*);
  115. tablerehash(tb->hash, 0, MINSTRTABSIZE); /* clear array */
  116. tb->size = MINSTRTABSIZE;
  117. /* pre-create memory-error message */
  118. g->memerrmsg = luaS_newliteral(L, MEMERRMSG);
  119. luaC_fix(L, obj2gco(g->memerrmsg)); /* it should never be collected */
  120. for (i = 0; i < STRCACHE_N; i++) /* fill cache with valid strings */
  121. for (j = 0; j < STRCACHE_M; j++)
  122. g->strcache[i][j] = g->memerrmsg;
  123. }
  124. /*
  125. ** creates a new string object
  126. */
  127. static TString *createstrobj (lua_State *L, size_t l, int tag, unsigned int h) {
  128. TString *ts;
  129. GCObject *o;
  130. size_t totalsize; /* total size of TString object */
  131. totalsize = sizelstring(l);
  132. o = luaC_newobj(L, tag, totalsize);
  133. ts = gco2ts(o);
  134. ts->hash = h;
  135. ts->extra = 0;
  136. getstr(ts)[l] = '\0'; /* ending 0 */
  137. return ts;
  138. }
  139. TString *luaS_createlngstrobj (lua_State *L, size_t l) {
  140. TString *ts = createstrobj(L, l, LUA_VLNGSTR, G(L)->seed);
  141. ts->u.lnglen = l;
  142. return ts;
  143. }
  144. void luaS_remove (lua_State *L, TString *ts) {
  145. stringtable *tb = &G(L)->strt;
  146. TString **p = &tb->hash[lmod(ts->hash, tb->size)];
  147. while (*p != ts) /* find previous element */
  148. p = &(*p)->u.hnext;
  149. *p = (*p)->u.hnext; /* remove element from its list */
  150. tb->nuse--;
  151. }
  152. static void growstrtab (lua_State *L, stringtable *tb) {
  153. if (unlikely(tb->nuse == MAX_INT)) { /* too many strings? */
  154. luaC_fullgc(L, 1); /* try to free some... */
  155. if (tb->nuse == MAX_INT) /* still too many? */
  156. luaM_error(L); /* cannot even create a message... */
  157. }
  158. if (tb->size <= MAXSTRTB / 2) /* can grow string table? */
  159. luaS_resize(L, tb->size * 2);
  160. }
  161. /*
  162. ** Checks whether short string exists and reuses it or creates a new one.
  163. */
  164. static TString *internshrstr (lua_State *L, const char *str, size_t l) {
  165. TString *ts;
  166. global_State *g = G(L);
  167. stringtable *tb = &g->strt;
  168. unsigned int h = luaS_hash(str, l, g->seed, 1);
  169. TString **list = &tb->hash[lmod(h, tb->size)];
  170. lua_assert(str != NULL); /* otherwise 'memcmp'/'memcpy' are undefined */
  171. for (ts = *list; ts != NULL; ts = ts->u.hnext) {
  172. if (l == ts->shrlen && (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) {
  173. /* found! */
  174. if (isdead(g, ts)) /* dead (but not collected yet)? */
  175. changewhite(ts); /* resurrect it */
  176. return ts;
  177. }
  178. }
  179. /* else must create a new string */
  180. if (tb->nuse >= tb->size) { /* need to grow string table? */
  181. growstrtab(L, tb);
  182. list = &tb->hash[lmod(h, tb->size)]; /* rehash with new size */
  183. }
  184. ts = createstrobj(L, l, LUA_VSHRSTR, h);
  185. memcpy(getstr(ts), str, l * sizeof(char));
  186. ts->shrlen = cast_byte(l);
  187. ts->u.hnext = *list;
  188. *list = ts;
  189. tb->nuse++;
  190. return ts;
  191. }
  192. /*
  193. ** new string (with explicit length)
  194. */
  195. TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
  196. if (l <= LUAI_MAXSHORTLEN) /* short string? */
  197. return internshrstr(L, str, l);
  198. else {
  199. TString *ts;
  200. if (unlikely(l >= (MAX_SIZE - sizeof(TString))/sizeof(char)))
  201. luaM_toobig(L);
  202. ts = luaS_createlngstrobj(L, l);
  203. memcpy(getstr(ts), str, l * sizeof(char));
  204. return ts;
  205. }
  206. }
  207. /*
  208. ** Create or reuse a zero-terminated string, first checking in the
  209. ** cache (using the string address as a key). The cache can contain
  210. ** only zero-terminated strings, so it is safe to use 'strcmp' to
  211. ** check hits.
  212. */
  213. TString *luaS_new (lua_State *L, const char *str) {
  214. unsigned int i = point2uint(str) % STRCACHE_N; /* hash */
  215. int j;
  216. TString **p = G(L)->strcache[i];
  217. for (j = 0; j < STRCACHE_M; j++) {
  218. if (strcmp(str, getstr(p[j])) == 0) /* hit? */
  219. return p[j]; /* that is it */
  220. }
  221. /* normal route */
  222. for (j = STRCACHE_M - 1; j > 0; j--)
  223. p[j] = p[j - 1]; /* move out last element */
  224. /* new element is first in the list */
  225. p[0] = luaS_newlstr(L, str, strlen(str));
  226. return p[0];
  227. }
  228. Udata *luaS_newudata (lua_State *L, size_t s, int nuvalue) {
  229. Udata *u;
  230. int i;
  231. GCObject *o;
  232. if (unlikely(s > MAX_SIZE - udatamemoffset(nuvalue)))
  233. luaM_toobig(L);
  234. o = luaC_newobj(L, LUA_VUSERDATA, sizeudata(nuvalue, s));
  235. u = gco2u(o);
  236. u->len = s;
  237. u->nuvalue = nuvalue;
  238. u->metatable = NULL;
  239. for (i = 0; i < nuvalue; i++)
  240. setnilvalue(&u->uv[i].uv);
  241. return u;
  242. }