lstring.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. #include "lua.h"
  9. #include <string.h>
  10. #include "lmem.h"
  11. #include "lobject.h"
  12. #include "lstate.h"
  13. #include "lstring.h"
  14. void luaS_resize (lua_State *L, int newsize) {
  15. stringtable *tb;
  16. int i;
  17. tb = &G(L)->strt;
  18. if (luaC_sweepstrgc(L) || newsize == tb->size || is_resizing_strings_gc(L))
  19. return; /* cannot resize during GC traverse or doesn't need to be resized */
  20. set_resizing_strings_gc(L);
  21. if (newsize > tb->size) {
  22. luaM_reallocvector(L, tb->hash, tb->size, newsize, GCObject *);
  23. for (i=tb->size; i<newsize; i++) tb->hash[i] = NULL;
  24. }
  25. /* rehash */
  26. for (i=0; i<tb->size; i++) {
  27. GCObject *p = tb->hash[i];
  28. tb->hash[i] = NULL;
  29. while (p) { /* for each node in the list */
  30. GCObject *next = p->gch.next; /* save next */
  31. unsigned int h = gco2ts(p)->hash;
  32. int h1 = lmod(h, newsize); /* new position */
  33. lua_assert(cast_int(h%newsize) == lmod(h, newsize));
  34. p->gch.next = tb->hash[h1]; /* chain it */
  35. tb->hash[h1] = p;
  36. p = next;
  37. }
  38. }
  39. if (newsize < tb->size)
  40. luaM_reallocvector(L, tb->hash, tb->size, newsize, GCObject *);
  41. tb->size = newsize;
  42. unset_resizing_strings_gc(L);
  43. }
  44. static TString *newlstr (lua_State *L, const char *str, size_t l,
  45. unsigned int h) {
  46. TString *ts;
  47. stringtable *tb = &G(L)->strt;
  48. if (l+1 > (MAX_SIZET - sizeof(TString))/sizeof(char))
  49. luaM_toobig(L);
  50. if ((tb->nuse + 1) > cast(lu_int32, tb->size) && tb->size <= MAX_INT/2)
  51. luaS_resize(L, tb->size*2); /* too crowded */
  52. ts = cast(TString *, luaM_malloc(L, sizeof(TString) + (l+1)*sizeof(char)));
  53. ts->tsv.len = l;
  54. ts->tsv.hash = h;
  55. ts->tsv.marked = luaC_white(G(L));
  56. ts->tsv.tt = LUA_TSTRING;
  57. memcpy(ts+1, str, l*sizeof(char));
  58. ((char *)(ts+1))[l] = '\0'; /* ending 0 */
  59. h = lmod(h, tb->size);
  60. ts->tsv.next = tb->hash[h]; /* chain new entry */
  61. tb->hash[h] = obj2gco(ts);
  62. tb->nuse++;
  63. return ts;
  64. }
  65. /*
  66. * The string algorithm has been modified to be LFS-friendly. The previous eLua
  67. * algo used the address of the string was in flash and the string was >4 bytes
  68. * This creates miminal savings and prevents the use of LFS based strings
  69. */
  70. LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
  71. GCObject *o;
  72. unsigned int h = cast(unsigned int, l); /* seed */
  73. size_t step = (l>>5)+1; /* if string is too long, don't hash all its chars */
  74. size_t l1;
  75. for (l1=l; l1>=step; l1-=step) /* compute hash */
  76. h = h ^ ((h<<5)+(h>>2)+cast(unsigned char, str[l1-1]));
  77. for (o = G(L)->strt.hash[lmod(h, G(L)->strt.size)];
  78. o != NULL;
  79. o = o->gch.next) {
  80. TString *ts = rawgco2ts(o);
  81. if (ts->tsv.len == l && (memcmp(str, getstr(ts), l) == 0)) {
  82. /* string may be dead */
  83. if (isdead(G(L), o)) changewhite(o);
  84. return ts;
  85. }
  86. }
  87. #ifndef LUA_CROSS_COMPILER
  88. /*
  89. * The RAM strt is searched first since RAM access is faster tham Flash access.
  90. * If a miss, then search the RO string table.
  91. */
  92. if (G(L)->ROstrt.hash) {
  93. for (o = G(L)->ROstrt.hash[lmod(h, G(L)->ROstrt.size)];
  94. o != NULL;
  95. o = o->gch.next) {
  96. TString *ts = rawgco2ts(o);
  97. if (ts->tsv.len == l && (memcmp(str, getstr(ts), l) == 0)) {
  98. return ts;
  99. }
  100. }
  101. }
  102. #endif
  103. return newlstr(L, str, l, h); /* not found */
  104. }
  105. Udata *luaS_newudata (lua_State *L, size_t s, Table *e) {
  106. Udata *u;
  107. if (s > MAX_SIZET - sizeof(Udata))
  108. luaM_toobig(L);
  109. u = cast(Udata *, luaM_malloc(L, s + sizeof(Udata)));
  110. u->uv.marked = luaC_white(G(L)); /* is not finalized */
  111. u->uv.tt = LUA_TUSERDATA;
  112. u->uv.len = s;
  113. u->uv.metatable = NULL;
  114. u->uv.env = e;
  115. /* chain it on udata list (after main thread) */
  116. u->uv.next = G(L)->mainthread->next;
  117. G(L)->mainthread->next = obj2gco(u);
  118. return u;
  119. }