GCC Code Coverage Report


Directory: src/
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 100.0% 29 / 0 / 29
Functions: 100.0% 4 / 0 / 4
Branches: 100.0% 18 / 0 / 18

engine/src/engine.c
Line Branch Exec Source
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright (c) 2026 Manuel Hernández Méndez
5 *
6 * Authors:
7 * Manuel Hernández Méndez <maherme.dev@gmail.com>
8 */
9
10 #include "engine.h"
11 #include "list.h"
12 #include "utils.h"
13 #include <stdbool.h>
14 #include <unistd.h>
15
16 typedef struct cb_node
17 {
18 engine_cb_t callback;
19 bool pending_remove;
20 struct list_head node;
21 } cb_node_t;
22
23 static LIST_HEAD(callbacks);
24
25 int
26 4 engineRegister(engine_cb_t cb)
27 {
28
2/2
✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 4 taken 3 times.
4 if (!cb)
29 {
30 1 return -1;
31 }
32
33 3 cb_node_t *n = utilsCalloc(1, sizeof(*n));
34 3 n->callback = cb;
35 3 list_add(&n->node, &callbacks);
36
37 3 return 0;
38 }
39
40 int
41 3 engineUnregister(engine_cb_t cb)
42 {
43
2/2
✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 4 taken 2 times.
3 if (!cb)
44 {
45 1 return -1;
46 }
47
48 2 int removed = 0;
49 cb_node_t *n;
50
51
2/2
✓ Branch 8 → 5 taken 2 times.
✓ Branch 8 → 9 taken 2 times.
4 list_for_each_entry(n, &callbacks, node)
52 {
53
2/2
✓ Branch 5 → 6 taken 1 time.
✓ Branch 5 → 7 taken 1 time.
2 if (n->callback == cb)
54 {
55 1 n->pending_remove = true;
56 1 removed++;
57 }
58 }
59
60 2 return removed;
61 }
62
63 void
64 6 engineRun(int rate)
65 {
66
2/2
✓ Branch 13 → 3 taken 6 times.
✓ Branch 13 → 14 taken 6 times.
12 for (int i = 0; i < rate; i++)
67 {
68 cb_node_t *n, *tmp;
69
2/2
✓ Branch 10 → 4 taken 4 times.
✓ Branch 10 → 11 taken 6 times.
10 list_for_each_entry_safe(n, tmp, &callbacks, node)
70 {
71
2/2
✓ Branch 4 → 5 taken 3 times.
✓ Branch 4 → 6 taken 1 time.
4 if (!n->pending_remove)
72 {
73 3 n->callback();
74 }
75
76
2/2
✓ Branch 6 → 7 taken 1 time.
✓ Branch 6 → 9 taken 3 times.
4 if (n->pending_remove)
77 {
78 1 list_del(&n->node);
79 1 UTILS_FREE(n);
80 }
81 }
82 6 usleep(1000);
83 }
84 6 }
85
86 #ifdef UNIT_TESTING
87 void
88 5 helperUT_engineResetRegisteredCallbacks(void)
89 {
90
2/2
✓ Branch 5 → 3 taken 2 times.
✓ Branch 5 → 6 taken 5 times.
7 list_clear_with_free(&callbacks, cb_node_t, node);
91 5 }
92 #endif
93