lstring.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. ** $Id: lstring.c,v 2.48 2015/03/25 13:42:19 roberto Exp roberto $
  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. #define MEMERRMSG "not enough memory"
  18. /*
  19. ** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to
  20. ** compute its hash
  21. */
  22. #if !defined(LUAI_HASHLIMIT)
  23. #define LUAI_HASHLIMIT 5
  24. #endif
  25. /*
  26. ** equality for long strings
  27. */
  28. int luaS_eqlngstr (TString *a, TString *b) {
  29. size_t len = a->u.lnglen;
  30. lua_assert(a->tt == LUA_TLNGSTR && b->tt == LUA_TLNGSTR);
  31. return (a == b) || /* same instance or... */
  32. ((len == b->u.lnglen) && /* equal length and ... */
  33. (memcmp(getstr(a), getstr(b), len) == 0)); /* equal contents */
  34. }
  35. unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) {
  36. unsigned int h = seed ^ cast(unsigned int, l);
  37. size_t l1;
  38. size_t step = (l >> LUAI_HASHLIMIT) + 1;
  39. for (l1 = l; l1 >= step; l1 -= step)
  40. h = h ^ ((h<<5) + (h>>2) + cast_byte(str[l1 - 1]));
  41. return h;
  42. }
  43. /*
  44. ** resizes the string table
  45. */
  46. void luaS_resize (lua_State *L, int newsize) {
  47. int i;
  48. stringtable *tb = &G(L)->strt;
  49. if (newsize > tb->size) { /* grow table if needed */
  50. luaM_reallocvector(L, tb->hash, tb->size, newsize, TString *);
  51. for (i = tb->size; i < newsize; i++)
  52. tb->hash[i] = NULL;
  53. }
  54. for (i = 0; i < tb->size; i++) { /* rehash */
  55. TString *p = tb->hash[i];
  56. tb->hash[i] = NULL;
  57. while (p) { /* for each node in the list */
  58. TString *hnext = p->u.hnext; /* save next */
  59. unsigned int h = lmod(p->hash, newsize); /* new position */
  60. p->u.hnext = tb->hash[h]; /* chain it */
  61. tb->hash[h] = p;
  62. p = hnext;
  63. }
  64. }
  65. if (newsize < tb->size) { /* shrink table if needed */
  66. /* vanishing slice should be empty */
  67. lua_assert(tb->hash[newsize] == NULL && tb->hash[tb->size - 1] == NULL);
  68. luaM_reallocvector(L, tb->hash, tb->size, newsize, TString *);
  69. }
  70. tb->size = newsize;
  71. }
  72. /*
  73. ** Clear API string cache. (Entries cannot be empty, so fill them with
  74. ** a non-collectable string.)
  75. */
  76. void luaS_clearcache (global_State *g) {
  77. int i;
  78. for (i = 0; i < STRCACHE_SIZE; i++) {
  79. if (iswhite(g->strcache[i][0])) /* will entry be collected? */
  80. g->strcache[i][0] = g->memerrmsg; /* replace it with something fixed */
  81. }
  82. }
  83. /*
  84. ** Initialize the string table and the string cache
  85. */
  86. void luaS_init (lua_State *L) {
  87. global_State *g = G(L);
  88. int i;
  89. luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */
  90. /* pre-create memory-error message */
  91. g->memerrmsg = luaS_newliteral(L, MEMERRMSG);
  92. luaC_fix(L, obj2gco(g->memerrmsg)); /* it should never be collected */
  93. for (i = 0; i < STRCACHE_SIZE; i++) /* fill cache with valid strings */
  94. g->strcache[i][0] = g->memerrmsg;
  95. }
  96. /*
  97. ** creates a new string object
  98. */
  99. static TString *createstrobj (lua_State *L, const char *str, size_t l,
  100. int tag, unsigned int h) {
  101. TString *ts;
  102. GCObject *o;
  103. size_t totalsize; /* total size of TString object */
  104. totalsize = sizelstring(l);
  105. o = luaC_newobj(L, tag, totalsize);
  106. ts = gco2ts(o);
  107. ts->hash = h;
  108. ts->extra = 0;
  109. memcpy(getaddrstr(ts), str, l * sizeof(char));
  110. getaddrstr(ts)[l] = '\0'; /* ending 0 */
  111. return ts;
  112. }
  113. void luaS_remove (lua_State *L, TString *ts) {
  114. stringtable *tb = &G(L)->strt;
  115. TString **p = &tb->hash[lmod(ts->hash, tb->size)];
  116. while (*p != ts) /* find previous element */
  117. p = &(*p)->u.hnext;
  118. *p = (*p)->u.hnext; /* remove element from its list */
  119. tb->nuse--;
  120. }
  121. /*
  122. ** checks whether short string exists and reuses it or creates a new one
  123. */
  124. static TString *internshrstr (lua_State *L, const char *str, size_t l) {
  125. TString *ts;
  126. global_State *g = G(L);
  127. unsigned int h = luaS_hash(str, l, g->seed);
  128. TString **list = &g->strt.hash[lmod(h, g->strt.size)];
  129. for (ts = *list; ts != NULL; ts = ts->u.hnext) {
  130. if (l == ts->shrlen &&
  131. (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) {
  132. /* found! */
  133. if (isdead(g, ts)) /* dead (but not collected yet)? */
  134. changewhite(ts); /* resurrect it */
  135. return ts;
  136. }
  137. }
  138. if (g->strt.nuse >= g->strt.size && g->strt.size <= MAX_INT/2) {
  139. luaS_resize(L, g->strt.size * 2);
  140. list = &g->strt.hash[lmod(h, g->strt.size)]; /* recompute with new size */
  141. }
  142. ts = createstrobj(L, str, l, LUA_TSHRSTR, h);
  143. ts->shrlen = cast_byte(l);
  144. ts->u.hnext = *list;
  145. *list = ts;
  146. g->strt.nuse++;
  147. return ts;
  148. }
  149. /*
  150. ** new string (with explicit length)
  151. */
  152. TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
  153. if (l <= LUAI_MAXSHORTLEN) /* short string? */
  154. return internshrstr(L, str, l);
  155. else {
  156. TString *ts;
  157. if (l + 1 > (MAX_SIZE - sizeof(TString))/sizeof(char))
  158. luaM_toobig(L);
  159. ts = createstrobj(L, str, l, LUA_TLNGSTR, G(L)->seed);
  160. ts->u.lnglen = l;
  161. return ts;
  162. }
  163. }
  164. /*
  165. ** Create or reuse a zero-terminated string, first checking in the
  166. ** cache (using the string address as a key). The cache can contain
  167. ** only zero-terminated strings, so it is safe to use 'strcmp' to
  168. ** check hits.
  169. */
  170. TString *luaS_new (lua_State *L, const char *str) {
  171. unsigned int i = point2uint(str) % STRCACHE_SIZE; /* hash */
  172. TString **p = G(L)->strcache[i];
  173. if (strcmp(str, getstr(p[0])) == 0) /* hit? */
  174. return p[0]; /* that it is */
  175. else { /* normal route */
  176. TString *s = luaS_newlstr(L, str, strlen(str));
  177. p[0] = s;
  178. return s;
  179. }
  180. }
  181. Udata *luaS_newudata (lua_State *L, size_t s) {
  182. Udata *u;
  183. GCObject *o;
  184. if (s > MAX_SIZE - sizeof(Udata))
  185. luaM_toobig(L);
  186. o = luaC_newobj(L, LUA_TUSERDATA, sizeludata(s));
  187. u = gco2u(o);
  188. u->len = s;
  189. u->metatable = NULL;
  190. setuservalue(L, u, luaO_nilobject);
  191. return u;
  192. }