Add bt_common_abort() and use it instead of abort() directly
[babeltrace.git] / src / lib / graph / mip.c
1 /*
2 * Copyright 2019 Philippe Proulx <pproulx@efficios.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 #define BT_LOG_TAG "LIB/MIP"
24 #include "lib/logging.h"
25
26 #include "lib/assert-pre.h"
27 #include "lib/assert-post.h"
28 #include <stdbool.h>
29 #include <unistd.h>
30 #include <glib.h>
31 #include <babeltrace2/graph/mip.h>
32
33 #include "common/assert.h"
34 #include "compat/compiler.h"
35 #include "common/common.h"
36 #include "lib/value.h"
37 #include "component-descriptor-set.h"
38 #include "lib/integer-range-set.h"
39
40 static
41 bool unsigned_integer_range_set_contains(
42 const struct bt_integer_range_set *range_set, uint64_t value)
43 {
44 bool contains = false;
45 uint64_t i;
46
47 BT_ASSERT(range_set);
48
49 for (i = 0; i < range_set->ranges->len; i++) {
50 const struct bt_integer_range *range =
51 BT_INTEGER_RANGE_SET_RANGE_AT_INDEX(range_set, i);
52
53 if (value >= range->lower.u && value <= range->upper.u) {
54 contains = true;
55 goto end;
56 }
57 }
58
59 end:
60 return contains;
61 }
62
63 /*
64 * As of this version, this function only validates that all the
65 * component descriptors in `descriptors` support MIP version 0, which
66 * is the only version supported by this library.
67 *
68 * If any component descriptor does not support MIP version 0, then this
69 * function returns `BT_FUNC_STATUS_NO_MATCH`.
70 */
71 static
72 int validate_operative_mip_version_in_array(GPtrArray *descriptors,
73 enum bt_logging_level log_level)
74 {
75 typedef bt_component_class_get_supported_mip_versions_method_status
76 (*method_t)(
77 void * /* component class */,
78 const struct bt_value *,
79 void * /* init method data */,
80 enum bt_logging_level,
81 struct bt_integer_range_set *);
82
83 int status = BT_FUNC_STATUS_OK;
84 uint64_t i;
85 struct bt_integer_range_set *range_set = NULL;
86
87 for (i = 0; i < descriptors->len; i++) {
88 struct bt_component_descriptor_set_entry *descr =
89 descriptors->pdata[i];
90 method_t method = NULL;
91 bt_component_class_get_supported_mip_versions_method_status method_status;
92
93 switch (descr->comp_cls->type) {
94 case BT_COMPONENT_CLASS_TYPE_SOURCE:
95 {
96 struct bt_component_class_source *src_cc = (void *)
97 descr->comp_cls;
98
99 method = (method_t) src_cc->methods.get_supported_mip_versions;
100 break;
101 }
102 case BT_COMPONENT_CLASS_TYPE_FILTER:
103 {
104 struct bt_component_class_filter *flt_cc = (void *)
105 descr->comp_cls;
106
107 method = (method_t) flt_cc->methods.get_supported_mip_versions;
108 break;
109 }
110 case BT_COMPONENT_CLASS_TYPE_SINK:
111 {
112 struct bt_component_class_sink *sink_cc = (void *)
113 descr->comp_cls;
114
115 method = (method_t) sink_cc->methods.get_supported_mip_versions;
116 break;
117 }
118 default:
119 bt_common_abort();
120 }
121
122 if (!method) {
123 /* Assume 0 */
124 continue;
125 }
126
127 range_set = (void *) bt_integer_range_set_unsigned_create();
128 if (!range_set) {
129 status = BT_FUNC_STATUS_MEMORY_ERROR;
130 goto end;
131 }
132
133 BT_ASSERT(descr->params);
134 BT_LIB_LOGD("Calling user's \"get supported MIP versions\" method: "
135 "%![cc-]+C, %![params-]+v, init-method-data=%p, "
136 "log-level=%s",
137 descr->comp_cls, descr->params,
138 descr->init_method_data,
139 bt_common_logging_level_string(log_level));
140 method_status = method(descr->comp_cls, descr->params,
141 descr->init_method_data, log_level,
142 range_set);
143 BT_LIB_LOGD("User method returned: status=%s",
144 bt_common_func_status_string(method_status));
145 BT_ASSERT_POST(method_status != BT_FUNC_STATUS_OK ||
146 range_set->ranges->len > 0,
147 "User method returned `BT_FUNC_STATUS_OK` without "
148 "adding a range to the supported MIP version range set.");
149 if (method_status < 0) {
150 BT_LIB_LOGW_APPEND_CAUSE(
151 "Component class's \"get supported MIP versions\" method failed: "
152 "%![cc-]+C, %![params-]+v, init-method-data=%p, "
153 "log-level=%s",
154 descr->comp_cls, descr->params,
155 descr->init_method_data,
156 bt_common_logging_level_string(log_level));
157 status = (int) method_status;
158 goto end;
159 }
160
161 if (!unsigned_integer_range_set_contains(range_set, 0)) {
162 /*
163 * Supported MIP versions do not include 0,
164 * which is the only MIP versions currently
165 * supported by the library itself.
166 */
167 status = BT_FUNC_STATUS_NO_MATCH;
168 goto end;
169 }
170
171 BT_OBJECT_PUT_REF_AND_RESET(range_set);
172 }
173
174 end:
175 bt_object_put_ref(range_set);
176 return status;
177 }
178
179 /*
180 * The purpose of this function is eventually to find the greatest
181 * common supported MIP version amongst all the component descriptors.
182 * But as of this version of the library, only MIP version 0 is
183 * supported, so it only checks that they all support MIP version 0 and
184 * always sets `*operative_mip_version` to 0.
185 *
186 * When any component descriptor does not support MIP version 0, this
187 * function returns `BT_FUNC_STATUS_NO_MATCH`.
188 */
189 enum bt_get_greatest_operative_mip_version_status
190 bt_get_greatest_operative_mip_version(
191 const struct bt_component_descriptor_set *comp_descr_set,
192 enum bt_logging_level log_level,
193 uint64_t *operative_mip_version)
194 {
195 int status = BT_FUNC_STATUS_OK;
196
197 BT_ASSERT_PRE_NON_NULL(comp_descr_set, "Component descriptor set");
198 BT_ASSERT_PRE_NON_NULL(operative_mip_version,
199 "Operative MIP version (output)");
200 BT_ASSERT_PRE(comp_descr_set->sources->len +
201 comp_descr_set->filters->len +
202 comp_descr_set->sinks->len > 0,
203 "Component descriptor set is empty: addr=%p", comp_descr_set);
204 status = validate_operative_mip_version_in_array(
205 comp_descr_set->sources, log_level);
206 if (status) {
207 goto end;
208 }
209
210 status = validate_operative_mip_version_in_array(
211 comp_descr_set->filters, log_level);
212 if (status) {
213 goto end;
214 }
215
216 status = validate_operative_mip_version_in_array(
217 comp_descr_set->sinks, log_level);
218 if (status) {
219 goto end;
220 }
221
222 *operative_mip_version = 0;
223
224 end:
225 return status;
226 }
227
228 uint64_t bt_get_maximal_mip_version(void)
229 {
230 return 0;
231 }
This page took 0.04037 seconds and 4 git commands to generate.