lstring.c 4.8 KB

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