Commit | Line | Data |
---|---|---|
55f09f52 | 1 | /* |
0235b0db MJ |
2 | * SPDX-License-Identifier: MIT |
3 | * | |
55f09f52 PP |
4 | * Copyright 2017-2019 Philippe Proulx <pproulx@efficios.com> |
5 | * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
55f09f52 PP |
6 | */ |
7 | ||
8 | #define BT_LOG_TAG "LIB/COMP-DESCR-SET" | |
9 | #include "lib/logging.h" | |
10 | ||
11 | #include "common/assert.h" | |
d98421f2 | 12 | #include "lib/assert-cond.h" |
5a3fec55 | 13 | #include "lib/func-status.h" |
55f09f52 PP |
14 | #include "compat/compiler.h" |
15 | #include "common/common.h" | |
16 | #include <babeltrace2/types.h> | |
17 | #include <babeltrace2/value.h> | |
55f09f52 PP |
18 | #include <unistd.h> |
19 | #include <glib.h> | |
20 | ||
21 | #include "component-class.h" | |
22 | #include "component-descriptor-set.h" | |
55f09f52 PP |
23 | #include "lib/value.h" |
24 | ||
25 | static | |
26 | void destroy_component_descriptor_set(struct bt_object *obj) | |
27 | { | |
28 | struct bt_component_descriptor_set *comp_descr_set = (void *) obj; | |
29 | ||
30 | if (comp_descr_set->sources) { | |
31 | BT_LOGD_STR("Destroying source component descriptors."); | |
32 | g_ptr_array_free(comp_descr_set->sources, TRUE); | |
33 | comp_descr_set->sources = NULL; | |
34 | } | |
35 | ||
36 | if (comp_descr_set->filters) { | |
37 | BT_LOGD_STR("Destroying filter component descriptors."); | |
38 | g_ptr_array_free(comp_descr_set->filters, TRUE); | |
39 | comp_descr_set->filters = NULL; | |
40 | } | |
41 | ||
42 | if (comp_descr_set->sinks) { | |
43 | BT_LOGD_STR("Destroying sink component descriptors."); | |
44 | g_ptr_array_free(comp_descr_set->sinks, TRUE); | |
45 | comp_descr_set->sinks = NULL; | |
46 | } | |
47 | ||
48 | g_free(comp_descr_set); | |
49 | } | |
50 | ||
51 | static | |
52 | void destroy_component_descriptor_set_entry(gpointer ptr) | |
53 | { | |
54 | struct bt_component_descriptor_set_entry *entry = ptr; | |
55 | ||
56 | if (!ptr) { | |
57 | goto end; | |
58 | } | |
59 | ||
60 | BT_OBJECT_PUT_REF_AND_RESET(entry->comp_cls); | |
61 | BT_OBJECT_PUT_REF_AND_RESET(entry->params); | |
62 | g_free(entry); | |
63 | ||
64 | end: | |
65 | return; | |
66 | } | |
67 | ||
1353b066 | 68 | BT_EXPORT |
55f09f52 PP |
69 | struct bt_component_descriptor_set *bt_component_descriptor_set_create(void) |
70 | { | |
71 | struct bt_component_descriptor_set *comp_descr_set; | |
72 | ||
17f3083a SM |
73 | BT_ASSERT_PRE_NO_ERROR(); |
74 | ||
55f09f52 PP |
75 | BT_LOGI_STR("Creating component descriptor set object."); |
76 | comp_descr_set = g_new0(struct bt_component_descriptor_set, 1); | |
77 | if (!comp_descr_set) { | |
78 | BT_LIB_LOGE_APPEND_CAUSE( | |
79 | "Failed to allocate one component descriptor set."); | |
80 | goto end; | |
81 | } | |
82 | ||
83 | bt_object_init_shared(&comp_descr_set->base, | |
84 | destroy_component_descriptor_set); | |
85 | comp_descr_set->sources = g_ptr_array_new_with_free_func( | |
86 | destroy_component_descriptor_set_entry); | |
87 | if (!comp_descr_set->sources) { | |
88 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray."); | |
89 | goto error; | |
90 | } | |
91 | ||
92 | comp_descr_set->filters = g_ptr_array_new_with_free_func( | |
93 | destroy_component_descriptor_set_entry); | |
94 | if (!comp_descr_set->filters) { | |
95 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray."); | |
96 | goto error; | |
97 | } | |
98 | ||
99 | comp_descr_set->sinks = g_ptr_array_new_with_free_func( | |
100 | destroy_component_descriptor_set_entry); | |
101 | if (!comp_descr_set->sinks) { | |
102 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray."); | |
103 | goto error; | |
104 | } | |
105 | ||
106 | BT_LOGI("Created component descriptor set object: addr=%p", | |
107 | comp_descr_set); | |
108 | goto end; | |
109 | ||
110 | error: | |
111 | BT_OBJECT_PUT_REF_AND_RESET(comp_descr_set); | |
112 | ||
113 | end: | |
114 | return comp_descr_set; | |
115 | } | |
116 | ||
1353b066 | 117 | BT_EXPORT |
55f09f52 | 118 | enum bt_component_descriptor_set_add_descriptor_status |
21a9f056 | 119 | bt_component_descriptor_set_add_descriptor_with_initialize_method_data( |
55f09f52 PP |
120 | struct bt_component_descriptor_set *comp_descr_set, |
121 | const struct bt_component_class *comp_cls, | |
122 | const struct bt_value *params, void *init_method_data) | |
123 | { | |
124 | bt_component_descriptor_set_add_descriptor_status status = | |
125 | BT_FUNC_STATUS_OK; | |
126 | struct bt_value *new_params = NULL; | |
127 | struct bt_component_descriptor_set_entry *entry = NULL; | |
128 | GPtrArray *comp_descr_array = NULL; | |
129 | ||
17f3083a | 130 | BT_ASSERT_PRE_NO_ERROR(); |
d5b13b9b PP |
131 | BT_ASSERT_PRE_COMP_DESCR_SET_NON_NULL(comp_descr_set); |
132 | BT_ASSERT_PRE_COMP_CLS_NON_NULL(comp_cls); | |
133 | BT_ASSERT_PRE_PARAM_VALUE_IS_MAP(params); | |
55f09f52 PP |
134 | BT_LIB_LOGI("Adding component descriptor to set: " |
135 | "set-addr=%p, %![cc-]+C, " | |
136 | "%![params-]+v, init-method-data-addr=%p", | |
137 | comp_descr_set, comp_cls, params, init_method_data); | |
138 | ||
139 | if (!params) { | |
140 | new_params = bt_value_map_create(); | |
141 | if (!new_params) { | |
142 | BT_LIB_LOGE_APPEND_CAUSE( | |
143 | "Cannot create empty map value object."); | |
144 | status = BT_FUNC_STATUS_MEMORY_ERROR; | |
145 | goto error; | |
146 | } | |
147 | ||
148 | params = new_params; | |
149 | } | |
150 | ||
151 | entry = g_new0(struct bt_component_descriptor_set_entry, 1); | |
152 | if (!entry) { | |
153 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray."); | |
154 | status = BT_FUNC_STATUS_MEMORY_ERROR; | |
155 | goto error; | |
156 | } | |
157 | ||
158 | entry->comp_cls = (void *) comp_cls; | |
6871026b | 159 | bt_object_get_ref_no_null_check(entry->comp_cls); |
55f09f52 PP |
160 | bt_component_class_freeze(entry->comp_cls); |
161 | entry->params = (void *) params; | |
6871026b | 162 | bt_object_get_ref_no_null_check(entry->params); |
55f09f52 PP |
163 | bt_value_freeze(entry->params); |
164 | entry->init_method_data = init_method_data; | |
165 | ||
166 | /* Move to array */ | |
167 | switch (comp_cls->type) { | |
168 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
169 | comp_descr_array = comp_descr_set->sources; | |
170 | break; | |
171 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
172 | comp_descr_array = comp_descr_set->filters; | |
173 | break; | |
174 | case BT_COMPONENT_CLASS_TYPE_SINK: | |
175 | comp_descr_array = comp_descr_set->sinks; | |
176 | break; | |
177 | default: | |
498e7994 | 178 | bt_common_abort(); |
55f09f52 PP |
179 | } |
180 | ||
181 | BT_ASSERT(comp_descr_array); | |
182 | g_ptr_array_add(comp_descr_array, entry); | |
183 | BT_LIB_LOGI("Added component descriptor to set: " | |
184 | "set-addr=%p, %![cc-]+C, " | |
185 | "%![params-]+v, init-method-data-addr=%p", | |
186 | comp_descr_set, comp_cls, params, init_method_data); | |
187 | goto end; | |
188 | ||
189 | error: | |
190 | destroy_component_descriptor_set_entry(entry); | |
191 | entry = NULL; | |
192 | ||
193 | end: | |
194 | bt_object_put_ref(new_params); | |
195 | return status; | |
196 | } | |
197 | ||
1353b066 | 198 | BT_EXPORT |
55f09f52 PP |
199 | enum bt_component_descriptor_set_add_descriptor_status |
200 | bt_component_descriptor_set_add_descriptor( | |
201 | struct bt_component_descriptor_set *comp_descr_set, | |
202 | const struct bt_component_class *comp_cls, | |
203 | const struct bt_value *params) | |
204 | { | |
17f3083a SM |
205 | BT_ASSERT_PRE_NO_ERROR(); |
206 | ||
21a9f056 | 207 | return bt_component_descriptor_set_add_descriptor_with_initialize_method_data( |
55f09f52 PP |
208 | comp_descr_set, comp_cls, params, NULL); |
209 | } | |
210 | ||
1353b066 | 211 | BT_EXPORT |
55f09f52 PP |
212 | void bt_component_descriptor_set_get_ref( |
213 | const struct bt_component_descriptor_set *comp_descr_set) | |
214 | { | |
215 | bt_object_get_ref(comp_descr_set); | |
216 | } | |
217 | ||
1353b066 | 218 | BT_EXPORT |
55f09f52 PP |
219 | void bt_component_descriptor_set_put_ref( |
220 | const struct bt_component_descriptor_set *comp_descr_set) | |
221 | { | |
222 | bt_object_put_ref(comp_descr_set); | |
223 | } |