rework completion iterators
[protos/libecoli.git] / src / ecoli_node.c
index 9789102..935f4e6 100644 (file)
@@ -12,7 +12,7 @@
 #include <ecoli_malloc.h>
 #include <ecoli_string.h>
 #include <ecoli_strvec.h>
-#include <ecoli_keyval.h>
+#include <ecoli_dict.h>
 #include <ecoli_log.h>
 #include <ecoli_config.h>
 #include <ecoli_test.h>
 
 EC_LOG_TYPE_REGISTER(node);
 
+struct ec_node {
+       const struct ec_node_type *type;
+       struct ec_config *config;    /**< Generic configuration. */
+       char *id;
+       char *desc;
+       struct ec_dict *attrs;
+       unsigned int refcnt;
+       struct {
+               enum ec_node_free_state state; /**< State of loop detection */
+               unsigned int refcnt;    /**< Number of reachable references
+                                        *   starting from node beeing freed */
+       } free; /**< Freeing state: used for loop detection */
+};
+
 static struct ec_node_type_list node_type_list =
        TAILQ_HEAD_INITIALIZER(node_type_list);
 
@@ -44,8 +58,6 @@ ec_node_type_lookup(const char *name)
 
 int ec_node_type_register(struct ec_node_type *type)
 {
-       EC_CHECK_ARG(type->size >= sizeof(struct ec_node), -1, EINVAL);
-
        if (ec_node_type_lookup(type->name) != NULL) {
                errno = EEXIST;
                return -1;
@@ -75,7 +87,7 @@ struct ec_node *ec_node_from_type(const struct ec_node_type *type, const char *i
                goto fail;
        }
 
-       node = ec_calloc(1, type->size);
+       node = ec_calloc(1, sizeof(*node) + type->size);
        if (node == NULL)
                goto fail;
 
@@ -90,7 +102,7 @@ struct ec_node *ec_node_from_type(const struct ec_node_type *type, const char *i
        if (ec_asprintf(&node->desc, "<%s>", type->name) < 0)
                goto fail;
 
-       node->attrs = ec_keyval();
+       node->attrs = ec_dict();
        if (node->attrs == NULL)
                goto fail;
 
@@ -103,7 +115,7 @@ struct ec_node *ec_node_from_type(const struct ec_node_type *type, const char *i
 
  fail:
        if (node != NULL) {
-               ec_keyval_free(node->attrs);
+               ec_dict_free(node->attrs);
                ec_free(node->desc);
                ec_free(node->id);
        }
@@ -245,7 +257,7 @@ void ec_node_free(struct ec_node *node)
                        node->type->free_priv(node);
                ec_free(node->id);
                ec_free(node->desc);
-               ec_keyval_free(node->attrs);
+               ec_dict_free(node->attrs);
        }
 
        node->refcnt--;
@@ -340,7 +352,7 @@ const struct ec_node_type *ec_node_type(const struct ec_node *node)
        return node->type;
 }
 
-struct ec_keyval *ec_node_attrs(const struct ec_node *node)
+struct ec_dict *ec_node_attrs(const struct ec_node *node)
 {
        return node->attrs;
 }
@@ -351,7 +363,7 @@ const char *ec_node_id(const struct ec_node *node)
 }
 
 static void __ec_node_dump(FILE *out,
-       const struct ec_node *node, size_t indent, struct ec_keyval *dict)
+       const struct ec_node *node, size_t indent, struct ec_dict *dict)
 {
        const char *id, *typename;
        struct ec_node *child;
@@ -364,13 +376,13 @@ static void __ec_node_dump(FILE *out,
        typename = node->type->name;
 
        snprintf(buf, sizeof(buf), "%p", node);
-       if (ec_keyval_has_key(dict, buf)) {
+       if (ec_dict_has_key(dict, buf)) {
                fprintf(out, "%*s" "type=%s id=%s %p... (loop)\n",
                        (int)indent * 4, "", typename, id, node);
                return;
        }
 
-       ec_keyval_set(dict, buf, NULL, NULL);
+       ec_dict_set(dict, buf, NULL, NULL);
        fprintf(out, "%*s" "type=%s id=%s %p refs=%u free_state=%d free_refs=%d\n",
                (int)indent * 4, "", typename, id, node, node->refcnt,
                node->free.state, node->free.refcnt);
@@ -386,7 +398,7 @@ static void __ec_node_dump(FILE *out,
 /* XXX this is too much debug-oriented, we should have a parameter or 2 funcs */
 void ec_node_dump(FILE *out, const struct ec_node *node)
 {
-       struct ec_keyval *dict = NULL;
+       struct ec_dict *dict = NULL;
 
        fprintf(out, "------------------- node dump:\n");
 
@@ -395,17 +407,17 @@ void ec_node_dump(FILE *out, const struct ec_node *node)
                return;
        }
 
-       dict = ec_keyval();
+       dict = ec_dict();
        if (dict == NULL)
                goto fail;
 
        __ec_node_dump(out, node, 0, dict);
 
-       ec_keyval_free(dict);
+       ec_dict_free(dict);
        return;
 
 fail:
-       ec_keyval_free(dict);
+       ec_dict_free(dict);
        EC_LOG(EC_LOG_ERR, "failed to dump node\n");
 }
 
@@ -428,6 +440,18 @@ int ec_node_check_type(const struct ec_node *node,
        return 0;
 }
 
+const char *ec_node_get_type_name(const struct ec_node *node)
+{
+       return node->type->name;
+}
+
+void *ec_node_priv(const struct ec_node *node)
+{
+       if (node == NULL)
+               return NULL;
+       return (void *)(node + 1);
+}
+
 /* LCOV_EXCL_START */
 static int ec_node_testcase(void)
 {
@@ -507,7 +531,7 @@ static int ec_node_testcase(void)
        testres |= EC_TEST_CHECK(child == NULL,
                "child with wrong id should be NULL");
 
-       ret = ec_keyval_set(ec_node_attrs(node), "key", "val", NULL);
+       ret = ec_dict_set(ec_node_attrs(node), "key", "val", NULL);
        testres |= EC_TEST_CHECK(ret == 0,
                "cannot set node attribute\n");