diff options
author | 3gg <3gg@shellblade.net> | 2024-01-19 07:01:37 -0800 |
---|---|---|
committer | 3gg <3gg@shellblade.net> | 2024-01-19 07:01:37 -0800 |
commit | 3bceb37b1cf75b1ffd9b8265209d9b0c3d8417f0 (patch) | |
tree | a37e68300409a3a30e6b9ec73b3f4f565fcb72d5 /plugin/src | |
parent | ced89ff7989fde2f3d1828c9be70600d70d72e3d (diff) |
Add comments, simplify deletion.
Diffstat (limited to 'plugin/src')
-rw-r--r-- | plugin/src/plugin.c | 50 |
1 files 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 @@ | |||
19 | // shared library before the compiler has fully written to it. | 19 | // shared library before the compiler has fully written to it. |
20 | static const int WATCH_MASK = IN_CLOSE_WRITE; | 20 | static const int WATCH_MASK = IN_CLOSE_WRITE; |
21 | 21 | ||
22 | /// Plugin state. | ||
23 | /// | ||
24 | /// Each Plugin object points to a shared library and holds a state object. | ||
25 | /// | ||
26 | /// The same shared library can be loaded multiple times, resulting in multiple | ||
27 | /// Plugin instances, each with its own internal state. | ||
22 | typedef struct Plugin { | 28 | typedef struct Plugin { |
23 | void* handle; // First member so that Plugin can be cast to handle. | 29 | void* handle; // First member so that Plugin can be cast to handle. |
24 | void* state; // Plugin's internal state. | 30 | void* state; // Plugin's internal state. |
25 | bool reloaded; // Whether the plugin has been reloaded state needs to be | 31 | bool reloaded; // Whether the plugin has been reloaded, state needs to be |
26 | // re-created. | 32 | // re-created. |
27 | PluginEngine* eng; // So that the public API can do stuff with just a Plugin*. | 33 | PluginEngine* eng; // So that the public API can do stuff with just a Plugin*. |
28 | mstring filename; | 34 | mstring filename; |
@@ -30,6 +36,7 @@ typedef struct Plugin { | |||
30 | 36 | ||
31 | DEF_LIST(Plugin); | 37 | DEF_LIST(Plugin); |
32 | 38 | ||
39 | /// Plugin engine. | ||
33 | typedef struct PluginEngine { | 40 | typedef struct PluginEngine { |
34 | int inotify_instance; | 41 | int inotify_instance; |
35 | int dir_watch; // inotify watch on the plugins directory. | 42 | int dir_watch; // inotify watch on the plugins directory. |
@@ -76,16 +83,31 @@ static bool load_library(Plugin* plugin) { | |||
76 | return false; | 83 | return false; |
77 | } | 84 | } |
78 | 85 | ||
86 | static void delete_plugin_state(Plugin* plugin) { | ||
87 | if (plugin->state) { | ||
88 | free(plugin->state); | ||
89 | plugin->state = 0; | ||
90 | } | ||
91 | } | ||
92 | |||
93 | void set_plugin_state(Plugin* plugin, void* state) { | ||
94 | assert(plugin); | ||
95 | delete_plugin_state(plugin); | ||
96 | plugin->state = state; | ||
97 | } | ||
98 | |||
99 | void* get_plugin_state(Plugin* plugin) { | ||
100 | assert(plugin); | ||
101 | return plugin->state; | ||
102 | } | ||
103 | |||
79 | static void destroy_plugin(Plugin* plugin) { | 104 | static void destroy_plugin(Plugin* plugin) { |
80 | if (plugin) { | 105 | if (plugin) { |
81 | if (plugin->handle) { | 106 | if (plugin->handle) { |
82 | dlclose(plugin->handle); | 107 | dlclose(plugin->handle); |
83 | plugin->handle = 0; | 108 | plugin->handle = 0; |
84 | } | 109 | } |
85 | if (plugin->state) { | 110 | delete_plugin_state(plugin); |
86 | free(plugin->state); | ||
87 | plugin->state = 0; | ||
88 | } | ||
89 | } | 111 | } |
90 | } | 112 | } |
91 | 113 | ||
@@ -114,24 +136,6 @@ void delete_plugin(Plugin** pPlugin) { | |||
114 | } | 136 | } |
115 | } | 137 | } |
116 | 138 | ||
117 | static void delete_plugin_state(Plugin* plugin) { | ||
118 | if (plugin->state) { | ||
119 | free(plugin->state); | ||
120 | plugin->state = 0; | ||
121 | } | ||
122 | } | ||
123 | |||
124 | void set_plugin_state(Plugin* plugin, void* state) { | ||
125 | assert(plugin); | ||
126 | delete_plugin_state(plugin); | ||
127 | plugin->state = state; | ||
128 | } | ||
129 | |||
130 | void* get_plugin_state(Plugin* plugin) { | ||
131 | assert(plugin); | ||
132 | return plugin->state; | ||
133 | } | ||
134 | |||
135 | bool plugin_reloaded(Plugin* plugin) { | 139 | bool plugin_reloaded(Plugin* plugin) { |
136 | assert(plugin); | 140 | assert(plugin); |
137 | const bool reloaded = plugin->reloaded; | 141 | const bool reloaded = plugin->reloaded; |