lib: strictly type function return status enumerations
[babeltrace.git] / src / lib / plugin / plugin.h
CommitLineData
33b34c43
PP
1#ifndef BABELTRACE_PLUGIN_PLUGIN_INTERNAL_H
2#define BABELTRACE_PLUGIN_PLUGIN_INTERNAL_H
fb2dcc52
JG
3
4/*
e2f7325d 5 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
fb2dcc52
JG
6 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 *
fb2dcc52
JG
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 */
26
91d81473 27#include "common/macros.h"
578e048b 28#include "lib/graph/component-class.h"
3fadfbc0
MJ
29#include <babeltrace2/plugin/plugin-const.h>
30#include <babeltrace2/plugin/plugin-dev.h>
578e048b 31#include "lib/object.h"
3fadfbc0 32#include <babeltrace2/types.h>
578e048b 33#include "common/assert.h"
55bb57e0 34#include <glib.h>
fb2dcc52 35
578e048b 36#include "plugin-so.h"
d24d5663 37#include "lib/func-status.h"
578e048b 38
d3207ebf 39/* Protection: this file uses BT_LIB_LOG*() macros directly */
7151fb67 40#ifndef BT_LIB_LOG_SUPPORTED
c2d9d9cf 41# error Please include "lib/logging.h" before including this file.
d3207ebf
PP
42#endif
43
55bb57e0
PP
44enum bt_plugin_type {
45 BT_PLUGIN_TYPE_SO = 0,
46 BT_PLUGIN_TYPE_PYTHON = 1,
fb2dcc52
JG
47};
48
33b34c43
PP
49struct bt_plugin {
50 struct bt_object base;
55bb57e0 51 enum bt_plugin_type type;
cba174d5 52
d94d92ac
PP
53 /* Arrays of `struct bt_component_class *` (owned by this) */
54 GPtrArray *src_comp_classes;
55 GPtrArray *flt_comp_classes;
56 GPtrArray *sink_comp_classes;
6ba0b073 57
55bb57e0
PP
58 /* Info (owned by this) */
59 struct {
60 GString *path;
61 GString *name;
62 GString *author;
63 GString *license;
64 GString *description;
65 struct {
66 unsigned int major;
67 unsigned int minor;
68 unsigned int patch;
69 GString *extra;
70 } version;
d94d92ac
PP
71 bool path_set;
72 bool name_set;
73 bool author_set;
74 bool license_set;
75 bool description_set;
76 bool version_set;
55bb57e0
PP
77 } info;
78
79 /* Value depends on the specific plugin type */
80 void *spec_data;
6fbd4105 81 void (*destroy_spec_data)(struct bt_plugin *);
33b34c43 82};
fb2dcc52 83
a8ff38ef
PP
84struct bt_plugin_set {
85 struct bt_object base;
86
87 /* Array of struct bt_plugin * */
88 GPtrArray *plugins;
89};
90
3fe0bf43
PP
91static inline
92const char *bt_plugin_type_string(enum bt_plugin_type type)
93{
94 switch (type) {
95 case BT_PLUGIN_TYPE_SO:
96 return "BT_PLUGIN_TYPE_SO";
97 case BT_PLUGIN_TYPE_PYTHON:
98 return "BT_PLUGIN_TYPE_PYTHON";
99 default:
100 return "(unknown)";
101 }
102}
103
6fbd4105
PP
104static inline
105void bt_plugin_destroy(struct bt_object *obj)
106{
107 struct bt_plugin *plugin;
108
f6ccaed9 109 BT_ASSERT(obj);
6fbd4105 110 plugin = container_of(obj, struct bt_plugin, base);
3f7d4d90 111 BT_LIB_LOGI("Destroying plugin object: %!+l", plugin);
6fbd4105
PP
112
113 if (plugin->destroy_spec_data) {
114 plugin->destroy_spec_data(plugin);
115 }
116
d94d92ac
PP
117 if (plugin->src_comp_classes) {
118 BT_LOGD_STR("Putting source component classes.");
119 g_ptr_array_free(plugin->src_comp_classes, TRUE);
db5504f9 120 plugin->src_comp_classes = NULL;
d94d92ac
PP
121 }
122
123 if (plugin->flt_comp_classes) {
124 BT_LOGD_STR("Putting filter component classes.");
125 g_ptr_array_free(plugin->flt_comp_classes, TRUE);
db5504f9 126 plugin->flt_comp_classes = NULL;
d94d92ac
PP
127 }
128
129 if (plugin->sink_comp_classes) {
130 BT_LOGD_STR("Putting sink component classes.");
131 g_ptr_array_free(plugin->sink_comp_classes, TRUE);
db5504f9 132 plugin->sink_comp_classes = NULL;
6fbd4105
PP
133 }
134
135 if (plugin->info.name) {
136 g_string_free(plugin->info.name, TRUE);
db5504f9 137 plugin->info.name = NULL;
6fbd4105
PP
138 }
139
140 if (plugin->info.path) {
141 g_string_free(plugin->info.path, TRUE);
db5504f9 142 plugin->info.path = NULL;
6fbd4105
PP
143 }
144
145 if (plugin->info.description) {
146 g_string_free(plugin->info.description, TRUE);
db5504f9 147 plugin->info.description = NULL;
6fbd4105
PP
148 }
149
150 if (plugin->info.author) {
151 g_string_free(plugin->info.author, TRUE);
db5504f9 152 plugin->info.author = NULL;
6fbd4105
PP
153 }
154
155 if (plugin->info.license) {
156 g_string_free(plugin->info.license, TRUE);
db5504f9 157 plugin->info.license = NULL;
6fbd4105
PP
158 }
159
160 if (plugin->info.version.extra) {
161 g_string_free(plugin->info.version.extra, TRUE);
db5504f9 162 plugin->info.version.extra = NULL;
6fbd4105
PP
163 }
164
165 g_free(plugin);
166}
167
168static inline
169struct bt_plugin *bt_plugin_create_empty(enum bt_plugin_type type)
170{
171 struct bt_plugin *plugin = NULL;
172
3fe0bf43
PP
173 BT_LOGD("Creating empty plugin object: type=%s",
174 bt_plugin_type_string(type));
175
6fbd4105
PP
176 plugin = g_new0(struct bt_plugin, 1);
177 if (!plugin) {
3fe0bf43 178 BT_LOGE_STR("Failed to allocate one plugin.");
6fbd4105
PP
179 goto error;
180 }
181
3fea54f6 182 bt_object_init_shared(&plugin->base, bt_plugin_destroy);
6fbd4105
PP
183 plugin->type = type;
184
d94d92ac
PP
185 /* Create empty arrays of component classes */
186 plugin->src_comp_classes =
187 g_ptr_array_new_with_free_func(
188 (GDestroyNotify) bt_object_put_ref);
189 if (!plugin->src_comp_classes) {
190 BT_LOGE_STR("Failed to allocate a GPtrArray.");
191 goto error;
192 }
193
194 plugin->flt_comp_classes =
195 g_ptr_array_new_with_free_func(
196 (GDestroyNotify) bt_object_put_ref);
197 if (!plugin->flt_comp_classes) {
198 BT_LOGE_STR("Failed to allocate a GPtrArray.");
199 goto error;
200 }
201
202 plugin->sink_comp_classes =
203 g_ptr_array_new_with_free_func(
204 (GDestroyNotify) bt_object_put_ref);
205 if (!plugin->sink_comp_classes) {
3fe0bf43 206 BT_LOGE_STR("Failed to allocate a GPtrArray.");
6fbd4105
PP
207 goto error;
208 }
209
210 /* Create empty info */
211 plugin->info.name = g_string_new(NULL);
212 if (!plugin->info.name) {
3fe0bf43 213 BT_LOGE_STR("Failed to allocate a GString.");
6fbd4105
PP
214 goto error;
215 }
216
217 plugin->info.path = g_string_new(NULL);
218 if (!plugin->info.path) {
3fe0bf43 219 BT_LOGE_STR("Failed to allocate a GString.");
6fbd4105
PP
220 goto error;
221 }
222
223 plugin->info.description = g_string_new(NULL);
224 if (!plugin->info.description) {
3fe0bf43 225 BT_LOGE_STR("Failed to allocate a GString.");
6fbd4105
PP
226 goto error;
227 }
228
229 plugin->info.author = g_string_new(NULL);
230 if (!plugin->info.author) {
3fe0bf43 231 BT_LOGE_STR("Failed to allocate a GString.");
6fbd4105
PP
232 goto error;
233 }
234
235 plugin->info.license = g_string_new(NULL);
236 if (!plugin->info.license) {
3fe0bf43 237 BT_LOGE_STR("Failed to allocate a GString.");
6fbd4105
PP
238 goto error;
239 }
240
241 plugin->info.version.extra = g_string_new(NULL);
242 if (!plugin->info.version.extra) {
3fe0bf43 243 BT_LOGE_STR("Failed to allocate a GString.");
6fbd4105
PP
244 goto error;
245 }
246
d94d92ac 247 BT_LIB_LOGD("Created empty plugin object: %!+l", plugin);
6fbd4105
PP
248 goto end;
249
250error:
65300d60 251 BT_OBJECT_PUT_REF_AND_RESET(plugin);
6fbd4105
PP
252
253end:
254 return plugin;
255}
55bb57e0
PP
256
257static inline
258void bt_plugin_set_path(struct bt_plugin *plugin, const char *path)
259{
f6ccaed9
PP
260 BT_ASSERT(plugin);
261 BT_ASSERT(path);
55bb57e0 262 g_string_assign(plugin->info.path, path);
c55a9f58 263 plugin->info.path_set = BT_TRUE;
3f7d4d90 264 BT_LIB_LOGD("Set plugin's path: %![plugin-]+l, path=\"%s\"",
d94d92ac 265 plugin, path);
55bb57e0
PP
266}
267
268static inline
269void bt_plugin_set_name(struct bt_plugin *plugin, const char *name)
270{
f6ccaed9
PP
271 BT_ASSERT(plugin);
272 BT_ASSERT(name);
55bb57e0 273 g_string_assign(plugin->info.name, name);
c55a9f58 274 plugin->info.name_set = BT_TRUE;
3f7d4d90 275 BT_LIB_LOGD("Set plugin's name: %![plugin-]+l, name=\"%s\"",
3fe0bf43 276 plugin, name);
55bb57e0
PP
277}
278
279static inline
280void bt_plugin_set_description(struct bt_plugin *plugin,
281 const char *description)
282{
f6ccaed9
PP
283 BT_ASSERT(plugin);
284 BT_ASSERT(description);
55bb57e0 285 g_string_assign(plugin->info.description, description);
c55a9f58 286 plugin->info.description_set = BT_TRUE;
3f7d4d90 287 BT_LIB_LOGD("Set plugin's description: %![plugin-]+l", plugin);
55bb57e0
PP
288}
289
290static inline
291void bt_plugin_set_author(struct bt_plugin *plugin, const char *author)
292{
f6ccaed9
PP
293 BT_ASSERT(plugin);
294 BT_ASSERT(author);
55bb57e0 295 g_string_assign(plugin->info.author, author);
c55a9f58 296 plugin->info.author_set = BT_TRUE;
3f7d4d90 297 BT_LIB_LOGD("Set plugin's author: %![plugin-]+l, author=\"%s\"",
d94d92ac 298 plugin, author);
55bb57e0
PP
299}
300
301static inline
302void bt_plugin_set_license(struct bt_plugin *plugin, const char *license)
303{
f6ccaed9
PP
304 BT_ASSERT(plugin);
305 BT_ASSERT(license);
55bb57e0 306 g_string_assign(plugin->info.license, license);
c55a9f58 307 plugin->info.license_set = BT_TRUE;
3f7d4d90 308 BT_LIB_LOGD("Set plugin's path: %![plugin-]+l, license=\"%s\"",
d94d92ac 309 plugin, license);
55bb57e0
PP
310}
311
312static inline
313void bt_plugin_set_version(struct bt_plugin *plugin, unsigned int major,
314 unsigned int minor, unsigned int patch, const char *extra)
315{
f6ccaed9 316 BT_ASSERT(plugin);
55bb57e0
PP
317 plugin->info.version.major = major;
318 plugin->info.version.minor = minor;
319 plugin->info.version.patch = patch;
320
321 if (extra) {
322 g_string_assign(plugin->info.version.extra, extra);
323 }
324
c55a9f58 325 plugin->info.version_set = BT_TRUE;
3f7d4d90 326 BT_LIB_LOGD("Set plugin's version: %![plugin-]+l, "
3fe0bf43 327 "major=%u, minor=%u, patch=%u, extra=\"%s\"",
d94d92ac 328 plugin, major, minor, patch, extra);
55bb57e0
PP
329}
330
331static inline
d24d5663 332int bt_plugin_add_component_class(
d94d92ac 333 struct bt_plugin *plugin, struct bt_component_class *comp_class)
55bb57e0 334{
d94d92ac
PP
335 GPtrArray *comp_classes;
336
f6ccaed9 337 BT_ASSERT(plugin);
d94d92ac
PP
338 BT_ASSERT(comp_class);
339
340 switch (comp_class->type) {
341 case BT_COMPONENT_CLASS_TYPE_SOURCE:
342 comp_classes = plugin->src_comp_classes;
343 break;
344 case BT_COMPONENT_CLASS_TYPE_FILTER:
345 comp_classes = plugin->flt_comp_classes;
346 break;
347 case BT_COMPONENT_CLASS_TYPE_SINK:
348 comp_classes = plugin->sink_comp_classes;
349 break;
350 default:
351 abort();
352 }
3fe0bf43 353
d94d92ac 354 /* Add new component class */
398454ed
PP
355 bt_object_get_ref(comp_class);
356 g_ptr_array_add(comp_classes, comp_class);
d94d92ac
PP
357
358 /* Special case for a shared object plugin */
359 if (plugin->type == BT_PLUGIN_TYPE_SO) {
360 bt_plugin_so_on_add_component_class(plugin, comp_class);
3fe0bf43
PP
361 }
362
d94d92ac
PP
363 BT_LIB_LOGD("Added component class to plugin: "
364 "%![plugin-]+l, %![cc-]+C", plugin, comp_class);
d24d5663 365 return BT_FUNC_STATUS_OK;
55bb57e0
PP
366}
367
a8ff38ef
PP
368static
369void bt_plugin_set_destroy(struct bt_object *obj)
370{
371 struct bt_plugin_set *plugin_set =
372 container_of(obj, struct bt_plugin_set, base);
373
374 if (!plugin_set) {
375 return;
376 }
377
3fe0bf43
PP
378 BT_LOGD("Destroying plugin set: addr=%p", plugin_set);
379
a8ff38ef 380 if (plugin_set->plugins) {
3fe0bf43 381 BT_LOGD_STR("Putting plugins.");
a8ff38ef
PP
382 g_ptr_array_free(plugin_set->plugins, TRUE);
383 }
384
385 g_free(plugin_set);
386}
387
388static inline
389struct bt_plugin_set *bt_plugin_set_create(void)
390{
391 struct bt_plugin_set *plugin_set = g_new0(struct bt_plugin_set, 1);
392
393 if (!plugin_set) {
394 goto end;
395 }
396
3fe0bf43 397 BT_LOGD_STR("Creating empty plugin set.");
3fea54f6 398 bt_object_init_shared(&plugin_set->base, bt_plugin_set_destroy);
a8ff38ef
PP
399
400 plugin_set->plugins = g_ptr_array_new_with_free_func(
65300d60 401 (GDestroyNotify) bt_object_put_ref);
a8ff38ef 402 if (!plugin_set->plugins) {
3fe0bf43 403 BT_LOGE_STR("Failed to allocate a GPtrArray.");
65300d60 404 BT_OBJECT_PUT_REF_AND_RESET(plugin_set);
a8ff38ef
PP
405 goto end;
406 }
407
3fe0bf43
PP
408 BT_LOGD("Created empty plugin set: addr=%p", plugin_set);
409
a8ff38ef
PP
410end:
411 return plugin_set;
412}
413
414static inline
415void bt_plugin_set_add_plugin(struct bt_plugin_set *plugin_set,
416 struct bt_plugin *plugin)
417{
f6ccaed9
PP
418 BT_ASSERT(plugin_set);
419 BT_ASSERT(plugin);
398454ed
PP
420 bt_object_get_ref(plugin);
421 g_ptr_array_add(plugin_set->plugins, plugin);
3f7d4d90 422 BT_LIB_LOGD("Added plugin to plugin set: "
d94d92ac
PP
423 "plugin-set-addr=%p, %![plugin-]+l",
424 plugin_set, plugin);
8c1a3187
PP
425}
426
33b34c43 427#endif /* BABELTRACE_PLUGIN_PLUGIN_INTERNAL_H */
This page took 0.075451 seconds and 4 git commands to generate.