lstring.c 8.2 KB

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