Introduce bt_plugin and bt_component_class
authorJérémie Galarneau <jeremie.galarneau@efficios.com>
Thu, 23 Jul 2015 17:45:47 +0000 (13:45 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Sat, 27 May 2017 16:57:25 +0000 (12:57 -0400)
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
18 files changed:
include/Makefile.am
include/babeltrace/plugin/component-class-internal.h [new file with mode: 0644]
include/babeltrace/plugin/component-factory-internal.h
include/babeltrace/plugin/component-factory.h
include/babeltrace/plugin/component-internal.h
include/babeltrace/plugin/notification/iterator-internal.h
include/babeltrace/plugin/notification/notification-internal.h
include/babeltrace/plugin/plugin-internal.h [new file with mode: 0644]
include/babeltrace/plugin/sink-internal.h
include/babeltrace/plugin/source-internal.h
plugins/Makefile.am
plugins/component-class.c [new file with mode: 0644]
plugins/component-factory.c
plugins/component.c
plugins/iterator.c
plugins/plugin.c [new file with mode: 0644]
plugins/sink.c
plugins/source.c

index d26647b24263ac825f9cb8deb1a98ac5e24d823c..bc1621ca4178eeb4b01c923aa71cf3d927b28ab7 100644 (file)
@@ -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 (file)
index 0000000..e516478
--- /dev/null
@@ -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 <jeremie.galarneau@efficios.com>
+ *
+ * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * 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 <babeltrace/babeltrace-internal.h>
+#include <babeltrace/ref-internal.h>
+#include <babeltrace/plugin/component-factory-internal.h>
+#include <babeltrace/plugin/plugin-internal.h>
+
+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 */
index 7aed5806bc4154ff1a2f4a1f0bfafbc9f7498678..257ff0f5716781550cacaebb9b887d7f6d060331 100644 (file)
  */
 
 #include <babeltrace/babeltrace-internal.h>
+#include <babeltrace/ref-internal.h>
 #include <babeltrace/plugin/component-factory.h>
 #include <babeltrace/plugin/component.h>
+#include <babeltrace/plugin/component-class-internal.h>
 #include <babeltrace/plugin/plugin-system.h>
 #include <babeltrace/plugin/plugin.h>
 #include <glib.h>
-#include <gmodule.h>
-
-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 */
index ded452f1430f487194a6c9ee89d5519f16a83cd5..5915a5b1b36c8f39cdac5c04e634d223dc882cdf 100644 (file)
@@ -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);
index 7f35eaf55af99a5f918aca8e8c72a255bfd7a07b..7a9361feb2545f63ec38f2e83b7249fa8372ed86 100644 (file)
 #include <babeltrace/babeltrace-internal.h>
 #include <babeltrace/plugin/plugin-system.h>
 #include <babeltrace/plugin/component.h>
-#include <babeltrace/ctf-writer/ref-internal.h>
+#include <babeltrace/plugin/component-class-internal.h>
+#include <babeltrace/ref-internal.h>
 #include <glib.h>
 #include <stdio.h>
 
 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 */
index 5a47377689d3378fe1bc29381a38fb606d14dd2a..e2ef852ed4c4e0eb2e83de4ab5a277fb3f78fcbe 100644 (file)
 
 #include <babeltrace/babeltrace-internal.h>
 #include <babeltrace/plugin/plugin-system.h>
-#include <babeltrace/ctf-writer/ref-internal.h>
+#include <babeltrace/ref-internal.h>
 
 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;
index 7b1a3954b8c813ce72085e0ddc2057ad2bdb957f..0d909db45c6041512db0b0bca2b41a9ba80e73cf 100644 (file)
@@ -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 (file)
index 0000000..96aa692
--- /dev/null
@@ -0,0 +1,69 @@
+#ifndef BABELTRACE_PLUGIN_INTERNAL_H
+#define BABELTRACE_PLUGIN__INTERNAL_H
+
+/*
+ * BabelTrace - Plug-in Internal
+ *
+ * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * 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 <babeltrace/babeltrace-internal.h>
+#include <babeltrace/ref-internal.h>
+#include <babeltrace/plugin/component.h>
+#include <babeltrace/plugin/plugin.h>
+#include <gmodule.h>
+
+/**
+ * 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 */
index b33333871ed5344c3b69597bd3528ed0b64c5341..f7b7bc4cbdfc0be76b4ae852e7f7a38d1fa85595 100644 (file)
 
 #include <babeltrace/babeltrace-internal.h>
 #include <babeltrace/plugin/component-internal.h>
+#include <babeltrace/plugin/component-class-internal.h>
 #include <babeltrace/plugin/plugin-system.h>
 
+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 */
index 1234339ac5da93c51d9256eaee9b41d670316a39..5c411fc1c83f04a68793559e1d4cba3407079566 100644 (file)
 
 #include <babeltrace/babeltrace-internal.h>
 #include <babeltrace/plugin/component-internal.h>
+#include <babeltrace/plugin/component-class-internal.h>
 #include <babeltrace/plugin/plugin-system.h>
 
+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 */
index 53b9ec32fa2b77df79a4d6f9fd60624a0aedfea9..88643977fa8bae81ae642d6171e81fa5dcfeb327 100644 (file)
@@ -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 (file)
index 0000000..191efdc
--- /dev/null
@@ -0,0 +1,91 @@
+/*
+ * component-class.c
+ *
+ * Babeltrace Plugin Component Class
+ * 
+ * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * 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 <babeltrace/compiler.h>
+#include <babeltrace/plugin/component-class-internal.h>
+#include <glib.h>
+
+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);
+}
index 74c6b72c30f02c69d245a57b25f5be38930fec48..a91b63a01a6099d76e729138547b78950fb5f03c 100644 (file)
@@ -33,6 +33,7 @@
 #include <unistd.h>
 #include <stdlib.h>
 #include <sys/stat.h>
+#include <gmodule.h>
 
 #define NATIVE_PLUGIN_SUFFIX ".so"
 #define NATIVE_PLUGIN_SUFFIX_LEN sizeof(NATIVE_PLUGIN_SUFFIX)
 #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:
index 7dbdaaae0c04e3467fa52c47e0412bb79c84e21e..86e86ade9ad8f69934f2a540850e90d44313d96b 100644 (file)
@@ -31,7 +31,7 @@
 #include <babeltrace/babeltrace-internal.h>
 #include <babeltrace/compiler.h>
 
-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);
 }
index 094c2ff010e99922ca503ef76c651942a4f794d1..cfa143a30ef79bfd43d12adebb2f6a5fabe6fa32 100644 (file)
@@ -33,7 +33,7 @@
 #include <babeltrace/plugin/notification/iterator-internal.h>
 
 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 (file)
index 0000000..e339c39
--- /dev/null
@@ -0,0 +1,91 @@
+/*
+ * plugin.c
+ *
+ * Babeltrace Plugin
+ * 
+ * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * 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 <babeltrace/compiler.h>
+#include <babeltrace/plugin/plugin-internal.h>
+#include <glib.h>
+
+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);
+}
index 9647f2191bd3c5c28470d953f8e046b8473a4641..bb9e1b0cc83932245279af9b42f35c9a40760609 100644 (file)
@@ -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);
index 094cab24cb2c1ac39de06b3635ffd263190f9bcf..38fc3110801662974223dba48a4609e18e621490 100644 (file)
@@ -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;
This page took 0.038475 seconds and 4 git commands to generate.