lstring.c 3.6 KB

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