lib: add pre condition asserts to check current thread has no error
[babeltrace.git] / src / lib / graph / component-descriptor-set.c
1 /*
2 * Copyright 2017-2019 Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #define BT_LOG_TAG "LIB/COMP-DESCR-SET"
25 #include "lib/logging.h"
26
27 #include "common/assert.h"
28 #include "lib/assert-pre.h"
29 #include "compat/compiler.h"
30 #include "common/common.h"
31 #include <babeltrace2/types.h>
32 #include <babeltrace2/value.h>
33 #include <babeltrace2/value-const.h>
34 #include <unistd.h>
35 #include <glib.h>
36
37 #include "component-class.h"
38 #include "component-descriptor-set.h"
39 #include "component-class-sink-simple.h"
40 #include "lib/value.h"
41
42 static
43 void destroy_component_descriptor_set(struct bt_object *obj)
44 {
45 struct bt_component_descriptor_set *comp_descr_set = (void *) obj;
46
47 if (comp_descr_set->sources) {
48 BT_LOGD_STR("Destroying source component descriptors.");
49 g_ptr_array_free(comp_descr_set->sources, TRUE);
50 comp_descr_set->sources = NULL;
51 }
52
53 if (comp_descr_set->filters) {
54 BT_LOGD_STR("Destroying filter component descriptors.");
55 g_ptr_array_free(comp_descr_set->filters, TRUE);
56 comp_descr_set->filters = NULL;
57 }
58
59 if (comp_descr_set->sinks) {
60 BT_LOGD_STR("Destroying sink component descriptors.");
61 g_ptr_array_free(comp_descr_set->sinks, TRUE);
62 comp_descr_set->sinks = NULL;
63 }
64
65 g_free(comp_descr_set);
66 }
67
68 static
69 void destroy_component_descriptor_set_entry(gpointer ptr)
70 {
71 struct bt_component_descriptor_set_entry *entry = ptr;
72
73 if (!ptr) {
74 goto end;
75 }
76
77 BT_OBJECT_PUT_REF_AND_RESET(entry->comp_cls);
78 BT_OBJECT_PUT_REF_AND_RESET(entry->params);
79 g_free(entry);
80
81 end:
82 return;
83 }
84
85 struct bt_component_descriptor_set *bt_component_descriptor_set_create(void)
86 {
87 struct bt_component_descriptor_set *comp_descr_set;
88
89 BT_ASSERT_PRE_NO_ERROR();
90
91 BT_LOGI_STR("Creating component descriptor set object.");
92 comp_descr_set = g_new0(struct bt_component_descriptor_set, 1);
93 if (!comp_descr_set) {
94 BT_LIB_LOGE_APPEND_CAUSE(
95 "Failed to allocate one component descriptor set.");
96 goto end;
97 }
98
99 bt_object_init_shared(&comp_descr_set->base,
100 destroy_component_descriptor_set);
101 comp_descr_set->sources = g_ptr_array_new_with_free_func(
102 destroy_component_descriptor_set_entry);
103 if (!comp_descr_set->sources) {
104 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
105 goto error;
106 }
107
108 comp_descr_set->filters = g_ptr_array_new_with_free_func(
109 destroy_component_descriptor_set_entry);
110 if (!comp_descr_set->filters) {
111 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
112 goto error;
113 }
114
115 comp_descr_set->sinks = g_ptr_array_new_with_free_func(
116 destroy_component_descriptor_set_entry);
117 if (!comp_descr_set->sinks) {
118 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
119 goto error;
120 }
121
122 BT_LOGI("Created component descriptor set object: addr=%p",
123 comp_descr_set);
124 goto end;
125
126 error:
127 BT_OBJECT_PUT_REF_AND_RESET(comp_descr_set);
128
129 end:
130 return comp_descr_set;
131 }
132
133 enum bt_component_descriptor_set_add_descriptor_status
134 bt_component_descriptor_set_add_descriptor_with_initialize_method_data(
135 struct bt_component_descriptor_set *comp_descr_set,
136 const struct bt_component_class *comp_cls,
137 const struct bt_value *params, void *init_method_data)
138 {
139 bt_component_descriptor_set_add_descriptor_status status =
140 BT_FUNC_STATUS_OK;
141 struct bt_value *new_params = NULL;
142 struct bt_component_descriptor_set_entry *entry = NULL;
143 GPtrArray *comp_descr_array = NULL;
144
145 BT_ASSERT_PRE_NO_ERROR();
146 BT_ASSERT_PRE_NON_NULL(comp_descr_set, "Component descriptor set");
147 BT_ASSERT_PRE_NON_NULL(comp_cls, "Component class");
148 BT_ASSERT_PRE(!params || bt_value_is_map(params),
149 "Parameter value is not a map value: %!+v", params);
150 BT_LIB_LOGI("Adding component descriptor to set: "
151 "set-addr=%p, %![cc-]+C, "
152 "%![params-]+v, init-method-data-addr=%p",
153 comp_descr_set, comp_cls, params, init_method_data);
154
155 if (!params) {
156 new_params = bt_value_map_create();
157 if (!new_params) {
158 BT_LIB_LOGE_APPEND_CAUSE(
159 "Cannot create empty map value object.");
160 status = BT_FUNC_STATUS_MEMORY_ERROR;
161 goto error;
162 }
163
164 params = new_params;
165 }
166
167 entry = g_new0(struct bt_component_descriptor_set_entry, 1);
168 if (!entry) {
169 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
170 status = BT_FUNC_STATUS_MEMORY_ERROR;
171 goto error;
172 }
173
174 entry->comp_cls = (void *) comp_cls;
175 bt_object_get_ref_no_null_check(entry->comp_cls);
176 bt_component_class_freeze(entry->comp_cls);
177 entry->params = (void *) params;
178 bt_object_get_ref_no_null_check(entry->params);
179 bt_value_freeze(entry->params);
180 entry->init_method_data = init_method_data;
181
182 /* Move to array */
183 switch (comp_cls->type) {
184 case BT_COMPONENT_CLASS_TYPE_SOURCE:
185 comp_descr_array = comp_descr_set->sources;
186 break;
187 case BT_COMPONENT_CLASS_TYPE_FILTER:
188 comp_descr_array = comp_descr_set->filters;
189 break;
190 case BT_COMPONENT_CLASS_TYPE_SINK:
191 comp_descr_array = comp_descr_set->sinks;
192 break;
193 default:
194 bt_common_abort();
195 }
196
197 BT_ASSERT(comp_descr_array);
198 g_ptr_array_add(comp_descr_array, entry);
199 BT_LIB_LOGI("Added component descriptor to set: "
200 "set-addr=%p, %![cc-]+C, "
201 "%![params-]+v, init-method-data-addr=%p",
202 comp_descr_set, comp_cls, params, init_method_data);
203 goto end;
204
205 error:
206 destroy_component_descriptor_set_entry(entry);
207 entry = NULL;
208
209 end:
210 bt_object_put_ref(new_params);
211 return status;
212 }
213
214 enum bt_component_descriptor_set_add_descriptor_status
215 bt_component_descriptor_set_add_descriptor(
216 struct bt_component_descriptor_set *comp_descr_set,
217 const struct bt_component_class *comp_cls,
218 const struct bt_value *params)
219 {
220 BT_ASSERT_PRE_NO_ERROR();
221
222 return bt_component_descriptor_set_add_descriptor_with_initialize_method_data(
223 comp_descr_set, comp_cls, params, NULL);
224 }
225
226 void bt_component_descriptor_set_get_ref(
227 const struct bt_component_descriptor_set *comp_descr_set)
228 {
229 bt_object_get_ref(comp_descr_set);
230 }
231
232 void bt_component_descriptor_set_put_ref(
233 const struct bt_component_descriptor_set *comp_descr_set)
234 {
235 bt_object_put_ref(comp_descr_set);
236 }
This page took 0.036124 seconds and 4 git commands to generate.