Skip to content

Commit a0386ae

Browse files
committed
Add optional cache free hook
1 parent 5f47024 commit a0386ae

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

libCacheSim/cache/eviction/plugin_cache.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
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
*/
206219
static 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);

libCacheSim/include/libCacheSim/plugin.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,16 @@ typedef obj_id_t (*cache_eviction_hook_t)(void *data, const request_t *req);
186186
*/
187187
typedef void (*cache_remove_hook_t)(void *data, const obj_id_t obj_id);
188188

189+
/**
190+
* @brief Cache free hook function type
191+
*
192+
* Optional cleanup function called when the cache is being destroyed.
193+
* The plugin should free any resources allocated in cache_init_hook.
194+
*
195+
* @param data Pointer to plugin's internal data (from cache_init_hook)
196+
*/
197+
typedef void (*cache_free_hook_t)(void *data);
198+
189199
/** @} */ // end of v2_plugin_api group
190200

191201
#ifdef __cplusplus

0 commit comments

Comments
 (0)