2
0

lstring.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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(INT_MAX, TString*))
  21. /*
  22. ** Initial size for the string table (must be power of 2).
  23. ** The Lua core alone registers ~50 strings (reserved words +
  24. ** metaevent keys + a few others). Libraries would typically add
  25. ** a few dozens more.
  26. */
  27. #if !defined(MINSTRTABSIZE)
  28. #define MINSTRTABSIZE 128
  29. #endif
  30. /*
  31. ** generic equality for strings
  32. */
  33. int luaS_eqstr (TString *a, TString *b) {
  34. size_t len1, len2;
  35. const char *s1 = getlstr(a, len1);
  36. const char *s2 = getlstr(b, len2);
  37. return ((len1 == len2) && /* equal length and ... */
  38. (memcmp(s1, s2, len1) == 0)); /* equal contents */
  39. }
  40. unsigned luaS_hash (const char *str, size_t l, unsigned seed) {
  41. unsigned int h = seed ^ cast_uint(l);
  42. for (; l > 0; l--)
  43. h ^= ((h<<5) + (h>>2) + cast_byte(str[l - 1]));
  44. return h;
  45. }
  46. unsigned luaS_hashlongstr (TString *ts) {
  47. lua_assert(ts->tt == LUA_VLNGSTR);
  48. if (ts->extra == 0) { /* no hash? */
  49. size_t len = ts->u.lnglen;
  50. ts->hash = luaS_hash(getlngstr(ts), len, ts->hash);
  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 (l_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. size_t luaS_sizelngstr (size_t len, int kind) {
  125. switch (kind) {
  126. case LSTRREG: /* regular long string */
  127. /* don't need 'falloc'/'ud', but need space for content */
  128. return offsetof(TString, falloc) + (len + 1) * sizeof(char);
  129. case LSTRFIX: /* fixed external long string */
  130. /* don't need 'falloc'/'ud' */
  131. return offsetof(TString, falloc);
  132. default: /* external long string with deallocation */
  133. lua_assert(kind == LSTRMEM);
  134. return sizeof(TString);
  135. }
  136. }
  137. /*
  138. ** creates a new string object
  139. */
  140. static TString *createstrobj (lua_State *L, size_t totalsize, lu_byte tag,
  141. unsigned h) {
  142. TString *ts;
  143. GCObject *o;
  144. o = luaC_newobj(L, tag, totalsize);
  145. ts = gco2ts(o);
  146. ts->hash = h;
  147. ts->extra = 0;
  148. return ts;
  149. }
  150. TString *luaS_createlngstrobj (lua_State *L, size_t l) {
  151. size_t totalsize = luaS_sizelngstr(l, LSTRREG);
  152. TString *ts = createstrobj(L, totalsize, LUA_VLNGSTR, G(L)->seed);
  153. ts->u.lnglen = l;
  154. ts->shrlen = LSTRREG; /* signals that it is a regular long string */
  155. ts->contents = cast_charp(ts) + offsetof(TString, falloc);
  156. ts->contents[l] = '\0'; /* ending 0 */
  157. return ts;
  158. }
  159. void luaS_remove (lua_State *L, TString *ts) {
  160. stringtable *tb = &G(L)->strt;
  161. TString **p = &tb->hash[lmod(ts->hash, tb->size)];
  162. while (*p != ts) /* find previous element */
  163. p = &(*p)->u.hnext;
  164. *p = (*p)->u.hnext; /* remove element from its list */
  165. tb->nuse--;
  166. }
  167. static void growstrtab (lua_State *L, stringtable *tb) {
  168. if (l_unlikely(tb->nuse == INT_MAX)) { /* too many strings? */
  169. luaC_fullgc(L, 1); /* try to free some... */
  170. if (tb->nuse == INT_MAX) /* still too many? */
  171. luaM_error(L); /* cannot even create a message... */
  172. }
  173. if (tb->size <= MAXSTRTB / 2) /* can grow string table? */
  174. luaS_resize(L, tb->size * 2);
  175. }
  176. /*
  177. ** Checks whether short string exists and reuses it or creates a new one.
  178. */
  179. static TString *internshrstr (lua_State *L, const char *str, size_t l) {
  180. TString *ts;
  181. global_State *g = G(L);
  182. stringtable *tb = &g->strt;
  183. unsigned int h = luaS_hash(str, l, g->seed);
  184. TString **list = &tb->hash[lmod(h, tb->size)];
  185. lua_assert(str != NULL); /* otherwise 'memcmp'/'memcpy' are undefined */
  186. for (ts = *list; ts != NULL; ts = ts->u.hnext) {
  187. if (l == cast_uint(ts->shrlen) &&
  188. (memcmp(str, getshrstr(ts), l * sizeof(char)) == 0)) {
  189. /* found! */
  190. if (isdead(g, ts)) /* dead (but not collected yet)? */
  191. changewhite(ts); /* resurrect it */
  192. return ts;
  193. }
  194. }
  195. /* else must create a new string */
  196. if (tb->nuse >= tb->size) { /* need to grow string table? */
  197. growstrtab(L, tb);
  198. list = &tb->hash[lmod(h, tb->size)]; /* rehash with new size */
  199. }
  200. ts = createstrobj(L, sizestrshr(l), LUA_VSHRSTR, h);
  201. ts->shrlen = cast(ls_byte, l);
  202. getshrstr(ts)[l] = '\0'; /* ending 0 */
  203. memcpy(getshrstr(ts), str, l * sizeof(char));
  204. ts->u.hnext = *list;
  205. *list = ts;
  206. tb->nuse++;
  207. return ts;
  208. }
  209. /*
  210. ** new string (with explicit length)
  211. */
  212. TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
  213. if (l <= LUAI_MAXSHORTLEN) /* short string? */
  214. return internshrstr(L, str, l);
  215. else {
  216. TString *ts;
  217. if (l_unlikely(l * sizeof(char) >= (MAX_SIZE - sizeof(TString))))
  218. luaM_toobig(L);
  219. ts = luaS_createlngstrobj(L, l);
  220. memcpy(getlngstr(ts), str, l * sizeof(char));
  221. return ts;
  222. }
  223. }
  224. /*
  225. ** Create or reuse a zero-terminated string, first checking in the
  226. ** cache (using the string address as a key). The cache can contain
  227. ** only zero-terminated strings, so it is safe to use 'strcmp' to
  228. ** check hits.
  229. */
  230. TString *luaS_new (lua_State *L, const char *str) {
  231. unsigned int i = point2uint(str) % STRCACHE_N; /* hash */
  232. int j;
  233. TString **p = G(L)->strcache[i];
  234. for (j = 0; j < STRCACHE_M; j++) {
  235. if (strcmp(str, getstr(p[j])) == 0) /* hit? */
  236. return p[j]; /* that is it */
  237. }
  238. /* normal route */
  239. for (j = STRCACHE_M - 1; j > 0; j--)
  240. p[j] = p[j - 1]; /* move out last element */
  241. /* new element is first in the list */
  242. p[0] = luaS_newlstr(L, str, strlen(str));
  243. return p[0];
  244. }
  245. Udata *luaS_newudata (lua_State *L, size_t s, unsigned short nuvalue) {
  246. Udata *u;
  247. int i;
  248. GCObject *o;
  249. if (l_unlikely(s > MAX_SIZE - udatamemoffset(nuvalue)))
  250. luaM_toobig(L);
  251. o = luaC_newobj(L, LUA_VUSERDATA, sizeudata(nuvalue, s));
  252. u = gco2u(o);
  253. u->len = s;
  254. u->nuvalue = nuvalue;
  255. u->metatable = NULL;
  256. for (i = 0; i < nuvalue; i++)
  257. setnilvalue(&u->uv[i].uv);
  258. return u;
  259. }
  260. struct NewExt {
  261. ls_byte kind;
  262. const char *s;
  263. size_t len;
  264. TString *ts; /* output */
  265. };
  266. static void f_newext (lua_State *L, void *ud) {
  267. struct NewExt *ne = cast(struct NewExt *, ud);
  268. size_t size = luaS_sizelngstr(0, ne->kind);
  269. ne->ts = createstrobj(L, size, LUA_VLNGSTR, G(L)->seed);
  270. }
  271. TString *luaS_newextlstr (lua_State *L,
  272. const char *s, size_t len, lua_Alloc falloc, void *ud) {
  273. struct NewExt ne;
  274. if (!falloc) {
  275. ne.kind = LSTRFIX;
  276. f_newext(L, &ne); /* just create header */
  277. }
  278. else {
  279. ne.kind = LSTRMEM;
  280. if (luaD_rawrunprotected(L, f_newext, &ne) != LUA_OK) { /* mem. error? */
  281. (*falloc)(ud, cast_voidp(s), len + 1, 0); /* free external string */
  282. luaM_error(L); /* re-raise memory error */
  283. }
  284. ne.ts->falloc = falloc;
  285. ne.ts->ud = ud;
  286. }
  287. ne.ts->shrlen = ne.kind;
  288. ne.ts->u.lnglen = len;
  289. ne.ts->contents = cast_charp(s);
  290. return ne.ts;
  291. }
  292. /*
  293. ** Normalize an external string: If it is short, internalize it.
  294. */
  295. TString *luaS_normstr (lua_State *L, TString *ts) {
  296. size_t len = ts->u.lnglen;
  297. if (len > LUAI_MAXSHORTLEN)
  298. return ts; /* long string; keep the original */
  299. else {
  300. const char *str = getlngstr(ts);
  301. return internshrstr(L, str, len);
  302. }
  303. }