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