Skip to content

move cache_obj to header file#201

Merged
1a1a11a merged 4 commits intodevelopfrom
1a1a11a/cacheobj
Jun 20, 2025
Merged

move cache_obj to header file#201
1a1a11a merged 4 commits intodevelopfrom
1a1a11a/cacheobj

Conversation

@1a1a11a
Copy link
Copy Markdown
Owner

@1a1a11a 1a1a11a commented Jun 19, 2025

#200 @haochengxia take a look?

@1a1a11a 1a1a11a requested review from Copilot and haochengxia June 19, 2025 20:42
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR moves the implementations of cache_obj functions into the header file as static inline functions and removes the separate source file. Key changes include:

  • Moving function implementations from libCacheSim/cache/cacheObj.c into libCacheSim/include/libCacheSim/cacheObj.h
  • Adjusting internal function formatting and enum definition style
  • Updating the CMakeLists.txt to no longer include cacheObj.c in the cachelib target

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
libCacheSim/include/libCacheSim/cacheObj.h Moved inline definitions for cache_obj functions and refactored style
libCacheSim/cache/cacheObj.c Removed file with inlined function definitions
libCacheSim/cache/CMakeLists.txt Updated target sources to match removal of cacheObj.c

Comment thread libCacheSim/include/libCacheSim/cacheObj.h Outdated
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@1a1a11a
Copy link
Copy Markdown
Owner Author

1a1a11a commented Jun 19, 2025

@haochengxia approve this?

cache_obj_t *cache_obj);
static inline void move_obj_to_head(cache_obj_t **head, cache_obj_t **tail,
cache_obj_t *cache_obj) {
DEBUG_ASSERT(head != NULL);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! One tiny issue, for crucial check like head != NULL, in other places we used assert instead of DEBUG_ASSERT. Should we unify it?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updating

@sonarqubecloud
Copy link
Copy Markdown

Please retry analysis of this Pull-Request directly on SonarQube Cloud

Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Critical Assertions Replaced with Debug Ones

The commit incorrectly changed crucial assert() calls to DEBUG_ASSERT() in the move_obj_to_tail, prepend_obj_to_head, and prev_obj_in_slist functions. This contradicts the PR discussion's intent to use assert() for critical checks. Since DEBUG_ASSERT() is typically disabled in release builds, these essential null pointer, invariant, and precondition validations are removed in production, risking crashes or incorrect behavior.

libCacheSim/include/libCacheSim/cacheObj.h#L281-L435

// the list only has one element
DEBUG_ASSERT(cache_obj == *head);
DEBUG_ASSERT(cache_obj->queue.next == NULL);
DEBUG_ASSERT(cache_obj->queue.prev == NULL);
return;
}
if (cache_obj == *head) {
// change head
*head = cache_obj->queue.next;
cache_obj->queue.next->queue.prev = NULL;
// move to tail
(*tail)->queue.next = cache_obj;
cache_obj->queue.next = NULL;
cache_obj->queue.prev = *tail;
*tail = cache_obj;
return;
}
if (cache_obj == *tail) {
return;
}
// bridge list_prev and next
cache_obj->queue.prev->queue.next = cache_obj->queue.next;
cache_obj->queue.next->queue.prev = cache_obj->queue.prev;
// handle current tail
(*tail)->queue.next = cache_obj;
// handle this moving object
cache_obj->queue.next = NULL;
cache_obj->queue.prev = *tail;
// handle tail
*tail = cache_obj;
}
/**
* move an object to the head of the doubly linked list
* @param head
* @param tail
* @param cache_obj
*/
static inline void move_obj_to_head(cache_obj_t **head, cache_obj_t **tail,
cache_obj_t *cache_obj) {
DEBUG_ASSERT(head != NULL);
if (tail != NULL && *head == *tail) {
// the list only has one element
DEBUG_ASSERT(cache_obj == *head);
DEBUG_ASSERT(cache_obj->queue.next == NULL);
DEBUG_ASSERT(cache_obj->queue.prev == NULL);
return;
}
if (cache_obj == *head) {
// already at head
return;
}
if (tail != NULL && cache_obj == *tail) {
// change tail
cache_obj->queue.prev->queue.next = cache_obj->queue.next;
*tail = cache_obj->queue.prev;
// move to head
(*head)->queue.prev = cache_obj;
cache_obj->queue.prev = NULL;
cache_obj->queue.next = *head;
*head = cache_obj;
return;
}
// bridge list_prev and next
cache_obj->queue.prev->queue.next = cache_obj->queue.next;
cache_obj->queue.next->queue.prev = cache_obj->queue.prev;
// handle current head
(*head)->queue.prev = cache_obj;
// handle this moving object
cache_obj->queue.prev = NULL;
cache_obj->queue.next = *head;
// handle head
*head = cache_obj;
}
/**
* prepend the object to the head of the doubly linked list
* the object is not in the list, otherwise, use move_obj_to_head
* @param head
* @param tail
* @param cache_obj
*/
static inline void prepend_obj_to_head(cache_obj_t **head, cache_obj_t **tail,
cache_obj_t *cache_obj) {
DEBUG_ASSERT(head != NULL);
cache_obj->queue.prev = NULL;
cache_obj->queue.next = *head;
if (tail != NULL && *tail == NULL) {
// the list is empty
DEBUG_ASSERT(*head == NULL);
*tail = cache_obj;
}
if (*head != NULL) {
// the list has at least one element
(*head)->queue.prev = cache_obj;
}
*head = cache_obj;
}
/**
* append the object to the tail of the doubly linked list
* the object is not in the list, otherwise, use move_obj_to_tail
* @param head
* @param tail
* @param cache_obj
*/
static inline void append_obj_to_tail(cache_obj_t **head, cache_obj_t **tail,
cache_obj_t *cache_obj) {
cache_obj->queue.next = NULL;
cache_obj->queue.prev = *tail;
if (head != NULL && *head == NULL) {
// the list is empty
DEBUG_ASSERT(*tail == NULL);
*head = cache_obj;
}
if (*tail != NULL) {
// the list has at least one element
(*tail)->queue.next = cache_obj;
}
*tail = cache_obj;
}
/**
* the cache_obj has built-in a doubly list, in the case the list is used as
* a singly list (list_prev is not used, next is used)
* so this function finds the list_prev element in the list
*
* NOTE: this is an expensive op
* @param head
* @param cache_obj
* @return
*/
static inline cache_obj_t *prev_obj_in_slist(cache_obj_t *head,
cache_obj_t *cache_obj) {
DEBUG_ASSERT(head != cache_obj);

Fix in Cursor


BugBot free trial expires on July 22, 2025
You have used $0.00 of your $20.00 spend limit so far. Manage your spend limit in the Cursor dashboard.

Was this report helpful? Give feedback by reacting with 👍 or 👎

@1a1a11a 1a1a11a merged commit f320500 into develop Jun 20, 2025
10 checks passed
@1a1a11a 1a1a11a deleted the 1a1a11a/cacheobj branch June 20, 2025 00:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants