1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
12 #include <ecoli_malloc.h>
13 #include <ecoli_string.h>
14 #include <ecoli_strvec.h>
15 #include <ecoli_keyval.h>
16 #include <ecoli_log.h>
17 #include <ecoli_config.h>
18 #include <ecoli_test.h>
19 #include <ecoli_node.h>
21 #include <ecoli_node_str.h>
22 #include <ecoli_node_seq.h>
23 #include <ecoli_node_or.h>
24 #include <ecoli_node_int.h>
26 EC_LOG_TYPE_REGISTER(node);
28 static struct ec_node_type_list node_type_list =
29 TAILQ_HEAD_INITIALIZER(node_type_list);
31 const struct ec_node_type *
32 ec_node_type_lookup(const char *name)
34 struct ec_node_type *type;
36 TAILQ_FOREACH(type, &node_type_list, next) {
37 if (!strcmp(name, type->name))
45 int ec_node_type_register(struct ec_node_type *type)
47 EC_CHECK_ARG(type->size >= sizeof(struct ec_node), -1, EINVAL);
49 if (ec_node_type_lookup(type->name) != NULL) {
54 TAILQ_INSERT_TAIL(&node_type_list, type, next);
59 void ec_node_type_dump(FILE *out)
61 struct ec_node_type *type;
63 TAILQ_FOREACH(type, &node_type_list, next)
64 fprintf(out, "%s\n", type->name);
67 struct ec_node *ec_node_from_type(const struct ec_node_type *type, const char *id)
69 struct ec_node *node = NULL;
71 EC_LOG(EC_LOG_DEBUG, "create node type=%s id=%s\n",
78 node = ec_calloc(1, type->size);
85 node->id = ec_strdup(id);
89 if (ec_asprintf(&node->desc, "<%s>", type->name) < 0)
92 node->attrs = ec_keyval();
93 if (node->attrs == NULL)
96 if (type->init_priv != NULL) {
97 if (type->init_priv(node) < 0)
105 ec_keyval_free(node->attrs);
114 const struct ec_config_schema *
115 ec_node_type_schema(const struct ec_node_type *type)
121 ec_node_type_name(const struct ec_node_type *type)
126 struct ec_node *ec_node(const char *typename, const char *id)
128 const struct ec_node_type *type;
130 type = ec_node_type_lookup(typename);
132 EC_LOG(EC_LOG_ERR, "type=%s does not exist\n",
137 return ec_node_from_type(type, id);
140 static void count_references(struct ec_node *node, unsigned int refs)
142 struct ec_node *child;
146 if (node->free.state == EC_NODE_FREE_STATE_TRAVERSED) {
147 node->free.refcnt += refs;
150 node->free.refcnt = refs;
151 node->free.state = EC_NODE_FREE_STATE_TRAVERSED;
152 n = ec_node_get_children_count(node);
153 for (i = 0; i < n; i++) {
154 ret = ec_node_get_child(node, i, &child, &refs);
156 count_references(child, refs);
160 static void mark_freeable(struct ec_node *node, enum ec_node_free_state mark)
162 struct ec_node *child;
167 if (mark == node->free.state)
170 if (node->refcnt > node->free.refcnt)
171 mark = EC_NODE_FREE_STATE_NOT_FREEABLE;
172 assert(node->refcnt >= node->free.refcnt);
173 node->free.state = mark;
175 n = ec_node_get_children_count(node);
176 for (i = 0; i < n; i++) {
177 ret = ec_node_get_child(node, i, &child, &refs);
179 mark_freeable(child, mark);
183 static void reset_mark(struct ec_node *node)
185 struct ec_node *child;
190 if (node->free.state == EC_NODE_FREE_STATE_NONE)
193 node->free.state = EC_NODE_FREE_STATE_NONE;
194 node->free.refcnt = 0;
196 n = ec_node_get_children_count(node);
197 for (i = 0; i < n; i++) {
198 ret = ec_node_get_child(node, i, &child, &refs);
204 /* free a node, taking care of loops in the node graph */
205 void ec_node_free(struct ec_node *node)
212 assert(node->refcnt > 0);
214 if (node->free.state == EC_NODE_FREE_STATE_NONE &&
217 /* Traverse the node tree starting from this node, and for each
218 * node, count the number of reachable references. Then, all
219 * nodes whose reachable references == total reference are
220 * marked as freeable, and other are marked as unfreeable. Any
221 * node reachable from an unfreeable node is also marked as
223 if (node->free.state == EC_NODE_FREE_STATE_NONE) {
224 count_references(node, 1);
225 mark_freeable(node, EC_NODE_FREE_STATE_FREEABLE);
229 if (node->free.state == EC_NODE_FREE_STATE_NOT_FREEABLE) {
235 if (node->free.state != EC_NODE_FREE_STATE_FREEING) {
236 node->free.state = EC_NODE_FREE_STATE_FREEING;
238 /* children will be freed by config_free() and free_priv() */
239 ec_config_free(node->config);
241 n = ec_node_get_children_count(node);
242 assert(n == 0 || node->type->free_priv != NULL);
243 if (node->type->free_priv != NULL)
244 node->type->free_priv(node);
247 ec_keyval_free(node->attrs);
251 if (node->refcnt != 0)
254 node->free.state = EC_NODE_FREE_STATE_NONE;
255 node->free.refcnt = 0;
260 struct ec_node *ec_node_clone(struct ec_node *node)
267 size_t ec_node_get_children_count(const struct ec_node *node)
269 if (node->type->get_children_count == NULL)
271 return node->type->get_children_count(node);
275 ec_node_get_child(const struct ec_node *node, size_t i,
276 struct ec_node **child, unsigned int *refs)
280 if (node->type->get_child == NULL)
282 return node->type->get_child(node, i, child, refs);
286 ec_node_set_config(struct ec_node *node, struct ec_config *config)
288 if (node->type->schema == NULL) {
292 if (ec_config_validate(config, node->type->schema) < 0)
294 if (node->type->set_config != NULL) {
295 if (node->type->set_config(node, config) < 0)
299 ec_config_free(node->config);
300 node->config = config;
305 ec_config_free(config);
309 const struct ec_config *ec_node_get_config(struct ec_node *node)
314 struct ec_node *ec_node_find(struct ec_node *node, const char *id)
316 struct ec_node *child, *retnode;
317 const char *node_id = ec_node_id(node);
322 if (id != NULL && node_id != NULL && !strcmp(node_id, id))
325 n = ec_node_get_children_count(node);
326 for (i = 0; i < n; i++) {
327 ret = ec_node_get_child(node, i, &child, &refs);
329 retnode = ec_node_find(child, id);
337 const struct ec_node_type *ec_node_type(const struct ec_node *node)
342 struct ec_keyval *ec_node_attrs(const struct ec_node *node)
347 const char *ec_node_id(const struct ec_node *node)
352 static void __ec_node_dump(FILE *out,
353 const struct ec_node *node, size_t indent, struct ec_keyval *dict)
355 const char *id, *typename;
356 struct ec_node *child;
362 id = ec_node_id(node);
363 typename = node->type->name;
365 snprintf(buf, sizeof(buf), "%p", node);
366 if (ec_keyval_has_key(dict, buf)) {
367 fprintf(out, "%*s" "type=%s id=%s %p... (loop)\n",
368 (int)indent * 4, "", typename, id, node);
372 ec_keyval_set(dict, buf, NULL, NULL);
373 fprintf(out, "%*s" "type=%s id=%s %p refs=%u free_state=%d free_refs=%d\n",
374 (int)indent * 4, "", typename, id, node, node->refcnt,
375 node->free.state, node->free.refcnt);
377 n = ec_node_get_children_count(node);
378 for (i = 0; i < n; i++) {
379 ret = ec_node_get_child(node, i, &child, &refs);
381 __ec_node_dump(out, child, indent + 1, dict);
385 /* XXX this is too much debug-oriented, we should have a parameter or 2 funcs */
386 void ec_node_dump(FILE *out, const struct ec_node *node)
388 struct ec_keyval *dict = NULL;
390 fprintf(out, "------------------- node dump:\n");
393 fprintf(out, "node is NULL\n");
401 __ec_node_dump(out, node, 0, dict);
403 ec_keyval_free(dict);
407 ec_keyval_free(dict);
408 EC_LOG(EC_LOG_ERR, "failed to dump node\n");
411 const char *ec_node_desc(const struct ec_node *node)
413 if (node->type->desc != NULL)
414 return node->type->desc(node);
419 int ec_node_check_type(const struct ec_node *node,
420 const struct ec_node_type *type)
422 if (strcmp(node->type->name, type->name)) {
430 /* LCOV_EXCL_START */
431 static int ec_node_testcase(void)
433 struct ec_node *node = NULL, *expr = NULL;
434 struct ec_node *expr2 = NULL, *val = NULL, *op = NULL, *seq = NULL;
435 const struct ec_node_type *type;
436 struct ec_node *child;
444 node = EC_NODE_SEQ(EC_NO_ID,
445 ec_node_str("id_x", "x"),
446 ec_node_str("id_y", "y"));
453 f = open_memstream(&buf, &buflen);
456 ec_node_dump(f, node);
457 ec_node_type_dump(f);
458 ec_node_dump(f, NULL);
462 testres |= EC_TEST_CHECK(
463 strstr(buf, "type=seq id=no-id"), "bad dump\n");
464 testres |= EC_TEST_CHECK(
465 strstr(buf, "type=str id=id_x") &&
466 strstr(strstr(buf, "type=str id=id_x") + 1,
472 testres |= EC_TEST_CHECK(
473 !strcmp(ec_node_type(node)->name, "seq") &&
474 !strcmp(ec_node_id(node), EC_NO_ID) &&
475 !strcmp(ec_node_desc(node), "<seq>"),
478 testres |= EC_TEST_CHECK(
479 ec_node_get_children_count(node) == 2,
480 "bad children count\n");
481 ret = ec_node_get_child(node, 0, &child, &refs);
482 testres |= EC_TEST_CHECK(ret == 0 &&
484 !strcmp(ec_node_type(child)->name, "str") &&
485 !strcmp(ec_node_id(child), "id_x"),
487 ret = ec_node_get_child(node, 1, &child, &refs);
488 testres |= EC_TEST_CHECK(ret == 0 &&
490 !strcmp(ec_node_type(child)->name, "str") &&
491 !strcmp(ec_node_id(child), "id_y"),
493 ret = ec_node_get_child(node, 2, &child, &refs);
494 testres |= EC_TEST_CHECK(ret != 0,
495 "ret should be != 0");
496 testres |= EC_TEST_CHECK(child == NULL,
497 "child 2 should be NULL");
499 child = ec_node_find(node, "id_x");
500 testres |= EC_TEST_CHECK(child != NULL &&
501 !strcmp(ec_node_type(child)->name, "str") &&
502 !strcmp(ec_node_id(child), "id_x") &&
503 !strcmp(ec_node_desc(child), "x"),
505 child = ec_node_find(node, "id_dezdex");
506 testres |= EC_TEST_CHECK(child == NULL,
507 "child with wrong id should be NULL");
509 ret = ec_keyval_set(ec_node_attrs(node), "key", "val", NULL);
510 testres |= EC_TEST_CHECK(ret == 0,
511 "cannot set node attribute\n");
513 type = ec_node_type_lookup("seq");
514 testres |= EC_TEST_CHECK(type != NULL &&
515 ec_node_check_type(node, type) == 0,
516 "cannot get seq node type");
517 type = ec_node_type_lookup("str");
518 testres |= EC_TEST_CHECK(type != NULL &&
519 ec_node_check_type(node, type) < 0,
520 "node type should not be str");
525 node = ec_node("deznuindez", EC_NO_ID);
526 testres |= EC_TEST_CHECK(node == NULL,
527 "should not be able to create node\n");
530 expr = ec_node("or", EC_NO_ID);
531 val = ec_node_int(EC_NO_ID, 0, 10, 0);
532 op = ec_node_str(EC_NO_ID, "!");
533 seq = EC_NODE_SEQ(EC_NO_ID,
535 ec_node_clone(expr));
537 if (expr == NULL || val == NULL || seq == NULL)
539 if (ec_node_or_add(expr, ec_node_clone(seq)) < 0)
543 if (ec_node_or_add(expr, ec_node_clone(val)) < 0)
548 testres |= EC_TEST_CHECK_PARSE(expr, 1, "1");
549 testres |= EC_TEST_CHECK_PARSE(expr, 3, "!", "!", "1");
550 testres |= EC_TEST_CHECK_PARSE(expr, -1, "!", "!", "!");
555 /* same loop test, but keep some refs (released later) */
556 expr = ec_node("or", EC_NO_ID);
559 val = ec_node_int(EC_NO_ID, 0, 10, 0);
560 op = ec_node_str(EC_NO_ID, "!");
561 seq = EC_NODE_SEQ(EC_NO_ID,
563 ec_node_clone(expr));
565 if (expr == NULL || val == NULL || seq == NULL)
567 if (ec_node_or_add(expr, ec_node_clone(seq)) < 0)
571 if (ec_node_or_add(expr, ec_node_clone(val)) < 0)
574 testres |= EC_TEST_CHECK_PARSE(expr, 1, "1");
575 testres |= EC_TEST_CHECK_PARSE(expr, 3, "!", "!", "1");
576 testres |= EC_TEST_CHECK_PARSE(expr, -1, "!", "!", "!");
602 static struct ec_test ec_node_test = {
604 .test = ec_node_testcase,
607 EC_TEST_REGISTER(ec_node_test);