Rename: bt_put(), bt_get() -> bt_object_put_ref(), bt_object_get_ref()
[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/*
5 * BabelTrace - Plug-in Internal
6 *
7 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 *
9 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * SOFTWARE.
28 */
29
30#include <babeltrace/babeltrace-internal.h>
3fe0bf43 31#include <babeltrace/plugin/plugin.h>
33b34c43 32#include <babeltrace/plugin/plugin-dev.h>
b8a06801 33#include <babeltrace/object-internal.h>
c55a9f58 34#include <babeltrace/types.h>
8b45963b 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;
c55a9f58 46 bt_bool frozen;
cba174d5 47
33b34c43
PP
48 /* Array of pointers to bt_component_class (owned by this) */
49 GPtrArray *comp_classes;
6ba0b073 50
55bb57e0
PP
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;
c55a9f58
PP
64 bt_bool path_set;
65 bt_bool name_set;
66 bt_bool author_set;
67 bt_bool license_set;
68 bt_bool description_set;
69 bt_bool version_set;
55bb57e0
PP
70 } info;
71
72 /* Value depends on the specific plugin type */
73 void *spec_data;
6fbd4105 74 void (*destroy_spec_data)(struct bt_plugin *);
33b34c43 75};
fb2dcc52 76
a8ff38ef
PP
77struct bt_plugin_set {
78 struct bt_object base;
79
80 /* Array of struct bt_plugin * */
81 GPtrArray *plugins;
82};
83
3fe0bf43
PP
84static inline
85const 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
99static inline
100const 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
6fbd4105
PP
112static inline
113void bt_plugin_destroy(struct bt_object *obj)
114{
115 struct bt_plugin *plugin;
116
8b45963b 117 BT_ASSERT(obj);
6fbd4105 118 plugin = container_of(obj, struct bt_plugin, base);
3fe0bf43
PP
119 BT_LOGD("Destroying plugin object: addr=%p, name=\"%s\"",
120 plugin, plugin->info.name ? plugin->info.name->str : NULL);
6fbd4105
PP
121
122 if (plugin->destroy_spec_data) {
123 plugin->destroy_spec_data(plugin);
124 }
125
126 if (plugin->comp_classes) {
3fe0bf43 127 BT_LOGD_STR("Putting component classes.");
6fbd4105
PP
128 g_ptr_array_free(plugin->comp_classes, TRUE);
129 }
130
131 if (plugin->info.name) {
132 g_string_free(plugin->info.name, TRUE);
133 }
134
135 if (plugin->info.path) {
136 g_string_free(plugin->info.path, TRUE);
137 }
138
139 if (plugin->info.description) {
140 g_string_free(plugin->info.description, TRUE);
141 }
142
143 if (plugin->info.author) {
144 g_string_free(plugin->info.author, TRUE);
145 }
146
147 if (plugin->info.license) {
148 g_string_free(plugin->info.license, TRUE);
149 }
150
151 if (plugin->info.version.extra) {
152 g_string_free(plugin->info.version.extra, TRUE);
153 }
154
155 g_free(plugin);
156}
157
158static inline
159struct bt_plugin *bt_plugin_create_empty(enum bt_plugin_type type)
160{
161 struct bt_plugin *plugin = NULL;
162
3fe0bf43
PP
163 BT_LOGD("Creating empty plugin object: type=%s",
164 bt_plugin_type_string(type));
165
6fbd4105
PP
166 plugin = g_new0(struct bt_plugin, 1);
167 if (!plugin) {
3fe0bf43 168 BT_LOGE_STR("Failed to allocate one plugin.");
6fbd4105
PP
169 goto error;
170 }
171
1d7bf349 172 bt_object_init_shared(&plugin->base, bt_plugin_destroy);
6fbd4105
PP
173 plugin->type = type;
174
175 /* Create empty array of component classes */
176 plugin->comp_classes =
8138bfe1 177 g_ptr_array_new_with_free_func((GDestroyNotify) bt_object_put_ref);
6fbd4105 178 if (!plugin->comp_classes) {
3fe0bf43 179 BT_LOGE_STR("Failed to allocate a GPtrArray.");
6fbd4105
PP
180 goto error;
181 }
182
183 /* Create empty info */
184 plugin->info.name = g_string_new(NULL);
185 if (!plugin->info.name) {
3fe0bf43 186 BT_LOGE_STR("Failed to allocate a GString.");
6fbd4105
PP
187 goto error;
188 }
189
190 plugin->info.path = g_string_new(NULL);
191 if (!plugin->info.path) {
3fe0bf43 192 BT_LOGE_STR("Failed to allocate a GString.");
6fbd4105
PP
193 goto error;
194 }
195
196 plugin->info.description = g_string_new(NULL);
197 if (!plugin->info.description) {
3fe0bf43 198 BT_LOGE_STR("Failed to allocate a GString.");
6fbd4105
PP
199 goto error;
200 }
201
202 plugin->info.author = g_string_new(NULL);
203 if (!plugin->info.author) {
3fe0bf43 204 BT_LOGE_STR("Failed to allocate a GString.");
6fbd4105
PP
205 goto error;
206 }
207
208 plugin->info.license = g_string_new(NULL);
209 if (!plugin->info.license) {
3fe0bf43 210 BT_LOGE_STR("Failed to allocate a GString.");
6fbd4105
PP
211 goto error;
212 }
213
214 plugin->info.version.extra = g_string_new(NULL);
215 if (!plugin->info.version.extra) {
3fe0bf43 216 BT_LOGE_STR("Failed to allocate a GString.");
6fbd4105
PP
217 goto error;
218 }
219
3fe0bf43
PP
220 BT_LOGD("Created empty plugin object: type=%s, addr=%p",
221 bt_plugin_type_string(type), plugin);
6fbd4105
PP
222 goto end;
223
224error:
8138bfe1 225 BT_OBJECT_PUT_REF_AND_RESET(plugin);
6fbd4105
PP
226
227end:
228 return plugin;
229}
55bb57e0
PP
230
231static inline
232void bt_plugin_set_path(struct bt_plugin *plugin, const char *path)
233{
8b45963b
PP
234 BT_ASSERT(plugin);
235 BT_ASSERT(path);
55bb57e0 236 g_string_assign(plugin->info.path, path);
c55a9f58 237 plugin->info.path_set = BT_TRUE;
3fe0bf43
PP
238 BT_LOGV("Set plugin's path: addr=%p, name=\"%s\", path=\"%s\"",
239 plugin, bt_plugin_get_name(plugin), path);
55bb57e0
PP
240}
241
242static inline
243void bt_plugin_set_name(struct bt_plugin *plugin, const char *name)
244{
8b45963b
PP
245 BT_ASSERT(plugin);
246 BT_ASSERT(name);
55bb57e0 247 g_string_assign(plugin->info.name, name);
c55a9f58 248 plugin->info.name_set = BT_TRUE;
3fe0bf43
PP
249 BT_LOGV("Set plugin's name: addr=%p, name=\"%s\"",
250 plugin, name);
55bb57e0
PP
251}
252
253static inline
254void bt_plugin_set_description(struct bt_plugin *plugin,
255 const char *description)
256{
8b45963b
PP
257 BT_ASSERT(plugin);
258 BT_ASSERT(description);
55bb57e0 259 g_string_assign(plugin->info.description, description);
c55a9f58 260 plugin->info.description_set = BT_TRUE;
3fe0bf43
PP
261 BT_LOGV("Set plugin's description: addr=%p, name=\"%s\"",
262 plugin, bt_plugin_get_name(plugin));
55bb57e0
PP
263}
264
265static inline
266void bt_plugin_set_author(struct bt_plugin *plugin, const char *author)
267{
8b45963b
PP
268 BT_ASSERT(plugin);
269 BT_ASSERT(author);
55bb57e0 270 g_string_assign(plugin->info.author, author);
c55a9f58 271 plugin->info.author_set = BT_TRUE;
3fe0bf43
PP
272 BT_LOGV("Set plugin's author: addr=%p, name=\"%s\", author=\"%s\"",
273 plugin, bt_plugin_get_name(plugin), author);
55bb57e0
PP
274}
275
276static inline
277void bt_plugin_set_license(struct bt_plugin *plugin, const char *license)
278{
8b45963b
PP
279 BT_ASSERT(plugin);
280 BT_ASSERT(license);
55bb57e0 281 g_string_assign(plugin->info.license, license);
c55a9f58 282 plugin->info.license_set = BT_TRUE;
3fe0bf43
PP
283 BT_LOGV("Set plugin's path: addr=%p, name=\"%s\", license=\"%s\"",
284 plugin, bt_plugin_get_name(plugin), license);
55bb57e0
PP
285}
286
287static inline
288void bt_plugin_set_version(struct bt_plugin *plugin, unsigned int major,
289 unsigned int minor, unsigned int patch, const char *extra)
290{
8b45963b 291 BT_ASSERT(plugin);
55bb57e0
PP
292 plugin->info.version.major = major;
293 plugin->info.version.minor = minor;
294 plugin->info.version.patch = patch;
295
296 if (extra) {
297 g_string_assign(plugin->info.version.extra, extra);
298 }
299
c55a9f58 300 plugin->info.version_set = BT_TRUE;
3fe0bf43
PP
301 BT_LOGV("Set plugin's version: addr=%p, name=\"%s\", "
302 "major=%u, minor=%u, patch=%u, extra=\"%s\"",
303 plugin, bt_plugin_get_name(plugin),
304 major, minor, patch, extra);
55bb57e0
PP
305}
306
307static inline
308void bt_plugin_freeze(struct bt_plugin *plugin)
309{
8b45963b 310 BT_ASSERT(plugin);
3fe0bf43
PP
311
312 if (plugin->frozen) {
313 return;
314 }
315
316 BT_LOGD("Freezing plugin: addr=%p, name=\"%s\", path=\"%s\"",
317 plugin, bt_plugin_get_name(plugin),
318 bt_plugin_get_path(plugin));
c55a9f58 319 plugin->frozen = BT_TRUE;
55bb57e0
PP
320}
321
a8ff38ef
PP
322static
323void bt_plugin_set_destroy(struct bt_object *obj)
324{
325 struct bt_plugin_set *plugin_set =
326 container_of(obj, struct bt_plugin_set, base);
327
328 if (!plugin_set) {
329 return;
330 }
331
3fe0bf43
PP
332 BT_LOGD("Destroying plugin set: addr=%p", plugin_set);
333
a8ff38ef 334 if (plugin_set->plugins) {
3fe0bf43 335 BT_LOGD_STR("Putting plugins.");
a8ff38ef
PP
336 g_ptr_array_free(plugin_set->plugins, TRUE);
337 }
338
339 g_free(plugin_set);
340}
341
342static inline
343struct bt_plugin_set *bt_plugin_set_create(void)
344{
345 struct bt_plugin_set *plugin_set = g_new0(struct bt_plugin_set, 1);
346
347 if (!plugin_set) {
348 goto end;
349 }
350
3fe0bf43 351 BT_LOGD_STR("Creating empty plugin set.");
1d7bf349 352 bt_object_init_shared(&plugin_set->base, bt_plugin_set_destroy);
a8ff38ef
PP
353
354 plugin_set->plugins = g_ptr_array_new_with_free_func(
8138bfe1 355 (GDestroyNotify) bt_object_put_ref);
a8ff38ef 356 if (!plugin_set->plugins) {
3fe0bf43 357 BT_LOGE_STR("Failed to allocate a GPtrArray.");
8138bfe1 358 BT_OBJECT_PUT_REF_AND_RESET(plugin_set);
a8ff38ef
PP
359 goto end;
360 }
361
3fe0bf43
PP
362 BT_LOGD("Created empty plugin set: addr=%p", plugin_set);
363
a8ff38ef
PP
364end:
365 return plugin_set;
366}
367
368static inline
369void bt_plugin_set_add_plugin(struct bt_plugin_set *plugin_set,
370 struct bt_plugin *plugin)
371{
8b45963b
PP
372 BT_ASSERT(plugin_set);
373 BT_ASSERT(plugin);
8138bfe1 374 g_ptr_array_add(plugin_set->plugins, bt_object_get_ref(plugin));
3fe0bf43
PP
375 BT_LOGV("Added plugin to plugin set: "
376 "plugin-set-addr=%p, plugin-addr=%p, plugin-name=\"%s\", "
377 "plugin-path=\"%s\"",
378 plugin_set, plugin, bt_plugin_get_name(plugin),
379 bt_plugin_get_path(plugin));
8c1a3187
PP
380}
381
33b34c43 382#endif /* BABELTRACE_PLUGIN_PLUGIN_INTERNAL_H */
This page took 0.052268 seconds and 4 git commands to generate.