]> git.droids-corp.org - protos/libecoli.git/commitdiff
draft for removing weak nodes
authorOlivier Matz <zer0@droids-corp.org>
Fri, 30 Mar 2018 20:46:25 +0000 (22:46 +0200)
committerOlivier Matz <zer0@droids-corp.org>
Tue, 10 Jul 2018 20:13:10 +0000 (22:13 +0200)
lib/ecoli_node.c

index 9a21d6304f211826347d5e89711302f8bad72ba7..eb12ff21b581f767766319af5b0d6044af31ae03 100644 (file)
@@ -123,16 +123,57 @@ struct ec_node *ec_node(const char *typename, const char *id)
        return __ec_node(type, id);
 }
 
+static void traverse_tree1(const struct ec_node *node)
+{
+       const struct ec_node *child;
+       size_t i, n;
+
+       if (node->free.state == 1) {
+               node->free.refcnt++;
+               return;
+       }
+       node->free.refcnt = 1;
+       node->free.state = 1; // traversed
+       n = node->n_children;
+       for (i = 0; i < n; i++) {
+               child = node->children[i];
+               traverse_tree1(child);
+       }
+}
+
 void ec_node_free(struct ec_node *node)
 {
+       struct ec_node *child;
+       bool free_it = false;
+       size_t i, n;
+
        if (node == NULL)
                return;
 
        assert(node->refcnt > 0);
 
-       if (--node->refcnt > 0)
+       // calc free refcnt
+       if (node->free.state == 0)
+               traverse_tree1(node);
+
+       node->refcnt--;
+       if (node->free.state == 2)
                return;
 
+       node->free.state = 2; // beeing freed
+       if (node->refcnt < node->free.refcnt) {
+               for (i = 0; i < n; i++) {
+                       child = node->children[i];
+                       ec_node_free(child);
+               }
+       }
+
+       if (node->refcnt > 0) {
+               node->free.refcnt = 0;
+               node->free.state = 0;
+               return;
+       }
+
        if (node->type != NULL && node->type->free_priv != NULL)
                node->type->free_priv(node);
        ec_free(node->id);
@@ -239,8 +280,9 @@ static void __ec_node_dump(FILE *out,
        id = ec_node_id(node);
        typename = node->type->name;
 
-       fprintf(out, "%*s" "type=%s id=%s %p\n",
-               (int)indent * 4, "", typename, id, node);
+       fprintf(out, "%*s" "type=%s id=%s %p free=(%d,%d)\n",
+               (int)indent * 4, "", typename, id, node,
+               node->free.state, node->free.refcnt);
        n = ec_node_get_children_count(node);
        for (i = 0; i < n; i++) {
                child = ec_node_get_child(node, i);