2
0

tree.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. ** tree.c
  3. ** TecCGraf - PUC-Rio
  4. */
  5. char *rcs_tree="$Id: tree.c,v 1.23 1997/03/31 14:02:58 roberto Exp roberto $";
  6. #include <string.h>
  7. #include "luamem.h"
  8. #include "lua.h"
  9. #include "tree.h"
  10. #include "lex.h"
  11. #include "hash.h"
  12. #include "table.h"
  13. #include "fallback.h"
  14. #define NUM_HASHS 64
  15. typedef struct {
  16. int size;
  17. int nuse; /* number of elements (including EMPTYs) */
  18. TaggedString **hash;
  19. } stringtable;
  20. static int initialized = 0;
  21. static stringtable string_root[NUM_HASHS];
  22. static TaggedString EMPTY = {LUA_T_STRING, 0, NOT_USED, NOT_USED, 0, 2, {0}};
  23. static unsigned long hash (char *buff, long size)
  24. {
  25. unsigned long h = 0;
  26. while (size--)
  27. h = ((h<<5)-h)^(unsigned char)*(buff++);
  28. return h;
  29. }
  30. static void initialize (void)
  31. {
  32. initialized = 1;
  33. luaI_addReserved();
  34. luaI_initsymbol();
  35. luaI_initconstant();
  36. luaI_initfallbacks();
  37. }
  38. static void grow (stringtable *tb)
  39. {
  40. int newsize = luaI_redimension(tb->size);
  41. TaggedString **newhash = newvector(newsize, TaggedString *);
  42. int i;
  43. for (i=0; i<newsize; i++)
  44. newhash[i] = NULL;
  45. /* rehash */
  46. tb->nuse = 0;
  47. for (i=0; i<tb->size; i++)
  48. if (tb->hash[i] != NULL && tb->hash[i] != &EMPTY)
  49. {
  50. int h = tb->hash[i]->hash%newsize;
  51. while (newhash[h])
  52. h = (h+1)%newsize;
  53. newhash[h] = tb->hash[i];
  54. tb->nuse++;
  55. }
  56. luaI_free(tb->hash);
  57. tb->size = newsize;
  58. tb->hash = newhash;
  59. }
  60. static TaggedString *insert (char *buff, long size, int tag, stringtable *tb)
  61. {
  62. TaggedString *ts;
  63. unsigned long h = hash(buff, size);
  64. int i;
  65. int j = -1;
  66. if ((Long)tb->nuse*3 >= (Long)tb->size*2)
  67. {
  68. if (!initialized)
  69. initialize();
  70. grow(tb);
  71. }
  72. i = h%tb->size;
  73. while ((ts = tb->hash[i]) != NULL)
  74. {
  75. if (ts == &EMPTY)
  76. j = i;
  77. else if (ts->size == size && ts->tag == tag &&
  78. memcmp(buff, ts->str, size) == 0)
  79. return ts;
  80. i = (i+1)%tb->size;
  81. }
  82. /* not found */
  83. lua_pack();
  84. if (j != -1) /* is there an EMPTY space? */
  85. i = j;
  86. else
  87. tb->nuse++;
  88. ts = tb->hash[i] = (TaggedString *)luaI_malloc(sizeof(TaggedString)+size-1);
  89. memcpy(ts->str, buff, size);
  90. ts->tag = tag;
  91. ts->size = size;
  92. ts->marked = 0;
  93. ts->hash = h;
  94. ts->varindex = ts->constindex = NOT_USED;
  95. return ts;
  96. }
  97. TaggedString *luaI_createuserdata (char *buff, long size, int tag)
  98. {
  99. return insert(buff, size, tag, &string_root[(unsigned)buff[0]%NUM_HASHS]);
  100. }
  101. TaggedString *lua_createstring (char *str)
  102. {
  103. return luaI_createuserdata(str, strlen(str)+1, LUA_T_STRING);
  104. }
  105. void luaI_strcallIM (void)
  106. {
  107. int i;
  108. TObject o;
  109. ttype(&o) = LUA_T_USERDATA;
  110. for (i=0; i<NUM_HASHS; i++) {
  111. stringtable *tb = &string_root[i];
  112. int j;
  113. for (j=0; j<tb->size; j++) {
  114. TaggedString *t = tb->hash[j];
  115. if (t != NULL && t->tag != LUA_T_STRING && t->marked == 0) {
  116. tsvalue(&o) = t;
  117. luaI_gcIM(&o);
  118. }
  119. }
  120. }
  121. }
  122. /*
  123. ** Garbage collection function.
  124. ** This function traverse the string list freeing unindexed strings
  125. */
  126. Long lua_strcollector (void)
  127. {
  128. Long counter = 0;
  129. int i;
  130. for (i=0; i<NUM_HASHS; i++)
  131. {
  132. stringtable *tb = &string_root[i];
  133. int j;
  134. for (j=0; j<tb->size; j++)
  135. {
  136. TaggedString *t = tb->hash[j];
  137. if (t != NULL && t->marked <= 1)
  138. {
  139. if (t->marked)
  140. t->marked = 0;
  141. else
  142. {
  143. luaI_free(t);
  144. tb->hash[j] = &EMPTY;
  145. counter++;
  146. }
  147. }
  148. }
  149. }
  150. return counter;
  151. }