tree.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. ** tree.c
  3. ** TecCGraf - PUC-Rio
  4. */
  5. char *rcs_tree="$Id: tree.c,v 1.19 1996/02/22 20:34:33 roberto Exp $";
  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. /* rehash */
  44. tb->nuse = 0;
  45. for (i=0; i<tb->size; i++)
  46. if (tb->hash[i] != NULL && tb->hash[i] != &EMPTY)
  47. {
  48. int h = tb->hash[i]->hash%newsize;
  49. while (newhash[h])
  50. h = (h+1)%newsize;
  51. newhash[h] = tb->hash[i];
  52. tb->nuse++;
  53. }
  54. luaI_free(tb->hash);
  55. tb->size = newsize;
  56. tb->hash = newhash;
  57. }
  58. static TaggedString *insert (char *str, stringtable *tb)
  59. {
  60. TaggedString *ts;
  61. unsigned long h = hash(str);
  62. int i;
  63. int j = -1;
  64. if ((Long)tb->nuse*3 >= (Long)tb->size*2)
  65. {
  66. if (!initialized)
  67. initialize();
  68. grow(tb);
  69. }
  70. i = h%tb->size;
  71. while (tb->hash[i])
  72. {
  73. if (tb->hash[i] == &EMPTY)
  74. j = i;
  75. else if (strcmp(str, tb->hash[i]->str) == 0)
  76. return tb->hash[i];
  77. i = (i+1)%tb->size;
  78. }
  79. /* not found */
  80. lua_pack();
  81. if (j != -1) /* is there an EMPTY space? */
  82. i = j;
  83. else
  84. tb->nuse++;
  85. ts = tb->hash[i] = (TaggedString *)luaI_malloc(sizeof(TaggedString)+strlen(str));
  86. strcpy(ts->str, str);
  87. ts->marked = 0;
  88. ts->hash = h;
  89. ts->varindex = ts->constindex = NOT_USED;
  90. return ts;
  91. }
  92. TaggedString *lua_createstring (char *str)
  93. {
  94. return insert(str, &string_root[(unsigned)str[0]%NUM_HASHS]);
  95. }
  96. /*
  97. ** Garbage collection function.
  98. ** This function traverse the string list freeing unindexed strings
  99. */
  100. Long lua_strcollector (void)
  101. {
  102. Long counter = 0;
  103. int i;
  104. for (i=0; i<NUM_HASHS; i++)
  105. {
  106. stringtable *tb = &string_root[i];
  107. int j;
  108. for (j=0; j<tb->size; j++)
  109. {
  110. TaggedString *t = tb->hash[j];
  111. if (t != NULL && t->marked <= 1)
  112. {
  113. if (t->marked)
  114. t->marked = 0;
  115. else
  116. {
  117. luaI_free(t);
  118. tb->hash[j] = &EMPTY;
  119. counter++;
  120. }
  121. }
  122. }
  123. }
  124. return counter;
  125. }