-
Notifications
You must be signed in to change notification settings - Fork 103
[Fix]: Tiny possible issues #283
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,8 @@ typedef struct LRU_Prob_params { | |
| int threshold; | ||
| } LRU_Prob_params_t; | ||
|
|
||
| static const char *DEFAULT_CACHE_PARAMS = "prob=0.5"; | ||
|
|
||
| // *********************************************************************** | ||
| // **** **** | ||
| // **** function declarations **** | ||
|
|
@@ -77,8 +79,10 @@ cache_t *LRU_Prob_init(const common_cache_params_t ccache_params, | |
| cache->eviction_params = | ||
| (LRU_Prob_params_t *)malloc(sizeof(LRU_Prob_params_t)); | ||
| LRU_Prob_params_t *params = (LRU_Prob_params_t *)(cache->eviction_params); | ||
| params->prob = 0.5; | ||
| params->q_head = NULL; | ||
| params->q_tail = NULL; | ||
|
Comment on lines
+82
to
+83
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| LRU_Prob_parse_params(cache, DEFAULT_CACHE_PARAMS); | ||
| if (cache_specific_params != NULL) { | ||
| LRU_Prob_parse_params(cache, cache_specific_params); | ||
| } | ||
|
|
@@ -169,6 +173,7 @@ static cache_obj_t *LRU_Prob_find(cache_t *cache, const request_t *req, | |
| */ | ||
| static cache_obj_t *LRU_Prob_insert(cache_t *cache, const request_t *req) { | ||
| LRU_Prob_params_t *params = (LRU_Prob_params_t *)cache->eviction_params; | ||
|
|
||
| cache_obj_t *obj = cache_insert_base(cache, req); | ||
| prepend_obj_to_head(¶ms->q_head, ¶ms->q_tail, obj); | ||
|
|
||
|
|
@@ -203,6 +208,8 @@ static cache_obj_t *LRU_Prob_to_evict(cache_t *cache, const request_t *req) { | |
| static void LRU_Prob_evict(cache_t *cache, const request_t *req) { | ||
| LRU_Prob_params_t *params = (LRU_Prob_params_t *)cache->eviction_params; | ||
| cache_obj_t *obj_to_evict = params->q_tail; | ||
| DEBUG_ASSERT(params->q_tail != NULL); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| remove_obj_from_list(¶ms->q_head, ¶ms->q_tail, obj_to_evict); | ||
| cache_remove_obj_base(cache, obj_to_evict, true); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -208,9 +208,13 @@ static cache_obj_t *SR_LRU_find(cache_t *cache, const request_t *req, | |
| params->C_demoted -= 1; | ||
| } | ||
|
|
||
| if (cache_hit_R || cache_hit_SR) { | ||
| if (cache_hit_R || (cache_hit_SR && likely(update_cache))) { | ||
| // if not update_cache obj_SR will not be updated to obj_R | ||
| DEBUG_ASSERT(obj_R != NULL); | ||
| return obj_R; | ||
| } else if (cache_hit_SR) { | ||
| // if not update_cache, directly return obj_SR | ||
| return obj_SR; | ||
|
Comment on lines
+211
to
+217
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is an important correctness fix. The previous logic would incorrectly modify the cache state (promoting an object from SR to R) even when For better readability and maintainability, you could consider restructuring it to handle each case more explicitly. if (cache_hit_R) {
DEBUG_ASSERT(obj_R != NULL);
return obj_R;
}
if (cache_hit_SR && likely(update_cache)) {
DEBUG_ASSERT(obj_R != NULL);
return obj_R;
} else if (cache_hit_SR) {
return obj_SR;
}
return NULL; |
||
| } | ||
| return NULL; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This reordering is a critical fix. The previous logic could lead to a null pointer dereference if
cache_find_basereturnedNULLandupdate_cachewasfalse, as it would attempt to accessobj->ARC.ghostbefore checking ifobjwasNULL. This change correctly handles this by checking forNULLfirst.