lstring.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. ** $Id: lstring.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $
  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 "lmem.h"
  11. #include "lobject.h"
  12. #include "lstate.h"
  13. #include "lstring.h"
  14. #define LUAS_READONLY_STRING 1
  15. #define LUAS_REGULAR_STRING 0
  16. void luaS_resize(lua_State *L, int newsize)
  17. {
  18. stringtable *tb;
  19. int i;
  20. tb = &G(L)->strt;
  21. if (luaC_sweepstrgc(L) || newsize == tb->size || is_resizing_strings_gc(L))
  22. return; /* cannot resize during GC traverse or doesn't need to be resized */
  23. set_resizing_strings_gc(L);
  24. if (newsize > tb->size)
  25. {
  26. luaM_reallocvector(L, tb->hash, tb->size, newsize, GCObject *);
  27. for (i = tb->size; i < newsize; i++) tb->hash[i] = NULL;
  28. }
  29. /* rehash */
  30. for (i = 0; i < tb->size; i++)
  31. {
  32. GCObject *p = tb->hash[i];
  33. tb->hash[i] = NULL;
  34. while (p) /* for each node in the list */
  35. {
  36. GCObject *next = p->gch.next; /* save next */
  37. unsigned int h = gco2ts(p)->hash;
  38. int h1 = lmod(h, newsize); /* new position */
  39. lua_assert(cast_int(h % newsize) == lmod(h, newsize));
  40. p->gch.next = tb->hash[h1]; /* chain it */
  41. tb->hash[h1] = p;
  42. p = next;
  43. }
  44. }
  45. if (newsize < tb->size)
  46. luaM_reallocvector(L, tb->hash, tb->size, newsize, GCObject *);
  47. tb->size = newsize;
  48. unset_resizing_strings_gc(L);
  49. }
  50. static TString *newlstr(lua_State *L, const char *str, size_t l,
  51. unsigned int h, int readonly)
  52. {
  53. TString *ts;
  54. stringtable *tb;
  55. if (l + 1 > (MAX_SIZET - sizeof(TString)) / sizeof(char))
  56. luaM_toobig(L);
  57. tb = &G(L)->strt;
  58. if ((tb->nuse + 1) > cast(lu_int32, tb->size) && tb->size <= MAX_INT / 2)
  59. luaS_resize(L, tb->size * 2); /* too crowded */
  60. ts = cast(TString *, luaM_malloc(L, readonly ? sizeof(char **) + sizeof(TString) : (l + 1) * sizeof(char) + sizeof(TString)));
  61. ts->tsv.len = l;
  62. ts->tsv.hash = h;
  63. ts->tsv.marked = luaC_white(G(L));
  64. ts->tsv.tt = LUA_TSTRING;
  65. if (!readonly)
  66. {
  67. memcpy(ts + 1, str, l * sizeof(char));
  68. ((char *)(ts + 1))[l] = '\0'; /* ending 0 */
  69. }
  70. else
  71. {
  72. *(char **)(ts + 1) = (char *)str;
  73. luaS_readonly(ts);
  74. }
  75. h = lmod(h, tb->size);
  76. ts->tsv.next = tb->hash[h]; /* chain new entry */
  77. tb->hash[h] = obj2gco(ts);
  78. tb->nuse++;
  79. return ts;
  80. }
  81. static TString *luaS_newlstr_helper(lua_State *L, const char *str, size_t l, int readonly)
  82. {
  83. GCObject *o;
  84. unsigned int h = cast(unsigned int, l); /* seed */
  85. size_t step = (l >> 5) + 1; /* if string is too long, don't hash all its chars */
  86. size_t l1;
  87. for (l1 = l; l1 >= step; l1 -= step) /* compute hash */
  88. h = h ^ ((h << 5) + (h >> 2) + cast(unsigned char, str[l1 - 1]));
  89. for (o = G(L)->strt.hash[lmod(h, G(L)->strt.size)];
  90. o != NULL;
  91. o = o->gch.next)
  92. {
  93. TString *ts = rawgco2ts(o);
  94. if (ts->tsv.len == l && (memcmp(str, getstr(ts), l) == 0))
  95. {
  96. /* string may be dead */
  97. if (isdead(G(L), o)) changewhite(o);
  98. return ts;
  99. }
  100. }
  101. return newlstr(L, str, l, h, readonly); /* not found */
  102. }
  103. static int lua_is_ptr_in_ro_area(const char *p)
  104. {
  105. #ifdef LUA_CROSS_COMPILER
  106. return 0;
  107. #else
  108. #include "compiler.h"
  109. return p >= RODATA_START_ADDRESS && p <= RODATA_END_ADDRESS;
  110. #endif
  111. }
  112. TString *luaS_newlstr(lua_State *L, const char *str, size_t l)
  113. {
  114. // If the pointer is in a read-only memory and the string is at least 4 chars in length,
  115. // create it as a read-only string instead
  116. if (lua_is_ptr_in_ro_area(str) && l + 1 > sizeof(char **) && l == strlen(str))
  117. return luaS_newlstr_helper(L, str, l, LUAS_READONLY_STRING);
  118. else
  119. return luaS_newlstr_helper(L, str, l, LUAS_REGULAR_STRING);
  120. }
  121. LUAI_FUNC TString *luaS_newrolstr(lua_State *L, const char *str, size_t l)
  122. {
  123. if (l + 1 > sizeof(char **) && l == strlen(str))
  124. return luaS_newlstr_helper(L, str, l, LUAS_READONLY_STRING);
  125. else // no point in creating a RO string, as it would actually be larger
  126. return luaS_newlstr_helper(L, str, l, LUAS_REGULAR_STRING);
  127. }
  128. Udata *luaS_newudata(lua_State *L, size_t s, Table *e)
  129. {
  130. Udata *u;
  131. if (s > MAX_SIZET - sizeof(Udata))
  132. luaM_toobig(L);
  133. u = cast(Udata *, luaM_malloc(L, s + sizeof(Udata)));
  134. u->uv.marked = luaC_white(G(L)); /* is not finalized */
  135. u->uv.tt = LUA_TUSERDATA;
  136. u->uv.len = s;
  137. u->uv.metatable = NULL;
  138. u->uv.env = e;
  139. /* chain it on udata list (after main thread) */
  140. u->uv.next = G(L)->mainthread->next;
  141. G(L)->mainthread->next = obj2gco(u);
  142. return u;
  143. }