Selaa lähdekoodia

feat: 调整了gc函数

SongZihuan 4 vuotta sitten
vanhempi
sitoutus
a129859510
6 muutettua tiedostoa jossa 52 lisäystä ja 91 poistoa
  1. 1 0
      CMakeLists.txt
  2. 5 2
      vmcore/CMakeLists.txt
  3. 33 72
      vmcore/gc/gc.c
  4. 13 9
      vmcore/include/gc.h
  5. 0 1
      vmcore/include/var.h
  6. 0 7
      vmcore/src/var.c

+ 1 - 0
CMakeLists.txt

@@ -3,6 +3,7 @@ PROJECT(VirtualMath C)
 SET(CMAKE_C_STANDARD 11)
 
 OPTION(GC "GC" ON)
+OPTION(PG "PG" OFF)
 OPTION(SET_DEBUG "SET_DEBUG" ON)
 
 SET(LIBRARY_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/lib-${CMAKE_BUILD_TYPE})

+ 5 - 2
vmcore/CMakeLists.txt

@@ -52,8 +52,11 @@ ELSE()
     MESSAGE(FATAL_ERROR "not found libdl")
 ENDIF()
 
-ADD_COMPILE_OPTIONS(-pg)
-ADD_LINK_OPTIONS(-pg)
+IF (PG)
+    ADD_COMPILE_OPTIONS(-pg)
+    ADD_LINK_OPTIONS(-pg)
+ENDIF()
+
 ADD_LIBRARY(vmcore SHARED ${SRC})
 TARGET_LINK_LIBRARIES(vmcore ${libffi} ${libdl} m)
 TARGET_INCLUDE_DIRECTORIES(vmcore PRIVATE VMCORE_INCLUDE_DICT)

+ 33 - 72
vmcore/gc/gc.c

@@ -3,42 +3,14 @@
 #if START_GC
 static void gc_iterVar(Var *var);
 static void gc_iterLinkValue(LinkValue *value);
-static void gc_fatherValue(Inherit *value);
 static void gc_iterValue(Value *value);
 static void gc_varList(VarList *vl);
 static void gc_iterHashTable(HashTable *ht);
 
-static void resetGC(GCStatus *gcs){
-    gcs->continue_ = false;
-    gcs->link = 0;
-}
-
-void setGC(GCStatus *gcs){
-    resetGC(gcs);
-    gcs->tmp_link = 0;
-    gcs->statement_link = 0;
-    gcs->c_value = not_free;
-}
-
-void gc_addTmpLink(GCStatus *gcs){
-    gcs->tmp_link ++;
-}
-
-void gc_addLink(GCStatus *gcs){
-    gcs->link ++;
-}
-
-void gc_addStatementLink(GCStatus *gcs){
-    gcs->statement_link ++;
-}
-
-void gc_freeStatementLink(GCStatus *gcs){
-    gcs->statement_link --;
-}
+#define resetGC(gcs) ((gcs)->continue_ = false, (gcs)->link = 0)
 
-void gc_freeTmpLink(GCStatus *gcs){
-    gcs->tmp_link --;
-}
+// 若(gcs)->continue_为true, 则直接返回; 若为false则自增(+1后为false), 并返回自增前的值
+//#define gc_iterAlready(gcs) ((gcs)->continue_) || (((gcs)->continue_)++)  // TODO-szh 不能达到预期的效果
 
 static bool gc_iterAlready(GCStatus *gcs){
     bool return_ = gcs->continue_;
@@ -46,18 +18,9 @@ static bool gc_iterAlready(GCStatus *gcs){
     return return_;
 }
 
-static bool gc_needFree(GCStatus *gcs){
-    return (gcs->statement_link == 0 && gcs->tmp_link == 0 && gcs->link == 0);
-}
-
-static void gc_resetValue(Value *value){
-    value->gc_status.c_value = not_free;
-}
-
-static bool gc_needFreeValue(Value *value){
-    return (gc_needFree(&value->gc_status) && value->gc_status.c_value == need_free);
-}
-
+#define gc_needFree(gcs) ((gcs)->statement_link == 0 && (gcs)->tmp_link == 0 && (gcs)->link == 0)
+#define gc_resetValue(value) ((value)->gc_status.c_value = not_free)
+#define gc_needFreeValue(value) (gc_needFree(&(value)->gc_status) && (value)->gc_status.c_value == need_free)
 
 static void gc_iterLinkValue(LinkValue *value){
     if (value == NULL)
@@ -69,11 +32,6 @@ static void gc_iterLinkValue(LinkValue *value){
     }
 }
 
-static void gc_fatherValue(Inherit *value){
-    for (PASS; value != NULL; value = value->next)
-        gc_iterLinkValue(value->value);
-}
-
 static void gc_iterValue(Value *value){
     if (value == NULL)
         return;
@@ -82,7 +40,11 @@ static void gc_iterValue(Value *value){
         return;
     gc_varList(value->object.var);
     gc_varList(value->object.out_var);
-    gc_fatherValue(value->object.inherit);
+    {
+        Inherit *ih = value->object.inherit;
+        for (PASS; ih != NULL; ih = ih->next)
+            gc_iterLinkValue(ih->value);
+    }
     gc_resetValue(value);
     switch (value->type) {
         case V_list:
@@ -127,19 +89,16 @@ static void gc_iterVar(Var *var){
     }
 }
 
-static void gc_resetBase(Inter *inter){
-    for (Value *value_base = inter->base; value_base != NULL; value_base = value_base->gc_next)
-        resetGC(&value_base->gc_status);
-
-    for (LinkValue *link_base = inter->link_base; link_base != NULL; link_base = link_base->gc_next)
-        resetGC(&link_base->gc_status);
-
-    for (HashTable *hash_base = inter->hash_base; hash_base != NULL; hash_base = hash_base->gc_next)
-        resetGC(&hash_base->gc_status);
-
-    for (Var *var_base = inter->base_var; var_base != NULL; var_base = var_base->gc_next)
-        resetGC(&var_base->gc_status);
-}
+#define gc_resetBase(inter) do {  \
+    for (Value *value_base = (inter)->base; value_base != NULL; value_base = value_base->gc_next)  \
+        {resetGC(&value_base->gc_status);}  \
+    for (LinkValue *link_base = (inter)->link_base; link_base != NULL; link_base = link_base->gc_next)   \
+        {resetGC(&link_base->gc_status);}   \
+    for (HashTable *hash_base = (inter)->hash_base; hash_base != NULL; hash_base = hash_base->gc_next)  \
+        {resetGC(&hash_base->gc_status);}  \
+    for (Var *var_base = (inter)->base_var; var_base != NULL; var_base = var_base->gc_next)  \
+        {resetGC(&var_base->gc_status);}  \
+} while(0)
 
 static void gc_checkBase(Inter *inter){
     for (Value *value_base = inter->base; value_base != NULL; value_base = value_base->gc_next)
@@ -247,6 +206,7 @@ void gc_run(Inter *inter, VarList *run_var, int var_list, int link_value, int va
         gc_iterValue(tmp);
     }
     va_end(arg);
+
     gc_checkBase(inter);
     gc_checkDel(inter);
     gc_freeBase(inter);
@@ -257,21 +217,15 @@ static void gc_freezeHashTable(HashTable *ht, bool is_lock){
     if (ht == NULL)
         return;
 
-    if (is_lock)
+    if (is_lock) {
         gc_addTmpLink(&ht->gc_status);
-    else
+    } else {
         gc_freeTmpLink(&ht->gc_status);
+    }
 
     gc_iterAlready(&ht->gc_status);
 }
 
-static void gc_iterFreezeVarList(VarList *freeze, VarList *base, bool is_lock){
-    for (PASS; freeze != NULL; freeze = freeze->next){
-        if (!comparVarList(freeze, base))
-            gc_freezeHashTable(freeze->hashtable, is_lock);
-    }
-}
-
 /**
  * 冻结不可达的VarList的hashTable
  * @param inter
@@ -281,6 +235,13 @@ static void gc_iterFreezeVarList(VarList *freeze, VarList *base, bool is_lock){
  */
 void gc_freeze(Inter *inter, VarList *freeze, VarList *base, bool is_lock){
     gc_resetBase(inter);
-    gc_iterFreezeVarList(freeze, base, is_lock);
+    for (PASS; freeze != NULL; freeze = freeze->next){
+        for (VarList *src = base; src != NULL; src = src->next) {
+            if (src->hashtable != freeze->hashtable) {
+                gc_freezeHashTable(freeze->hashtable, is_lock);
+                break;
+            }
+        }
+    }
 }
 #endif

+ 13 - 9
vmcore/include/gc.h

@@ -28,23 +28,27 @@ typedef struct GCStatus GCStatus;
 
 
 #if START_GC
-void setGC(GCStatus *gcs);
-void gc_addTmpLink(GCStatus *gcs);
-void gc_addStatementLink(GCStatus *gcs);
-void gc_freeTmpLink(GCStatus *gcs);
-void gc_freeStatementLink(GCStatus *gcs);
+#define gc_addTmpLink(gcs) ((gcs)->tmp_link ++)
+#define gc_addStatementLink(gcs) ((gcs)->statement_link ++)
+#define gc_addLink(gcs) ((gcs)->link ++)
+#define gc_freeTmpLink(gcs) ((gcs)->tmp_link --)
+#define gc_freeStatementLink(gcs) ((gcs)->statement_link --)
+#define setGC(gcs) ((gcs)->continue_ = false, (gcs)->link = 0, (gcs)->tmp_link = 0, (gcs)->statement_link = 0, (gcs)->c_value = not_free)
+
 void gc_runDelAll(struct Inter *inter);
 
 void gc_freeze(struct Inter *inter, struct VarList *freeze, struct VarList *base, bool is_lock);
 void gc_run(struct Inter *inter, struct VarList *run_var, int var_list, int link_value, int value, ...);
 #else
+#define gc_addTmpLink(gcs) ((void)0)
+#define gc_addStatementLink(gcs) ((void)0)
+#define gc_addLink(gcs) ((void)0)
+#define gc_freeTmpLink(gcs) ((void)0)
+
 #define gc_freeze(...) ((void)0)
 #define gc_run(...) ((void)0)
 #define setGC(...) ((void)0)
-#define gc_addTmpLink(...) ((void)0)
-#define gc_addStatementLink(...) ((void)0)
-#define gc_freeTmpLink(...) ((void)0)
-#define gc_freeStatementLink(...) ((void)0)
+#define gc_freeStatementLink(gcs) ((void)0)
 #define gc_runDelAll(...) ((void)0)
 #endif
 

+ 0 - 1
vmcore/include/var.h

@@ -65,7 +65,6 @@ VarList *popVarList(VarList *base);
 VarList *copyVarListCore(VarList *base, Inter *inter);
 VarList *copyVarList(VarList *base, bool n_new, Inter *inter);
 VarList *connectVarListBack(VarList *base, VarList *back);
-bool comparVarList(VarList *dest, VarList *src);
 VarList *makeObjectVarList(Inherit *value, Inter *inter, VarList *base);
 
 vint findDefault(DefaultVar *base, wchar_t *name);

+ 0 - 7
vmcore/src/var.c

@@ -269,13 +269,6 @@ VarList *connectVarListBack(VarList *base, VarList *back){
     return base;
 }
 
-bool comparVarList(VarList *dest, VarList *src) {
-    for (PASS; src != NULL; src = src->next)
-        if (src->hashtable == dest->hashtable)
-            return true;
-    return false;
-}
-
 VarList *makeObjectVarList(Inherit *value, Inter *inter, VarList *base) {
     VarList *tmp = base == NULL ? makeVarList(inter, true) : base;
     for (PASS; value != NULL; value = value->next) {