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 // XXX check that id matches [_a-zA-Z][:-_0-9a-zA-Z]*
86 node->id = ec_strdup(id);
90 if (ec_asprintf(&node->desc, "<%s>", type->name) < 0)
93 node->attrs = ec_keyval();
94 if (node->attrs == NULL)
97 if (type->init_priv != NULL) {
98 if (type->init_priv(node) < 0)
106 ec_keyval_free(node->attrs);
115 const struct ec_config_schema *
116 ec_node_type_schema(const struct ec_node_type *type)
122 ec_node_type_name(const struct ec_node_type *type)
127 struct ec_node *ec_node(const char *typename, const char *id)
129 const struct ec_node_type *type;
131 type = ec_node_type_lookup(typename);
133 EC_LOG(EC_LOG_ERR, "type=%s does not exist\n",
138 return ec_node_from_type(type, id);
141 static void count_references(struct ec_node *node, unsigned int refs)
143 struct ec_node *child;
147 if (node->free.state == EC_NODE_FREE_STATE_TRAVERSED) {
148 node->free.refcnt += refs;
151 node->free.refcnt = refs;
152 node->free.state = EC_NODE_FREE_STATE_TRAVERSED;
153 n = ec_node_get_children_count(node);
154 for (i = 0; i < n; i++) {
155 ret = ec_node_get_child(node, i, &child, &refs);
157 count_references(child, refs);
161 static void mark_freeable(struct ec_node *node, enum ec_node_free_state mark)
163 struct ec_node *child;
168 if (mark == node->free.state)
171 if (node->refcnt > node->free.refcnt)
172 mark = EC_NODE_FREE_STATE_NOT_FREEABLE;
173 assert(node->refcnt >= node->free.refcnt);
174 node->free.state = mark;
176 n = ec_node_get_children_count(node);
177 for (i = 0; i < n; i++) {
178 ret = ec_node_get_child(node, i, &child, &refs);
180 mark_freeable(child, mark);
184 static void reset_mark(struct ec_node *node)
186 struct ec_node *child;
191 if (node->free.state == EC_NODE_FREE_STATE_NONE)
194 node->free.state = EC_NODE_FREE_STATE_NONE;
195 node->free.refcnt = 0;
197 n = ec_node_get_children_count(node);
198 for (i = 0; i < n; i++) {
199 ret = ec_node_get_child(node, i, &child, &refs);
205 /* free a node, taking care of loops in the node graph */
206 void ec_node_free(struct ec_node *node)
213 assert(node->refcnt > 0);
215 if (node->free.state == EC_NODE_FREE_STATE_NONE &&
218 /* Traverse the node tree starting from this node, and for each
219 * node, count the number of reachable references. Then, all
220 * nodes whose reachable references == total reference are
221 * marked as freeable, and other are marked as unfreeable. Any
222 * node reachable from an unfreeable node is also marked as
224 if (node->free.state == EC_NODE_FREE_STATE_NONE) {
225 count_references(node, 1);
226 mark_freeable(node, EC_NODE_FREE_STATE_FREEABLE);
230 if (node->free.state == EC_NODE_FREE_STATE_NOT_FREEABLE) {
236 if (node->free.state != EC_NODE_FREE_STATE_FREEING) {
237 node->free.state = EC_NODE_FREE_STATE_FREEING;
239 /* children will be freed by config_free() and free_priv() */
240 ec_config_free(node->config);
242 n = ec_node_get_children_count(node);
243 assert(n == 0 || node->type->free_priv != NULL);
244 if (node->type->free_priv != NULL)
245 node->type->free_priv(node);
248 ec_keyval_free(node->attrs);
252 if (node->refcnt != 0)
255 node->free.state = EC_NODE_FREE_STATE_NONE;
256 node->free.refcnt = 0;
261 struct ec_node *ec_node_clone(struct ec_node *node)
268 size_t ec_node_get_children_count(const struct ec_node *node)
270 if (node->type->get_children_count == NULL)
272 return node->type->get_children_count(node);
276 ec_node_get_child(const struct ec_node *node, size_t i,
277 struct ec_node **child, unsigned int *refs)
281 if (node->type->get_child == NULL)
283 return node->type->get_child(node, i, child, refs);
287 ec_node_set_config(struct ec_node *node, struct ec_config *config)
289 if (node->type->schema == NULL) {
293 if (ec_config_validate(config, node->type->schema) < 0)
295 if (node->type->set_config != NULL) {
296 if (node->type->set_config(node, config) < 0)
300 ec_config_free(node->config);
301 node->config = config;
306 ec_config_free(config);
310 const struct ec_config *ec_node_get_config(struct ec_node *node)
315 struct ec_node *ec_node_find(struct ec_node *node, const char *id)
317 struct ec_node *child, *retnode;
318 const char *node_id = ec_node_id(node);
323 if (id != NULL && node_id != NULL && !strcmp(node_id, id))
326 n = ec_node_get_children_count(node);
327 for (i = 0; i < n; i++) {
328 ret = ec_node_get_child(node, i, &child, &refs);
330 retnode = ec_node_find(child, id);
338 const struct ec_node_type *ec_node_type(const struct ec_node *node)
343 struct ec_keyval *ec_node_attrs(const struct ec_node *node)
348 const char *ec_node_id(const struct ec_node *node)
353 static void __ec_node_dump(FILE *out,
354 const struct ec_node *node, size_t indent, struct ec_keyval *dict)
356 const char *id, *typename;
357 struct ec_node *child;
363 id = ec_node_id(node);
364 typename = node->type->name;
366 snprintf(buf, sizeof(buf), "%p", node);
367 if (ec_keyval_has_key(dict, buf)) {
368 fprintf(out, "%*s" "type=%s id=%s %p... (loop)\n",
369 (int)indent * 4, "", typename, id, node);
373 ec_keyval_set(dict, buf, NULL, NULL);
374 fprintf(out, "%*s" "type=%s id=%s %p refs=%u free_state=%d free_refs=%d\n",
375 (int)indent * 4, "", typename, id, node, node->refcnt,
376 node->free.state, node->free.refcnt);
378 n = ec_node_get_children_count(node);
379 for (i = 0; i < n; i++) {
380 ret = ec_node_get_child(node, i, &child, &refs);
382 __ec_node_dump(out, child, indent + 1, dict);
386 /* XXX this is too much debug-oriented, we should have a parameter or 2 funcs */
387 void ec_node_dump(FILE *out, const struct ec_node *node)
389 struct ec_keyval *dict = NULL;
391 fprintf(out, "------------------- node dump:\n");
394 fprintf(out, "node is NULL\n");
402 __ec_node_dump(out, node, 0, dict);
404 ec_keyval_free(dict);
408 ec_keyval_free(dict);
409 EC_LOG(EC_LOG_ERR, "failed to dump node\n");
412 const char *ec_node_desc(const struct ec_node *node)
414 if (node->type->desc != NULL)
415 return node->type->desc(node);
420 int ec_node_check_type(const struct ec_node *node,
421 const struct ec_node_type *type)
423 if (strcmp(node->type->name, type->name)) {
431 /* LCOV_EXCL_START */
432 static int ec_node_testcase(void)
434 struct ec_node *node = NULL, *expr = NULL;
435 struct ec_node *expr2 = NULL, *val = NULL, *op = NULL, *seq = NULL;
436 const struct ec_node_type *type;
437 struct ec_node *child;
445 node = EC_NODE_SEQ(EC_NO_ID,
446 ec_node_str("id_x", "x"),
447 ec_node_str("id_y", "y"));
454 f = open_memstream(&buf, &buflen);
457 ec_node_dump(f, node);
458 ec_node_type_dump(f);
459 ec_node_dump(f, NULL);
463 testres |= EC_TEST_CHECK(
464 strstr(buf, "type=seq id=no-id"), "bad dump\n");
465 testres |= EC_TEST_CHECK(
466 strstr(buf, "type=str id=id_x") &&
467 strstr(strstr(buf, "type=str id=id_x") + 1,
473 testres |= EC_TEST_CHECK(
474 !strcmp(ec_node_type(node)->name, "seq") &&
475 !strcmp(ec_node_id(node), EC_NO_ID) &&
476 !strcmp(ec_node_desc(node), "<seq>"),
479 testres |= EC_TEST_CHECK(
480 ec_node_get_children_count(node) == 2,
481 "bad children count\n");
482 ret = ec_node_get_child(node, 0, &child, &refs);
483 testres |= EC_TEST_CHECK(ret == 0 &&
485 !strcmp(ec_node_type(child)->name, "str") &&
486 !strcmp(ec_node_id(child), "id_x"),
488 ret = ec_node_get_child(node, 1, &child, &refs);
489 testres |= EC_TEST_CHECK(ret == 0 &&
491 !strcmp(ec_node_type(child)->name, "str") &&
492 !strcmp(ec_node_id(child), "id_y"),
494 ret = ec_node_get_child(node, 2, &child, &refs);
495 testres |= EC_TEST_CHECK(ret != 0,
496 "ret should be != 0");
497 testres |= EC_TEST_CHECK(child == NULL,
498 "child 2 should be NULL");
500 child = ec_node_find(node, "id_x");
501 testres |= EC_TEST_CHECK(child != NULL &&
502 !strcmp(ec_node_type(child)->name, "str") &&
503 !strcmp(ec_node_id(child), "id_x") &&
504 !strcmp(ec_node_desc(child), "x"),
506 child = ec_node_find(node, "id_dezdex");
507 testres |= EC_TEST_CHECK(child == NULL,
508 "child with wrong id should be NULL");
510 ret = ec_keyval_set(ec_node_attrs(node), "key", "val", NULL);
511 testres |= EC_TEST_CHECK(ret == 0,
512 "cannot set node attribute\n");
514 type = ec_node_type_lookup("seq");
515 testres |= EC_TEST_CHECK(type != NULL &&
516 ec_node_check_type(node, type) == 0,
517 "cannot get seq node type");
518 type = ec_node_type_lookup("str");
519 testres |= EC_TEST_CHECK(type != NULL &&
520 ec_node_check_type(node, type) < 0,
521 "node type should not be str");
526 node = ec_node("deznuindez", EC_NO_ID);
527 testres |= EC_TEST_CHECK(node == NULL,
528 "should not be able to create node\n");
531 expr = ec_node("or", EC_NO_ID);
532 val = ec_node_int(EC_NO_ID, 0, 10, 0);
533 op = ec_node_str(EC_NO_ID, "!");
534 seq = EC_NODE_SEQ(EC_NO_ID,
536 ec_node_clone(expr));
538 if (expr == NULL || val == NULL || seq == NULL)
540 if (ec_node_or_add(expr, ec_node_clone(seq)) < 0)
544 if (ec_node_or_add(expr, ec_node_clone(val)) < 0)
549 testres |= EC_TEST_CHECK_PARSE(expr, 1, "1");
550 testres |= EC_TEST_CHECK_PARSE(expr, 3, "!", "!", "1");
551 testres |= EC_TEST_CHECK_PARSE(expr, -1, "!", "!", "!");
556 /* same loop test, but keep some refs (released later) */
557 expr = ec_node("or", EC_NO_ID);
560 val = ec_node_int(EC_NO_ID, 0, 10, 0);
561 op = ec_node_str(EC_NO_ID, "!");
562 seq = EC_NODE_SEQ(EC_NO_ID,
564 ec_node_clone(expr));
566 if (expr == NULL || val == NULL || seq == NULL)
568 if (ec_node_or_add(expr, ec_node_clone(seq)) < 0)
572 if (ec_node_or_add(expr, ec_node_clone(val)) < 0)
575 testres |= EC_TEST_CHECK_PARSE(expr, 1, "1");
576 testres |= EC_TEST_CHECK_PARSE(expr, 3, "!", "!", "1");
577 testres |= EC_TEST_CHECK_PARSE(expr, -1, "!", "!", "!");
603 static struct ec_test ec_node_test = {
605 .test = ec_node_testcase,
608 EC_TEST_REGISTER(ec_node_test);