gc.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #include "__virtualmath.h"
  2. #if START_GC
  3. static void gc_iterVar(Var *var);
  4. static void gc_iterLinkValue(LinkValue *value);
  5. static void gc_fatherValue(Inherit *value);
  6. static void gc_iterValue(Value *value);
  7. static void gc_varList(VarList *vl);
  8. static void gc_iterHashTable(HashTable *ht);
  9. static void resetGC(GCStatus *gcs){
  10. gcs->continue_ = false;
  11. gcs->link = 0;
  12. }
  13. void setGC(GCStatus *gcs){
  14. resetGC(gcs);
  15. gcs->tmp_link = 0;
  16. gcs->statement_link = 0;
  17. gcs->c_value = not_free;
  18. }
  19. void gc_addTmpLink(GCStatus *gcs){
  20. gcs->tmp_link ++;
  21. }
  22. void gc_addLink(GCStatus *gcs){
  23. gcs->link ++;
  24. }
  25. void gc_addStatementLink(GCStatus *gcs){
  26. gcs->statement_link ++;
  27. }
  28. void gc_freeStatementLink(GCStatus *gcs){
  29. gcs->statement_link --;
  30. }
  31. void gc_freeTmpLink(GCStatus *gcs){
  32. gcs->tmp_link --;
  33. }
  34. static bool gc_iterAlready(GCStatus *gcs){
  35. bool return_ = gcs->continue_;
  36. gcs->continue_ = true;
  37. return return_;
  38. }
  39. static bool gc_needFree(GCStatus *gcs){
  40. return (gcs->statement_link == 0 && gcs->tmp_link == 0 && gcs->link == 0);
  41. }
  42. static void gc_resetValue(Value *value){
  43. value->gc_status.c_value = not_free;
  44. }
  45. static bool gc_needFreeValue(Value *value){
  46. return (gc_needFree(&value->gc_status) && value->gc_status.c_value == need_free);
  47. }
  48. static void gc_iterLinkValue(LinkValue *value){
  49. if (value == NULL)
  50. return;
  51. gc_addLink(&value->gc_status);
  52. if (!gc_iterAlready(&value->gc_status)){
  53. gc_iterLinkValue(value->belong);
  54. gc_iterValue(value->value);
  55. }
  56. }
  57. static void gc_fatherValue(Inherit *value){
  58. for (PASS; value != NULL; value = value->next)
  59. gc_iterLinkValue(value->value);
  60. }
  61. static void gc_iterValue(Value *value){
  62. if (value == NULL)
  63. return;
  64. gc_addLink(&value->gc_status);
  65. if (gc_iterAlready(&value->gc_status))
  66. return;
  67. gc_varList(value->object.var);
  68. gc_varList(value->object.out_var);
  69. gc_fatherValue(value->object.inherit);
  70. gc_resetValue(value);
  71. switch (value->type) {
  72. case list:
  73. for (int i=0;i < value->data.list.size;i++)
  74. gc_iterLinkValue(value->data.list.list[i]);
  75. break;
  76. case dict:
  77. gc_iterHashTable(value->data.dict.dict);
  78. break;
  79. default:
  80. break;
  81. }
  82. }
  83. static void gc_varList(VarList *vl){
  84. for (PASS; vl != NULL; vl = vl->next)
  85. gc_iterHashTable(vl->hashtable);
  86. }
  87. static void gc_iterHashTable(HashTable *ht){
  88. if (ht == NULL)
  89. return;
  90. gc_addLink(&ht->gc_status);
  91. if (gc_iterAlready(&ht->gc_status))
  92. return;
  93. for (int i=0;i < MAX_SIZE;i++)
  94. gc_iterVar(ht->hashtable[i]);
  95. }
  96. static void gc_iterVar(Var *var){
  97. if (var == NULL)
  98. return;
  99. if (gc_iterAlready(&var->gc_status))
  100. return;
  101. for (PASS; var != NULL; var = var->next){
  102. gc_addLink(&var->gc_status);
  103. gc_iterLinkValue(var->name_);
  104. gc_iterLinkValue(var->value);
  105. }
  106. }
  107. static void gc_resetBase(Inter *inter){
  108. for (Value *value_base = inter->base; value_base != NULL; value_base = value_base->gc_next)
  109. resetGC(&value_base->gc_status);
  110. for (LinkValue *link_base = inter->link_base; link_base != NULL; link_base = link_base->gc_next)
  111. resetGC(&link_base->gc_status);
  112. for (HashTable *hash_base = inter->hash_base; hash_base != NULL; hash_base = hash_base->gc_next)
  113. resetGC(&hash_base->gc_status);
  114. for (Var *var_base = inter->base_var; var_base != NULL; var_base = var_base->gc_next)
  115. resetGC(&var_base->gc_status);
  116. }
  117. static void gc_checkBase(Inter *inter){
  118. for (Value *value_base = inter->base; value_base != NULL; value_base = value_base->gc_next)
  119. if (!gc_needFree(&value_base->gc_status) && !value_base->gc_status.continue_)
  120. gc_iterValue(value_base);
  121. for (LinkValue *link_base = inter->link_base; link_base != NULL; link_base = link_base->gc_next)
  122. if (!gc_needFree(&link_base->gc_status) && !link_base->gc_status.continue_)
  123. gc_iterLinkValue(link_base);
  124. for (HashTable *hash_base = inter->hash_base; hash_base != NULL; hash_base = hash_base->gc_next)
  125. if (!gc_needFree(&hash_base->gc_status) && !hash_base->gc_status.continue_)
  126. gc_iterHashTable(hash_base);
  127. for (Var *var_base = inter->base_var; var_base != NULL; var_base = var_base->gc_next)
  128. if (!gc_needFree(&var_base->gc_status) && !var_base->gc_status.continue_)
  129. gc_iterVar(var_base);
  130. }
  131. static void gc_checkDel(Inter *inter){
  132. for (Value *value = inter->base; value != NULL; value = value->gc_next)
  133. if (!gc_needFree(&value->gc_status))
  134. gc_resetValue(value);
  135. else if (value->gc_status.c_value == not_free){
  136. if (needDel(value, inter)){
  137. gc_iterValue(value);
  138. value->gc_status.c_value = run_del;
  139. }
  140. else
  141. value->gc_status.c_value = need_free;
  142. }
  143. }
  144. void gc_runDelAll(Inter *inter){
  145. Result result;
  146. setResultCore(&result);
  147. for (Value *value = inter->base; value != NULL; value = value->gc_next) {
  148. freeResult(&result);
  149. gc_addTmpLink(&value->gc_status);
  150. if (needDel(value, inter)) {
  151. callDel(value, &result, inter, inter->var_list);
  152. if (!RUN_TYPE(result.type))
  153. printError(&result, inter, true);
  154. }
  155. gc_freeTmpLink(&value->gc_status);
  156. }
  157. }
  158. static void gc_runDel(Inter *inter, VarList *var_list){
  159. Result result;
  160. setResultCore(&result);
  161. for (Value *value = inter->base; value != NULL; value = value->gc_next) {
  162. freeResult(&result);
  163. if (value->gc_status.c_value == run_del) {
  164. gc_addTmpLink(&value->gc_status);
  165. callDel(value, &result, inter, var_list);
  166. if (!RUN_TYPE(result.type))
  167. printError(&result, inter, true);
  168. gc_freeTmpLink(&value->gc_status);
  169. value->gc_status.c_value = need_free;
  170. }
  171. }
  172. }
  173. static void gc_freeBase(Inter *inter){
  174. for (Value **value_base = &inter->base; *value_base != NULL;)
  175. if (gc_needFreeValue(*value_base))
  176. freeValue(value_base);
  177. else
  178. value_base = &(*value_base)->gc_next;
  179. for (LinkValue **link_base = &inter->link_base; *link_base != NULL;)
  180. if (gc_needFree(&(*link_base)->gc_status))
  181. freeLinkValue(link_base);
  182. else
  183. link_base = &(*link_base)->gc_next;
  184. for (HashTable **hash_base = &inter->hash_base; *hash_base != NULL;)
  185. if (gc_needFree(&(*hash_base)->gc_status))
  186. freeHashTable(hash_base);
  187. else
  188. hash_base = &(*hash_base)->gc_next;
  189. for (Var **var_base = &inter->base_var; *var_base != NULL;)
  190. if (gc_needFree(&(*var_base)->gc_status))
  191. freeVar(var_base);
  192. else
  193. var_base = &(*var_base)->gc_next;
  194. }
  195. void gc_run(Inter *inter, VarList *run_var, int var_list, int link_value, int value, ...){
  196. gc_resetBase(inter);
  197. va_list arg;
  198. va_start(arg, value);
  199. for (int i =0;i < var_list;i ++){
  200. VarList *tmp = va_arg(arg, VarList *);
  201. gc_varList(tmp);
  202. }
  203. for (int i =0;i < link_value;i ++){
  204. LinkValue *tmp = va_arg(arg, LinkValue *);
  205. gc_iterLinkValue(tmp);
  206. }
  207. for (int i =0;i < value;i ++){
  208. Value *tmp = va_arg(arg, Value *);
  209. gc_iterValue(tmp);
  210. }
  211. va_end(arg);
  212. gc_checkBase(inter);
  213. gc_checkDel(inter);
  214. gc_freeBase(inter);
  215. gc_runDel(inter, run_var);
  216. }
  217. static void gc_freezeHashTable(HashTable *ht, bool is_lock){
  218. if (ht == NULL)
  219. return;
  220. if (is_lock)
  221. gc_addTmpLink(&ht->gc_status);
  222. else
  223. gc_freeTmpLink(&ht->gc_status);
  224. gc_iterAlready(&ht->gc_status);
  225. }
  226. static void gc_iterFreezeVarList(VarList *freeze, VarList *base, bool is_lock){
  227. for (PASS; freeze != NULL; freeze = freeze->next){
  228. if (!comparVarList(freeze, base))
  229. gc_freezeHashTable(freeze->hashtable, is_lock);
  230. }
  231. }
  232. /**
  233. * 冻结不可达的VarList的hashTable
  234. * @param inter
  235. * @param freeze
  236. * @param base
  237. * @param is_lock
  238. */
  239. void gc_freeze(Inter *inter, VarList *freeze, VarList *base, bool is_lock){
  240. gc_resetBase(inter);
  241. gc_iterFreezeVarList(freeze, base, is_lock);
  242. }
  243. #endif