From e408ae3eba47d087510ea29f37cd93a28e696899 Mon Sep 17 00:00:00 2001 From: Tilka Date: Tue, 20 Mar 2012 22:29:15 +0100 Subject: [PATCH] automatically clean up plugin commands --- src/core/plugincallback.c | 47 ++++++++++++++++++++++----------------- src/core/plugincallback.h | 4 ++-- src/core/pluginloader.c | 4 ++-- 3 files changed, 31 insertions(+), 24 deletions(-) diff --git a/src/core/plugincallback.c b/src/core/plugincallback.c index 7701241..01193e5 100644 --- a/src/core/plugincallback.c +++ b/src/core/plugincallback.c @@ -54,26 +54,6 @@ static int plugin_command_dispatch(struct command_base* cbase, struct hub_user* return 0; } -struct plugin_callback_data* plugin_callback_data_create() -{ - struct plugin_callback_data* data = (struct plugin_callback_data*) hub_malloc_zero(sizeof(struct plugin_callback_data)); - LOG_PLUGIN("plugin_callback_data_create()"); - data->commands = list_create(); - return data; -} - -void plugin_callback_data_destroy(struct plugin_callback_data* data) -{ - LOG_PLUGIN("plugin_callback_data_destroy()"); - if (data->commands) - { - uhub_assert(list_size(data->commands) == 0); - list_destroy(data->commands); - } - - hub_free(data); -} - static struct hub_user* convert_user_type(struct plugin_user* user) { struct hub_user* huser = (struct hub_user*) user; @@ -173,3 +153,30 @@ void plugin_register_callback_functions(struct plugin_handle* handle) void plugin_unregister_callback_functions(struct plugin_handle* handle) { } + +struct plugin_callback_data* plugin_callback_data_create() +{ + struct plugin_callback_data* data = (struct plugin_callback_data*) hub_malloc_zero(sizeof(struct plugin_callback_data)); + LOG_PLUGIN("plugin_callback_data_create()"); + data->commands = list_create(); + return data; +} + +void plugin_callback_data_destroy(struct plugin_handle* plugin, struct plugin_callback_data* data) +{ + LOG_PLUGIN("plugin_callback_data_destroy()"); + if (data->commands) + { + // delete commands not deleted by the plugin itself: + struct plugin_command_handle* cmd = list_get_first(data->commands); + while (cmd) + { + cbfunc_command_del(plugin, cmd); + list_remove(data->commands, cmd); + cmd = list_get_next(data->commands); + } + list_destroy(data->commands); + } + + hub_free(data); +} diff --git a/src/core/plugincallback.h b/src/core/plugincallback.h index ab82366..923790a 100644 --- a/src/core/plugincallback.h +++ b/src/core/plugincallback.h @@ -24,9 +24,9 @@ struct plugin_handle; struct uhub_plugin; extern struct plugin_callback_data* plugin_callback_data_create(); -extern void plugin_callback_data_destroy(struct plugin_callback_data* data); +extern void plugin_callback_data_destroy(struct plugin_handle* plugin, struct plugin_callback_data* data); extern void plugin_register_callback_functions(struct plugin_handle* plugin); extern void plugin_unregister_callback_functions(struct plugin_handle* plugin); -#endif /* HAVE_UHUB_PLUGIN_CALLBACK_H */ \ No newline at end of file +#endif /* HAVE_UHUB_PLUGIN_CALLBACK_H */ diff --git a/src/core/pluginloader.c b/src/core/pluginloader.c index cf960d6..c0fe8a5 100644 --- a/src/core/pluginloader.c +++ b/src/core/pluginloader.c @@ -76,7 +76,7 @@ void plugin_close(struct uhub_plugin* plugin) struct plugin_hub_internals* internals = (struct plugin_hub_internals*) plugin->internals; LOG_PLUGIN("plugin_close: \"%s\"", plugin->filename); - plugin_callback_data_destroy(internals->callback_data); + plugin_callback_data_destroy(plugin->handle, internals->callback_data); hub_free(internals); plugin->internals = NULL; @@ -162,8 +162,8 @@ struct plugin_handle* plugin_load(const char* filename, const char* config, stru void plugin_unload(struct plugin_handle* plugin) { struct plugin_hub_internals* internals = get_internals(plugin); - plugin_unregister_callback_functions(plugin); internals->unregister(plugin); + plugin_unregister_callback_functions(plugin); plugin_close(plugin->handle); hub_free(plugin); }