Commit | Line | Data |
---|---|---|
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> | |
55f09f52 PP |
33 | #include <unistd.h> |
34 | #include <glib.h> | |
35 | ||
36 | #include "component-class.h" | |
37 | #include "component-descriptor-set.h" | |
38 | #include "component-class-sink-simple.h" | |
39 | #include "lib/value.h" | |
40 | ||
41 | static | |
42 | void destroy_component_descriptor_set(struct bt_object *obj) | |
43 | { | |
44 | struct bt_component_descriptor_set *comp_descr_set = (void *) obj; | |
45 | ||
46 | if (comp_descr_set->sources) { | |
47 | BT_LOGD_STR("Destroying source component descriptors."); | |
48 | g_ptr_array_free(comp_descr_set->sources, TRUE); | |
49 | comp_descr_set->sources = NULL; | |
50 | } | |
51 | ||
52 | if (comp_descr_set->filters) { | |
53 | BT_LOGD_STR("Destroying filter component descriptors."); | |
54 | g_ptr_array_free(comp_descr_set->filters, TRUE); | |
55 | comp_descr_set->filters = NULL; | |
56 | } | |
57 | ||
58 | if (comp_descr_set->sinks) { | |
59 | BT_LOGD_STR("Destroying sink component descriptors."); | |
60 | g_ptr_array_free(comp_descr_set->sinks, TRUE); | |
61 | comp_descr_set->sinks = NULL; | |
62 | } | |
63 | ||
64 | g_free(comp_descr_set); | |
65 | } | |
66 | ||
67 | static | |
68 | void destroy_component_descriptor_set_entry(gpointer ptr) | |
69 | { | |
70 | struct bt_component_descriptor_set_entry *entry = ptr; | |
71 | ||
72 | if (!ptr) { | |
73 | goto end; | |
74 | } | |
75 | ||
76 | BT_OBJECT_PUT_REF_AND_RESET(entry->comp_cls); | |
77 | BT_OBJECT_PUT_REF_AND_RESET(entry->params); | |
78 | g_free(entry); | |
79 | ||
80 | end: | |
81 | return; | |
82 | } | |
83 | ||
84 | struct bt_component_descriptor_set *bt_component_descriptor_set_create(void) | |
85 | { | |
86 | struct bt_component_descriptor_set *comp_descr_set; | |
87 | ||
17f3083a SM |
88 | BT_ASSERT_PRE_NO_ERROR(); |
89 | ||
55f09f52 PP |
90 | BT_LOGI_STR("Creating component descriptor set object."); |
91 | comp_descr_set = g_new0(struct bt_component_descriptor_set, 1); | |
92 | if (!comp_descr_set) { | |
93 | BT_LIB_LOGE_APPEND_CAUSE( | |
94 | "Failed to allocate one component descriptor set."); | |
95 | goto end; | |
96 | } | |
97 | ||
98 | bt_object_init_shared(&comp_descr_set->base, | |
99 | destroy_component_descriptor_set); | |
100 | comp_descr_set->sources = g_ptr_array_new_with_free_func( | |
101 | destroy_component_descriptor_set_entry); | |
102 | if (!comp_descr_set->sources) { | |
103 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray."); | |
104 | goto error; | |
105 | } | |
106 | ||
107 | comp_descr_set->filters = g_ptr_array_new_with_free_func( | |
108 | destroy_component_descriptor_set_entry); | |
109 | if (!comp_descr_set->filters) { | |
110 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray."); | |
111 | goto error; | |
112 | } | |
113 | ||
114 | comp_descr_set->sinks = g_ptr_array_new_with_free_func( | |
115 | destroy_component_descriptor_set_entry); | |
116 | if (!comp_descr_set->sinks) { | |
117 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray."); | |
118 | goto error; | |
119 | } | |
120 | ||
121 | BT_LOGI("Created component descriptor set object: addr=%p", | |
122 | comp_descr_set); | |
123 | goto end; | |
124 | ||
125 | error: | |
126 | BT_OBJECT_PUT_REF_AND_RESET(comp_descr_set); | |
127 | ||
128 | end: | |
129 | return comp_descr_set; | |
130 | } | |
131 | ||
132 | enum bt_component_descriptor_set_add_descriptor_status | |
21a9f056 | 133 | bt_component_descriptor_set_add_descriptor_with_initialize_method_data( |
55f09f52 PP |
134 | struct bt_component_descriptor_set *comp_descr_set, |
135 | const struct bt_component_class *comp_cls, | |
136 | const struct bt_value *params, void *init_method_data) | |
137 | { | |
138 | bt_component_descriptor_set_add_descriptor_status status = | |
139 | BT_FUNC_STATUS_OK; | |
140 | struct bt_value *new_params = NULL; | |
141 | struct bt_component_descriptor_set_entry *entry = NULL; | |
142 | GPtrArray *comp_descr_array = NULL; | |
143 | ||
17f3083a | 144 | BT_ASSERT_PRE_NO_ERROR(); |
55f09f52 PP |
145 | BT_ASSERT_PRE_NON_NULL(comp_descr_set, "Component descriptor set"); |
146 | BT_ASSERT_PRE_NON_NULL(comp_cls, "Component class"); | |
147 | BT_ASSERT_PRE(!params || bt_value_is_map(params), | |
148 | "Parameter value is not a map value: %!+v", params); | |
149 | BT_LIB_LOGI("Adding component descriptor to set: " | |
150 | "set-addr=%p, %![cc-]+C, " | |
151 | "%![params-]+v, init-method-data-addr=%p", | |
152 | comp_descr_set, comp_cls, params, init_method_data); | |
153 | ||
154 | if (!params) { | |
155 | new_params = bt_value_map_create(); | |
156 | if (!new_params) { | |
157 | BT_LIB_LOGE_APPEND_CAUSE( | |
158 | "Cannot create empty map value object."); | |
159 | status = BT_FUNC_STATUS_MEMORY_ERROR; | |
160 | goto error; | |
161 | } | |
162 | ||
163 | params = new_params; | |
164 | } | |
165 | ||
166 | entry = g_new0(struct bt_component_descriptor_set_entry, 1); | |
167 | if (!entry) { | |
168 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray."); | |
169 | status = BT_FUNC_STATUS_MEMORY_ERROR; | |
170 | goto error; | |
171 | } | |
172 | ||
173 | entry->comp_cls = (void *) comp_cls; | |
6871026b | 174 | bt_object_get_ref_no_null_check(entry->comp_cls); |
55f09f52 PP |
175 | bt_component_class_freeze(entry->comp_cls); |
176 | entry->params = (void *) params; | |
6871026b | 177 | bt_object_get_ref_no_null_check(entry->params); |
55f09f52 PP |
178 | bt_value_freeze(entry->params); |
179 | entry->init_method_data = init_method_data; | |
180 | ||
181 | /* Move to array */ | |
182 | switch (comp_cls->type) { | |
183 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
184 | comp_descr_array = comp_descr_set->sources; | |
185 | break; | |
186 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
187 | comp_descr_array = comp_descr_set->filters; | |
188 | break; | |
189 | case BT_COMPONENT_CLASS_TYPE_SINK: | |
190 | comp_descr_array = comp_descr_set->sinks; | |
191 | break; | |
192 | default: | |
498e7994 | 193 | bt_common_abort(); |
55f09f52 PP |
194 | } |
195 | ||
196 | BT_ASSERT(comp_descr_array); | |
197 | g_ptr_array_add(comp_descr_array, entry); | |
198 | BT_LIB_LOGI("Added component descriptor to set: " | |
199 | "set-addr=%p, %![cc-]+C, " | |
200 | "%![params-]+v, init-method-data-addr=%p", | |
201 | comp_descr_set, comp_cls, params, init_method_data); | |
202 | goto end; | |
203 | ||
204 | error: | |
205 | destroy_component_descriptor_set_entry(entry); | |
206 | entry = NULL; | |
207 | ||
208 | end: | |
209 | bt_object_put_ref(new_params); | |
210 | return status; | |
211 | } | |
212 | ||
213 | enum bt_component_descriptor_set_add_descriptor_status | |
214 | bt_component_descriptor_set_add_descriptor( | |
215 | struct bt_component_descriptor_set *comp_descr_set, | |
216 | const struct bt_component_class *comp_cls, | |
217 | const struct bt_value *params) | |
218 | { | |
17f3083a SM |
219 | BT_ASSERT_PRE_NO_ERROR(); |
220 | ||
21a9f056 | 221 | return bt_component_descriptor_set_add_descriptor_with_initialize_method_data( |
55f09f52 PP |
222 | comp_descr_set, comp_cls, params, NULL); |
223 | } | |
224 | ||
225 | void bt_component_descriptor_set_get_ref( | |
226 | const struct bt_component_descriptor_set *comp_descr_set) | |
227 | { | |
228 | bt_object_get_ref(comp_descr_set); | |
229 | } | |
230 | ||
231 | void bt_component_descriptor_set_put_ref( | |
232 | const struct bt_component_descriptor_set *comp_descr_set) | |
233 | { | |
234 | bt_object_put_ref(comp_descr_set); | |
235 | } |