Commit | Line | Data |
---|---|---|
36e611bb 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 | ||
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_LOGI_STR("Creating component descriptor set object."); | |
90 | comp_descr_set = g_new0(struct bt_component_descriptor_set, 1); | |
91 | if (!comp_descr_set) { | |
92 | BT_LIB_LOGE_APPEND_CAUSE( | |
93 | "Failed to allocate one component descriptor set."); | |
94 | goto end; | |
95 | } | |
96 | ||
97 | bt_object_init_shared(&comp_descr_set->base, | |
98 | destroy_component_descriptor_set); | |
99 | comp_descr_set->sources = g_ptr_array_new_with_free_func( | |
100 | destroy_component_descriptor_set_entry); | |
101 | if (!comp_descr_set->sources) { | |
102 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray."); | |
103 | goto error; | |
104 | } | |
105 | ||
106 | comp_descr_set->filters = g_ptr_array_new_with_free_func( | |
107 | destroy_component_descriptor_set_entry); | |
108 | if (!comp_descr_set->filters) { | |
109 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray."); | |
110 | goto error; | |
111 | } | |
112 | ||
113 | comp_descr_set->sinks = g_ptr_array_new_with_free_func( | |
114 | destroy_component_descriptor_set_entry); | |
115 | if (!comp_descr_set->sinks) { | |
116 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray."); | |
117 | goto error; | |
118 | } | |
119 | ||
120 | BT_LOGI("Created component descriptor set object: addr=%p", | |
121 | comp_descr_set); | |
122 | goto end; | |
123 | ||
124 | error: | |
125 | BT_OBJECT_PUT_REF_AND_RESET(comp_descr_set); | |
126 | ||
127 | end: | |
128 | return comp_descr_set; | |
129 | } | |
130 | ||
131 | enum bt_component_descriptor_set_add_descriptor_status | |
4175c1d5 | 132 | bt_component_descriptor_set_add_descriptor_with_initialize_method_data( |
36e611bb PP |
133 | struct bt_component_descriptor_set *comp_descr_set, |
134 | const struct bt_component_class *comp_cls, | |
135 | const struct bt_value *params, void *init_method_data) | |
136 | { | |
137 | bt_component_descriptor_set_add_descriptor_status status = | |
138 | BT_FUNC_STATUS_OK; | |
139 | struct bt_value *new_params = NULL; | |
140 | struct bt_component_descriptor_set_entry *entry = NULL; | |
141 | GPtrArray *comp_descr_array = NULL; | |
142 | ||
143 | BT_ASSERT_PRE_NON_NULL(comp_descr_set, "Component descriptor set"); | |
144 | BT_ASSERT_PRE_NON_NULL(comp_cls, "Component class"); | |
145 | BT_ASSERT_PRE(!params || bt_value_is_map(params), | |
146 | "Parameter value is not a map value: %!+v", params); | |
147 | BT_LIB_LOGI("Adding component descriptor to set: " | |
148 | "set-addr=%p, %![cc-]+C, " | |
149 | "%![params-]+v, init-method-data-addr=%p", | |
150 | comp_descr_set, comp_cls, params, init_method_data); | |
151 | ||
152 | if (!params) { | |
153 | new_params = bt_value_map_create(); | |
154 | if (!new_params) { | |
155 | BT_LIB_LOGE_APPEND_CAUSE( | |
156 | "Cannot create empty map value object."); | |
157 | status = BT_FUNC_STATUS_MEMORY_ERROR; | |
158 | goto error; | |
159 | } | |
160 | ||
161 | params = new_params; | |
162 | } | |
163 | ||
164 | entry = g_new0(struct bt_component_descriptor_set_entry, 1); | |
165 | if (!entry) { | |
166 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray."); | |
167 | status = BT_FUNC_STATUS_MEMORY_ERROR; | |
168 | goto error; | |
169 | } | |
170 | ||
171 | entry->comp_cls = (void *) comp_cls; | |
172 | bt_object_get_no_null_check(entry->comp_cls); | |
173 | bt_component_class_freeze(entry->comp_cls); | |
174 | entry->params = (void *) params; | |
175 | bt_object_get_no_null_check(entry->params); | |
176 | bt_value_freeze(entry->params); | |
177 | entry->init_method_data = init_method_data; | |
178 | ||
179 | /* Move to array */ | |
180 | switch (comp_cls->type) { | |
181 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
182 | comp_descr_array = comp_descr_set->sources; | |
183 | break; | |
184 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
185 | comp_descr_array = comp_descr_set->filters; | |
186 | break; | |
187 | case BT_COMPONENT_CLASS_TYPE_SINK: | |
188 | comp_descr_array = comp_descr_set->sinks; | |
189 | break; | |
190 | default: | |
191 | abort(); | |
192 | } | |
193 | ||
194 | BT_ASSERT(comp_descr_array); | |
195 | g_ptr_array_add(comp_descr_array, entry); | |
196 | BT_LIB_LOGI("Added component descriptor to set: " | |
197 | "set-addr=%p, %![cc-]+C, " | |
198 | "%![params-]+v, init-method-data-addr=%p", | |
199 | comp_descr_set, comp_cls, params, init_method_data); | |
200 | goto end; | |
201 | ||
202 | error: | |
203 | destroy_component_descriptor_set_entry(entry); | |
204 | entry = NULL; | |
205 | ||
206 | end: | |
207 | bt_object_put_ref(new_params); | |
208 | return status; | |
209 | } | |
210 | ||
211 | enum bt_component_descriptor_set_add_descriptor_status | |
212 | bt_component_descriptor_set_add_descriptor( | |
213 | struct bt_component_descriptor_set *comp_descr_set, | |
214 | const struct bt_component_class *comp_cls, | |
215 | const struct bt_value *params) | |
216 | { | |
4175c1d5 | 217 | return bt_component_descriptor_set_add_descriptor_with_initialize_method_data( |
36e611bb PP |
218 | comp_descr_set, comp_cls, params, NULL); |
219 | } | |
220 | ||
221 | void bt_component_descriptor_set_get_ref( | |
222 | const struct bt_component_descriptor_set *comp_descr_set) | |
223 | { | |
224 | bt_object_get_ref(comp_descr_set); | |
225 | } | |
226 | ||
227 | void bt_component_descriptor_set_put_ref( | |
228 | const struct bt_component_descriptor_set *comp_descr_set) | |
229 | { | |
230 | bt_object_put_ref(comp_descr_set); | |
231 | } |