From 3bceb37b1cf75b1ffd9b8265209d9b0c3d8417f0 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Fri, 19 Jan 2024 07:01:37 -0800 Subject: Add comments, simplify deletion. --- plugin/src/plugin.c | 50 +++++++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/plugin/src/plugin.c b/plugin/src/plugin.c index f65132f..cd05faf 100644 --- a/plugin/src/plugin.c +++ b/plugin/src/plugin.c @@ -19,10 +19,16 @@ // shared library before the compiler has fully written to it. static const int WATCH_MASK = IN_CLOSE_WRITE; +/// Plugin state. +/// +/// Each Plugin object points to a shared library and holds a state object. +/// +/// The same shared library can be loaded multiple times, resulting in multiple +/// Plugin instances, each with its own internal state. typedef struct Plugin { void* handle; // First member so that Plugin can be cast to handle. void* state; // Plugin's internal state. - bool reloaded; // Whether the plugin has been reloaded state needs to be + bool reloaded; // Whether the plugin has been reloaded, state needs to be // re-created. PluginEngine* eng; // So that the public API can do stuff with just a Plugin*. mstring filename; @@ -30,6 +36,7 @@ typedef struct Plugin { DEF_LIST(Plugin); +/// Plugin engine. typedef struct PluginEngine { int inotify_instance; int dir_watch; // inotify watch on the plugins directory. @@ -76,16 +83,31 @@ static bool load_library(Plugin* plugin) { return false; } +static void delete_plugin_state(Plugin* plugin) { + if (plugin->state) { + free(plugin->state); + plugin->state = 0; + } +} + +void set_plugin_state(Plugin* plugin, void* state) { + assert(plugin); + delete_plugin_state(plugin); + plugin->state = state; +} + +void* get_plugin_state(Plugin* plugin) { + assert(plugin); + return plugin->state; +} + static void destroy_plugin(Plugin* plugin) { if (plugin) { if (plugin->handle) { dlclose(plugin->handle); plugin->handle = 0; } - if (plugin->state) { - free(plugin->state); - plugin->state = 0; - } + delete_plugin_state(plugin); } } @@ -114,24 +136,6 @@ void delete_plugin(Plugin** pPlugin) { } } -static void delete_plugin_state(Plugin* plugin) { - if (plugin->state) { - free(plugin->state); - plugin->state = 0; - } -} - -void set_plugin_state(Plugin* plugin, void* state) { - assert(plugin); - delete_plugin_state(plugin); - plugin->state = state; -} - -void* get_plugin_state(Plugin* plugin) { - assert(plugin); - return plugin->state; -} - bool plugin_reloaded(Plugin* plugin) { assert(plugin); const bool reloaded = plugin->reloaded; -- cgit v1.2.3