lstring.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. ** $Id: lstring.c,v 2.22 2012/02/01 21:57:15 roberto Exp roberto $
  3. ** String table (keeps all strings handled by Lua)
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <string.h>
  7. #define lstring_c
  8. #define LUA_CORE
  9. #include "lua.h"
  10. #include "lmem.h"
  11. #include "lobject.h"
  12. #include "lstate.h"
  13. #include "lstring.h"
  14. /*
  15. ** maximum length for short strings, that is, strings that are
  16. ** internalized. (Cannot be smaller than reserved words or tags
  17. ** for metamethods, as these strings must be internalized;
  18. ** #("function") = 8, #("__newindex") = 10.)
  19. */
  20. #if !defined(LUAI_MAXSHORTLEN)
  21. #define LUAI_MAXSHORTLEN 40
  22. #endif
  23. /*
  24. ** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to
  25. ** compute its hash
  26. */
  27. #if !defined(LUAI_HASHLIMIT)
  28. #define LUAI_HASHLIMIT 5
  29. #endif
  30. /*
  31. ** equality for long strings
  32. */
  33. int luaS_eqlngstr (TString *a, TString *b) {
  34. size_t len = a->tsv.len;
  35. lua_assert(a->tsv.tt == LUA_TLNGSTR && b->tsv.tt == LUA_TLNGSTR);
  36. return (a == b) || /* same instance or... */
  37. ((len == b->tsv.len) && /* equal length and ... */
  38. (memcmp(getstr(a), getstr(b), len) == 0)); /* equal contents */
  39. }
  40. /*
  41. ** equality for strings
  42. */
  43. int luaS_eqstr (TString *a, TString *b) {
  44. return (a->tsv.tt == b->tsv.tt) &&
  45. (a->tsv.tt == LUA_TSHRSTR ? eqshrstr(a, b) : luaS_eqlngstr(a, b));
  46. }
  47. unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) {
  48. unsigned int h = seed ^ l;
  49. size_t l1;
  50. size_t step = (l >> LUAI_HASHLIMIT) + 1;
  51. for (l1 = l; l1 >= step; l1 -= step)
  52. h = h ^ ((h<<5) + (h>>2) + cast_byte(str[l1 - 1]));
  53. return h;
  54. }
  55. /*
  56. ** resizes the string table
  57. */
  58. void luaS_resize (lua_State *L, int newsize) {
  59. int i;
  60. stringtable *tb = &G(L)->strt;
  61. /* cannot resize while GC is traversing strings */
  62. luaC_runtilstate(L, ~bitmask(GCSsweepstring));
  63. if (newsize > tb->size) {
  64. luaM_reallocvector(L, tb->hash, tb->size, newsize, GCObject *);
  65. for (i = tb->size; i < newsize; i++) tb->hash[i] = NULL;
  66. }
  67. /* rehash */
  68. for (i=0; i<tb->size; i++) {
  69. GCObject *p = tb->hash[i];
  70. tb->hash[i] = NULL;
  71. while (p) { /* for each node in the list */
  72. GCObject *next = gch(p)->next; /* save next */
  73. unsigned int h = lmod(gco2ts(p)->hash, newsize); /* new position */
  74. gch(p)->next = tb->hash[h]; /* chain it */
  75. tb->hash[h] = p;
  76. resetoldbit(p); /* see MOVE OLD rule */
  77. p = next;
  78. }
  79. }
  80. if (newsize < tb->size) {
  81. /* shrinking slice must be empty */
  82. lua_assert(tb->hash[newsize] == NULL && tb->hash[tb->size - 1] == NULL);
  83. luaM_reallocvector(L, tb->hash, tb->size, newsize, GCObject *);
  84. }
  85. tb->size = newsize;
  86. }
  87. /*
  88. ** creates a new string object
  89. */
  90. static TString *createstrobj (lua_State *L, const char *str, size_t l,
  91. int tag, unsigned int h, GCObject **list) {
  92. TString *ts;
  93. size_t totalsize; /* total size of TString object */
  94. totalsize = sizeof(TString) + ((l + 1) * sizeof(char));
  95. ts = &luaC_newobj(L, tag, totalsize, list, 0)->ts;
  96. ts->tsv.len = l;
  97. ts->tsv.hash = h;
  98. ts->tsv.extra = 0;
  99. memcpy(ts+1, str, l*sizeof(char));
  100. ((char *)(ts+1))[l] = '\0'; /* ending 0 */
  101. return ts;
  102. }
  103. /*
  104. ** creates a new short string, inserting it into string table
  105. */
  106. static TString *newshrstr (lua_State *L, const char *str, size_t l,
  107. unsigned int h) {
  108. GCObject **list; /* (pointer to) list where it will be inserted */
  109. stringtable *tb = &G(L)->strt;
  110. TString *s;
  111. if (tb->nuse >= cast(lu_int32, tb->size) && tb->size <= MAX_INT/2)
  112. luaS_resize(L, tb->size*2); /* too crowded */
  113. list = &tb->hash[lmod(h, tb->size)];
  114. s = createstrobj(L, str, l, LUA_TSHRSTR, h, list);
  115. tb->nuse++;
  116. return s;
  117. }
  118. /*
  119. ** checks whether short string exists and reuses it or creates a new one
  120. */
  121. static TString *internshrstr (lua_State *L, const char *str, size_t l) {
  122. GCObject *o;
  123. global_State *g = G(L);
  124. unsigned int h = luaS_hash(str, l, g->seed);
  125. for (o = g->strt.hash[lmod(h, g->strt.size)];
  126. o != NULL;
  127. o = gch(o)->next) {
  128. TString *ts = rawgco2ts(o);
  129. if (h == ts->tsv.hash &&
  130. ts->tsv.len == l &&
  131. (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) {
  132. if (isdead(G(L), o)) /* string is dead (but was not collected yet)? */
  133. changewhite(o); /* resurrect it */
  134. return ts;
  135. }
  136. }
  137. return newshrstr(L, str, l, h); /* not found; create a new string */
  138. }
  139. /*
  140. ** new string (with explicit length)
  141. */
  142. TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
  143. if (l <= LUAI_MAXSHORTLEN) /* short string? */
  144. return internshrstr(L, str, l);
  145. else {
  146. if (l + 1 > (MAX_SIZET - sizeof(TString))/sizeof(char))
  147. luaM_toobig(L);
  148. return createstrobj(L, str, l, LUA_TLNGSTR, G(L)->seed, NULL);
  149. }
  150. }
  151. /*
  152. ** new zero-terminated string
  153. */
  154. TString *luaS_new (lua_State *L, const char *str) {
  155. return luaS_newlstr(L, str, strlen(str));
  156. }
  157. Udata *luaS_newudata (lua_State *L, size_t s, Table *e) {
  158. Udata *u;
  159. if (s > MAX_SIZET - sizeof(Udata))
  160. luaM_toobig(L);
  161. u = &luaC_newobj(L, LUA_TUSERDATA, sizeof(Udata) + s, NULL, 0)->u;
  162. u->uv.len = s;
  163. u->uv.metatable = NULL;
  164. u->uv.env = e;
  165. return u;
  166. }