lstring.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. ** $Id: lstring.c,v 1.84 2003/12/04 17:22:42 roberto Exp roberto $
  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. #include "lua.h"
  9. #include "lmem.h"
  10. #include "lobject.h"
  11. #include "lstate.h"
  12. #include "lstring.h"
  13. void luaS_freeall (lua_State *L) {
  14. lua_assert(G(L)->strt.nuse==0);
  15. luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size, TString *);
  16. }
  17. void luaS_resize (lua_State *L, int newsize) {
  18. GCObject **newhash;
  19. stringtable *tb;
  20. int i;
  21. if (G(L)->sweepstrgc > 0) 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 = gcotots(p)->tsv.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 = cast(TString *, luaM_malloc(L, sizestring(l)));
  45. stringtable *tb;
  46. ts->tsv.len = l;
  47. ts->tsv.hash = h;
  48. ts->tsv.marked = luaC_white(G(L));
  49. ts->tsv.tt = LUA_TSTRING;
  50. ts->tsv.reserved = 0;
  51. memcpy(ts+1, str, l*sizeof(char));
  52. ((char *)(ts+1))[l] = '\0'; /* ending 0 */
  53. tb = &G(L)->strt;
  54. h = lmod(h, tb->size);
  55. ts->tsv.next = tb->hash[h]; /* chain new entry */
  56. tb->hash[h] = valtogco(ts);
  57. tb->nuse++;
  58. if (tb->nuse > cast(lu_int32, tb->size) && tb->size <= MAX_INT/2)
  59. luaS_resize(L, tb->size*2); /* too crowded */
  60. return ts;
  61. }
  62. TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
  63. GCObject *o;
  64. unsigned int h = cast(unsigned int, l); /* seed */
  65. size_t step = (l>>5)+1; /* if string is too long, don't hash all its chars */
  66. size_t l1;
  67. for (l1=l; l1>=step; l1-=step) /* compute hash */
  68. h = h ^ ((h<<5)+(h>>2)+(unsigned char)(str[l1-1]));
  69. for (o = G(L)->strt.hash[lmod(h, G(L)->strt.size)];
  70. o != NULL;
  71. o = o->gch.next) {
  72. TString *ts = gcotots(o);
  73. if (ts->tsv.len == l && (memcmp(str, getstr(ts), l) == 0)) {
  74. /* string may be dead */
  75. if (isdead(G(L), o)) changewhite(o);
  76. return ts;
  77. }
  78. }
  79. return newlstr(L, str, l, h); /* not found */
  80. }
  81. Udata *luaS_newudata (lua_State *L, size_t s) {
  82. Udata *u;
  83. u = cast(Udata *, luaM_malloc(L, sizeudata(s)));
  84. u->uv.marked = luaC_white(G(L)); /* is not finalized */
  85. u->uv.tt = LUA_TUSERDATA;
  86. u->uv.len = s;
  87. u->uv.metatable = NULL;
  88. /* chain it on udata list */
  89. u->uv.next = G(L)->firstudata->uv.next;
  90. G(L)->firstudata->uv.next = valtogco(u);
  91. return u;
  92. }