gc.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include "__virtualmath.h"
  2. void gc_iterLinkValue(LinkValue *value){
  3. if (value == NULL)
  4. return;
  5. gcAddLink(&value->gc_status);
  6. if (!setIterAlready(&value->gc_status)){
  7. gc_iterLinkValue(value->father);
  8. gc_iterValue(value->value);
  9. }
  10. }
  11. void gc_iterValue(Value *value){
  12. if (value == NULL)
  13. return;
  14. gcAddLink(&value->gc_status);
  15. if (setIterAlready(&value->gc_status))
  16. return;
  17. gc_varList(value->object.var);
  18. gc_varList(value->object.out_var);
  19. // TODO-szh 处理father_value
  20. switch (value->type) {
  21. case list:
  22. for (int i=0;i < value->data.list.size;i++)
  23. gc_iterLinkValue(value->data.list.list[i]);
  24. break;
  25. case dict:
  26. gc_iterHashTable(value->data.dict.dict);
  27. break;
  28. default:
  29. break;
  30. }
  31. }
  32. void gc_varList(VarList *vl){
  33. for (PASS; vl != NULL; vl = vl->next)
  34. gc_iterHashTable(vl->hashtable);
  35. }
  36. void gc_iterHashTable(HashTable *ht){
  37. if (ht == NULL)
  38. return;
  39. gcAddLink(&ht->gc_status);
  40. if (setIterAlready(&ht->gc_status))
  41. return;
  42. for (int i=0;i < MAX_SIZE;i++)
  43. gc_iterVar(ht->hashtable[i]);
  44. }
  45. void gc_iterVar(Var *var){
  46. if (var == NULL)
  47. return;
  48. gcAddLink(&var->gc_status);
  49. if (setIterAlready(&var->gc_status))
  50. return;
  51. for (PASS; var != NULL; var = var->next){
  52. gc_iterLinkValue(var->name_);
  53. gc_iterLinkValue(var->value);
  54. }
  55. }
  56. void gc_resetBase(Inter *inter){
  57. for (Value *value_base = inter->base; value_base != NULL; value_base = value_base->gc_next)
  58. resetGC(&value_base->gc_status);
  59. for (LinkValue *link_base = inter->link_base; link_base != NULL; link_base = link_base->gc_next)
  60. resetGC(&link_base->gc_status);
  61. for (HashTable *hash_base = inter->hash_base; hash_base != NULL; hash_base = hash_base->gc_next)
  62. resetGC(&hash_base->gc_status);
  63. for (Var *var_base = inter->base_var; var_base != NULL; var_base = var_base->gc_next)
  64. resetGC(&var_base->gc_status);
  65. }
  66. void gc_checkBase(Inter *inter){
  67. for (Value *value_base = inter->base; value_base != NULL; value_base = value_base->gc_next)
  68. if (!needFree(&value_base->gc_status) && !value_base->gc_status.continue_)
  69. gc_iterValue(value_base);
  70. for (LinkValue *link_base = inter->link_base; link_base != NULL; link_base = link_base->gc_next)
  71. if (!needFree(&link_base->gc_status) && !link_base->gc_status.continue_)
  72. gc_iterLinkValue(link_base);
  73. for (HashTable *hash_base = inter->hash_base; hash_base != NULL; hash_base = hash_base->gc_next)
  74. if (!needFree(&hash_base->gc_status) && !hash_base->gc_status.continue_)
  75. gc_iterHashTable(hash_base);
  76. }
  77. void gc_freeBase(Inter *inter){
  78. #if START_GC
  79. for (Value *value_base = inter->base; value_base != NULL;)
  80. if (needFree(&value_base->gc_status))
  81. value_base = freeValue(value_base, inter);
  82. else
  83. value_base = value_base->gc_next;
  84. for (LinkValue *link_base = inter->link_base; link_base != NULL;)
  85. if (needFree(&link_base->gc_status))
  86. link_base = freeLinkValue(link_base, inter);
  87. else
  88. link_base = link_base->gc_next;
  89. for (HashTable *hash_base = inter->hash_base; hash_base != NULL;)
  90. if (needFree(&hash_base->gc_status))
  91. hash_base = freeHashTable(hash_base, inter);
  92. else
  93. hash_base = hash_base->gc_next;
  94. for (Var *var_base = inter->base_var; var_base != NULL;)
  95. if (needFree(&var_base->gc_status))
  96. var_base = freeVar(var_base, inter);
  97. else
  98. var_base = var_base->gc_next;
  99. #endif
  100. }
  101. void runGC(Inter *inter, int var_list, int link_value, int value, ...){
  102. #if START_GC
  103. gc_resetBase(inter);
  104. va_list arg;
  105. va_start(arg, value);
  106. for (int i =0;i < var_list;i ++){
  107. VarList *tmp = va_arg(arg, VarList *);
  108. gc_varList(tmp);
  109. }
  110. for (int i =0;i < link_value;i ++){
  111. LinkValue *tmp = va_arg(arg, LinkValue *);
  112. gc_iterLinkValue(tmp);
  113. }
  114. for (int i =0;i < value;i ++){
  115. Value *tmp = va_arg(arg, Value *);
  116. gc_iterValue(tmp);
  117. }
  118. va_end(arg);
  119. gc_checkBase(inter);
  120. gc_freeBase(inter);
  121. #endif
  122. }