189 righe
4.8 KiB

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