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