X-Git-Url: https://git.efficios.com/?a=blobdiff_plain;f=include%2Fbabeltrace%2Fplugin%2Fplugin-internal.h;h=4cb57513a3073296637a8452df9d91ccc1aad860;hb=5cd6d0e5fa67c2bdd21dc153313618260595d7bc;hp=673b7818f4838d8ed6998a2a18ef9e1e1981a0ca;hpb=38b48196e2b127cceb022056bb22a15086644e10;p=babeltrace.git diff --git a/include/babeltrace/plugin/plugin-internal.h b/include/babeltrace/plugin/plugin-internal.h index 673b7818..4cb57513 100644 --- a/include/babeltrace/plugin/plugin-internal.h +++ b/include/babeltrace/plugin/plugin-internal.h @@ -1,5 +1,5 @@ -#ifndef BABELTRACE_PLUGIN_INTERNAL_H -#define BABELTRACE_PLUGIN_INTERNAL_H +#ifndef BABELTRACE_PLUGIN_PLUGIN_INTERNAL_H +#define BABELTRACE_PLUGIN_PLUGIN_INTERNAL_H /* * BabelTrace - Plug-in Internal @@ -28,37 +28,355 @@ */ #include -#include -#include -#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. - */ +#include +#include +#include + +enum bt_plugin_type { + BT_PLUGIN_TYPE_SO = 0, + BT_PLUGIN_TYPE_PYTHON = 1, +}; + struct bt_plugin { struct bt_object base; - const char *name; - const char *author; - const char *license; - bt_plugin_init_func init; - bt_plugin_exit_func exit; - GModule *module; + enum bt_plugin_type type; + bt_bool frozen; + + /* Array of pointers to bt_component_class (owned by this) */ + GPtrArray *comp_classes; + + /* Info (owned by this) */ + struct { + GString *path; + GString *name; + GString *author; + GString *license; + GString *description; + struct { + unsigned int major; + unsigned int minor; + unsigned int patch; + GString *extra; + } version; + bt_bool path_set; + bt_bool name_set; + bt_bool author_set; + bt_bool license_set; + bt_bool description_set; + bt_bool version_set; + } info; + + /* Value depends on the specific plugin type */ + void *spec_data; + void (*destroy_spec_data)(struct bt_plugin *); }; -BT_HIDDEN -struct bt_plugin *bt_plugin_create(GModule *module); +struct bt_plugin_set { + struct bt_object base; + + /* Array of struct bt_plugin * */ + GPtrArray *plugins; +}; + +static inline +const char *bt_plugin_status_string(enum bt_plugin_status status) +{ + switch (status) { + case BT_PLUGIN_STATUS_OK: + return "BT_PLUGIN_STATUS_OK"; + case BT_PLUGIN_STATUS_ERROR: + return "BT_PLUGIN_STATUS_ERROR"; + case BT_PLUGIN_STATUS_NOMEM: + return "BT_PLUGIN_STATUS_NOMEM"; + default: + return "(unknown)"; + } +} + +static inline +const char *bt_plugin_type_string(enum bt_plugin_type type) +{ + switch (type) { + case BT_PLUGIN_TYPE_SO: + return "BT_PLUGIN_TYPE_SO"; + case BT_PLUGIN_TYPE_PYTHON: + return "BT_PLUGIN_TYPE_PYTHON"; + default: + return "(unknown)"; + } +} + +static inline +void bt_plugin_destroy(struct bt_object *obj) +{ + struct bt_plugin *plugin; + + BT_ASSERT(obj); + plugin = container_of(obj, struct bt_plugin, base); + BT_LOGD("Destroying plugin object: addr=%p, name=\"%s\"", + plugin, plugin->info.name ? plugin->info.name->str : NULL); + + if (plugin->destroy_spec_data) { + plugin->destroy_spec_data(plugin); + } + + if (plugin->comp_classes) { + BT_LOGD_STR("Putting component classes."); + g_ptr_array_free(plugin->comp_classes, TRUE); + } + + if (plugin->info.name) { + g_string_free(plugin->info.name, TRUE); + } + + if (plugin->info.path) { + g_string_free(plugin->info.path, TRUE); + } + + if (plugin->info.description) { + g_string_free(plugin->info.description, TRUE); + } + + if (plugin->info.author) { + g_string_free(plugin->info.author, TRUE); + } + + if (plugin->info.license) { + g_string_free(plugin->info.license, TRUE); + } + + if (plugin->info.version.extra) { + g_string_free(plugin->info.version.extra, TRUE); + } + + g_free(plugin); +} + +static inline +struct bt_plugin *bt_plugin_create_empty(enum bt_plugin_type type) +{ + struct bt_plugin *plugin = NULL; + + BT_LOGD("Creating empty plugin object: type=%s", + bt_plugin_type_string(type)); + + plugin = g_new0(struct bt_plugin, 1); + if (!plugin) { + BT_LOGE_STR("Failed to allocate one plugin."); + goto error; + } + + bt_object_init_shared(&plugin->base, bt_plugin_destroy); + plugin->type = type; + + /* Create empty array of component classes */ + plugin->comp_classes = + g_ptr_array_new_with_free_func((GDestroyNotify) bt_put); + if (!plugin->comp_classes) { + BT_LOGE_STR("Failed to allocate a GPtrArray."); + goto error; + } + + /* Create empty info */ + plugin->info.name = g_string_new(NULL); + if (!plugin->info.name) { + BT_LOGE_STR("Failed to allocate a GString."); + goto error; + } + + plugin->info.path = g_string_new(NULL); + if (!plugin->info.path) { + BT_LOGE_STR("Failed to allocate a GString."); + goto error; + } + + plugin->info.description = g_string_new(NULL); + if (!plugin->info.description) { + BT_LOGE_STR("Failed to allocate a GString."); + goto error; + } + + plugin->info.author = g_string_new(NULL); + if (!plugin->info.author) { + BT_LOGE_STR("Failed to allocate a GString."); + goto error; + } + + plugin->info.license = g_string_new(NULL); + if (!plugin->info.license) { + BT_LOGE_STR("Failed to allocate a GString."); + goto error; + } + + plugin->info.version.extra = g_string_new(NULL); + if (!plugin->info.version.extra) { + BT_LOGE_STR("Failed to allocate a GString."); + goto error; + } + + BT_LOGD("Created empty plugin object: type=%s, addr=%p", + bt_plugin_type_string(type), plugin); + goto end; + +error: + BT_PUT(plugin); + +end: + return plugin; +} + +static inline +void bt_plugin_set_path(struct bt_plugin *plugin, const char *path) +{ + BT_ASSERT(plugin); + BT_ASSERT(path); + g_string_assign(plugin->info.path, path); + plugin->info.path_set = BT_TRUE; + BT_LOGV("Set plugin's path: addr=%p, name=\"%s\", path=\"%s\"", + plugin, bt_plugin_get_name(plugin), path); +} + +static inline +void bt_plugin_set_name(struct bt_plugin *plugin, const char *name) +{ + BT_ASSERT(plugin); + BT_ASSERT(name); + g_string_assign(plugin->info.name, name); + plugin->info.name_set = BT_TRUE; + BT_LOGV("Set plugin's name: addr=%p, name=\"%s\"", + plugin, name); +} + +static inline +void bt_plugin_set_description(struct bt_plugin *plugin, + const char *description) +{ + BT_ASSERT(plugin); + BT_ASSERT(description); + g_string_assign(plugin->info.description, description); + plugin->info.description_set = BT_TRUE; + BT_LOGV("Set plugin's description: addr=%p, name=\"%s\"", + plugin, bt_plugin_get_name(plugin)); +} + +static inline +void bt_plugin_set_author(struct bt_plugin *plugin, const char *author) +{ + BT_ASSERT(plugin); + BT_ASSERT(author); + g_string_assign(plugin->info.author, author); + plugin->info.author_set = BT_TRUE; + BT_LOGV("Set plugin's author: addr=%p, name=\"%s\", author=\"%s\"", + plugin, bt_plugin_get_name(plugin), author); +} + +static inline +void bt_plugin_set_license(struct bt_plugin *plugin, const char *license) +{ + BT_ASSERT(plugin); + BT_ASSERT(license); + g_string_assign(plugin->info.license, license); + plugin->info.license_set = BT_TRUE; + BT_LOGV("Set plugin's path: addr=%p, name=\"%s\", license=\"%s\"", + plugin, bt_plugin_get_name(plugin), license); +} + +static inline +void bt_plugin_set_version(struct bt_plugin *plugin, unsigned int major, + unsigned int minor, unsigned int patch, const char *extra) +{ + BT_ASSERT(plugin); + plugin->info.version.major = major; + plugin->info.version.minor = minor; + plugin->info.version.patch = patch; + + if (extra) { + g_string_assign(plugin->info.version.extra, extra); + } + + plugin->info.version_set = BT_TRUE; + BT_LOGV("Set plugin's version: addr=%p, name=\"%s\", " + "major=%u, minor=%u, patch=%u, extra=\"%s\"", + plugin, bt_plugin_get_name(plugin), + major, minor, patch, extra); +} + +static inline +void bt_plugin_freeze(struct bt_plugin *plugin) +{ + BT_ASSERT(plugin); + + if (plugin->frozen) { + return; + } + + BT_LOGD("Freezing plugin: addr=%p, name=\"%s\", path=\"%s\"", + plugin, bt_plugin_get_name(plugin), + bt_plugin_get_path(plugin)); + plugin->frozen = BT_TRUE; +} + +static +void bt_plugin_set_destroy(struct bt_object *obj) +{ + struct bt_plugin_set *plugin_set = + container_of(obj, struct bt_plugin_set, base); + + if (!plugin_set) { + return; + } + + BT_LOGD("Destroying plugin set: addr=%p", plugin_set); + + if (plugin_set->plugins) { + BT_LOGD_STR("Putting plugins."); + g_ptr_array_free(plugin_set->plugins, TRUE); + } + + g_free(plugin_set); +} + +static inline +struct bt_plugin_set *bt_plugin_set_create(void) +{ + struct bt_plugin_set *plugin_set = g_new0(struct bt_plugin_set, 1); + + if (!plugin_set) { + goto end; + } + + BT_LOGD_STR("Creating empty plugin set."); + bt_object_init_shared(&plugin_set->base, bt_plugin_set_destroy); + + plugin_set->plugins = g_ptr_array_new_with_free_func( + (GDestroyNotify) bt_put); + if (!plugin_set->plugins) { + BT_LOGE_STR("Failed to allocate a GPtrArray."); + BT_PUT(plugin_set); + goto end; + } + + BT_LOGD("Created empty plugin set: addr=%p", plugin_set); + +end: + return plugin_set; +} -BT_HIDDEN -enum bt_component_status bt_plugin_register_component_classes( - struct bt_plugin *plugin, struct bt_component_factory *factory); +static inline +void bt_plugin_set_add_plugin(struct bt_plugin_set *plugin_set, + struct bt_plugin *plugin) +{ + BT_ASSERT(plugin_set); + BT_ASSERT(plugin); + g_ptr_array_add(plugin_set->plugins, bt_get(plugin)); + BT_LOGV("Added plugin to plugin set: " + "plugin-set-addr=%p, plugin-addr=%p, plugin-name=\"%s\", " + "plugin-path=\"%s\"", + plugin_set, plugin, bt_plugin_get_name(plugin), + bt_plugin_get_path(plugin)); +} -#endif /* BABELTRACE_PLUGIN_INTERNAL_H */ +#endif /* BABELTRACE_PLUGIN_PLUGIN_INTERNAL_H */