add get_child_refs, tests are ok
[protos/libecoli.git] / lib / ecoli_node.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <stdint.h>
8 #include <string.h>
9 #include <assert.h>
10 #include <errno.h>
11
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>
20
21 #include <ecoli_node_str.h>
22 #include <ecoli_node_seq.h>
23 #include <ecoli_node_or.h>
24 #include <ecoli_node_int.h>
25
26 EC_LOG_TYPE_REGISTER(node);
27
28 static struct ec_node_type_list node_type_list =
29         TAILQ_HEAD_INITIALIZER(node_type_list);
30
31 const struct ec_node_type *
32 ec_node_type_lookup(const char *name)
33 {
34         struct ec_node_type *type;
35
36         TAILQ_FOREACH(type, &node_type_list, next) {
37                 if (!strcmp(name, type->name))
38                         return type;
39         }
40
41         errno = ENOENT;
42         return NULL;
43 }
44
45 int ec_node_type_register(struct ec_node_type *type)
46 {
47         EC_CHECK_ARG(type->size >= sizeof(struct ec_node), -1, EINVAL);
48
49         if (ec_node_type_lookup(type->name) != NULL) {
50                 errno = EEXIST;
51                 return -1;
52         }
53
54         TAILQ_INSERT_TAIL(&node_type_list, type, next);
55
56         return 0;
57 }
58
59 void ec_node_type_dump(FILE *out)
60 {
61         struct ec_node_type *type;
62
63         TAILQ_FOREACH(type, &node_type_list, next)
64                 fprintf(out, "%s\n", type->name);
65 }
66
67 struct ec_node *__ec_node(const struct ec_node_type *type, const char *id)
68 {
69         struct ec_node *node = NULL;
70
71         EC_LOG(EC_LOG_DEBUG, "create node type=%s id=%s\n",
72                 type->name, id);
73         if (id == NULL) {
74                 errno = EINVAL;
75                 goto fail;
76         }
77
78         node = ec_calloc(1, type->size);
79         if (node == NULL)
80                 goto fail;
81
82         node->type = type;
83         node->refcnt = 1;
84
85         node->id = ec_strdup(id);
86         if (node->id == NULL)
87                 goto fail;
88
89         if (ec_asprintf(&node->desc, "<%s>", type->name) < 0)
90                 goto fail;
91
92         node->attrs = ec_keyval();
93         if (node->attrs == NULL)
94                 goto fail;
95
96         if (type->init_priv != NULL) {
97                 if (type->init_priv(node) < 0)
98                         goto fail;
99         }
100
101         return node;
102
103  fail:
104         if (node != NULL) {
105                 ec_keyval_free(node->attrs);
106                 ec_free(node->desc);
107                 ec_free(node->id);
108         }
109         ec_free(node);
110
111         return NULL;
112 }
113
114 struct ec_node *ec_node(const char *typename, const char *id)
115 {
116         const struct ec_node_type *type;
117
118         type = ec_node_type_lookup(typename);
119         if (type == NULL) {
120                 EC_LOG(EC_LOG_ERR, "type=%s does not exist\n",
121                         typename);
122                 return NULL;
123         }
124
125         return __ec_node(type, id);
126 }
127
128 static void count_references(struct ec_node *node, unsigned int refs)
129 {
130         struct ec_node *child;
131         size_t i, n;
132
133         if (node->free.state == EC_NODE_FREE_STATE_TRAVERSED) {
134                 node->free.refcnt += refs;
135                 return;
136         }
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);
144         }
145 }
146
147 static void mark_freeable(struct ec_node *node, enum ec_node_free_state mark)
148 {
149         struct ec_node *child;
150         size_t i, n;
151
152         if (mark == node->free.state)
153                 return;
154
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;
159
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);
164         }
165 }
166
167 static void reset_mark(struct ec_node *node)
168 {
169         struct ec_node *child;
170         size_t i, n;
171
172         if (node->free.state == EC_NODE_FREE_STATE_NONE)
173                 return;
174
175         node->free.state = EC_NODE_FREE_STATE_NONE;
176         node->free.refcnt = 0;
177
178         n = ec_node_get_children_count(node);
179         for (i = 0; i < n; i++) {
180                 child = ec_node_get_child(node, i);
181                 reset_mark(child);
182         }
183 }
184
185 /* free a node, taking care of loops in the node graph */
186 void ec_node_free(struct ec_node *node)
187 {
188         struct ec_node *child;
189         size_t i, n;
190
191         if (node == NULL)
192                 return;
193
194         assert(node->refcnt > 0);
195
196         if (node->free.state == EC_NODE_FREE_STATE_NONE &&
197                         node->refcnt != 1) {
198
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
204                  * unfreeable. */
205                 if (node->free.state == EC_NODE_FREE_STATE_NONE) {
206                         count_references(node, 1);
207                         mark_freeable(node, EC_NODE_FREE_STATE_FREEABLE);
208                 }
209         }
210
211         if (node->free.state == EC_NODE_FREE_STATE_NOT_FREEABLE) {
212                 node->refcnt--;
213                 reset_mark(node);
214                 return;
215         }
216
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);
222                         ec_node_free(child);
223                 }
224         }
225
226         node->refcnt--;
227         if (node->refcnt != 0)
228                 return;
229
230         node->free.state = EC_NODE_FREE_STATE_NONE;
231         node->free.refcnt = 0;
232
233         if (node->type != NULL && node->type->free_priv != NULL)
234                 node->type->free_priv(node);
235         ec_free(node->id);
236         ec_free(node->desc);
237         ec_keyval_free(node->attrs);
238         ec_config_free(node->config);
239         ec_free(node);
240 }
241
242 struct ec_node *ec_node_clone(struct ec_node *node)
243 {
244         if (node != NULL)
245                 node->refcnt++;
246         return node;
247 }
248
249 size_t ec_node_get_children_count(const struct ec_node *node)
250 {
251         if (node->type->get_children_count == NULL)
252                 return 0;
253         return node->type->get_children_count(node);
254 }
255
256 struct ec_node *
257 ec_node_get_child(const struct ec_node *node, size_t i)
258 {
259         if (node->type->get_child == NULL)
260                 return NULL;
261         return node->type->get_child(node, i);
262 }
263
264 unsigned int
265 ec_node_get_child_refs(const struct ec_node *node, size_t i)
266 {
267         if (node->type->get_child_refs == NULL)
268                 return 1;
269         return node->type->get_child_refs(node, i);
270 }
271
272 int
273 ec_node_set_config(struct ec_node *node, struct ec_config *config)
274 {
275         if (node->type->schema == NULL) {
276                 errno = EINVAL;
277                 goto fail;
278         }
279         if (ec_config_validate(config, node->type->schema,
280                                 node->type->schema_len) < 0)
281                 goto fail;
282         if (node->type->set_config != NULL) {
283                 if (node->type->set_config(node, config) < 0)
284                         goto fail;
285         }
286
287         ec_config_free(node->config);
288         node->config = config;
289
290         return 0;
291
292 fail:
293         ec_config_free(config);
294         return -1;
295 }
296
297 const struct ec_config *ec_node_get_config(struct ec_node *node)
298 {
299         return node->config;
300 }
301
302 struct ec_node *ec_node_find(struct ec_node *node, const char *id)
303 {
304         struct ec_node *child, *ret;
305         const char *node_id = ec_node_id(node);
306         size_t i, n;
307
308         if (id != NULL && node_id != NULL && !strcmp(node_id, id))
309                 return node;
310
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);
315                 if (ret != NULL)
316                         return ret;
317         }
318
319         return NULL;
320 }
321
322 const struct ec_node_type *ec_node_type(const struct ec_node *node)
323 {
324         return node->type;
325 }
326
327 struct ec_keyval *ec_node_attrs(const struct ec_node *node)
328 {
329         return node->attrs;
330 }
331
332 const char *ec_node_id(const struct ec_node *node)
333 {
334         return node->id;
335 }
336
337 static void __ec_node_dump(FILE *out,
338         const struct ec_node *node, size_t indent, struct ec_keyval *dict)
339 {
340         const char *id, *typename;
341         struct ec_node *child;
342         char buf[32];
343         size_t i, n;
344
345         id = ec_node_id(node);
346         typename = node->type->name;
347
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);
352                 return;
353         }
354
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);
359
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);
364         }
365 }
366
367 void ec_node_dump(FILE *out, const struct ec_node *node)
368 {
369         struct ec_keyval *dict = NULL;
370
371         fprintf(out, "------------------- node dump:\n");
372
373         if (node == NULL) {
374                 fprintf(out, "node is NULL\n");
375                 return;
376         }
377
378         dict = ec_keyval();
379         if (dict == NULL)
380                 goto fail;
381
382         __ec_node_dump(out, node, 0, dict);
383
384         ec_keyval_free(dict);
385         return;
386
387 fail:
388         ec_keyval_free(dict);
389         EC_LOG(EC_LOG_ERR, "failed to dump node\n");
390 }
391
392 const char *ec_node_desc(const struct ec_node *node)
393 {
394         if (node->type->desc != NULL)
395                 return node->type->desc(node);
396
397         return node->desc;
398 }
399
400 int ec_node_check_type(const struct ec_node *node,
401                 const struct ec_node_type *type)
402 {
403         if (strcmp(node->type->name, type->name)) {
404                 errno = EINVAL;
405                 return -1;
406         }
407
408         return 0;
409 }
410
411 /* LCOV_EXCL_START */
412 static int ec_node_testcase(void)
413 {
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;
418         FILE *f = NULL;
419         char *buf = NULL;
420         size_t buflen = 0;
421         int testres = 0;
422         int ret;
423
424         node = EC_NODE_SEQ(EC_NO_ID,
425                         ec_node_str("id_x", "x"),
426                         ec_node_str("id_y", "y"));
427         if (node == NULL)
428                 goto fail;
429
430         ec_node_clone(node);
431         ec_node_free(node);
432
433         f = open_memstream(&buf, &buflen);
434         if (f == NULL)
435                 goto fail;
436         ec_node_dump(f, node);
437         ec_node_type_dump(f);
438         ec_node_dump(f, NULL);
439         fclose(f);
440         f = NULL;
441
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,
447                         "type=str id=id_y"),
448                 "bad dump\n");
449         free(buf);
450         buf = NULL;
451
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>"),
456                 "bad child 0");
457
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"),
465                 "bad child 0");
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"),
470                 "bad child 1");
471         child = ec_node_get_child(node, 2);
472         testres |= EC_TEST_CHECK(child == NULL,
473                 "child 2 should be NULL");
474
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"),
480                 "bad child id_x");
481         child = ec_node_find(node, "id_dezdex");
482         testres |= EC_TEST_CHECK(child == NULL,
483                 "child with wrong id should be NULL");
484
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");
488
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");
497
498         ec_node_free(node);
499         node = NULL;
500
501         node = ec_node("deznuindez", EC_NO_ID);
502         testres |= EC_TEST_CHECK(node == NULL,
503                         "should not be able to create node\n");
504
505         /* test loop */
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,
510                         op,
511                         ec_node_clone(expr));
512         op = NULL;
513         if (expr == NULL || val == NULL || seq == NULL)
514                 goto fail;
515         if (ec_node_or_add(expr, ec_node_clone(seq)) < 0)
516                 goto fail;
517         ec_node_free(seq);
518         seq = NULL;
519         if (ec_node_or_add(expr, ec_node_clone(val)) < 0)
520                 goto fail;
521         ec_node_free(val);
522         val = NULL;
523
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, "!", "!", "!");
527
528         ec_node_free(expr);
529         expr = NULL;
530
531         /* same loop test, but keep some refs (released later) */
532         expr = ec_node("or", EC_NO_ID);
533         ec_node_clone(expr);
534         expr2 = expr;
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,
538                         op,
539                         ec_node_clone(expr));
540         op = NULL;
541         if (expr == NULL || val == NULL || seq == NULL)
542                 goto fail;
543         if (ec_node_or_add(expr, ec_node_clone(seq)) < 0)
544                 goto fail;
545         ec_node_free(seq);
546         seq = NULL;
547         if (ec_node_or_add(expr, ec_node_clone(val)) < 0)
548                 goto fail;
549
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, "!", "!", "!");
553
554         ec_node_free(expr2);
555         expr2 = NULL;
556         ec_node_free(val);
557         val = NULL;
558         ec_node_free(expr);
559         expr = NULL;
560
561         return testres;
562
563 fail:
564         ec_node_free(expr);
565         ec_node_free(expr2);
566         ec_node_free(val);
567         ec_node_free(seq);
568         ec_node_free(node);
569         if (f != NULL)
570                 fclose(f);
571         free(buf);
572
573         assert(errno != 0);
574         return -1;
575 }
576 /* LCOV_EXCL_STOP */
577
578 static struct ec_test ec_node_test = {
579         .name = "node",
580         .test = ec_node_testcase,
581 };
582
583 EC_TEST_REGISTER(ec_node_test);