lstring.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. ** $Id: lstring.c,v 2.45 2014/11/02 19:19:04 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->u.lnglen;
  29. lua_assert(a->tt == LUA_TLNGSTR && b->tt == LUA_TLNGSTR);
  30. return (a == b) || /* same instance or... */
  31. ((len == b->u.lnglen) && /* 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->u.hnext; /* save next */
  58. unsigned int h = lmod(p->hash, newsize); /* new position */
  59. p->u.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->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)->u.hnext;
  93. *p = (*p)->u.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->u.hnext) {
  105. if (l == ts->shrlen &&
  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->shrlen = cast_byte(l);
  119. ts->u.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. TString *ts;
  132. if (l + 1 > (MAX_SIZE - sizeof(TString))/sizeof(char))
  133. luaM_toobig(L);
  134. ts = createstrobj(L, str, l, LUA_TLNGSTR, G(L)->seed);
  135. ts->u.lnglen = l;
  136. return ts;
  137. }
  138. }
  139. /*
  140. ** new zero-terminated string
  141. */
  142. TString *luaS_new (lua_State *L, const char *str) {
  143. return luaS_newlstr(L, str, strlen(str));
  144. }
  145. Udata *luaS_newudata (lua_State *L, size_t s) {
  146. Udata *u;
  147. GCObject *o;
  148. if (s > MAX_SIZE - sizeof(Udata))
  149. luaM_toobig(L);
  150. o = luaC_newobj(L, LUA_TUSERDATA, sizeludata(s));
  151. u = gco2u(o);
  152. u->len = s;
  153. u->metatable = NULL;
  154. setuservalue(L, u, luaO_nilobject);
  155. return u;
  156. }