lib: add pre condition asserts to check current thread has no error
[babeltrace.git] / src / lib / graph / component-descriptor-set.c
CommitLineData
55f09f52
PP
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
42static
43void 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
68static
69void 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
81end:
82 return;
83}
84
85struct bt_component_descriptor_set *bt_component_descriptor_set_create(void)
86{
87 struct bt_component_descriptor_set *comp_descr_set;
88
17f3083a
SM
89 BT_ASSERT_PRE_NO_ERROR();
90
55f09f52
PP
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
126error:
127 BT_OBJECT_PUT_REF_AND_RESET(comp_descr_set);
128
129end:
130 return comp_descr_set;
131}
132
133enum bt_component_descriptor_set_add_descriptor_status
21a9f056 134bt_component_descriptor_set_add_descriptor_with_initialize_method_data(
55f09f52
PP
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
17f3083a 145 BT_ASSERT_PRE_NO_ERROR();
55f09f52
PP
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;
6871026b 175 bt_object_get_ref_no_null_check(entry->comp_cls);
55f09f52
PP
176 bt_component_class_freeze(entry->comp_cls);
177 entry->params = (void *) params;
6871026b 178 bt_object_get_ref_no_null_check(entry->params);
55f09f52
PP
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:
498e7994 194 bt_common_abort();
55f09f52
PP
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
205error:
206 destroy_component_descriptor_set_entry(entry);
207 entry = NULL;
208
209end:
210 bt_object_put_ref(new_params);
211 return status;
212}
213
214enum bt_component_descriptor_set_add_descriptor_status
215bt_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{
17f3083a
SM
220 BT_ASSERT_PRE_NO_ERROR();
221
21a9f056 222 return bt_component_descriptor_set_add_descriptor_with_initialize_method_data(
55f09f52
PP
223 comp_descr_set, comp_cls, params, NULL);
224}
225
226void 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
232void 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.040112 seconds and 4 git commands to generate.