Fix: add cache free hook and default param free#253
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR introduces a new hook for releasing plugin resources and enhances parameter cleanup in the plugin-based cache implementation.
- Adds
cache_free_hookto the plugin API and invokes it on cache destruction - Tracks and closes the dynamic library handle for plugins
- Validates and frees previously set
plugin_pathandcache_nameparameters
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| libCacheSim/include/libCacheSim/plugin.h | Defined cache_free_hook_t in the public plugin API |
| libCacheSim/cache/eviction/plugin_cache.c | Loaded, stored, and called the new free hook; managed dlopen handle; validated/free parameters |
| example/plugin_v2/plugin_lru.cpp | Implemented cache_free_hook in the LRU plugin example |
| example/plugin_v2/README.md | Documented the new cache_free_hook in usage section |
Comments suppressed due to low confidence (2)
libCacheSim/cache/eviction/plugin_cache.c:220
- There are no existing tests to verify that the free hook is invoked and the plugin handle is closed. Consider adding unit tests that register a dummy plugin and confirm the free hook and
dlcloseare called.
if (params->cache_free_hook != NULL) params->cache_free_hook(params->data);
libCacheSim/cache/eviction/plugin_cache.c:417
- Using
cache->cache_namein the error message beforecache_nameis set can lead to a NULL dereference. Consider validating or assigning a default name before reporting errors.
if (strlen(value) == 0) {
| cache_remove_u.obj = dlsym(handle, "cache_remove_hook"); | ||
| params->cache_remove_hook = cache_remove_u.func; | ||
|
|
||
| cache_free_u.obj = dlsym(handle, "cache_free_hook"); |
There was a problem hiding this comment.
The result of dlsym for cache_free_hook isn't checked; if the symbol load fails unexpectedly, you may call a garbage pointer. Add a NULL check or error log similar to other hooks.
Suggested change
| cache_free_u.obj = dlsym(handle, "cache_free_hook"); | |
| cache_free_u.obj = dlsym(handle, "cache_free_hook"); | |
| if (cache_free_u.obj == NULL) { | |
| fprintf(stderr, "Error: Failed to load symbol 'cache_free_hook'.\n"); | |
| return -1; // or appropriate error handling | |
| } |
1a1a11a
approved these changes
Jul 10, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Subsequent PR of #252, aimed at better memory management
cahce_free_hook