tree.c 2.7 KB

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