c02a8dedc1703632b7582845d4a756aa556a85dc
[protos/libecoli.git] / libecoli / 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_from_type(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 const struct ec_config_schema *
115 ec_node_type_schema(const struct ec_node_type *type)
116 {
117         return type->schema;
118 }
119
120 const char *
121 ec_node_type_name(const struct ec_node_type *type)
122 {
123         return type->name;
124 }
125
126 struct ec_node *ec_node(const char *typename, const char *id)
127 {
128         const struct ec_node_type *type;
129
130         type = ec_node_type_lookup(typename);
131         if (type == NULL) {
132                 EC_LOG(EC_LOG_ERR, "type=%s does not exist\n",
133                         typename);
134                 return NULL;
135         }
136
137         return ec_node_from_type(type, id);
138 }
139
140 static void count_references(struct ec_node *node, unsigned int refs)
141 {
142         struct ec_node *child;
143         size_t i, n;
144         int ret;
145
146         if (node->free.state == EC_NODE_FREE_STATE_TRAVERSED) {
147                 node->free.refcnt += refs;
148                 return;
149         }
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);
155                 assert(ret == 0);
156                 count_references(child, refs);
157         }
158 }
159
160 static void mark_freeable(struct ec_node *node, enum ec_node_free_state mark)
161 {
162         struct ec_node *child;
163         unsigned int refs;
164         size_t i, n;
165         int ret;
166
167         if (mark == node->free.state)
168                 return;
169
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;
174
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);
178                 assert(ret == 0);
179                 mark_freeable(child, mark);
180         }
181 }
182
183 static void reset_mark(struct ec_node *node)
184 {
185         struct ec_node *child;
186         unsigned int refs;
187         size_t i, n;
188         int ret;
189
190         if (node->free.state == EC_NODE_FREE_STATE_NONE)
191                 return;
192
193         node->free.state = EC_NODE_FREE_STATE_NONE;
194         node->free.refcnt = 0;
195
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);
199                 assert(ret == 0);
200                 reset_mark(child);
201         }
202 }
203
204 /* free a node, taking care of loops in the node graph */
205 void ec_node_free(struct ec_node *node)
206 {
207         size_t n;
208
209         if (node == NULL)
210                 return;
211
212         assert(node->refcnt > 0);
213
214         if (node->free.state == EC_NODE_FREE_STATE_NONE &&
215                         node->refcnt != 1) {
216
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
222                  * unfreeable. */
223                 if (node->free.state == EC_NODE_FREE_STATE_NONE) {
224                         count_references(node, 1);
225                         mark_freeable(node, EC_NODE_FREE_STATE_FREEABLE);
226                 }
227         }
228
229         if (node->free.state == EC_NODE_FREE_STATE_NOT_FREEABLE) {
230                 node->refcnt--;
231                 reset_mark(node);
232                 return;
233         }
234
235         if (node->free.state != EC_NODE_FREE_STATE_FREEING) {
236                 node->free.state = EC_NODE_FREE_STATE_FREEING;
237
238                 /* children will be freed by config_free() and free_priv() */
239                 ec_config_free(node->config);
240                 node->config = NULL;
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);
245                 ec_free(node->id);
246                 ec_free(node->desc);
247                 ec_keyval_free(node->attrs);
248         }
249
250         node->refcnt--;
251         if (node->refcnt != 0)
252                 return;
253
254         node->free.state = EC_NODE_FREE_STATE_NONE;
255         node->free.refcnt = 0;
256
257         ec_free(node);
258 }
259
260 struct ec_node *ec_node_clone(struct ec_node *node)
261 {
262         if (node != NULL)
263                 node->refcnt++;
264         return node;
265 }
266
267 size_t ec_node_get_children_count(const struct ec_node *node)
268 {
269         if (node->type->get_children_count == NULL)
270                 return 0;
271         return node->type->get_children_count(node);
272 }
273
274 int
275 ec_node_get_child(const struct ec_node *node, size_t i,
276         struct ec_node **child, unsigned int *refs)
277 {
278         *child = NULL;
279         *refs = 0;
280         if (node->type->get_child == NULL)
281                 return -1;
282         return node->type->get_child(node, i, child, refs);
283 }
284
285 int
286 ec_node_set_config(struct ec_node *node, struct ec_config *config)
287 {
288         if (node->type->schema == NULL) {
289                 errno = EINVAL;
290                 goto fail;
291         }
292         if (ec_config_validate(config, node->type->schema) < 0)
293                 goto fail;
294         if (node->type->set_config != NULL) {
295                 if (node->type->set_config(node, config) < 0)
296                         goto fail;
297         }
298
299         ec_config_free(node->config);
300         node->config = config;
301
302         return 0;
303
304 fail:
305         ec_config_free(config);
306         return -1;
307 }
308
309 const struct ec_config *ec_node_get_config(struct ec_node *node)
310 {
311         return node->config;
312 }
313
314 struct ec_node *ec_node_find(struct ec_node *node, const char *id)
315 {
316         struct ec_node *child, *retnode;
317         const char *node_id = ec_node_id(node);
318         unsigned int refs;
319         size_t i, n;
320         int ret;
321
322         if (id != NULL && node_id != NULL && !strcmp(node_id, id))
323                 return node;
324
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);
328                 assert(ret == 0);
329                 retnode = ec_node_find(child, id);
330                 if (retnode != NULL)
331                         return retnode;
332         }
333
334         return NULL;
335 }
336
337 const struct ec_node_type *ec_node_type(const struct ec_node *node)
338 {
339         return node->type;
340 }
341
342 struct ec_keyval *ec_node_attrs(const struct ec_node *node)
343 {
344         return node->attrs;
345 }
346
347 const char *ec_node_id(const struct ec_node *node)
348 {
349         return node->id;
350 }
351
352 static void __ec_node_dump(FILE *out,
353         const struct ec_node *node, size_t indent, struct ec_keyval *dict)
354 {
355         const char *id, *typename;
356         struct ec_node *child;
357         unsigned int refs;
358         char buf[32];
359         size_t i, n;
360         int ret;
361
362         id = ec_node_id(node);
363         typename = node->type->name;
364
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);
369                 return;
370         }
371
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);
376
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);
380                 assert(ret == 0);
381                 __ec_node_dump(out, child, indent + 1, dict);
382         }
383 }
384
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)
387 {
388         struct ec_keyval *dict = NULL;
389
390         fprintf(out, "------------------- node dump:\n");
391
392         if (node == NULL) {
393                 fprintf(out, "node is NULL\n");
394                 return;
395         }
396
397         dict = ec_keyval();
398         if (dict == NULL)
399                 goto fail;
400
401         __ec_node_dump(out, node, 0, dict);
402
403         ec_keyval_free(dict);
404         return;
405
406 fail:
407         ec_keyval_free(dict);
408         EC_LOG(EC_LOG_ERR, "failed to dump node\n");
409 }
410
411 const char *ec_node_desc(const struct ec_node *node)
412 {
413         if (node->type->desc != NULL)
414                 return node->type->desc(node);
415
416         return node->desc;
417 }
418
419 int ec_node_check_type(const struct ec_node *node,
420                 const struct ec_node_type *type)
421 {
422         if (strcmp(node->type->name, type->name)) {
423                 errno = EINVAL;
424                 return -1;
425         }
426
427         return 0;
428 }
429
430 /* LCOV_EXCL_START */
431 static int ec_node_testcase(void)
432 {
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;
437         unsigned int refs;
438         FILE *f = NULL;
439         char *buf = NULL;
440         size_t buflen = 0;
441         int testres = 0;
442         int ret;
443
444         node = EC_NODE_SEQ(EC_NO_ID,
445                         ec_node_str("id_x", "x"),
446                         ec_node_str("id_y", "y"));
447         if (node == NULL)
448                 goto fail;
449
450         ec_node_clone(node);
451         ec_node_free(node);
452
453         f = open_memstream(&buf, &buflen);
454         if (f == NULL)
455                 goto fail;
456         ec_node_dump(f, node);
457         ec_node_type_dump(f);
458         ec_node_dump(f, NULL);
459         fclose(f);
460         f = NULL;
461
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,
467                         "type=str id=id_y"),
468                 "bad dump\n");
469         free(buf);
470         buf = NULL;
471
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>"),
476                 "bad child 0");
477
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 &&
483                 child != NULL &&
484                 !strcmp(ec_node_type(child)->name, "str") &&
485                 !strcmp(ec_node_id(child), "id_x"),
486                 "bad child 0");
487         ret = ec_node_get_child(node, 1, &child, &refs);
488         testres |= EC_TEST_CHECK(ret == 0 &&
489                 child != NULL &&
490                 !strcmp(ec_node_type(child)->name, "str") &&
491                 !strcmp(ec_node_id(child), "id_y"),
492                 "bad child 1");
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");
498
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"),
504                 "bad child id_x");
505         child = ec_node_find(node, "id_dezdex");
506         testres |= EC_TEST_CHECK(child == NULL,
507                 "child with wrong id should be NULL");
508
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");
512
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");
521
522         ec_node_free(node);
523         node = NULL;
524
525         node = ec_node("deznuindez", EC_NO_ID);
526         testres |= EC_TEST_CHECK(node == NULL,
527                         "should not be able to create node\n");
528
529         /* test loop */
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,
534                         op,
535                         ec_node_clone(expr));
536         op = NULL;
537         if (expr == NULL || val == NULL || seq == NULL)
538                 goto fail;
539         if (ec_node_or_add(expr, ec_node_clone(seq)) < 0)
540                 goto fail;
541         ec_node_free(seq);
542         seq = NULL;
543         if (ec_node_or_add(expr, ec_node_clone(val)) < 0)
544                 goto fail;
545         ec_node_free(val);
546         val = NULL;
547
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, "!", "!", "!");
551
552         ec_node_free(expr);
553         expr = NULL;
554
555         /* same loop test, but keep some refs (released later) */
556         expr = ec_node("or", EC_NO_ID);
557         ec_node_clone(expr);
558         expr2 = expr;
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,
562                         op,
563                         ec_node_clone(expr));
564         op = NULL;
565         if (expr == NULL || val == NULL || seq == NULL)
566                 goto fail;
567         if (ec_node_or_add(expr, ec_node_clone(seq)) < 0)
568                 goto fail;
569         ec_node_free(seq);
570         seq = NULL;
571         if (ec_node_or_add(expr, ec_node_clone(val)) < 0)
572                 goto fail;
573
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, "!", "!", "!");
577
578         ec_node_free(expr2);
579         expr2 = NULL;
580         ec_node_free(val);
581         val = NULL;
582         ec_node_free(expr);
583         expr = NULL;
584
585         return testres;
586
587 fail:
588         ec_node_free(expr);
589         ec_node_free(expr2);
590         ec_node_free(val);
591         ec_node_free(seq);
592         ec_node_free(node);
593         if (f != NULL)
594                 fclose(f);
595         free(buf);
596
597         assert(errno != 0);
598         return -1;
599 }
600 /* LCOV_EXCL_STOP */
601
602 static struct ec_test ec_node_test = {
603         .name = "node",
604         .test = ec_node_testcase,
605 };
606
607 EC_TEST_REGISTER(ec_node_test);