| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright 2017-2019 Philippe Proulx <pproulx@efficios.com> |
| 5 | * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 6 | */ |
| 7 | |
| 8 | #define BT_LOG_TAG "LIB/COMP-DESCR-SET" |
| 9 | #include "lib/logging.h" |
| 10 | |
| 11 | #include "common/assert.h" |
| 12 | #include "lib/assert-cond.h" |
| 13 | #include "compat/compiler.h" |
| 14 | #include "common/common.h" |
| 15 | #include <babeltrace2/types.h> |
| 16 | #include <babeltrace2/value.h> |
| 17 | #include <unistd.h> |
| 18 | #include <glib.h> |
| 19 | |
| 20 | #include "component-class.h" |
| 21 | #include "component-descriptor-set.h" |
| 22 | #include "component-class-sink-simple.h" |
| 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 | |
| 68 | struct bt_component_descriptor_set *bt_component_descriptor_set_create(void) |
| 69 | { |
| 70 | struct bt_component_descriptor_set *comp_descr_set; |
| 71 | |
| 72 | BT_ASSERT_PRE_NO_ERROR(); |
| 73 | |
| 74 | BT_LOGI_STR("Creating component descriptor set object."); |
| 75 | comp_descr_set = g_new0(struct bt_component_descriptor_set, 1); |
| 76 | if (!comp_descr_set) { |
| 77 | BT_LIB_LOGE_APPEND_CAUSE( |
| 78 | "Failed to allocate one component descriptor set."); |
| 79 | goto end; |
| 80 | } |
| 81 | |
| 82 | bt_object_init_shared(&comp_descr_set->base, |
| 83 | destroy_component_descriptor_set); |
| 84 | comp_descr_set->sources = g_ptr_array_new_with_free_func( |
| 85 | destroy_component_descriptor_set_entry); |
| 86 | if (!comp_descr_set->sources) { |
| 87 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray."); |
| 88 | goto error; |
| 89 | } |
| 90 | |
| 91 | comp_descr_set->filters = g_ptr_array_new_with_free_func( |
| 92 | destroy_component_descriptor_set_entry); |
| 93 | if (!comp_descr_set->filters) { |
| 94 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray."); |
| 95 | goto error; |
| 96 | } |
| 97 | |
| 98 | comp_descr_set->sinks = g_ptr_array_new_with_free_func( |
| 99 | destroy_component_descriptor_set_entry); |
| 100 | if (!comp_descr_set->sinks) { |
| 101 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray."); |
| 102 | goto error; |
| 103 | } |
| 104 | |
| 105 | BT_LOGI("Created component descriptor set object: addr=%p", |
| 106 | comp_descr_set); |
| 107 | goto end; |
| 108 | |
| 109 | error: |
| 110 | BT_OBJECT_PUT_REF_AND_RESET(comp_descr_set); |
| 111 | |
| 112 | end: |
| 113 | return comp_descr_set; |
| 114 | } |
| 115 | |
| 116 | enum bt_component_descriptor_set_add_descriptor_status |
| 117 | bt_component_descriptor_set_add_descriptor_with_initialize_method_data( |
| 118 | struct bt_component_descriptor_set *comp_descr_set, |
| 119 | const struct bt_component_class *comp_cls, |
| 120 | const struct bt_value *params, void *init_method_data) |
| 121 | { |
| 122 | bt_component_descriptor_set_add_descriptor_status status = |
| 123 | BT_FUNC_STATUS_OK; |
| 124 | struct bt_value *new_params = NULL; |
| 125 | struct bt_component_descriptor_set_entry *entry = NULL; |
| 126 | GPtrArray *comp_descr_array = NULL; |
| 127 | |
| 128 | BT_ASSERT_PRE_NO_ERROR(); |
| 129 | BT_ASSERT_PRE_NON_NULL(comp_descr_set, "Component descriptor set"); |
| 130 | BT_ASSERT_PRE_NON_NULL(comp_cls, "Component class"); |
| 131 | BT_ASSERT_PRE(!params || bt_value_is_map(params), |
| 132 | "Parameter value is not a map value: %!+v", params); |
| 133 | BT_LIB_LOGI("Adding component descriptor to set: " |
| 134 | "set-addr=%p, %![cc-]+C, " |
| 135 | "%![params-]+v, init-method-data-addr=%p", |
| 136 | comp_descr_set, comp_cls, params, init_method_data); |
| 137 | |
| 138 | if (!params) { |
| 139 | new_params = bt_value_map_create(); |
| 140 | if (!new_params) { |
| 141 | BT_LIB_LOGE_APPEND_CAUSE( |
| 142 | "Cannot create empty map value object."); |
| 143 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 144 | goto error; |
| 145 | } |
| 146 | |
| 147 | params = new_params; |
| 148 | } |
| 149 | |
| 150 | entry = g_new0(struct bt_component_descriptor_set_entry, 1); |
| 151 | if (!entry) { |
| 152 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray."); |
| 153 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 154 | goto error; |
| 155 | } |
| 156 | |
| 157 | entry->comp_cls = (void *) comp_cls; |
| 158 | bt_object_get_ref_no_null_check(entry->comp_cls); |
| 159 | bt_component_class_freeze(entry->comp_cls); |
| 160 | entry->params = (void *) params; |
| 161 | bt_object_get_ref_no_null_check(entry->params); |
| 162 | bt_value_freeze(entry->params); |
| 163 | entry->init_method_data = init_method_data; |
| 164 | |
| 165 | /* Move to array */ |
| 166 | switch (comp_cls->type) { |
| 167 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
| 168 | comp_descr_array = comp_descr_set->sources; |
| 169 | break; |
| 170 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 171 | comp_descr_array = comp_descr_set->filters; |
| 172 | break; |
| 173 | case BT_COMPONENT_CLASS_TYPE_SINK: |
| 174 | comp_descr_array = comp_descr_set->sinks; |
| 175 | break; |
| 176 | default: |
| 177 | bt_common_abort(); |
| 178 | } |
| 179 | |
| 180 | BT_ASSERT(comp_descr_array); |
| 181 | g_ptr_array_add(comp_descr_array, entry); |
| 182 | BT_LIB_LOGI("Added component descriptor to set: " |
| 183 | "set-addr=%p, %![cc-]+C, " |
| 184 | "%![params-]+v, init-method-data-addr=%p", |
| 185 | comp_descr_set, comp_cls, params, init_method_data); |
| 186 | goto end; |
| 187 | |
| 188 | error: |
| 189 | destroy_component_descriptor_set_entry(entry); |
| 190 | entry = NULL; |
| 191 | |
| 192 | end: |
| 193 | bt_object_put_ref(new_params); |
| 194 | return status; |
| 195 | } |
| 196 | |
| 197 | enum bt_component_descriptor_set_add_descriptor_status |
| 198 | bt_component_descriptor_set_add_descriptor( |
| 199 | struct bt_component_descriptor_set *comp_descr_set, |
| 200 | const struct bt_component_class *comp_cls, |
| 201 | const struct bt_value *params) |
| 202 | { |
| 203 | BT_ASSERT_PRE_NO_ERROR(); |
| 204 | |
| 205 | return bt_component_descriptor_set_add_descriptor_with_initialize_method_data( |
| 206 | comp_descr_set, comp_cls, params, NULL); |
| 207 | } |
| 208 | |
| 209 | void bt_component_descriptor_set_get_ref( |
| 210 | const struct bt_component_descriptor_set *comp_descr_set) |
| 211 | { |
| 212 | bt_object_get_ref(comp_descr_set); |
| 213 | } |
| 214 | |
| 215 | void bt_component_descriptor_set_put_ref( |
| 216 | const struct bt_component_descriptor_set *comp_descr_set) |
| 217 | { |
| 218 | bt_object_put_ref(comp_descr_set); |
| 219 | } |