From fb2dcc52d2d351c4fb46878414f51d2cdecdfc6f Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Galarneau?= Date: Thu, 23 Jul 2015 13:45:47 -0400 Subject: [PATCH] Introduce bt_plugin and bt_component_class MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérémie Galarneau --- include/Makefile.am | 2 + .../plugin/component-class-internal.h | 53 +++++++++++ .../plugin/component-factory-internal.h | 32 +------ include/babeltrace/plugin/component-factory.h | 37 ++++---- .../babeltrace/plugin/component-internal.h | 9 +- .../plugin/notification/iterator-internal.h | 4 +- .../notification/notification-internal.h | 2 +- include/babeltrace/plugin/plugin-internal.h | 69 ++++++++++++++ include/babeltrace/plugin/sink-internal.h | 10 +- include/babeltrace/plugin/source-internal.h | 10 +- plugins/Makefile.am | 2 + plugins/component-class.c | 91 +++++++++++++++++++ plugins/component-factory.c | 91 ++++++++----------- plugins/component.c | 22 +++-- plugins/iterator.c | 10 +- plugins/plugin.c | 91 +++++++++++++++++++ plugins/sink.c | 5 +- plugins/source.c | 7 +- 18 files changed, 420 insertions(+), 127 deletions(-) create mode 100644 include/babeltrace/plugin/component-class-internal.h create mode 100644 include/babeltrace/plugin/plugin-internal.h create mode 100644 plugins/component-class.c create mode 100644 plugins/plugin.c diff --git a/include/Makefile.am b/include/Makefile.am index d26647b2..bc1621ca 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -110,6 +110,8 @@ noinst_HEADERS = \ babeltrace/mmap-align.h \ babeltrace/plugin/component-factory-internal.h \ babeltrace/plugin/component-internal.h \ + babeltrace/plugin/component-class-internal.h \ + babeltrace/plugin/plugin-internal.h \ babeltrace/plugin/filter-internal.h \ babeltrace/plugin/sink-internal.h \ babeltrace/plugin/source-internal.h \ diff --git a/include/babeltrace/plugin/component-class-internal.h b/include/babeltrace/plugin/component-class-internal.h new file mode 100644 index 00000000..e516478b --- /dev/null +++ b/include/babeltrace/plugin/component-class-internal.h @@ -0,0 +1,53 @@ +#ifndef BABELTRACE_PLUGIN_COMPONENT_CLASS_INTERNAL_H +#define BABELTRACE_PLUGIN_COMPONENT_CLASS_INTERNAL_H + +/* + * BabelTrace - Component Class Internal + * + * Copyright 2015 Jérémie Galarneau + * + * Author: Jérémie Galarneau + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include +#include +#include +#include + +struct bt_component_class { + struct bt_ref ref; + enum bt_component_type type; + GString *name; + struct bt_plugin *plugin; +}; + +BT_HIDDEN +struct bt_component_class *bt_component_class_create( + enum bt_component_type type, const char *name, + struct bt_plugin *plugin); + +BT_HIDDEN +void bt_component_class_get(struct bt_component_class *class); + +BT_HIDDEN +void bt_component_class_put(struct bt_component_class *class); + +#endif /* BABELTRACE_PLUGIN_COMPONENT_CLASS_INTERNAL_H */ diff --git a/include/babeltrace/plugin/component-factory-internal.h b/include/babeltrace/plugin/component-factory-internal.h index 7aed5806..257ff0f5 100644 --- a/include/babeltrace/plugin/component-factory-internal.h +++ b/include/babeltrace/plugin/component-factory-internal.h @@ -28,43 +28,19 @@ */ #include +#include #include #include +#include #include #include #include -#include - -struct component_class { - enum bt_component_type type; - GString *name; -}; - -struct source_component_class { - struct component_class parent; - bt_component_source_init_cb init; - -}; - -struct sink_component_class { - struct component_class parent; - bt_component_sink_init_cb init; -}; - -struct plugin { - const char *name; - const char *author; - const char *license; - bt_plugin_init_func init; - bt_plugin_init_func exit; - GModule *module; - /** Array of pointers to struct component_class */ - GPtrArray *components; -}; struct bt_component_factory { /** Array of pointers to struct plugin */ GPtrArray *plugins; + /** Array of pointers to struct bt_component_class */ + GPtrArray *components; }; #endif /* BABELTRACE_PLUGIN_COMPONENT_FACTORY_INTERNAL_H */ diff --git a/include/babeltrace/plugin/component-factory.h b/include/babeltrace/plugin/component-factory.h index ded452f1..5915a5b1 100644 --- a/include/babeltrace/plugin/component-factory.h +++ b/include/babeltrace/plugin/component-factory.h @@ -40,30 +40,28 @@ extern "C" { */ enum bt_component_factory_status { /** General error. */ - BT_COMPONENT_FACTORY_STATUS_ERROR = -128, + BT_COMPONENT_FACTORY_STATUS_ERROR = -128, + + /** Invalid plugin. */ + BT_COMPONENT_FACTORY_STATUS_INVAL_PLUGIN = -6, /** Invalid arguments. */ - /* -22 for compatibility with -EINVAL */ - BT_COMPONENT_FACTORY_STATUS_INVAL = -22, + BT_COMPONENT_FACTORY_STATUS_INVAL = -5, /** Memory allocation failure. */ - /* -12 for compatibility with -ENOMEM */ - BT_COMPONENT_FACTORY_STATUS_NOMEM = -12, + BT_COMPONENT_FACTORY_STATUS_NOMEM = -4, /** I/O error. */ - /* -5 for compatibility with -EIO */ - BT_COMPONENT_FACTORY_STATUS_IO = -5, + BT_COMPONENT_FACTORY_STATUS_IO = -3, /** No such file or directory. */ - /* -2 for compatibility with -ENOENT */ - BT_COMPONENT_FACTORY_STATUS_NOENT = -2, + BT_COMPONENT_FACTORY_STATUS_NOENT = -2, /** Operation not permitted. */ - /* -1 for compatibility with -EPERM */ - BT_COMPONENT_FACTORY_STATUS_PERM = -1, + BT_COMPONENT_FACTORY_STATUS_PERM = -1, /** No error, okay. */ - BT_COMPONENT_FACTORY_STATUS_OK = 0, + BT_COMPONENT_FACTORY_STATUS_OK = 0, }; struct bt_component_factory; @@ -76,6 +74,13 @@ struct bt_component_factory; extern struct bt_component_factory *bt_component_factory_create(void); +/** + * Get the list of components registered to this factory. + */ +extern +struct bt_object *bt_component_factory_get_components( + struct bt_component_factory *factory); + /** * Recursively load and register Babeltrace plugins under a given path. * @@ -93,13 +98,13 @@ enum bt_component_factory_status bt_component_factory_load( extern enum bt_component_factory_status bt_component_factory_register_source_component_class( - struct bt_component_factory *factory, const char *name, - bt_component_source_init_cb init); + struct bt_component_factory *factory, const char *name, + bt_component_source_init_cb init); extern enum bt_component_factory_status bt_component_factory_register_sink_component_class( - struct bt_component_factory *factory, const char *name, - bt_component_sink_init_cb init); + struct bt_component_factory *factory, const char *name, + bt_component_sink_init_cb init); extern void bt_component_factory_destroy(struct bt_component_factory *factory); diff --git a/include/babeltrace/plugin/component-internal.h b/include/babeltrace/plugin/component-internal.h index 7f35eaf5..7a9361fe 100644 --- a/include/babeltrace/plugin/component-internal.h +++ b/include/babeltrace/plugin/component-internal.h @@ -30,14 +30,15 @@ #include #include #include -#include +#include +#include #include #include struct bt_component { - struct bt_ctf_ref ref_count; + struct bt_ref ref; + struct bt_component_class *class; GString *name; - enum bt_component_type type; /** No ownership taken */ FILE *error_stream; /** source, sink or filter destroy */ @@ -49,7 +50,7 @@ struct bt_component { BT_HIDDEN enum bt_component_status bt_component_init(struct bt_component *component, - const char *name, enum bt_component_type component_type, + struct bt_component_class *class, const char *name, bt_component_destroy_cb destroy); #endif /* BABELTRACE_PLUGIN_COMPONENT_INTERNAL_H */ diff --git a/include/babeltrace/plugin/notification/iterator-internal.h b/include/babeltrace/plugin/notification/iterator-internal.h index 5a473776..e2ef852e 100644 --- a/include/babeltrace/plugin/notification/iterator-internal.h +++ b/include/babeltrace/plugin/notification/iterator-internal.h @@ -29,10 +29,10 @@ #include #include -#include +#include struct bt_notification_iterator { - struct bt_ctf_ref ref_count; + struct bt_ref ref; bt_notification_iterator_get_cb get; bt_notification_iterator_next_cb next; void *user_data; diff --git a/include/babeltrace/plugin/notification/notification-internal.h b/include/babeltrace/plugin/notification/notification-internal.h index 7b1a3954..0d909db4 100644 --- a/include/babeltrace/plugin/notification/notification-internal.h +++ b/include/babeltrace/plugin/notification/notification-internal.h @@ -36,7 +36,7 @@ extern "C" { #endif struct bt_notification { - struct bt_ctf_ref ref_count; + struct bt_ctf_ref ref; enum bt_notification_type type; }; diff --git a/include/babeltrace/plugin/plugin-internal.h b/include/babeltrace/plugin/plugin-internal.h new file mode 100644 index 00000000..96aa692a --- /dev/null +++ b/include/babeltrace/plugin/plugin-internal.h @@ -0,0 +1,69 @@ +#ifndef BABELTRACE_PLUGIN_INTERNAL_H +#define BABELTRACE_PLUGIN__INTERNAL_H + +/* + * BabelTrace - Plug-in Internal + * + * Copyright 2015 Jérémie Galarneau + * + * Author: Jérémie Galarneau + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include +#include +#include +#include +#include + +/** + * Plug-ins are owned by bt_component_factory and the bt_component_class-es + * it provides. This means that its lifetime bound by either the component + * factory's, or the concrete components' lifetime which may be in use and which + * have hold a reference to their bt_component_class which, in turn, have a + * reference to their plugin. + * + * This ensures that a plugin's library is not closed while it is being used + * even if the bt_component_factory which created its components is destroyed. + */ +struct bt_plugin { + struct bt_ref ref; + const char *name; + const char *author; + const char *license; + bt_plugin_init_func init; + bt_plugin_exit_func exit; + GModule *module; +}; + +BT_HIDDEN +struct bt_plugin *bt_plugin_create(GModule *module); + +BT_HIDDEN +enum bt_component_status bt_plugin_register_component_classes( + struct bt_plugin *plugin, struct bt_component_factory *factory); + +BT_HIDDEN +void bt_plugin_get(struct bt_plugin *plugin); + +BT_HIDDEN +void bt_plugin_put(struct bt_plugin *plugin); + +#endif /* BABELTRACE_PLUGIN_COMPONENT_CLASS_INTERNAL_H */ diff --git a/include/babeltrace/plugin/sink-internal.h b/include/babeltrace/plugin/sink-internal.h index b3333387..f7b7bc4c 100644 --- a/include/babeltrace/plugin/sink-internal.h +++ b/include/babeltrace/plugin/sink-internal.h @@ -29,8 +29,14 @@ #include #include +#include #include +struct bt_component_sink_class { + struct bt_component_class parent; + bt_component_sink_init_cb init; +}; + struct bt_component_sink { struct bt_component parent; @@ -41,10 +47,12 @@ struct bt_component_sink { /** * Allocate a sink component. * + * @param class Component class * @param name Component instance name (will be copied) * @returns A sink component instance */ BT_HIDDEN -extern struct bt_component *bt_component_sink_create(const char *name); +extern struct bt_component *bt_component_sink_create( + struct bt_component_class *class, const char *name); #endif /* BABELTRACE_PLUGIN_SINK_INTERNAL_H */ diff --git a/include/babeltrace/plugin/source-internal.h b/include/babeltrace/plugin/source-internal.h index 1234339a..5c411fc1 100644 --- a/include/babeltrace/plugin/source-internal.h +++ b/include/babeltrace/plugin/source-internal.h @@ -29,8 +29,14 @@ #include #include +#include #include +struct bt_component_source_class { + struct bt_component_class parent; + bt_component_source_init_cb init; +}; + struct bt_component_source { struct bt_component parent; @@ -41,10 +47,12 @@ struct bt_component_source { /** * Allocate a source component. * + * @param class Component class * @param name Component instance name (will be copied) * @returns A source component instance */ BT_HIDDEN -extern struct bt_component *bt_component_source_create(const char *name); +extern struct bt_component *bt_component_source_create( + struct bt_component_class *class, const char *name); #endif /* BABELTRACE_PLUGIN_SOURCE_INTERNAL_H */ diff --git a/plugins/Makefile.am b/plugins/Makefile.am index 53b9ec32..88643977 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am @@ -7,7 +7,9 @@ lib_LTLIBRARIES = libbabeltrace-plugin.la # Plug-in system library libbabeltrace_plugin_la_SOURCES = \ component.c \ + component-class.c \ component-factory.c \ + plugin.c \ source.c \ sink.c \ iterator.c diff --git a/plugins/component-class.c b/plugins/component-class.c new file mode 100644 index 00000000..191efdcb --- /dev/null +++ b/plugins/component-class.c @@ -0,0 +1,91 @@ +/* + * component-class.c + * + * Babeltrace Plugin Component Class + * + * Copyright 2015 Jérémie Galarneau + * + * Author: Jérémie Galarneau + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include +#include +#include + +static +void bt_component_class_destroy(struct bt_ref *ref) +{ + struct bt_component_class *class; + + assert(ref); + class = container_of(ref, struct bt_component_class, ref); + if (class->name) { + g_string_free(class->name, TRUE); + } + bt_plugin_put(class->plugin); + g_free(class); +} + +BT_HIDDEN +struct bt_component_class *bt_component_class_create( + enum bt_component_type type, const char *name, + struct bt_plugin *plugin) +{ + struct bt_component_class *class; + + class = g_new0(struct bt_component_class, 1); + if (!class) { + goto end; + } + + bt_ref_init(&class->ref, bt_component_class_destroy); + class->type = type; + class->name = g_string_new(name); + if (!class->name) { + bt_component_class_put(class); + class = NULL; + goto end; + } + bt_plugin_get(plugin); + class->plugin = plugin; +end: + return class; +} + +BT_HIDDEN +void bt_component_class_get(struct bt_component_class *class) +{ + if (!class) { + return; + } + + bt_ref_get(&class->ref); +} + +BT_HIDDEN +void bt_component_class_put(struct bt_component_class *class) +{ + if (!class) { + return; + } + + bt_ref_put(&class->ref); +} diff --git a/plugins/component-factory.c b/plugins/component-factory.c index 74c6b72c..a91b63a0 100644 --- a/plugins/component-factory.c +++ b/plugins/component-factory.c @@ -33,6 +33,7 @@ #include #include #include +#include #define NATIVE_PLUGIN_SUFFIX ".so" #define NATIVE_PLUGIN_SUFFIX_LEN sizeof(NATIVE_PLUGIN_SUFFIX) @@ -41,47 +42,6 @@ #define PLUGIN_SUFFIX_LEN max_t(size_t, sizeof(NATIVE_PLUGIN_SUFFIX), \ sizeof(LIBTOOL_PLUGIN_SUFFIX)) -static -void component_destroy(gpointer data) -{ - g_free(data); -} - -static -void plugin_destroy(gpointer data) -{ - struct plugin *plugin = data; - - if (plugin->module && g_module_close(plugin->module)) { - printf_error("Failed to close plugin"); - } - - if (plugin->components) { - g_ptr_array_free(plugin->components, TRUE); - } - - g_free(plugin); -} - -static -struct plugin *plugin_create(void) -{ - struct plugin *plugin = g_new0(struct plugin, 1); - - if (!plugin) { - goto end; - } - - plugin->components = g_ptr_array_new_with_free_func(component_destroy); - if (!plugin->components) { - g_free(plugin); - plugin = NULL; - goto end; - } -end: - return plugin; -} - /* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */ static struct dirent *alloc_dirent(const char *path) @@ -99,21 +59,16 @@ struct dirent *alloc_dirent(const char *path) return entry; } -static -struct plugin *load_plugin(GModule *module) -{ - return NULL; -} - static enum bt_component_factory_status bt_component_factory_load_file(struct bt_component_factory *factory, const char *path) { enum bt_component_factory_status ret = BT_COMPONENT_FACTORY_STATUS_OK; + enum bt_component_status component_status; size_t path_len; GModule *module; - struct plugin *plugin; + struct bt_plugin *plugin; if (!factory || !path) { ret = BT_COMPONENT_FACTORY_STATUS_INVAL; @@ -149,17 +104,33 @@ bt_component_factory_load_file(struct bt_component_factory *factory, goto end; } - /* Check if the module defines the appropriate entry points */ - plugin = load_plugin(module); + /* Load plugin and make sure it defines the required entry points */ + plugin = bt_plugin_create(module); if (!plugin) { - /* Not a Babeltrace plugin */ - ret = BT_COMPONENT_FACTORY_STATUS_INVAL; + ret = BT_COMPONENT_FACTORY_STATUS_INVAL_PLUGIN; if (!g_module_close(module)) { printf_error("Module close error: %s", - g_module_error()); + g_module_error()); + } + goto end; + } + + component_status = bt_plugin_register_component_classes(plugin, + factory); + if (component_status != BT_COMPONENT_STATUS_OK) { + switch (component_status) { + case BT_COMPONENT_STATUS_NOMEM: + ret = BT_COMPONENT_FACTORY_STATUS_NOMEM; + break; + default: + ret = BT_COMPONENT_FACTORY_STATUS_ERROR; + break; } + bt_plugin_put(plugin); + plugin = NULL; goto end; } + g_ptr_array_add(factory->plugins, plugin); end: return ret; } @@ -240,6 +211,12 @@ void bt_component_factory_destroy(struct bt_component_factory *factory) return; } + if (factory->plugins) { + g_ptr_array_free(factory->plugins, TRUE); + } + if (factory->components) { + g_ptr_array_free(factory->components, TRUE); + } g_free(factory); } @@ -253,10 +230,16 @@ bt_component_factory_create(void) goto end; } - factory->plugins = g_ptr_array_new_with_free_func(plugin_destroy); + factory->plugins = g_ptr_array_new_with_free_func( + (GDestroyNotify) bt_plugin_put); if (!factory->plugins) { goto error; } + factory->components = g_ptr_array_new_with_free_func( + (GDestroyNotify) bt_component_put); + if (!factory->components) { + goto error; + } end: return factory; error: diff --git a/plugins/component.c b/plugins/component.c index 7dbdaaae..86e86ade 100644 --- a/plugins/component.c +++ b/plugins/component.c @@ -31,7 +31,7 @@ #include #include -static void bt_component_destroy(struct bt_ctf_ref *ref); +static void bt_component_destroy(struct bt_ref *ref); const char *bt_component_get_name(struct bt_component *component) { @@ -69,7 +69,7 @@ enum bt_component_type bt_component_get_type(struct bt_component *component) goto end; } - type = component->type; + type = component->class->type; end: return type; } @@ -95,7 +95,7 @@ void bt_component_get(struct bt_component *component) return; } - bt_ctf_ref_get(&component->ref_count); + bt_ref_get(&component->ref); } void bt_component_put(struct bt_component *component) @@ -104,23 +104,24 @@ void bt_component_put(struct bt_component *component) return; } - bt_ctf_ref_put(&component->ref_count, bt_component_destroy); + bt_ref_put(&component->ref); } BT_HIDDEN enum bt_component_status bt_component_init(struct bt_component *component, - const char *name, enum bt_component_type component_type, + struct bt_component_class *class, const char *name, bt_component_destroy_cb destroy) { enum bt_component_status ret = BT_COMPONENT_STATUS_OK; - if (!component || !name || name[0] == '\0' || destroy) { + if (!component || !class || !name || name[0] == '\0' || destroy) { ret = BT_COMPONENT_STATUS_INVAL; goto end; } - bt_ctf_ref_init(&component->ref_count); - component->type = component_type; + bt_ref_init(&component->ref, bt_component_destroy); + bt_component_class_get(class); + component->class = class; component->name = g_string_new(name); if (!component->name) { ret = BT_COMPONENT_STATUS_NOMEM; @@ -161,7 +162,7 @@ end: } static -void bt_component_destroy(struct bt_ctf_ref *ref) +void bt_component_destroy(struct bt_ref *ref) { struct bt_component *component = NULL; @@ -169,7 +170,7 @@ void bt_component_destroy(struct bt_ctf_ref *ref) return; } - component = container_of(ref, struct bt_component, ref_count); + component = container_of(ref, struct bt_component, ref); /** * User data is destroyed first, followed by the concrete component @@ -182,4 +183,5 @@ void bt_component_destroy(struct bt_ctf_ref *ref) assert(component->destroy); component->destroy(component); + bt_component_class_put(component->class); } diff --git a/plugins/iterator.c b/plugins/iterator.c index 094c2ff0..cfa143a3 100644 --- a/plugins/iterator.c +++ b/plugins/iterator.c @@ -33,7 +33,7 @@ #include static -void bt_notification_iterator_destroy(struct bt_ctf_ref *ref) +void bt_notification_iterator_destroy(struct bt_ref *ref) { struct bt_notification_iterator *iterator; @@ -42,7 +42,7 @@ void bt_notification_iterator_destroy(struct bt_ctf_ref *ref) } iterator = container_of(ref, struct bt_notification_iterator, - ref_count); + ref); assert(iterator->user_destroy || !iterator->user_data); iterator->user_destroy(iterator); g_free(iterator); @@ -64,7 +64,7 @@ struct bt_notification_iterator *bt_notification_iterator_create( goto end; } - bt_ctf_ref_init(&iterator->ref_count); + bt_ref_init(&iterator->ref, bt_notification_iterator_destroy); end: return iterator; } @@ -90,7 +90,7 @@ void bt_notification_iterator_get(struct bt_notification_iterator *iterator) return; } - bt_ctf_ref_get(&iterator->ref_count); + bt_ref_get(&iterator->ref); } void bt_notification_iterator_put(struct bt_notification_iterator *iterator) @@ -99,7 +99,7 @@ void bt_notification_iterator_put(struct bt_notification_iterator *iterator) return; } - bt_ctf_ref_put(&iterator->ref_count, bt_notification_iterator_destroy); + bt_ref_put(&iterator->ref); } enum bt_notification_iterator_status bt_notification_iterator_set_get_cb( diff --git a/plugins/plugin.c b/plugins/plugin.c new file mode 100644 index 00000000..e339c394 --- /dev/null +++ b/plugins/plugin.c @@ -0,0 +1,91 @@ +/* + * plugin.c + * + * Babeltrace Plugin + * + * Copyright 2015 Jérémie Galarneau + * + * Author: Jérémie Galarneau + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include +#include +#include + +static +void bt_plugin_destroy(struct bt_ref *ref) +{ + struct bt_plugin *plugin; + + assert(ref); + plugin = container_of(ref, struct bt_plugin, ref); + if (plugin->module) { + if (!g_module_close(plugin->module)) { + printf_error("Module close error: %s", + g_module_error()); + + } + } + g_free(plugin); +} + +BT_HIDDEN +struct bt_plugin *bt_plugin_create(GModule *module) +{ + struct bt_plugin *plugin; + + plugin = g_new0(struct bt_plugin, 1); + if (!plugin) { + goto end; + } + + bt_ref_init(&plugin->ref, bt_plugin_destroy); +end: + return plugin; +} + +BT_HIDDEN +enum bt_component_status bt_plugin_register_component_classes( + struct bt_plugin *plugin, struct bt_component_factory *factory) +{ + assert(plugin && factory); + return plugin->init(factory); +} + +BT_HIDDEN +void bt_plugin_get(struct bt_plugin *plugin) +{ + if (!plugin) { + return; + } + + bt_ref_get(&plugin->ref); +} + +BT_HIDDEN +void bt_plugin_put(struct bt_plugin *plugin) +{ + if (!plugin) { + return; + } + + bt_ref_put(&plugin->ref); +} diff --git a/plugins/sink.c b/plugins/sink.c index 9647f219..bb9e1b0c 100644 --- a/plugins/sink.c +++ b/plugins/sink.c @@ -44,7 +44,8 @@ void bt_component_sink_destroy(struct bt_component *component) } BT_HIDDEN -struct bt_component *bt_component_sink_create(const char *name) +struct bt_component *bt_component_sink_create( + struct bt_component_class *class, const char *name) { struct bt_component_sink *sink = NULL; enum bt_component_status ret; @@ -54,7 +55,7 @@ struct bt_component *bt_component_sink_create(const char *name) goto end; } - ret = bt_component_init(&sink->parent, name, BT_COMPONENT_TYPE_SINK, + ret = bt_component_init(&sink->parent, class, name, bt_component_sink_destroy); if (ret != BT_COMPONENT_STATUS_OK) { g_free(sink); diff --git a/plugins/source.c b/plugins/source.c index 094cab24..38fc3110 100644 --- a/plugins/source.c +++ b/plugins/source.c @@ -46,7 +46,8 @@ void bt_component_source_destroy(struct bt_component *component) } BT_HIDDEN -struct bt_component *bt_component_source_create(const char *name) +struct bt_component *bt_component_source_create( + struct bt_component_class *class, const char *name) { struct bt_component_source *source = NULL; enum bt_component_status ret; @@ -55,8 +56,8 @@ struct bt_component *bt_component_source_create(const char *name) goto end; } - ret = bt_component_init(&source->parent, name, - BT_COMPONENT_TYPE_SOURCE, bt_component_source_destroy); + ret = bt_component_init(&source->parent, class, name, + bt_component_source_destroy); if (ret != BT_COMPONENT_STATUS_OK) { g_free(source); source = NULL; -- 2.34.1