tree.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. ** tree.c
  3. ** TecCGraf - PUC-Rio
  4. */
  5. char *rcs_tree="$Id: tree.c,v 1.17 1996/02/14 13:35:51 roberto Exp roberto $";
  6. #include <string.h>
  7. #include "mem.h"
  8. #include "lua.h"
  9. #include "tree.h"
  10. #include "lex.h"
  11. #include "hash.h"
  12. #include "table.h"
  13. #define NUM_HASHS 64
  14. typedef struct {
  15. int size;
  16. int nuse; /* number of elements (including EMPTYs) */
  17. TaggedString **hash;
  18. } stringtable;
  19. static int initialized = 0;
  20. static stringtable string_root[NUM_HASHS];
  21. static TaggedString EMPTY = {NOT_USED, NOT_USED, 0, 2, {0}};
  22. static unsigned long hash (char *str)
  23. {
  24. unsigned long h = 0;
  25. while (*str)
  26. h = ((h<<5)-h)^(unsigned char)*(str++);
  27. return h;
  28. }
  29. static void initialize (void)
  30. {
  31. initialized = 1;
  32. luaI_addReserved();
  33. luaI_initsymbol();
  34. luaI_initconstant();
  35. }
  36. static void grow (stringtable *tb)
  37. {
  38. int newsize = luaI_redimension(tb->size);
  39. TaggedString **newhash = newvector(newsize, TaggedString *);
  40. int i;
  41. for (i=0; i<newsize; i++)
  42. newhash[i] = NULL;
  43. if (tb->size > 0)
  44. { /* rehash */
  45. tb->nuse = 0;
  46. for (i=0; i<tb->size; i++)
  47. if (tb->hash[i] != NULL && tb->hash[i] != &EMPTY)
  48. {
  49. int h = tb->hash[i]->hash%newsize;
  50. while (newhash[h])
  51. h = (h+1)%newsize;
  52. newhash[h] = tb->hash[i];
  53. tb->nuse++;
  54. }
  55. luaI_free(tb->hash);
  56. }
  57. tb->size = newsize;
  58. tb->hash = newhash;
  59. }
  60. static TaggedString *insert (char *str, stringtable *tb)
  61. {
  62. TaggedString *ts;
  63. unsigned long h = hash(str);
  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 (tb->hash[i])
  74. {
  75. if (tb->hash[i] == &EMPTY)
  76. j = i;
  77. else if (strcmp(str, tb->hash[i]->str) == 0)
  78. return tb->hash[i];
  79. i = (i+1)%tb->size;
  80. }
  81. /* not found */
  82. if (j != -1) /* is there an EMPTY space? */
  83. i = j;
  84. else
  85. tb->nuse++;
  86. ts = tb->hash[i] = (TaggedString *)luaI_malloc(sizeof(TaggedString)+strlen(str));
  87. strcpy(ts->str, str);
  88. ts->marked = 0;
  89. ts->hash = h;
  90. ts->varindex = ts->constindex = NOT_USED;
  91. return ts;
  92. }
  93. TaggedString *lua_createstring (char *str)
  94. {
  95. return insert(str, &string_root[(unsigned)str[0]%NUM_HASHS]);
  96. }
  97. /*
  98. ** Garbage collection function.
  99. ** This function traverse the string list freeing unindexed strings
  100. */
  101. Long lua_strcollector (void)
  102. {
  103. Long counter = 0;
  104. int i;
  105. for (i=0; i<NUM_HASHS; i++)
  106. {
  107. stringtable *tb = &string_root[i];
  108. int j;
  109. for (j=0; j<tb->size; j++)
  110. {
  111. TaggedString *t = tb->hash[j];
  112. if (t != NULL && t->marked <= 1)
  113. {
  114. if (t->marked)
  115. t->marked = 0;
  116. else
  117. {
  118. luaI_free(t);
  119. tb->hash[j] = &EMPTY;
  120. counter++;
  121. }
  122. }
  123. }
  124. }
  125. return counter;
  126. }