1ebbcf10abb8b4f4c0474cfacce4eaf3849da40b
[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         int ret;
133
134         if (node->free.state == EC_NODE_FREE_STATE_TRAVERSED) {
135                 node->free.refcnt += refs;
136                 return;
137         }
138         node->free.refcnt = refs;
139         node->free.state = EC_NODE_FREE_STATE_TRAVERSED;
140         n = ec_node_get_children_count(node);
141         for (i = 0; i < n; i++) {
142                 ret = ec_node_get_child(node, i, &child, &refs);
143                 assert(ret == 0);
144                 count_references(child, refs);
145         }
146 }
147
148 static void mark_freeable(struct ec_node *node, enum ec_node_free_state mark)
149 {
150         struct ec_node *child;
151         unsigned int refs;
152         size_t i, n;
153         int ret;
154
155         if (mark == node->free.state)
156                 return;
157
158         if (node->refcnt > node->free.refcnt)
159                 mark = EC_NODE_FREE_STATE_NOT_FREEABLE;
160         assert(node->refcnt >= node->free.refcnt);
161         node->free.state = mark;
162
163         n = ec_node_get_children_count(node);
164         for (i = 0; i < n; i++) {
165                 ret = ec_node_get_child(node, i, &child, &refs);
166                 assert(ret == 0);
167                 mark_freeable(child, mark);
168         }
169 }
170
171 static void reset_mark(struct ec_node *node)
172 {
173         struct ec_node *child;
174         unsigned int refs;
175         size_t i, n;
176         int ret;
177
178         if (node->free.state == EC_NODE_FREE_STATE_NONE)
179                 return;
180
181         node->free.state = EC_NODE_FREE_STATE_NONE;
182         node->free.refcnt = 0;
183
184         n = ec_node_get_children_count(node);
185         for (i = 0; i < n; i++) {
186                 ret = ec_node_get_child(node, i, &child, &refs);
187                 assert(ret == 0);
188                 reset_mark(child);
189         }
190 }
191
192 /* free a node, taking care of loops in the node graph */
193 void ec_node_free(struct ec_node *node)
194 {
195         size_t n;
196
197         if (node == NULL)
198                 return;
199
200         assert(node->refcnt > 0);
201
202         if (node->free.state == EC_NODE_FREE_STATE_NONE &&
203                         node->refcnt != 1) {
204
205                 /* Traverse the node tree starting from this node, and for each
206                  * node, count the number of reachable references. Then, all
207                  * nodes whose reachable references == total reference are
208                  * marked as freeable, and other are marked as unfreeable. Any
209                  * node reachable from an unfreeable node is also marked as
210                  * unfreeable. */
211                 if (node->free.state == EC_NODE_FREE_STATE_NONE) {
212                         count_references(node, 1);
213                         mark_freeable(node, EC_NODE_FREE_STATE_FREEABLE);
214                 }
215         }
216
217         if (node->free.state == EC_NODE_FREE_STATE_NOT_FREEABLE) {
218                 node->refcnt--;
219                 reset_mark(node);
220                 return;
221         }
222
223         if (node->free.state != EC_NODE_FREE_STATE_FREEING) {
224                 node->free.state = EC_NODE_FREE_STATE_FREEING;
225                 n = ec_node_get_children_count(node);
226                 /* children should be freed by free_priv() */
227                 assert(n == 0 || node->type->free_priv != NULL);
228                 if (node->type->free_priv != NULL)
229                         node->type->free_priv(node);
230         }
231
232         node->refcnt--;
233         if (node->refcnt != 0)
234                 return;
235
236         node->free.state = EC_NODE_FREE_STATE_NONE;
237         node->free.refcnt = 0;
238
239         ec_free(node->id);
240         ec_free(node->desc);
241         ec_keyval_free(node->attrs);
242         ec_config_free(node->config);
243         ec_free(node);
244 }
245
246 struct ec_node *ec_node_clone(struct ec_node *node)
247 {
248         if (node != NULL)
249                 node->refcnt++;
250         return node;
251 }
252
253 size_t ec_node_get_children_count(const struct ec_node *node)
254 {
255         if (node->type->get_children_count == NULL)
256                 return 0;
257         return node->type->get_children_count(node);
258 }
259
260 int
261 ec_node_get_child(const struct ec_node *node, size_t i,
262         struct ec_node **child, unsigned int *refs)
263 {
264         *child = NULL;
265         *refs = 0;
266         if (node->type->get_child == NULL)
267                 return -1;
268         return node->type->get_child(node, i, child, refs);
269 }
270
271 int
272 ec_node_set_config(struct ec_node *node, struct ec_config *config)
273 {
274         if (node->type->schema == NULL) {
275                 errno = EINVAL;
276                 goto fail;
277         }
278         if (ec_config_validate(config, node->type->schema,
279                                 node->type->schema_len) < 0)
280                 goto fail;
281         if (node->type->set_config != NULL) {
282                 if (node->type->set_config(node, config) < 0)
283                         goto fail;
284         }
285
286         ec_config_free(node->config);
287         node->config = config;
288
289         return 0;
290
291 fail:
292         ec_config_free(config);
293         return -1;
294 }
295
296 const struct ec_config *ec_node_get_config(struct ec_node *node)
297 {
298         return node->config;
299 }
300
301 struct ec_node *ec_node_find(struct ec_node *node, const char *id)
302 {
303         struct ec_node *child, *retnode;
304         const char *node_id = ec_node_id(node);
305         unsigned int refs;
306         size_t i, n;
307         int ret;
308
309         if (id != NULL && node_id != NULL && !strcmp(node_id, id))
310                 return node;
311
312         n = ec_node_get_children_count(node);
313         for (i = 0; i < n; i++) {
314                 ret = ec_node_get_child(node, i, &child, &refs);
315                 assert(ret == 0);
316                 retnode = ec_node_find(child, id);
317                 if (retnode != NULL)
318                         return retnode;
319         }
320
321         return NULL;
322 }
323
324 const struct ec_node_type *ec_node_type(const struct ec_node *node)
325 {
326         return node->type;
327 }
328
329 struct ec_keyval *ec_node_attrs(const struct ec_node *node)
330 {
331         return node->attrs;
332 }
333
334 const char *ec_node_id(const struct ec_node *node)
335 {
336         return node->id;
337 }
338
339 static void __ec_node_dump(FILE *out,
340         const struct ec_node *node, size_t indent, struct ec_keyval *dict)
341 {
342         const char *id, *typename;
343         struct ec_node *child;
344         unsigned int refs;
345         char buf[32];
346         size_t i, n;
347         int ret;
348
349         id = ec_node_id(node);
350         typename = node->type->name;
351
352         snprintf(buf, sizeof(buf), "%p", node);
353         if (ec_keyval_has_key(dict, buf)) {
354                 fprintf(out, "%*s" "type=%s id=%s %p... (loop)\n",
355                         (int)indent * 4, "", typename, id, node);
356                 return;
357         }
358
359         ec_keyval_set(dict, buf, NULL, NULL);
360         fprintf(out, "%*s" "type=%s id=%s %p refs=%u free_state=%d free_refs=%d\n",
361                 (int)indent * 4, "", typename, id, node, node->refcnt,
362                 node->free.state, node->free.refcnt);
363
364         n = ec_node_get_children_count(node);
365         for (i = 0; i < n; i++) {
366                 ret = ec_node_get_child(node, i, &child, &refs);
367                 assert(ret == 0);
368                 __ec_node_dump(out, child, indent + 1, dict);
369         }
370 }
371
372 void ec_node_dump(FILE *out, const struct ec_node *node)
373 {
374         struct ec_keyval *dict = NULL;
375
376         fprintf(out, "------------------- node dump:\n");
377
378         if (node == NULL) {
379                 fprintf(out, "node is NULL\n");
380                 return;
381         }
382
383         dict = ec_keyval();
384         if (dict == NULL)
385                 goto fail;
386
387         __ec_node_dump(out, node, 0, dict);
388
389         ec_keyval_free(dict);
390         return;
391
392 fail:
393         ec_keyval_free(dict);
394         EC_LOG(EC_LOG_ERR, "failed to dump node\n");
395 }
396
397 const char *ec_node_desc(const struct ec_node *node)
398 {
399         if (node->type->desc != NULL)
400                 return node->type->desc(node);
401
402         return node->desc;
403 }
404
405 int ec_node_check_type(const struct ec_node *node,
406                 const struct ec_node_type *type)
407 {
408         if (strcmp(node->type->name, type->name)) {
409                 errno = EINVAL;
410                 return -1;
411         }
412
413         return 0;
414 }
415
416 /* LCOV_EXCL_START */
417 static int ec_node_testcase(void)
418 {
419         struct ec_node *node = NULL, *expr = NULL;
420         struct ec_node *expr2 = NULL, *val = NULL, *op = NULL, *seq = NULL;
421         const struct ec_node_type *type;
422         struct ec_node *child;
423         unsigned int refs;
424         FILE *f = NULL;
425         char *buf = NULL;
426         size_t buflen = 0;
427         int testres = 0;
428         int ret;
429
430         node = EC_NODE_SEQ(EC_NO_ID,
431                         ec_node_str("id_x", "x"),
432                         ec_node_str("id_y", "y"));
433         if (node == NULL)
434                 goto fail;
435
436         ec_node_clone(node);
437         ec_node_free(node);
438
439         f = open_memstream(&buf, &buflen);
440         if (f == NULL)
441                 goto fail;
442         ec_node_dump(f, node);
443         ec_node_type_dump(f);
444         ec_node_dump(f, NULL);
445         fclose(f);
446         f = NULL;
447
448         testres |= EC_TEST_CHECK(
449                 strstr(buf, "type=seq id=no-id"), "bad dump\n");
450         testres |= EC_TEST_CHECK(
451                 strstr(buf, "type=str id=id_x") &&
452                 strstr(strstr(buf, "type=str id=id_x") + 1,
453                         "type=str id=id_y"),
454                 "bad dump\n");
455         free(buf);
456         buf = NULL;
457
458         testres |= EC_TEST_CHECK(
459                 !strcmp(ec_node_type(node)->name, "seq") &&
460                 !strcmp(ec_node_id(node), EC_NO_ID) &&
461                 !strcmp(ec_node_desc(node), "<seq>"),
462                 "bad child 0");
463
464         testres |= EC_TEST_CHECK(
465                 ec_node_get_children_count(node) == 2,
466                 "bad children count\n");
467         ret = ec_node_get_child(node, 0, &child, &refs);
468         testres |= EC_TEST_CHECK(ret == 0 &&
469                 child != NULL &&
470                 !strcmp(ec_node_type(child)->name, "str") &&
471                 !strcmp(ec_node_id(child), "id_x"),
472                 "bad child 0");
473         ret = ec_node_get_child(node, 1, &child, &refs);
474         testres |= EC_TEST_CHECK(ret == 0 &&
475                 child != NULL &&
476                 !strcmp(ec_node_type(child)->name, "str") &&
477                 !strcmp(ec_node_id(child), "id_y"),
478                 "bad child 1");
479         ret = ec_node_get_child(node, 2, &child, &refs);
480         testres |= EC_TEST_CHECK(ret != 0,
481                 "ret should be != 0");
482         testres |= EC_TEST_CHECK(child == NULL,
483                 "child 2 should be NULL");
484
485         child = ec_node_find(node, "id_x");
486         testres |= EC_TEST_CHECK(child != NULL &&
487                 !strcmp(ec_node_type(child)->name, "str") &&
488                 !strcmp(ec_node_id(child), "id_x") &&
489                 !strcmp(ec_node_desc(child), "x"),
490                 "bad child id_x");
491         child = ec_node_find(node, "id_dezdex");
492         testres |= EC_TEST_CHECK(child == NULL,
493                 "child with wrong id should be NULL");
494
495         ret = ec_keyval_set(ec_node_attrs(node), "key", "val", NULL);
496         testres |= EC_TEST_CHECK(ret == 0,
497                 "cannot set node attribute\n");
498
499         type = ec_node_type_lookup("seq");
500         testres |= EC_TEST_CHECK(type != NULL &&
501                 ec_node_check_type(node, type) == 0,
502                 "cannot get seq node type");
503         type = ec_node_type_lookup("str");
504         testres |= EC_TEST_CHECK(type != NULL &&
505                 ec_node_check_type(node, type) < 0,
506                 "node type should not be str");
507
508         ec_node_free(node);
509         node = NULL;
510
511         node = ec_node("deznuindez", EC_NO_ID);
512         testres |= EC_TEST_CHECK(node == NULL,
513                         "should not be able to create node\n");
514
515         /* test loop */
516         expr = ec_node("or", EC_NO_ID);
517         val = ec_node_int(EC_NO_ID, 0, 10, 0);
518         op = ec_node_str(EC_NO_ID, "!");
519         seq = EC_NODE_SEQ(EC_NO_ID,
520                         op,
521                         ec_node_clone(expr));
522         op = NULL;
523         if (expr == NULL || val == NULL || seq == NULL)
524                 goto fail;
525         if (ec_node_or_add(expr, ec_node_clone(seq)) < 0)
526                 goto fail;
527         ec_node_free(seq);
528         seq = NULL;
529         if (ec_node_or_add(expr, ec_node_clone(val)) < 0)
530                 goto fail;
531         ec_node_free(val);
532         val = NULL;
533
534         testres |= EC_TEST_CHECK_PARSE(expr, 1, "1");
535         testres |= EC_TEST_CHECK_PARSE(expr, 3, "!", "!", "1");
536         testres |= EC_TEST_CHECK_PARSE(expr, -1, "!", "!", "!");
537
538         ec_node_free(expr);
539         expr = NULL;
540
541         /* same loop test, but keep some refs (released later) */
542         expr = ec_node("or", EC_NO_ID);
543         ec_node_clone(expr);
544         expr2 = expr;
545         val = ec_node_int(EC_NO_ID, 0, 10, 0);
546         op = ec_node_str(EC_NO_ID, "!");
547         seq = EC_NODE_SEQ(EC_NO_ID,
548                         op,
549                         ec_node_clone(expr));
550         op = NULL;
551         if (expr == NULL || val == NULL || seq == NULL)
552                 goto fail;
553         if (ec_node_or_add(expr, ec_node_clone(seq)) < 0)
554                 goto fail;
555         ec_node_free(seq);
556         seq = NULL;
557         if (ec_node_or_add(expr, ec_node_clone(val)) < 0)
558                 goto fail;
559
560         testres |= EC_TEST_CHECK_PARSE(expr, 1, "1");
561         testres |= EC_TEST_CHECK_PARSE(expr, 3, "!", "!", "1");
562         testres |= EC_TEST_CHECK_PARSE(expr, -1, "!", "!", "!");
563
564         ec_node_free(expr2);
565         expr2 = NULL;
566         ec_node_free(val);
567         val = NULL;
568         ec_node_free(expr);
569         expr = NULL;
570
571         return testres;
572
573 fail:
574         ec_node_free(expr);
575         ec_node_free(expr2);
576         ec_node_free(val);
577         ec_node_free(seq);
578         ec_node_free(node);
579         if (f != NULL)
580                 fclose(f);
581         free(buf);
582
583         assert(errno != 0);
584         return -1;
585 }
586 /* LCOV_EXCL_STOP */
587
588 static struct ec_test ec_node_test = {
589         .name = "node",
590         .test = ec_node_testcase,
591 };
592
593 EC_TEST_REGISTER(ec_node_test);