lstring.c 4.4 KB

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