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