Skip to content

Commit 5f47024

Browse files
authored
Refactor: Enhance plugin cache structure and parameter parsing (#252)
- Reintroduced necessary includes and restructured the plugin cache parameters to include a cache name. - Updated the default cache parameters to include a cache name. - Improved the parameter parsing function to handle the new cache name parameter and ensure proper memory management. - Adjusted cache initialization to set the cache name based on the plugin path if not specified. - Added error handling for missing parameters during parsing.
1 parent efd02b4 commit 5f47024

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

libCacheSim/cache/eviction/plugin_cache.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,12 @@ typedef struct pluginCache_params {
5252
cache_miss_hook_t cache_miss_hook; ///< Cache miss handler function
5353
cache_eviction_hook_t cache_eviction_hook; ///< Eviction decision function
5454
cache_remove_hook_t cache_remove_hook; ///< Object removal handler function
55+
char *cache_name;
5556
} pluginCache_params_t;
5657

5758
/** @brief Default plugin parameters if none specified */
5859
static const char *DEFAULT_CACHE_PARAMS =
59-
"plugin_path=./libplugin_lru_hooks.so";
60+
"plugin_path=./libplugin_lru_hooks.so,cache_name=pluginCache";
6061

6162
// ***********************************************************************
6263
// **** ****
@@ -177,9 +178,19 @@ cache_t *pluginCache_init(const common_cache_params_t ccache_params,
177178
// Initialize the plugin with cache parameters
178179
params->data = params->cache_init_hook(ccache_params);
179180

180-
// Set cache name to include plugin path for identification
181-
snprintf(cache->cache_name, CACHE_NAME_ARRAY_LEN, "pluginCache-%s",
182-
params->plugin_path);
181+
if (strcmp(params->cache_name, "pluginCache") != 0) {
182+
snprintf(cache->cache_name, CACHE_NAME_ARRAY_LEN, "%s", params->cache_name);
183+
} else {
184+
// Set cache name to include plugin name for identification
185+
char *plugin_name = strrchr(params->plugin_path, '/');
186+
if (plugin_name == NULL) {
187+
plugin_name = params->plugin_path;
188+
} else {
189+
plugin_name++;
190+
}
191+
snprintf(cache->cache_name, CACHE_NAME_ARRAY_LEN, "pluginCache-%s",
192+
plugin_name);
193+
}
183194

184195
return cache;
185196
}
@@ -193,6 +204,9 @@ cache_t *pluginCache_init(const common_cache_params_t ccache_params,
193204
* @param cache Pointer to the cache instance to free
194205
*/
195206
static void pluginCache_free(cache_t *cache) {
207+
pluginCache_params_t *params = (pluginCache_params_t *)cache->eviction_params;
208+
if (params->plugin_path != NULL) free(params->plugin_path);
209+
if (params->cache_name != NULL) free(params->cache_name);
196210
free(cache->eviction_params);
197211
cache_struct_free(cache);
198212
}
@@ -385,6 +399,8 @@ static void pluginCache_parse_params(cache_t *cache,
385399
// Process recognized parameters
386400
if (strcasecmp(key, "plugin") == 0 || strcasecmp(key, "plugin_path") == 0) {
387401
params->plugin_path = strdup(value);
402+
} else if (strcasecmp(key, "cache_name") == 0) {
403+
params->cache_name = strdup(value);
388404
} else if (strcasecmp(key, "print") == 0) {
389405
printf("current parameters: plugin_path=%s\n", params->plugin_path);
390406
exit(0);

0 commit comments

Comments
 (0)