lstring.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. ** $Id: lstring.c,v 1.48 2000/12/28 12:55:41 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. #include "lua.h"
  8. #include "lmem.h"
  9. #include "lobject.h"
  10. #include "lstate.h"
  11. #include "lstring.h"
  12. #define lmod(s,size) ((int)((s) & ((size)-1)))
  13. void luaS_init (lua_State *L) {
  14. luaS_resize(L, &L->strt, MINPOWER2);
  15. luaS_resize(L, &L->udt, MINPOWER2);
  16. }
  17. void luaS_freeall (lua_State *L) {
  18. LUA_ASSERT(L->strt.nuse==0, "non-empty string table");
  19. luaM_freearray(L, L->strt.hash, L->strt.size, TString *);
  20. LUA_ASSERT(L->udt.nuse==0, "non-empty udata table");
  21. luaM_freearray(L, L->udt.hash, L->udt.size, TString *);
  22. }
  23. void luaS_resize (lua_State *L, stringtable *tb, int newsize) {
  24. TString **newhash = luaM_newvector(L, newsize, TString *);
  25. int i;
  26. for (i=0; i<newsize; i++) newhash[i] = NULL;
  27. /* rehash */
  28. for (i=0; i<tb->size; i++) {
  29. TString *p = tb->hash[i];
  30. while (p) { /* for each node in the list */
  31. TString *next = p->nexthash; /* save next */
  32. luint32 h = (tb == &L->strt) ? p->u.s.hash : IntPoint(p->u.d.value);
  33. int h1 = lmod(h, newsize); /* new position */
  34. LUA_ASSERT(h%newsize == lmod(h, newsize),
  35. "a&(x-1) == a%x, for x power of 2");
  36. p->nexthash = newhash[h1]; /* chain it in new position */
  37. newhash[h1] = p;
  38. p = next;
  39. }
  40. }
  41. luaM_freearray(L, tb->hash, tb->size, TString *);
  42. tb->size = newsize;
  43. tb->hash = newhash;
  44. }
  45. static void newentry (lua_State *L, stringtable *tb, TString *ts, int h) {
  46. ts->nexthash = tb->hash[h]; /* chain new entry */
  47. tb->hash[h] = ts;
  48. tb->nuse++;
  49. if (tb->nuse > (luint32)tb->size && tb->size < MAX_INT/2) /* too crowded? */
  50. luaS_resize(L, tb, tb->size*2);
  51. }
  52. TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
  53. TString *ts;
  54. luint32 h = l; /* seed */
  55. size_t step = (l>>5)+1; /* if string is too long, don't hash all its chars */
  56. size_t l1;
  57. for (l1=l; l1>=step; l1-=step) /* compute hash */
  58. h = h ^ ((h<<5)+(h>>2)+(unsigned char)str[l1-1]);
  59. for (ts = L->strt.hash[lmod(h, L->strt.size)]; ts; ts = ts->nexthash) {
  60. if (ts->len == l && (memcmp(str, ts->str, l) == 0))
  61. return ts;
  62. }
  63. /* not found */
  64. ts = (TString *)luaM_malloc(L, sizestring(l));
  65. ts->marked = 0;
  66. ts->nexthash = NULL;
  67. ts->len = l;
  68. ts->u.s.hash = h;
  69. ts->u.s.constindex = 0;
  70. memcpy(ts->str, str, l);
  71. ts->str[l] = 0; /* ending 0 */
  72. newentry(L, &L->strt, ts, lmod(h, L->strt.size)); /* insert it on table */
  73. return ts;
  74. }
  75. TString *luaS_newudata (lua_State *L, size_t s, void *udata) {
  76. union L_UTString *uts = (union L_UTString *)luaM_malloc(L, sizeudata(s));
  77. TString *ts = &uts->ts;
  78. ts->marked = 0;
  79. ts->nexthash = NULL;
  80. ts->len = s;
  81. ts->u.d.tag = 0;
  82. ts->u.d.value = (udata == NULL) ? uts+1 : udata;
  83. /* insert it on table */
  84. newentry(L, &L->udt, ts, lmod(IntPoint(ts->u.d.value), L->udt.size));
  85. return ts;
  86. }
  87. TString *luaS_createudata (lua_State *L, void *udata, int tag) {
  88. int h1 = lmod(IntPoint(udata), L->udt.size);
  89. TString *ts;
  90. for (ts = L->udt.hash[h1]; ts; ts = ts->nexthash) {
  91. if (udata == ts->u.d.value && (tag == ts->u.d.tag || tag == LUA_ANYTAG))
  92. return ts;
  93. }
  94. /* not found */
  95. ts = luaS_newudata(L, 0, udata);
  96. if (tag != LUA_ANYTAG)
  97. ts->u.d.tag = tag;
  98. return ts;
  99. }