2 * Copyright 2017-2019 Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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
24 #define BT_LOG_TAG "LIB/COMP-DESCR-SET"
25 #include "lib/logging.h"
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>
36 #include "component-class.h"
37 #include "component-descriptor-set.h"
38 #include "component-class-sink-simple.h"
39 #include "lib/value.h"
42 void destroy_component_descriptor_set(struct bt_object
*obj
)
44 struct bt_component_descriptor_set
*comp_descr_set
= (void *) obj
;
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
;
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
;
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
;
64 g_free(comp_descr_set
);
68 void destroy_component_descriptor_set_entry(gpointer ptr
)
70 struct bt_component_descriptor_set_entry
*entry
= ptr
;
76 BT_OBJECT_PUT_REF_AND_RESET(entry
->comp_cls
);
77 BT_OBJECT_PUT_REF_AND_RESET(entry
->params
);
84 struct bt_component_descriptor_set
*bt_component_descriptor_set_create(void)
86 struct bt_component_descriptor_set
*comp_descr_set
;
88 BT_ASSERT_PRE_NO_ERROR();
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.");
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.");
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.");
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.");
121 BT_LOGI("Created component descriptor set object: addr=%p",
126 BT_OBJECT_PUT_REF_AND_RESET(comp_descr_set
);
129 return comp_descr_set
;
132 enum bt_component_descriptor_set_add_descriptor_status
133 bt_component_descriptor_set_add_descriptor_with_initialize_method_data(
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
)
138 bt_component_descriptor_set_add_descriptor_status status
=
140 struct bt_value
*new_params
= NULL
;
141 struct bt_component_descriptor_set_entry
*entry
= NULL
;
142 GPtrArray
*comp_descr_array
= NULL
;
144 BT_ASSERT_PRE_NO_ERROR();
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
);
155 new_params
= bt_value_map_create();
157 BT_LIB_LOGE_APPEND_CAUSE(
158 "Cannot create empty map value object.");
159 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
166 entry
= g_new0(struct bt_component_descriptor_set_entry
, 1);
168 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
169 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
173 entry
->comp_cls
= (void *) comp_cls
;
174 bt_object_get_ref_no_null_check(entry
->comp_cls
);
175 bt_component_class_freeze(entry
->comp_cls
);
176 entry
->params
= (void *) params
;
177 bt_object_get_ref_no_null_check(entry
->params
);
178 bt_value_freeze(entry
->params
);
179 entry
->init_method_data
= init_method_data
;
182 switch (comp_cls
->type
) {
183 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
184 comp_descr_array
= comp_descr_set
->sources
;
186 case BT_COMPONENT_CLASS_TYPE_FILTER
:
187 comp_descr_array
= comp_descr_set
->filters
;
189 case BT_COMPONENT_CLASS_TYPE_SINK
:
190 comp_descr_array
= comp_descr_set
->sinks
;
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
);
205 destroy_component_descriptor_set_entry(entry
);
209 bt_object_put_ref(new_params
);
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
)
219 BT_ASSERT_PRE_NO_ERROR();
221 return bt_component_descriptor_set_add_descriptor_with_initialize_method_data(
222 comp_descr_set
, comp_cls
, params
, NULL
);
225 void bt_component_descriptor_set_get_ref(
226 const struct bt_component_descriptor_set
*comp_descr_set
)
228 bt_object_get_ref(comp_descr_set
);
231 void bt_component_descriptor_set_put_ref(
232 const struct bt_component_descriptor_set
*comp_descr_set
)
234 bt_object_put_ref(comp_descr_set
);