lstring.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. ** $Id: lstring.c,v 2.44 2014/07/21 16:02:10 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. /*
  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->len;
  29. lua_assert(a->tt == LUA_TLNGSTR && b->tt == LUA_TLNGSTR);
  30. return (a == b) || /* same instance or... */
  31. ((len == b->len) && /* equal length and ... */
  32. (memcmp(getstr(a), getstr(b), len) == 0)); /* equal contents */
  33. }
  34. unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) {
  35. unsigned int h = seed ^ cast(unsigned int, l);
  36. size_t l1;
  37. size_t step = (l >> LUAI_HASHLIMIT) + 1;
  38. for (l1 = l; l1 >= step; l1 -= step)
  39. h = h ^ ((h<<5) + (h>>2) + cast_byte(str[l1 - 1]));
  40. return h;
  41. }
  42. /*
  43. ** resizes the string table
  44. */
  45. void luaS_resize (lua_State *L, int newsize) {
  46. int i;
  47. stringtable *tb = &G(L)->strt;
  48. if (newsize > tb->size) { /* grow table if needed */
  49. luaM_reallocvector(L, tb->hash, tb->size, newsize, TString *);
  50. for (i = tb->size; i < newsize; i++)
  51. tb->hash[i] = NULL;
  52. }
  53. for (i = 0; i < tb->size; i++) { /* rehash */
  54. TString *p = tb->hash[i];
  55. tb->hash[i] = NULL;
  56. while (p) { /* for each node in the list */
  57. TString *hnext = p->hnext; /* save next */
  58. unsigned int h = lmod(p->hash, newsize); /* new position */
  59. p->hnext = tb->hash[h]; /* chain it */
  60. tb->hash[h] = p;
  61. p = hnext;
  62. }
  63. }
  64. if (newsize < tb->size) { /* shrink table if needed */
  65. /* vanishing slice should be empty */
  66. lua_assert(tb->hash[newsize] == NULL && tb->hash[tb->size - 1] == NULL);
  67. luaM_reallocvector(L, tb->hash, tb->size, newsize, TString *);
  68. }
  69. tb->size = newsize;
  70. }
  71. /*
  72. ** creates a new string object
  73. */
  74. static TString *createstrobj (lua_State *L, const char *str, size_t l,
  75. int tag, unsigned int h) {
  76. TString *ts;
  77. GCObject *o;
  78. size_t totalsize; /* total size of TString object */
  79. totalsize = sizelstring(l);
  80. o = luaC_newobj(L, tag, totalsize);
  81. ts = gco2ts(o);
  82. ts->len = l;
  83. ts->hash = h;
  84. ts->extra = 0;
  85. memcpy(getaddrstr(ts), str, l * sizeof(char));
  86. getaddrstr(ts)[l] = '\0'; /* ending 0 */
  87. return ts;
  88. }
  89. void luaS_remove (lua_State *L, TString *ts) {
  90. stringtable *tb = &G(L)->strt;
  91. TString **p = &tb->hash[lmod(ts->hash, tb->size)];
  92. while (*p != ts) /* find previous element */
  93. p = &(*p)->hnext;
  94. *p = (*p)->hnext; /* remove element from its list */
  95. tb->nuse--;
  96. }
  97. /*
  98. ** checks whether short string exists and reuses it or creates a new one
  99. */
  100. static TString *internshrstr (lua_State *L, const char *str, size_t l) {
  101. TString *ts;
  102. global_State *g = G(L);
  103. unsigned int h = luaS_hash(str, l, g->seed);
  104. TString **list = &g->strt.hash[lmod(h, g->strt.size)];
  105. for (ts = *list; ts != NULL; ts = ts->hnext) {
  106. if (l == ts->len &&
  107. (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) {
  108. /* found! */
  109. if (isdead(g, ts)) /* dead (but not collected yet)? */
  110. changewhite(ts); /* resurrect it */
  111. return ts;
  112. }
  113. }
  114. if (g->strt.nuse >= g->strt.size && g->strt.size <= MAX_INT/2) {
  115. luaS_resize(L, g->strt.size * 2);
  116. list = &g->strt.hash[lmod(h, g->strt.size)]; /* recompute with new size */
  117. }
  118. ts = createstrobj(L, str, l, LUA_TSHRSTR, h);
  119. ts->hnext = *list;
  120. *list = ts;
  121. g->strt.nuse++;
  122. return ts;
  123. }
  124. /*
  125. ** new string (with explicit length)
  126. */
  127. TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
  128. if (l <= LUAI_MAXSHORTLEN) /* short string? */
  129. return internshrstr(L, str, l);
  130. else {
  131. if (l + 1 > (MAX_SIZE - sizeof(TString))/sizeof(char))
  132. luaM_toobig(L);
  133. return createstrobj(L, str, l, LUA_TLNGSTR, G(L)->seed);
  134. }
  135. }
  136. /*
  137. ** new zero-terminated string
  138. */
  139. TString *luaS_new (lua_State *L, const char *str) {
  140. return luaS_newlstr(L, str, strlen(str));
  141. }
  142. Udata *luaS_newudata (lua_State *L, size_t s) {
  143. Udata *u;
  144. GCObject *o;
  145. if (s > MAX_SIZE - sizeof(Udata))
  146. luaM_toobig(L);
  147. o = luaC_newobj(L, LUA_TUSERDATA, sizeludata(s));
  148. u = gco2u(o);
  149. u->len = s;
  150. u->metatable = NULL;
  151. setuservalue(L, u, luaO_nilobject);
  152. return u;
  153. }