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>
33 #include <babeltrace2/value-const.h>
37 #include "component-class.h"
38 #include "component-descriptor-set.h"
39 #include "component-class-sink-simple.h"
40 #include "lib/value.h"
43 void destroy_component_descriptor_set(struct bt_object
*obj
)
45 struct bt_component_descriptor_set
*comp_descr_set
= (void *) obj
;
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
;
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
;
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
;
65 g_free(comp_descr_set
);
69 void destroy_component_descriptor_set_entry(gpointer ptr
)
71 struct bt_component_descriptor_set_entry
*entry
= ptr
;
77 BT_OBJECT_PUT_REF_AND_RESET(entry
->comp_cls
);
78 BT_OBJECT_PUT_REF_AND_RESET(entry
->params
);
85 struct bt_component_descriptor_set
*bt_component_descriptor_set_create(void)
87 struct bt_component_descriptor_set
*comp_descr_set
;
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.");
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.");
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.");
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.");
120 BT_LOGI("Created component descriptor set object: addr=%p",
125 BT_OBJECT_PUT_REF_AND_RESET(comp_descr_set
);
128 return comp_descr_set
;
131 enum bt_component_descriptor_set_add_descriptor_status
132 bt_component_descriptor_set_add_descriptor_with_init_method_data(
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
)
137 bt_component_descriptor_set_add_descriptor_status status
=
139 struct bt_value
*new_params
= NULL
;
140 struct bt_component_descriptor_set_entry
*entry
= NULL
;
141 GPtrArray
*comp_descr_array
= NULL
;
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
);
153 new_params
= bt_value_map_create();
155 BT_LIB_LOGE_APPEND_CAUSE(
156 "Cannot create empty map value object.");
157 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
164 entry
= g_new0(struct bt_component_descriptor_set_entry
, 1);
166 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
167 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
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
;
180 switch (comp_cls
->type
) {
181 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
182 comp_descr_array
= comp_descr_set
->sources
;
184 case BT_COMPONENT_CLASS_TYPE_FILTER
:
185 comp_descr_array
= comp_descr_set
->filters
;
187 case BT_COMPONENT_CLASS_TYPE_SINK
:
188 comp_descr_array
= comp_descr_set
->sinks
;
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
);
203 destroy_component_descriptor_set_entry(entry
);
207 bt_object_put_ref(new_params
);
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
)
217 return bt_component_descriptor_set_add_descriptor_with_init_method_data(
218 comp_descr_set
, comp_cls
, params
, NULL
);
221 void bt_component_descriptor_set_get_ref(
222 const struct bt_component_descriptor_set
*comp_descr_set
)
224 bt_object_get_ref(comp_descr_set
);
227 void bt_component_descriptor_set_put_ref(
228 const struct bt_component_descriptor_set
*comp_descr_set
)
230 bt_object_put_ref(comp_descr_set
);