2
0

lstring.c 4.1 KB

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