lstring.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #if 0
  7. #include <string.h>
  8. #endif
  9. #define lstring_c
  10. #define LUA_CORE
  11. #include "lua.h"
  12. #include "lmem.h"
  13. #include "lobject.h"
  14. #include "lstate.h"
  15. #include "lstring.h"
  16. void luaS_resize (lua_State *L, int newsize) {
  17. GCObject **newhash;
  18. stringtable *tb;
  19. int i;
  20. if (G(L)->gcstate == GCSsweepstring)
  21. return; /* cannot resize during GC traverse */
  22. newhash = luaM_newvector(L, newsize, GCObject *);
  23. tb = &G(L)->strt;
  24. for (i=0; i<newsize; i++) newhash[i] = NULL;
  25. /* rehash */
  26. for (i=0; i<tb->size; i++) {
  27. GCObject *p = tb->hash[i];
  28. while (p) { /* for each node in the list */
  29. GCObject *next = p->gch.next; /* save next */
  30. unsigned int h = gco2ts(p)->hash;
  31. int h1 = lmod(h, newsize); /* new position */
  32. lua_assert(cast_int(h%newsize) == lmod(h, newsize));
  33. p->gch.next = newhash[h1]; /* chain it */
  34. newhash[h1] = p;
  35. p = next;
  36. }
  37. }
  38. luaM_freearray(L, tb->hash, tb->size, TString *);
  39. tb->size = newsize;
  40. tb->hash = newhash;
  41. }
  42. static TString *newlstr (lua_State *L, const char *str, size_t l,
  43. unsigned int h) {
  44. TString *ts;
  45. stringtable *tb;
  46. if (l+1 > (MAX_SIZET - sizeof(TString))/sizeof(char))
  47. luaM_toobig(L);
  48. ts = cast(TString *, luaM_malloc(L, (l+1)*sizeof(char)+sizeof(TString)));
  49. ts->tsv.len = l;
  50. ts->tsv.hash = h;
  51. ts->tsv.marked = luaC_white(G(L));
  52. ts->tsv.tt = LUA_TSTRING;
  53. ts->tsv.reserved = 0;
  54. memcpy(ts+1, str, l*sizeof(char));
  55. ((char *)(ts+1))[l] = '\0'; /* ending 0 */
  56. tb = &G(L)->strt;
  57. h = lmod(h, tb->size);
  58. ts->tsv.next = tb->hash[h]; /* chain new entry */
  59. tb->hash[h] = obj2gco(ts);
  60. tb->nuse++;
  61. if (tb->nuse > cast(lu_int32, tb->size) && tb->size <= MAX_INT/2)
  62. luaS_resize(L, tb->size*2); /* too crowded */
  63. return ts;
  64. }
  65. TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
  66. GCObject *o;
  67. unsigned int h = cast(unsigned int, l); /* seed */
  68. size_t step = (l>>5)+1; /* if string is too long, don't hash all its chars */
  69. size_t l1;
  70. for (l1=l; l1>=step; l1-=step) /* compute hash */
  71. h = h ^ ((h<<5)+(h>>2)+cast(unsigned char, str[l1-1]));
  72. for (o = G(L)->strt.hash[lmod(h, G(L)->strt.size)];
  73. o != NULL;
  74. o = o->gch.next) {
  75. TString *ts = rawgco2ts(o);
  76. if (ts->tsv.len == l && (memcmp(str, getstr(ts), l) == 0)) {
  77. /* string may be dead */
  78. if (isdead(G(L), o)) changewhite(o);
  79. return ts;
  80. }
  81. }
  82. return newlstr(L, str, l, h); /* not found */
  83. }
  84. Udata *luaS_newudata (lua_State *L, size_t s, Table *e) {
  85. Udata *u;
  86. if (s > MAX_SIZET - sizeof(Udata))
  87. luaM_toobig(L);
  88. u = cast(Udata *, luaM_malloc(L, s + sizeof(Udata)));
  89. u->uv.marked = luaC_white(G(L)); /* is not finalized */
  90. u->uv.tt = LUA_TUSERDATA;
  91. u->uv.len = s;
  92. u->uv.metatable = NULL;
  93. u->uv.env = e;
  94. /* chain it on udata list (after main thread) */
  95. u->uv.next = G(L)->mainthread->next;
  96. G(L)->mainthread->next = obj2gco(u);
  97. return u;
  98. }