lib: update copyrights
[babeltrace.git] / include / babeltrace / plugin / plugin-internal.h
1 #ifndef BABELTRACE_PLUGIN_PLUGIN_INTERNAL_H
2 #define BABELTRACE_PLUGIN_PLUGIN_INTERNAL_H
3
4 /*
5 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
6 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 *
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>
28 #include <babeltrace/graph/component-class-internal.h>
29 #include <babeltrace/plugin/plugin-const.h>
30 #include <babeltrace/plugin/plugin-dev.h>
31 #include <babeltrace/plugin/plugin-so-internal.h>
32 #include <babeltrace/object-internal.h>
33 #include <babeltrace/object.h>
34 #include <babeltrace/types.h>
35 #include <babeltrace/assert-internal.h>
36 #include <glib.h>
37
38 enum bt_plugin_type {
39 BT_PLUGIN_TYPE_SO = 0,
40 BT_PLUGIN_TYPE_PYTHON = 1,
41 };
42
43 struct bt_plugin {
44 struct bt_object base;
45 enum bt_plugin_type type;
46
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;
51
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;
65 bool path_set;
66 bool name_set;
67 bool author_set;
68 bool license_set;
69 bool description_set;
70 bool version_set;
71 } info;
72
73 /* Value depends on the specific plugin type */
74 void *spec_data;
75 void (*destroy_spec_data)(struct bt_plugin *);
76 };
77
78 struct bt_plugin_set {
79 struct bt_object base;
80
81 /* Array of struct bt_plugin * */
82 GPtrArray *plugins;
83 };
84
85 static inline
86 const 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
100 static inline
101 const 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
113 static inline
114 void bt_plugin_destroy(struct bt_object *obj)
115 {
116 struct bt_plugin *plugin;
117
118 BT_ASSERT(obj);
119 plugin = container_of(obj, struct bt_plugin, base);
120 BT_LIB_LOGD("Destroying plugin object: %!+l", plugin);
121
122 if (plugin->destroy_spec_data) {
123 plugin->destroy_spec_data(plugin);
124 }
125
126 if (plugin->src_comp_classes) {
127 BT_LOGD_STR("Putting source component classes.");
128 g_ptr_array_free(plugin->src_comp_classes, TRUE);
129 plugin->src_comp_classes = NULL;
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);
135 plugin->flt_comp_classes = NULL;
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);
141 plugin->sink_comp_classes = NULL;
142 }
143
144 if (plugin->info.name) {
145 g_string_free(plugin->info.name, TRUE);
146 plugin->info.name = NULL;
147 }
148
149 if (plugin->info.path) {
150 g_string_free(plugin->info.path, TRUE);
151 plugin->info.path = NULL;
152 }
153
154 if (plugin->info.description) {
155 g_string_free(plugin->info.description, TRUE);
156 plugin->info.description = NULL;
157 }
158
159 if (plugin->info.author) {
160 g_string_free(plugin->info.author, TRUE);
161 plugin->info.author = NULL;
162 }
163
164 if (plugin->info.license) {
165 g_string_free(plugin->info.license, TRUE);
166 plugin->info.license = NULL;
167 }
168
169 if (plugin->info.version.extra) {
170 g_string_free(plugin->info.version.extra, TRUE);
171 plugin->info.version.extra = NULL;
172 }
173
174 g_free(plugin);
175 }
176
177 static inline
178 struct bt_plugin *bt_plugin_create_empty(enum bt_plugin_type type)
179 {
180 struct bt_plugin *plugin = NULL;
181
182 BT_LOGD("Creating empty plugin object: type=%s",
183 bt_plugin_type_string(type));
184
185 plugin = g_new0(struct bt_plugin, 1);
186 if (!plugin) {
187 BT_LOGE_STR("Failed to allocate one plugin.");
188 goto error;
189 }
190
191 bt_object_init_shared(&plugin->base, bt_plugin_destroy);
192 plugin->type = type;
193
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) {
215 BT_LOGE_STR("Failed to allocate a GPtrArray.");
216 goto error;
217 }
218
219 /* Create empty info */
220 plugin->info.name = g_string_new(NULL);
221 if (!plugin->info.name) {
222 BT_LOGE_STR("Failed to allocate a GString.");
223 goto error;
224 }
225
226 plugin->info.path = g_string_new(NULL);
227 if (!plugin->info.path) {
228 BT_LOGE_STR("Failed to allocate a GString.");
229 goto error;
230 }
231
232 plugin->info.description = g_string_new(NULL);
233 if (!plugin->info.description) {
234 BT_LOGE_STR("Failed to allocate a GString.");
235 goto error;
236 }
237
238 plugin->info.author = g_string_new(NULL);
239 if (!plugin->info.author) {
240 BT_LOGE_STR("Failed to allocate a GString.");
241 goto error;
242 }
243
244 plugin->info.license = g_string_new(NULL);
245 if (!plugin->info.license) {
246 BT_LOGE_STR("Failed to allocate a GString.");
247 goto error;
248 }
249
250 plugin->info.version.extra = g_string_new(NULL);
251 if (!plugin->info.version.extra) {
252 BT_LOGE_STR("Failed to allocate a GString.");
253 goto error;
254 }
255
256 BT_LIB_LOGD("Created empty plugin object: %!+l", plugin);
257 goto end;
258
259 error:
260 BT_OBJECT_PUT_REF_AND_RESET(plugin);
261
262 end:
263 return plugin;
264 }
265
266 static inline
267 void bt_plugin_set_path(struct bt_plugin *plugin, const char *path)
268 {
269 BT_ASSERT(plugin);
270 BT_ASSERT(path);
271 g_string_assign(plugin->info.path, path);
272 plugin->info.path_set = BT_TRUE;
273 BT_LIB_LOGV("Set plugin's path: %![plugin-]+l, path=\"%s\"",
274 plugin, path);
275 }
276
277 static inline
278 void bt_plugin_set_name(struct bt_plugin *plugin, const char *name)
279 {
280 BT_ASSERT(plugin);
281 BT_ASSERT(name);
282 g_string_assign(plugin->info.name, name);
283 plugin->info.name_set = BT_TRUE;
284 BT_LIB_LOGV("Set plugin's name: %![plugin-]+l, name=\"%s\"",
285 plugin, name);
286 }
287
288 static inline
289 void bt_plugin_set_description(struct bt_plugin *plugin,
290 const char *description)
291 {
292 BT_ASSERT(plugin);
293 BT_ASSERT(description);
294 g_string_assign(plugin->info.description, description);
295 plugin->info.description_set = BT_TRUE;
296 BT_LIB_LOGV("Set plugin's description: %![plugin-]+l", plugin);
297 }
298
299 static inline
300 void bt_plugin_set_author(struct bt_plugin *plugin, const char *author)
301 {
302 BT_ASSERT(plugin);
303 BT_ASSERT(author);
304 g_string_assign(plugin->info.author, author);
305 plugin->info.author_set = BT_TRUE;
306 BT_LIB_LOGV("Set plugin's author: %![plugin-]+l, author=\"%s\"",
307 plugin, author);
308 }
309
310 static inline
311 void bt_plugin_set_license(struct bt_plugin *plugin, const char *license)
312 {
313 BT_ASSERT(plugin);
314 BT_ASSERT(license);
315 g_string_assign(plugin->info.license, license);
316 plugin->info.license_set = BT_TRUE;
317 BT_LIB_LOGV("Set plugin's path: %![plugin-]+l, license=\"%s\"",
318 plugin, license);
319 }
320
321 static inline
322 void bt_plugin_set_version(struct bt_plugin *plugin, unsigned int major,
323 unsigned int minor, unsigned int patch, const char *extra)
324 {
325 BT_ASSERT(plugin);
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
334 plugin->info.version_set = BT_TRUE;
335 BT_LIB_LOGV("Set plugin's version: %![plugin-]+l, "
336 "major=%u, minor=%u, patch=%u, extra=\"%s\"",
337 plugin, major, minor, patch, extra);
338 }
339
340 static inline
341 enum bt_plugin_status bt_plugin_add_component_class(
342 struct bt_plugin *plugin, struct bt_component_class *comp_class)
343 {
344 GPtrArray *comp_classes;
345
346 BT_ASSERT(plugin);
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 }
362
363 /* Add new component class */
364 bt_object_get_ref(comp_class);
365 g_ptr_array_add(comp_classes, comp_class);
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);
370 }
371
372 BT_LIB_LOGD("Added component class to plugin: "
373 "%![plugin-]+l, %![cc-]+C", plugin, comp_class);
374 return BT_PLUGIN_STATUS_OK;
375 }
376
377 static
378 void 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
387 BT_LOGD("Destroying plugin set: addr=%p", plugin_set);
388
389 if (plugin_set->plugins) {
390 BT_LOGD_STR("Putting plugins.");
391 g_ptr_array_free(plugin_set->plugins, TRUE);
392 }
393
394 g_free(plugin_set);
395 }
396
397 static inline
398 struct 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
406 BT_LOGD_STR("Creating empty plugin set.");
407 bt_object_init_shared(&plugin_set->base, bt_plugin_set_destroy);
408
409 plugin_set->plugins = g_ptr_array_new_with_free_func(
410 (GDestroyNotify) bt_object_put_ref);
411 if (!plugin_set->plugins) {
412 BT_LOGE_STR("Failed to allocate a GPtrArray.");
413 BT_OBJECT_PUT_REF_AND_RESET(plugin_set);
414 goto end;
415 }
416
417 BT_LOGD("Created empty plugin set: addr=%p", plugin_set);
418
419 end:
420 return plugin_set;
421 }
422
423 static inline
424 void bt_plugin_set_add_plugin(struct bt_plugin_set *plugin_set,
425 struct bt_plugin *plugin)
426 {
427 BT_ASSERT(plugin_set);
428 BT_ASSERT(plugin);
429 bt_object_get_ref(plugin);
430 g_ptr_array_add(plugin_set->plugins, plugin);
431 BT_LIB_LOGV("Added plugin to plugin set: "
432 "plugin-set-addr=%p, %![plugin-]+l",
433 plugin_set, plugin);
434 }
435
436 #endif /* BABELTRACE_PLUGIN_PLUGIN_INTERNAL_H */
This page took 0.037754 seconds and 4 git commands to generate.