Cleanup: remove private babeltrace.h
[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
MJ
33#include "common/assert.h"
34#include "lib/lib-logging.h"
55bb57e0 35#include <glib.h>
fb2dcc52 36
578e048b
MJ
37#include "plugin-so.h"
38
55bb57e0
PP
39enum bt_plugin_type {
40 BT_PLUGIN_TYPE_SO = 0,
41 BT_PLUGIN_TYPE_PYTHON = 1,
fb2dcc52
JG
42};
43
9724cce9
PP
44enum bt_plugin_status {
45 BT_PLUGIN_STATUS_OK = 0,
46 BT_PLUGIN_STATUS_ERROR = -1,
47 BT_PLUGIN_STATUS_NOMEM = -12,
48};
49
33b34c43
PP
50struct bt_plugin {
51 struct bt_object base;
55bb57e0 52 enum bt_plugin_type type;
cba174d5 53
d94d92ac
PP
54 /* Arrays of `struct bt_component_class *` (owned by this) */
55 GPtrArray *src_comp_classes;
56 GPtrArray *flt_comp_classes;
57 GPtrArray *sink_comp_classes;
6ba0b073 58
55bb57e0
PP
59 /* Info (owned by this) */
60 struct {
61 GString *path;
62 GString *name;
63 GString *author;
64 GString *license;
65 GString *description;
66 struct {
67 unsigned int major;
68 unsigned int minor;
69 unsigned int patch;
70 GString *extra;
71 } version;
d94d92ac
PP
72 bool path_set;
73 bool name_set;
74 bool author_set;
75 bool license_set;
76 bool description_set;
77 bool version_set;
55bb57e0
PP
78 } info;
79
80 /* Value depends on the specific plugin type */
81 void *spec_data;
6fbd4105 82 void (*destroy_spec_data)(struct bt_plugin *);
33b34c43 83};
fb2dcc52 84
a8ff38ef
PP
85struct bt_plugin_set {
86 struct bt_object base;
87
88 /* Array of struct bt_plugin * */
89 GPtrArray *plugins;
90};
91
3fe0bf43
PP
92static inline
93const char *bt_plugin_status_string(enum bt_plugin_status status)
94{
95 switch (status) {
96 case BT_PLUGIN_STATUS_OK:
97 return "BT_PLUGIN_STATUS_OK";
98 case BT_PLUGIN_STATUS_ERROR:
99 return "BT_PLUGIN_STATUS_ERROR";
100 case BT_PLUGIN_STATUS_NOMEM:
101 return "BT_PLUGIN_STATUS_NOMEM";
102 default:
103 return "(unknown)";
104 }
105}
106
107static inline
108const char *bt_plugin_type_string(enum bt_plugin_type type)
109{
110 switch (type) {
111 case BT_PLUGIN_TYPE_SO:
112 return "BT_PLUGIN_TYPE_SO";
113 case BT_PLUGIN_TYPE_PYTHON:
114 return "BT_PLUGIN_TYPE_PYTHON";
115 default:
116 return "(unknown)";
117 }
118}
119
6fbd4105
PP
120static inline
121void bt_plugin_destroy(struct bt_object *obj)
122{
123 struct bt_plugin *plugin;
124
f6ccaed9 125 BT_ASSERT(obj);
6fbd4105 126 plugin = container_of(obj, struct bt_plugin, base);
d94d92ac 127 BT_LIB_LOGD("Destroying plugin object: %!+l", plugin);
6fbd4105
PP
128
129 if (plugin->destroy_spec_data) {
130 plugin->destroy_spec_data(plugin);
131 }
132
d94d92ac
PP
133 if (plugin->src_comp_classes) {
134 BT_LOGD_STR("Putting source component classes.");
135 g_ptr_array_free(plugin->src_comp_classes, TRUE);
db5504f9 136 plugin->src_comp_classes = NULL;
d94d92ac
PP
137 }
138
139 if (plugin->flt_comp_classes) {
140 BT_LOGD_STR("Putting filter component classes.");
141 g_ptr_array_free(plugin->flt_comp_classes, TRUE);
db5504f9 142 plugin->flt_comp_classes = NULL;
d94d92ac
PP
143 }
144
145 if (plugin->sink_comp_classes) {
146 BT_LOGD_STR("Putting sink component classes.");
147 g_ptr_array_free(plugin->sink_comp_classes, TRUE);
db5504f9 148 plugin->sink_comp_classes = NULL;
6fbd4105
PP
149 }
150
151 if (plugin->info.name) {
152 g_string_free(plugin->info.name, TRUE);
db5504f9 153 plugin->info.name = NULL;
6fbd4105
PP
154 }
155
156 if (plugin->info.path) {
157 g_string_free(plugin->info.path, TRUE);
db5504f9 158 plugin->info.path = NULL;
6fbd4105
PP
159 }
160
161 if (plugin->info.description) {
162 g_string_free(plugin->info.description, TRUE);
db5504f9 163 plugin->info.description = NULL;
6fbd4105
PP
164 }
165
166 if (plugin->info.author) {
167 g_string_free(plugin->info.author, TRUE);
db5504f9 168 plugin->info.author = NULL;
6fbd4105
PP
169 }
170
171 if (plugin->info.license) {
172 g_string_free(plugin->info.license, TRUE);
db5504f9 173 plugin->info.license = NULL;
6fbd4105
PP
174 }
175
176 if (plugin->info.version.extra) {
177 g_string_free(plugin->info.version.extra, TRUE);
db5504f9 178 plugin->info.version.extra = NULL;
6fbd4105
PP
179 }
180
181 g_free(plugin);
182}
183
184static inline
185struct bt_plugin *bt_plugin_create_empty(enum bt_plugin_type type)
186{
187 struct bt_plugin *plugin = NULL;
188
3fe0bf43
PP
189 BT_LOGD("Creating empty plugin object: type=%s",
190 bt_plugin_type_string(type));
191
6fbd4105
PP
192 plugin = g_new0(struct bt_plugin, 1);
193 if (!plugin) {
3fe0bf43 194 BT_LOGE_STR("Failed to allocate one plugin.");
6fbd4105
PP
195 goto error;
196 }
197
3fea54f6 198 bt_object_init_shared(&plugin->base, bt_plugin_destroy);
6fbd4105
PP
199 plugin->type = type;
200
d94d92ac
PP
201 /* Create empty arrays of component classes */
202 plugin->src_comp_classes =
203 g_ptr_array_new_with_free_func(
204 (GDestroyNotify) bt_object_put_ref);
205 if (!plugin->src_comp_classes) {
206 BT_LOGE_STR("Failed to allocate a GPtrArray.");
207 goto error;
208 }
209
210 plugin->flt_comp_classes =
211 g_ptr_array_new_with_free_func(
212 (GDestroyNotify) bt_object_put_ref);
213 if (!plugin->flt_comp_classes) {
214 BT_LOGE_STR("Failed to allocate a GPtrArray.");
215 goto error;
216 }
217
218 plugin->sink_comp_classes =
219 g_ptr_array_new_with_free_func(
220 (GDestroyNotify) bt_object_put_ref);
221 if (!plugin->sink_comp_classes) {
3fe0bf43 222 BT_LOGE_STR("Failed to allocate a GPtrArray.");
6fbd4105
PP
223 goto error;
224 }
225
226 /* Create empty info */
227 plugin->info.name = g_string_new(NULL);
228 if (!plugin->info.name) {
3fe0bf43 229 BT_LOGE_STR("Failed to allocate a GString.");
6fbd4105
PP
230 goto error;
231 }
232
233 plugin->info.path = g_string_new(NULL);
234 if (!plugin->info.path) {
3fe0bf43 235 BT_LOGE_STR("Failed to allocate a GString.");
6fbd4105
PP
236 goto error;
237 }
238
239 plugin->info.description = g_string_new(NULL);
240 if (!plugin->info.description) {
3fe0bf43 241 BT_LOGE_STR("Failed to allocate a GString.");
6fbd4105
PP
242 goto error;
243 }
244
245 plugin->info.author = g_string_new(NULL);
246 if (!plugin->info.author) {
3fe0bf43 247 BT_LOGE_STR("Failed to allocate a GString.");
6fbd4105
PP
248 goto error;
249 }
250
251 plugin->info.license = g_string_new(NULL);
252 if (!plugin->info.license) {
3fe0bf43 253 BT_LOGE_STR("Failed to allocate a GString.");
6fbd4105
PP
254 goto error;
255 }
256
257 plugin->info.version.extra = g_string_new(NULL);
258 if (!plugin->info.version.extra) {
3fe0bf43 259 BT_LOGE_STR("Failed to allocate a GString.");
6fbd4105
PP
260 goto error;
261 }
262
d94d92ac 263 BT_LIB_LOGD("Created empty plugin object: %!+l", plugin);
6fbd4105
PP
264 goto end;
265
266error:
65300d60 267 BT_OBJECT_PUT_REF_AND_RESET(plugin);
6fbd4105
PP
268
269end:
270 return plugin;
271}
55bb57e0
PP
272
273static inline
274void bt_plugin_set_path(struct bt_plugin *plugin, const char *path)
275{
f6ccaed9
PP
276 BT_ASSERT(plugin);
277 BT_ASSERT(path);
55bb57e0 278 g_string_assign(plugin->info.path, path);
c55a9f58 279 plugin->info.path_set = BT_TRUE;
d94d92ac
PP
280 BT_LIB_LOGV("Set plugin's path: %![plugin-]+l, path=\"%s\"",
281 plugin, path);
55bb57e0
PP
282}
283
284static inline
285void bt_plugin_set_name(struct bt_plugin *plugin, const char *name)
286{
f6ccaed9
PP
287 BT_ASSERT(plugin);
288 BT_ASSERT(name);
55bb57e0 289 g_string_assign(plugin->info.name, name);
c55a9f58 290 plugin->info.name_set = BT_TRUE;
d94d92ac 291 BT_LIB_LOGV("Set plugin's name: %![plugin-]+l, name=\"%s\"",
3fe0bf43 292 plugin, name);
55bb57e0
PP
293}
294
295static inline
296void bt_plugin_set_description(struct bt_plugin *plugin,
297 const char *description)
298{
f6ccaed9
PP
299 BT_ASSERT(plugin);
300 BT_ASSERT(description);
55bb57e0 301 g_string_assign(plugin->info.description, description);
c55a9f58 302 plugin->info.description_set = BT_TRUE;
d94d92ac 303 BT_LIB_LOGV("Set plugin's description: %![plugin-]+l", plugin);
55bb57e0
PP
304}
305
306static inline
307void bt_plugin_set_author(struct bt_plugin *plugin, const char *author)
308{
f6ccaed9
PP
309 BT_ASSERT(plugin);
310 BT_ASSERT(author);
55bb57e0 311 g_string_assign(plugin->info.author, author);
c55a9f58 312 plugin->info.author_set = BT_TRUE;
d94d92ac
PP
313 BT_LIB_LOGV("Set plugin's author: %![plugin-]+l, author=\"%s\"",
314 plugin, author);
55bb57e0
PP
315}
316
317static inline
318void bt_plugin_set_license(struct bt_plugin *plugin, const char *license)
319{
f6ccaed9
PP
320 BT_ASSERT(plugin);
321 BT_ASSERT(license);
55bb57e0 322 g_string_assign(plugin->info.license, license);
c55a9f58 323 plugin->info.license_set = BT_TRUE;
d94d92ac
PP
324 BT_LIB_LOGV("Set plugin's path: %![plugin-]+l, license=\"%s\"",
325 plugin, license);
55bb57e0
PP
326}
327
328static inline
329void bt_plugin_set_version(struct bt_plugin *plugin, unsigned int major,
330 unsigned int minor, unsigned int patch, const char *extra)
331{
f6ccaed9 332 BT_ASSERT(plugin);
55bb57e0
PP
333 plugin->info.version.major = major;
334 plugin->info.version.minor = minor;
335 plugin->info.version.patch = patch;
336
337 if (extra) {
338 g_string_assign(plugin->info.version.extra, extra);
339 }
340
c55a9f58 341 plugin->info.version_set = BT_TRUE;
d94d92ac 342 BT_LIB_LOGV("Set plugin's version: %![plugin-]+l, "
3fe0bf43 343 "major=%u, minor=%u, patch=%u, extra=\"%s\"",
d94d92ac 344 plugin, major, minor, patch, extra);
55bb57e0
PP
345}
346
347static inline
d94d92ac
PP
348enum bt_plugin_status bt_plugin_add_component_class(
349 struct bt_plugin *plugin, struct bt_component_class *comp_class)
55bb57e0 350{
d94d92ac
PP
351 GPtrArray *comp_classes;
352
f6ccaed9 353 BT_ASSERT(plugin);
d94d92ac
PP
354 BT_ASSERT(comp_class);
355
356 switch (comp_class->type) {
357 case BT_COMPONENT_CLASS_TYPE_SOURCE:
358 comp_classes = plugin->src_comp_classes;
359 break;
360 case BT_COMPONENT_CLASS_TYPE_FILTER:
361 comp_classes = plugin->flt_comp_classes;
362 break;
363 case BT_COMPONENT_CLASS_TYPE_SINK:
364 comp_classes = plugin->sink_comp_classes;
365 break;
366 default:
367 abort();
368 }
3fe0bf43 369
d94d92ac 370 /* Add new component class */
398454ed
PP
371 bt_object_get_ref(comp_class);
372 g_ptr_array_add(comp_classes, comp_class);
d94d92ac
PP
373
374 /* Special case for a shared object plugin */
375 if (plugin->type == BT_PLUGIN_TYPE_SO) {
376 bt_plugin_so_on_add_component_class(plugin, comp_class);
3fe0bf43
PP
377 }
378
d94d92ac
PP
379 BT_LIB_LOGD("Added component class to plugin: "
380 "%![plugin-]+l, %![cc-]+C", plugin, comp_class);
381 return BT_PLUGIN_STATUS_OK;
55bb57e0
PP
382}
383
a8ff38ef
PP
384static
385void bt_plugin_set_destroy(struct bt_object *obj)
386{
387 struct bt_plugin_set *plugin_set =
388 container_of(obj, struct bt_plugin_set, base);
389
390 if (!plugin_set) {
391 return;
392 }
393
3fe0bf43
PP
394 BT_LOGD("Destroying plugin set: addr=%p", plugin_set);
395
a8ff38ef 396 if (plugin_set->plugins) {
3fe0bf43 397 BT_LOGD_STR("Putting plugins.");
a8ff38ef
PP
398 g_ptr_array_free(plugin_set->plugins, TRUE);
399 }
400
401 g_free(plugin_set);
402}
403
404static inline
405struct bt_plugin_set *bt_plugin_set_create(void)
406{
407 struct bt_plugin_set *plugin_set = g_new0(struct bt_plugin_set, 1);
408
409 if (!plugin_set) {
410 goto end;
411 }
412
3fe0bf43 413 BT_LOGD_STR("Creating empty plugin set.");
3fea54f6 414 bt_object_init_shared(&plugin_set->base, bt_plugin_set_destroy);
a8ff38ef
PP
415
416 plugin_set->plugins = g_ptr_array_new_with_free_func(
65300d60 417 (GDestroyNotify) bt_object_put_ref);
a8ff38ef 418 if (!plugin_set->plugins) {
3fe0bf43 419 BT_LOGE_STR("Failed to allocate a GPtrArray.");
65300d60 420 BT_OBJECT_PUT_REF_AND_RESET(plugin_set);
a8ff38ef
PP
421 goto end;
422 }
423
3fe0bf43
PP
424 BT_LOGD("Created empty plugin set: addr=%p", plugin_set);
425
a8ff38ef
PP
426end:
427 return plugin_set;
428}
429
430static inline
431void bt_plugin_set_add_plugin(struct bt_plugin_set *plugin_set,
432 struct bt_plugin *plugin)
433{
f6ccaed9
PP
434 BT_ASSERT(plugin_set);
435 BT_ASSERT(plugin);
398454ed
PP
436 bt_object_get_ref(plugin);
437 g_ptr_array_add(plugin_set->plugins, plugin);
d94d92ac
PP
438 BT_LIB_LOGV("Added plugin to plugin set: "
439 "plugin-set-addr=%p, %![plugin-]+l",
440 plugin_set, plugin);
8c1a3187
PP
441}
442
33b34c43 443#endif /* BABELTRACE_PLUGIN_PLUGIN_INTERNAL_H */
This page took 0.073558 seconds and 4 git commands to generate.