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(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 struct ec_node *ec_node(const char *typename, const char *id)
116 const struct ec_node_type *type;
118 type = ec_node_type_lookup(typename);
120 EC_LOG(EC_LOG_ERR, "type=%s does not exist\n",
125 return __ec_node(type, id);
128 static void count_references(struct ec_node *node, unsigned int refs)
130 struct ec_node *child;
133 if (node->free.state == EC_NODE_FREE_STATE_TRAVERSED) {
134 node->free.refcnt += refs;
137 node->free.refcnt = refs;
138 node->free.state = EC_NODE_FREE_STATE_TRAVERSED;
139 n = ec_node_get_children_count(node);
140 for (i = 0; i < n; i++) {
141 child = ec_node_get_child(node, i);
142 refs = ec_node_get_child_refs(node, i);
143 count_references(child, refs);
147 static void mark_freeable(struct ec_node *node, enum ec_node_free_state mark)
149 struct ec_node *child;
152 if (mark == node->free.state)
155 if (node->refcnt > node->free.refcnt)
156 mark = EC_NODE_FREE_STATE_NOT_FREEABLE;
157 assert(node->refcnt >= node->free.refcnt);
158 node->free.state = mark;
160 n = ec_node_get_children_count(node);
161 for (i = 0; i < n; i++) {
162 child = ec_node_get_child(node, i);
163 mark_freeable(child, mark);
167 static void reset_mark(struct ec_node *node)
169 struct ec_node *child;
172 if (node->free.state == EC_NODE_FREE_STATE_NONE)
175 node->free.state = EC_NODE_FREE_STATE_NONE;
176 node->free.refcnt = 0;
178 n = ec_node_get_children_count(node);
179 for (i = 0; i < n; i++) {
180 child = ec_node_get_child(node, i);
185 /* free a node, taking care of loops in the node graph */
186 void ec_node_free(struct ec_node *node)
188 struct ec_node *child;
194 assert(node->refcnt > 0);
196 if (node->free.state == EC_NODE_FREE_STATE_NONE &&
199 /* Traverse the node tree starting from this node, and for each
200 * node, count the number of reachable references. Then, all
201 * nodes whose reachable references == total reference are
202 * marked as freeable, and other are marked as unfreeable. Any
203 * node reachable from an unfreeable node is also marked as
205 if (node->free.state == EC_NODE_FREE_STATE_NONE) {
206 count_references(node, 1);
207 mark_freeable(node, EC_NODE_FREE_STATE_FREEABLE);
211 if (node->free.state == EC_NODE_FREE_STATE_NOT_FREEABLE) {
217 if (node->free.state != EC_NODE_FREE_STATE_FREEING) {
218 node->free.state = EC_NODE_FREE_STATE_FREEING;
219 n = ec_node_get_children_count(node);
220 for (i = 0; i < n; i++) {
221 child = ec_node_get_child(node, i);
227 if (node->refcnt != 0)
230 node->free.state = EC_NODE_FREE_STATE_NONE;
231 node->free.refcnt = 0;
233 if (node->type != NULL && node->type->free_priv != NULL)
234 node->type->free_priv(node);
237 ec_keyval_free(node->attrs);
238 ec_config_free(node->config);
242 struct ec_node *ec_node_clone(struct ec_node *node)
249 size_t ec_node_get_children_count(const struct ec_node *node)
251 if (node->type->get_children_count == NULL)
253 return node->type->get_children_count(node);
257 ec_node_get_child(const struct ec_node *node, size_t i)
259 if (node->type->get_child == NULL)
261 return node->type->get_child(node, i);
265 ec_node_get_child_refs(const struct ec_node *node, size_t i)
267 if (node->type->get_child_refs == NULL)
269 return node->type->get_child_refs(node, i);
273 ec_node_set_config(struct ec_node *node, struct ec_config *config)
275 if (node->type->schema == NULL) {
279 if (ec_config_validate(config, node->type->schema,
280 node->type->schema_len) < 0)
282 if (node->type->set_config != NULL) {
283 if (node->type->set_config(node, config) < 0)
287 ec_config_free(node->config);
288 node->config = config;
293 ec_config_free(config);
297 const struct ec_config *ec_node_get_config(struct ec_node *node)
302 struct ec_node *ec_node_find(struct ec_node *node, const char *id)
304 struct ec_node *child, *ret;
305 const char *node_id = ec_node_id(node);
308 if (id != NULL && node_id != NULL && !strcmp(node_id, id))
311 n = ec_node_get_children_count(node);
312 for (i = 0; i < n; i++) {
313 child = ec_node_get_child(node, i);
314 ret = ec_node_find(child, id);
322 const struct ec_node_type *ec_node_type(const struct ec_node *node)
327 struct ec_keyval *ec_node_attrs(const struct ec_node *node)
332 const char *ec_node_id(const struct ec_node *node)
337 static void __ec_node_dump(FILE *out,
338 const struct ec_node *node, size_t indent, struct ec_keyval *dict)
340 const char *id, *typename;
341 struct ec_node *child;
345 id = ec_node_id(node);
346 typename = node->type->name;
348 snprintf(buf, sizeof(buf), "%p", node);
349 if (ec_keyval_has_key(dict, buf)) {
350 fprintf(out, "%*s" "type=%s id=%s %p... (loop)\n",
351 (int)indent * 4, "", typename, id, node);
355 ec_keyval_set(dict, buf, NULL, NULL);
356 fprintf(out, "%*s" "type=%s id=%s %p refs=%u free_state=%d free_refs=%d\n",
357 (int)indent * 4, "", typename, id, node, node->refcnt,
358 node->free.state, node->free.refcnt);
360 n = ec_node_get_children_count(node);
361 for (i = 0; i < n; i++) {
362 child = ec_node_get_child(node, i);
363 __ec_node_dump(out, child, indent + 1, dict);
367 void ec_node_dump(FILE *out, const struct ec_node *node)
369 struct ec_keyval *dict = NULL;
371 fprintf(out, "------------------- node dump:\n");
374 fprintf(out, "node is NULL\n");
382 __ec_node_dump(out, node, 0, dict);
384 ec_keyval_free(dict);
388 ec_keyval_free(dict);
389 EC_LOG(EC_LOG_ERR, "failed to dump node\n");
392 const char *ec_node_desc(const struct ec_node *node)
394 if (node->type->desc != NULL)
395 return node->type->desc(node);
400 int ec_node_check_type(const struct ec_node *node,
401 const struct ec_node_type *type)
403 if (strcmp(node->type->name, type->name)) {
411 /* LCOV_EXCL_START */
412 static int ec_node_testcase(void)
414 struct ec_node *node = NULL, *expr = NULL;
415 struct ec_node *expr2 = NULL, *val = NULL, *op = NULL, *seq = NULL;
416 const struct ec_node *child;
417 const struct ec_node_type *type;
424 node = EC_NODE_SEQ(EC_NO_ID,
425 ec_node_str("id_x", "x"),
426 ec_node_str("id_y", "y"));
433 f = open_memstream(&buf, &buflen);
436 ec_node_dump(f, node);
437 ec_node_type_dump(f);
438 ec_node_dump(f, NULL);
442 testres |= EC_TEST_CHECK(
443 strstr(buf, "type=seq id=no-id"), "bad dump\n");
444 testres |= EC_TEST_CHECK(
445 strstr(buf, "type=str id=id_x") &&
446 strstr(strstr(buf, "type=str id=id_x") + 1,
452 testres |= EC_TEST_CHECK(
453 !strcmp(ec_node_type(node)->name, "seq") &&
454 !strcmp(ec_node_id(node), EC_NO_ID) &&
455 !strcmp(ec_node_desc(node), "<seq>"),
458 testres |= EC_TEST_CHECK(
459 ec_node_get_children_count(node) == 2,
460 "bad children count\n");
461 child = ec_node_get_child(node, 0);
462 testres |= EC_TEST_CHECK(child != NULL &&
463 !strcmp(ec_node_type(child)->name, "str") &&
464 !strcmp(ec_node_id(child), "id_x"),
466 child = ec_node_get_child(node, 1);
467 testres |= EC_TEST_CHECK(child != NULL &&
468 !strcmp(ec_node_type(child)->name, "str") &&
469 !strcmp(ec_node_id(child), "id_y"),
471 child = ec_node_get_child(node, 2);
472 testres |= EC_TEST_CHECK(child == NULL,
473 "child 2 should be NULL");
475 child = ec_node_find(node, "id_x");
476 testres |= EC_TEST_CHECK(child != NULL &&
477 !strcmp(ec_node_type(child)->name, "str") &&
478 !strcmp(ec_node_id(child), "id_x") &&
479 !strcmp(ec_node_desc(child), "x"),
481 child = ec_node_find(node, "id_dezdex");
482 testres |= EC_TEST_CHECK(child == NULL,
483 "child with wrong id should be NULL");
485 ret = ec_keyval_set(ec_node_attrs(node), "key", "val", NULL);
486 testres |= EC_TEST_CHECK(ret == 0,
487 "cannot set node attribute\n");
489 type = ec_node_type_lookup("seq");
490 testres |= EC_TEST_CHECK(type != NULL &&
491 ec_node_check_type(node, type) == 0,
492 "cannot get seq node type");
493 type = ec_node_type_lookup("str");
494 testres |= EC_TEST_CHECK(type != NULL &&
495 ec_node_check_type(node, type) < 0,
496 "node type should not be str");
501 node = ec_node("deznuindez", EC_NO_ID);
502 testres |= EC_TEST_CHECK(node == NULL,
503 "should not be able to create node\n");
506 expr = ec_node("or", EC_NO_ID);
507 val = ec_node_int(EC_NO_ID, 0, 10, 0);
508 op = ec_node_str(EC_NO_ID, "!");
509 seq = EC_NODE_SEQ(EC_NO_ID,
511 ec_node_clone(expr));
513 if (expr == NULL || val == NULL || seq == NULL)
515 if (ec_node_or_add(expr, ec_node_clone(seq)) < 0)
519 if (ec_node_or_add(expr, ec_node_clone(val)) < 0)
524 testres |= EC_TEST_CHECK_PARSE(expr, 1, "1");
525 testres |= EC_TEST_CHECK_PARSE(expr, 3, "!", "!", "1");
526 testres |= EC_TEST_CHECK_PARSE(expr, -1, "!", "!", "!");
531 /* same loop test, but keep some refs (released later) */
532 expr = ec_node("or", EC_NO_ID);
535 val = ec_node_int(EC_NO_ID, 0, 10, 0);
536 op = ec_node_str(EC_NO_ID, "!");
537 seq = EC_NODE_SEQ(EC_NO_ID,
539 ec_node_clone(expr));
541 if (expr == NULL || val == NULL || seq == NULL)
543 if (ec_node_or_add(expr, ec_node_clone(seq)) < 0)
547 if (ec_node_or_add(expr, ec_node_clone(val)) < 0)
550 testres |= EC_TEST_CHECK_PARSE(expr, 1, "1");
551 testres |= EC_TEST_CHECK_PARSE(expr, 3, "!", "!", "1");
552 testres |= EC_TEST_CHECK_PARSE(expr, -1, "!", "!", "!");
578 static struct ec_test ec_node_test = {
580 .test = ec_node_testcase,
583 EC_TEST_REGISTER(ec_node_test);