GCC Code Coverage Report


Directory: src/
File: src/mngo/mngo_regions.c
Date: 2026-09-15 07:37:49
Exec Total Coverage
Lines: 51 54 94.4%
Functions: 10 10 100.0%
Branches: 10 16 62.5%

Line Branch Exec Source
1 /*********************************************************************************/
2 /* Copyright 2009-2026 Barcelona Supercomputing Center */
3 /* */
4 /* This file is part of the DLB library. */
5 /* */
6 /* DLB is free software: you can redistribute it and/or modify */
7 /* it under the terms of the GNU Lesser General Public License as published by */
8 /* the Free Software Foundation, either version 3 of the License, or */
9 /* (at your option) any later version. */
10 /* */
11 /* DLB is distributed in the hope that it will be useful, */
12 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
13 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
14 /* GNU Lesser General Public License for more details. */
15 /* */
16 /* You should have received a copy of the GNU Lesser General Public License */
17 /* along with DLB. If not, see <https://www.gnu.org/licenses/>. */
18 /*********************************************************************************/
19
20 #include "mngo/mngo_regions.h"
21 #include "dlb_errors.h"
22 #include "mngo/mngo_resources.h"
23 #include "support/debug.h"
24 #include <stdio.h>
25 #include <string.h>
26
27 static mngo_state_t *_default_state;
28
29 15 void mngo_regions__manager_init(mngo_regions_manager_t *region_manager,
30 mngo_state_t *default_state) {
31 15 region_manager->regions =
32 15 queue__init(sizeof(dlb_mngo_region_t *), 1, NULL, QUEUE_ALLOC_REALLOC);
33
34 15 region_manager->active_regions =
35 15 queue__init(sizeof(dlb_mngo_region_t *), 1, NULL, QUEUE_ALLOC_REALLOC);
36
37 15 _default_state = default_state;
38 15 }
39
40 13 static void free_element(void *args, void *curr) {
41 13 dlb_mngo_region_t **region = curr;
42 13 free(*region);
43 13 }
44
45 15 void mngo_regions__manager_finalize(mngo_regions_manager_t *region_manager) {
46 queue_iter_head2tail_t iter =
47 15 queue__into_head2tail_iter(region_manager->regions);
48 15 queue_iter__foreach(&iter, free_element, NULL);
49
50 15 queue__destroy(region_manager->regions);
51 15 region_manager->regions = NULL;
52 15 queue__destroy(region_manager->active_regions);
53 15 region_manager->active_regions = NULL;
54 15 }
55
56 mngo_state_t *
57 45 mngo_regions__get_current_resources(mngo_regions_manager_t *manager) {
58 dlb_mngo_region_t **region;
59
2/2
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 21 times.
45 if (queue__peek_head(manager->active_regions, (void **)&region) ==
60 DLB_SUCCESS) {
61 24 return &(*region)->region_info.state;
62 } else {
63 21 return _default_state;
64 }
65 }
66
67 61 bool mngo_regions__check_top(const mngo_regions_manager_t *manager,
68 const dlb_mngo_region_t *region) {
69 dlb_mngo_region_t **current_region;
70
2/2
✓ Branch 1 taken 17 times.
✓ Branch 2 taken 44 times.
61 if (queue__peek_head(manager->active_regions, (void **)&current_region) !=
71 DLB_SUCCESS) {
72 17 return false;
73 }
74
75
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 42 times.
44 if (strcmp((*current_region)->name, region->name) != 0) {
76 2 return false;
77 }
78
79 42 return true;
80 }
81
82 19 int mngo_regions__push_active(mngo_regions_manager_t *manager,
83 const dlb_mngo_region_t *new_region) {
84
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
19 if (queue__push_head(manager->active_regions, &new_region) == NULL) {
85 verbose(VB_MNGO, "Could not push new region (%s) into active queue.",
86 new_region->name);
87 }
88
89 19 return DLB_SUCCESS;
90 }
91
92 16 int mngo_regions__pop_active(mngo_regions_manager_t *manager,
93 const dlb_mngo_region_t *old_region) {
94
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
16 if (!mngo_regions__check_top(manager, old_region)) {
95 verbose(VB_MNGO, "region (%s) is not on top", old_region->name);
96 return DLB_NOUPDT;
97 }
98
99 16 queue__take_head(manager->active_regions, NULL);
100
101 16 return DLB_SUCCESS;
102 }
103
104 struct mngo_regions_find_foreach_t {
105 const char *name;
106 dlb_mngo_region_t *found;
107 };
108
109 76 static void mngo_regions_find_foreach(void *args, void *curr) {
110 76 struct mngo_regions_find_foreach_t *a = args;
111 76 dlb_mngo_region_t **region = curr;
112
113
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 58 times.
76 if (strcmp((*region)->name, a->name) == 0) {
114 18 a->found = *region;
115 }
116 76 }
117
118 dlb_mngo_region_t *
119 26 mngo_regions__find(const mngo_regions_manager_t *region_manager,
120 const char *name) {
121 queue_iter_head2tail_t iter =
122 26 queue__into_head2tail_iter(region_manager->regions);
123
124 26 struct mngo_regions_find_foreach_t find_cfg = {
125 .name = name,
126 .found = NULL,
127 };
128
129 26 queue_iter__foreach(&iter, mngo_regions_find_foreach, &find_cfg);
130
131 26 return find_cfg.found;
132 }
133
134 13 dlb_mngo_region_t *mngo_regions__alloc(mngo_regions_manager_t *region_manager) {
135 dlb_mngo_region_t **new_region_place =
136 13 queue__emplace_head(region_manager->regions);
137 13 *new_region_place = malloc(sizeof(dlb_mngo_region_t));
138 13 return *new_region_place;
139 }
140