Skip to content

Commit b8d2b68

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

4 files changed

Lines changed: 34 additions & 1 deletion

File tree

example/plugin_v2/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ The plugin implements these required hook functions:
4343
- `cache_miss_hook()` - Handle cache misses (insert new object)
4444
- `cache_eviction_hook()` - Evict least recently used object and return its ID
4545
- `cache_remove_hook()` - Remove specific object from cache
46+
- `cache_free_hook()` - Clean up and free the LRU cache data structure
4647

4748
## Usage
4849

example/plugin_v2/plugin_lru.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,11 @@ void cache_remove_hook(void *data, const obj_id_t obj_id) {
134134
lru_cache->cache_remove(obj_id);
135135
}
136136

137-
} // extern "C"
137+
// implement the cache free hook
138+
void cache_free_hook(void *data) {
139+
// free the LRU cache (destructor handles all cleanup)
140+
StandaloneLRU *lru_cache = (StandaloneLRU *)data;
141+
delete lru_cache;
142+
}
143+
144+
} // extern "C"

libCacheSim/cache/eviction/plugin_cache.c

Lines changed: 14 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
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; ///< Cache 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,9 @@ 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+
cache_free_u.obj = dlsym(handle, "cache_free_hook");
185+
params->cache_free_hook = cache_free_u.func;
186+
178187
// Initialize the plugin with cache parameters
179188
params->data = params->cache_init_hook(ccache_params);
180189

@@ -205,6 +214,9 @@ cache_t *pluginCache_init(const common_cache_params_t ccache_params,
205214
*/
206215
static void pluginCache_free(cache_t *cache) {
207216
pluginCache_params_t *params = (pluginCache_params_t *)cache->eviction_params;
217+
218+
if (params->cache_free_hook != NULL) params->cache_free_hook(params->data);
219+
208220
if (params->plugin_path != NULL) free(params->plugin_path);
209221
if (params->cache_name != NULL) free(params->cache_name);
210222
free(cache->eviction_params);
@@ -398,8 +410,10 @@ static void pluginCache_parse_params(cache_t *cache,
398410

399411
// Process recognized parameters
400412
if (strcasecmp(key, "plugin") == 0 || strcasecmp(key, "plugin_path") == 0) {
413+
if (params->plugin_path != NULL) free(params->plugin_path);
401414
params->plugin_path = strdup(value);
402415
} else if (strcasecmp(key, "cache_name") == 0) {
416+
if (params->cache_name != NULL) free(params->cache_name);
403417
params->cache_name = strdup(value);
404418
} else if (strcasecmp(key, "print") == 0) {
405419
printf("current parameters: plugin_path=%s\n", params->plugin_path);

libCacheSim/include/libCacheSim/plugin.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ cache_t *create_cache_external(const char *const cache_alg_name,
109109
* - cache_miss_hook: Handle cache miss events
110110
* - cache_eviction_hook: Determine which object to evict
111111
* - cache_remove_hook: Clean up when objects are removed
112+
* - cache_free_hook: Free plugin resources
112113
*
113114
* @see example/plugin_v2/plugin_lru.c for a complete implementation example
114115
* @{
@@ -186,6 +187,16 @@ typedef obj_id_t (*cache_eviction_hook_t)(void *data, const request_t *req);
186187
*/
187188
typedef void (*cache_remove_hook_t)(void *data, const obj_id_t obj_id);
188189

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

191202
#ifdef __cplusplus

0 commit comments

Comments
 (0)