| 1 | /* |
| 2 | * component.c |
| 3 | * |
| 4 | * Babeltrace Plugin Component |
| 5 | * |
| 6 | * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 7 | * |
| 8 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 9 | * |
| 10 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 11 | * of this software and associated documentation files (the "Software"), to deal |
| 12 | * in the Software without restriction, including without limitation the rights |
| 13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 14 | * copies of the Software, and to permit persons to whom the Software is |
| 15 | * furnished to do so, subject to the following conditions: |
| 16 | * |
| 17 | * The above copyright notice and this permission notice shall be included in |
| 18 | * all copies or substantial portions of the Software. |
| 19 | * |
| 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 26 | * SOFTWARE. |
| 27 | */ |
| 28 | |
| 29 | #include <babeltrace/plugin/component.h> |
| 30 | #include <babeltrace/plugin/component-internal.h> |
| 31 | #include <babeltrace/plugin/source-internal.h> |
| 32 | #include <babeltrace/plugin/sink-internal.h> |
| 33 | #include <babeltrace/babeltrace-internal.h> |
| 34 | #include <babeltrace/compiler.h> |
| 35 | #include <babeltrace/ref.h> |
| 36 | |
| 37 | static |
| 38 | struct bt_component * (* const component_create_funcs[])( |
| 39 | struct bt_component_class *, struct bt_value *) = { |
| 40 | [BT_COMPONENT_TYPE_SOURCE] = bt_component_source_create, |
| 41 | [BT_COMPONENT_TYPE_SINK] = bt_component_sink_create, |
| 42 | }; |
| 43 | |
| 44 | static |
| 45 | enum bt_component_status (* const component_validation_funcs[])( |
| 46 | struct bt_component *) = { |
| 47 | [BT_COMPONENT_TYPE_SOURCE] = bt_component_source_validate, |
| 48 | [BT_COMPONENT_TYPE_SINK] = bt_component_sink_validate, |
| 49 | }; |
| 50 | |
| 51 | static |
| 52 | void bt_component_destroy(struct bt_object *obj) |
| 53 | { |
| 54 | struct bt_component *component = NULL; |
| 55 | struct bt_component_class *component_class = NULL; |
| 56 | |
| 57 | if (!obj) { |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | component = container_of(obj, struct bt_component, base); |
| 62 | |
| 63 | assert(component->destroy); |
| 64 | component_class = component->class; |
| 65 | |
| 66 | /* |
| 67 | * User data is destroyed first, followed by the concrete component |
| 68 | * instance. |
| 69 | */ |
| 70 | if (component->user_destroy) { |
| 71 | component->user_destroy(component->user_data); |
| 72 | } |
| 73 | |
| 74 | component->destroy(component); |
| 75 | g_string_free(component->name, TRUE); |
| 76 | bt_put(component_class); |
| 77 | g_free(component); |
| 78 | } |
| 79 | |
| 80 | BT_HIDDEN |
| 81 | enum bt_component_status bt_component_init(struct bt_component *component, |
| 82 | bt_component_destroy_cb destroy) |
| 83 | { |
| 84 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
| 85 | |
| 86 | if (!component || !destroy) { |
| 87 | ret = BT_COMPONENT_STATUS_INVALID; |
| 88 | goto end; |
| 89 | } |
| 90 | |
| 91 | component->destroy = destroy; |
| 92 | end: |
| 93 | return ret; |
| 94 | } |
| 95 | |
| 96 | BT_HIDDEN |
| 97 | enum bt_component_type bt_component_get_type(struct bt_component *component) |
| 98 | { |
| 99 | return component ? component->class->type : BT_COMPONENT_TYPE_UNKNOWN; |
| 100 | } |
| 101 | |
| 102 | struct bt_component *bt_component_create( |
| 103 | struct bt_component_class *component_class, const char *name, |
| 104 | struct bt_value *params) |
| 105 | { |
| 106 | int ret; |
| 107 | struct bt_component *component = NULL; |
| 108 | enum bt_component_type type; |
| 109 | |
| 110 | if (!component_class) { |
| 111 | goto end; |
| 112 | } |
| 113 | |
| 114 | type = bt_component_class_get_type(component_class); |
| 115 | if (type <= BT_COMPONENT_TYPE_UNKNOWN || |
| 116 | type >= BT_COMPONENT_TYPE_FILTER) { |
| 117 | /* Filter components are not supported yet. */ |
| 118 | goto end; |
| 119 | } |
| 120 | |
| 121 | component = component_create_funcs[type](component_class, params); |
| 122 | if (!component) { |
| 123 | goto end; |
| 124 | } |
| 125 | |
| 126 | bt_object_init(component, bt_component_destroy); |
| 127 | component->name = g_string_new(name); |
| 128 | if (!component->name) { |
| 129 | BT_PUT(component); |
| 130 | goto end; |
| 131 | } |
| 132 | |
| 133 | component_class->init(component, params); |
| 134 | ret = component_validation_funcs[type](component); |
| 135 | if (ret) { |
| 136 | BT_PUT(component); |
| 137 | goto end; |
| 138 | } |
| 139 | end: |
| 140 | return component; |
| 141 | } |
| 142 | |
| 143 | const char *bt_component_get_name(struct bt_component *component) |
| 144 | { |
| 145 | const char *ret = NULL; |
| 146 | |
| 147 | if (!component) { |
| 148 | goto end; |
| 149 | } |
| 150 | |
| 151 | ret = component->name->str; |
| 152 | end: |
| 153 | return ret; |
| 154 | } |
| 155 | |
| 156 | enum bt_component_status bt_component_set_name(struct bt_component *component, |
| 157 | const char *name) |
| 158 | { |
| 159 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
| 160 | |
| 161 | if (!component || !name || name[0] == '\0') { |
| 162 | ret = BT_COMPONENT_STATUS_INVALID; |
| 163 | goto end; |
| 164 | } |
| 165 | |
| 166 | g_string_assign(component->name, name); |
| 167 | end: |
| 168 | return ret; |
| 169 | } |
| 170 | |
| 171 | struct bt_component_class *bt_component_get_class( |
| 172 | struct bt_component *component) |
| 173 | { |
| 174 | return component ? bt_get(component->class) : NULL; |
| 175 | } |
| 176 | |
| 177 | void *bt_component_get_private_data(struct bt_component *component) |
| 178 | { |
| 179 | return component ? component->user_data : NULL; |
| 180 | } |
| 181 | |
| 182 | enum bt_component_status |
| 183 | bt_component_set_private_data(struct bt_component *component, |
| 184 | void *data) |
| 185 | { |
| 186 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
| 187 | |
| 188 | if (!component) { |
| 189 | ret = BT_COMPONENT_STATUS_INVALID; |
| 190 | goto end; |
| 191 | } |
| 192 | |
| 193 | component->user_data = data; |
| 194 | end: |
| 195 | return ret; |
| 196 | } |