lstring.c 3.8 KB

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