lstring.c 4.4 KB

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