lstring.c 4.4 KB

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