lstring.c 7.4 KB

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