1313 * - cache_miss_hook: Handle cache miss events
1414 * - cache_eviction_hook: Determine which object to evict
1515 * - cache_remove_hook: Clean up when objects are removed
16+ * - cache_free_hook: Free plugin resources (optional)
1617 *
1718 * The plugin cache delegates core cache operations to these hooks, enabling
1819 * flexible and extensible cache policies.
@@ -52,6 +53,7 @@ typedef struct pluginCache_params {
5253 cache_miss_hook_t cache_miss_hook ; ///< Cache miss handler function
5354 cache_eviction_hook_t cache_eviction_hook ; ///< Eviction decision function
5455 cache_remove_hook_t cache_remove_hook ; ///< Object removal handler function
56+ cache_free_hook_t cache_free_hook ; ///< Optional cleanup function
5557 char * cache_name ;
5658} pluginCache_params_t ;
5759
@@ -159,6 +161,10 @@ cache_t *pluginCache_init(const common_cache_params_t ccache_params,
159161 void * obj ;
160162 cache_remove_hook_t func ;
161163 } cache_remove_u ;
164+ union {
165+ void * obj ;
166+ cache_free_hook_t func ;
167+ } cache_free_u ;
162168
163169 cache_init_u .obj = dlsym (handle , "cache_init_hook" );
164170 params -> cache_init_hook = cache_init_u .func ;
@@ -175,6 +181,13 @@ cache_t *pluginCache_init(const common_cache_params_t ccache_params,
175181 cache_remove_u .obj = dlsym (handle , "cache_remove_hook" );
176182 params -> cache_remove_hook = cache_remove_u .func ;
177183
184+ // Optional cleanup function, may be NULL if not provided by the plugin
185+ cache_free_u .obj = dlsym (handle , "cache_free_hook" );
186+ params -> cache_free_hook = cache_free_u .func ;
187+ // Clear any dlsym error since this hook is optional
188+ dlerror ();
189+ DEBUG ("Provided cache_free_hook: %p\n" , params -> cache_free_hook );
190+
178191 // Initialize the plugin with cache parameters
179192 params -> data = params -> cache_init_hook (ccache_params );
180193
@@ -205,6 +218,12 @@ cache_t *pluginCache_init(const common_cache_params_t ccache_params,
205218 */
206219static void pluginCache_free (cache_t * cache ) {
207220 pluginCache_params_t * params = (pluginCache_params_t * )cache -> eviction_params ;
221+
222+ // Call the plugin's cleanup function first if it exists
223+ if (params -> cache_free_hook != NULL ) {
224+ params -> cache_free_hook (params -> data );
225+ }
226+
208227 if (params -> plugin_path != NULL ) free (params -> plugin_path );
209228 if (params -> cache_name != NULL ) free (params -> cache_name );
210229 free (cache -> eviction_params );
@@ -398,8 +417,10 @@ static void pluginCache_parse_params(cache_t *cache,
398417
399418 // Process recognized parameters
400419 if (strcasecmp (key , "plugin" ) == 0 || strcasecmp (key , "plugin_path" ) == 0 ) {
420+ if (params -> plugin_path != NULL ) free (params -> plugin_path );
401421 params -> plugin_path = strdup (value );
402422 } else if (strcasecmp (key , "cache_name" ) == 0 ) {
423+ if (params -> cache_name != NULL ) free (params -> cache_name );
403424 params -> cache_name = strdup (value );
404425 } else if (strcasecmp (key , "print" ) == 0 ) {
405426 printf ("current parameters: plugin_path=%s\n" , params -> plugin_path );
0 commit comments