lstring.c 7.5 KB

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