lstring.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. ** $Id: lstring.c,v 2.38 2014/03/19 18:51:42 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->tsv.len;
  28. lua_assert(a->tsv.tt == LUA_TLNGSTR && b->tsv.tt == LUA_TLNGSTR);
  29. return (a == b) || /* same instance or... */
  30. ((len == b->tsv.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->tsv.hnext; /* save next */
  57. unsigned int h = lmod(p->tsv.hash, newsize); /* new position */
  58. p->tsv.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. size_t totalsize; /* total size of TString object */
  77. totalsize = sizeof(TString) + ((l + 1) * sizeof(char));
  78. ts = &luaC_newobj(L, tag, totalsize)->ts;
  79. ts->tsv.len = l;
  80. ts->tsv.hash = h;
  81. ts->tsv.extra = 0;
  82. memcpy(ts+1, str, l*sizeof(char));
  83. ((char *)(ts+1))[l] = '\0'; /* ending 0 */
  84. return ts;
  85. }
  86. void luaS_remove (lua_State *L, TString *ts) {
  87. stringtable *tb = &G(L)->strt;
  88. TString **p = &tb->hash[lmod(ts->tsv.hash, tb->size)];
  89. while (*p != ts) /* find previous element */
  90. p = &(*p)->tsv.hnext;
  91. *p = (*p)->tsv.hnext; /* remove element from its list */
  92. tb->nuse--;
  93. }
  94. /*
  95. ** checks whether short string exists and reuses it or creates a new one
  96. */
  97. static TString *internshrstr (lua_State *L, const char *str, size_t l) {
  98. TString *ts;
  99. global_State *g = G(L);
  100. unsigned int h = luaS_hash(str, l, g->seed);
  101. TString **list = &g->strt.hash[lmod(h, g->strt.size)];
  102. for (ts = *list; ts != NULL; ts = ts->tsv.hnext) {
  103. if (l == ts->tsv.len &&
  104. (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) {
  105. /* found! */
  106. if (isdead(g, obj2gco(ts))) /* dead (but not collected yet)? */
  107. changewhite(obj2gco(ts)); /* resurrect it */
  108. return ts;
  109. }
  110. }
  111. if (g->strt.nuse >= g->strt.size && g->strt.size <= MAX_INT/2) {
  112. luaS_resize(L, g->strt.size * 2);
  113. list = &g->strt.hash[lmod(h, g->strt.size)]; /* recompute with new size */
  114. }
  115. ts = createstrobj(L, str, l, LUA_TSHRSTR, h);
  116. ts->tsv.hnext = *list;
  117. *list = ts;
  118. g->strt.nuse++;
  119. return ts;
  120. }
  121. /*
  122. ** new string (with explicit length)
  123. */
  124. TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
  125. if (l <= LUAI_MAXSHORTLEN) /* short string? */
  126. return internshrstr(L, str, l);
  127. else {
  128. if (l + 1 > (MAX_SIZE - sizeof(TString))/sizeof(char))
  129. luaM_toobig(L);
  130. return createstrobj(L, str, l, LUA_TLNGSTR, G(L)->seed);
  131. }
  132. }
  133. /*
  134. ** new zero-terminated string
  135. */
  136. TString *luaS_new (lua_State *L, const char *str) {
  137. return luaS_newlstr(L, str, strlen(str));
  138. }
  139. Udata *luaS_newudata (lua_State *L, size_t s) {
  140. Udata *u;
  141. if (s > MAX_SIZE - sizeof(Udata))
  142. luaM_toobig(L);
  143. u = &luaC_newobj(L, LUA_TUSERDATA, sizeof(Udata) + s)->u;
  144. u->uv.len = s;
  145. u->uv.metatable = NULL;
  146. setuservalue(L, u, luaO_nilobject);
  147. return u;
  148. }