Minor refactoring: new malloc'd entries to head of list #2

Merged
stephenseo merged 2 commits from dev into main 2024-11-14 01:27:49 +00:00
2 changed files with 19 additions and 19 deletions
Showing only changes of commit df0d861bfc - Show all commits

View file

@ -35,21 +35,21 @@ namespace SC_AM_Internal {
Malloced::Malloced() : address(nullptr), size(0), id(0) {} Malloced::Malloced() : address(nullptr), size(0), id(0) {}
Malloced::Malloced(void *address, std::size_t size) : address(address), size(size), id(0) {} Malloced::Malloced(void *address, std::size_t size) : address(address), size(size), id(0) {}
void ListNode::add_to_list(ListNode *tail, Malloced *data) { void ListNode::add_to_list(ListNode *head, Malloced *data) {
ListNode *new_node = reinterpret_cast<ListNode*>(real_malloc(sizeof(ListNode))); ListNode *new_node = reinterpret_cast<ListNode*>(real_malloc(sizeof(ListNode)));
new_node->next = tail; new_node->prev = head;
new_node->prev = tail->prev; new_node->next = head->next;
tail->prev->next = new_node; head->next->prev = new_node;
tail->prev = new_node; head->next = new_node;
new_node->data = data; new_node->data = data;
} }
void ListNode::add_to_list(ListNode *tail, ListNode *node) { void ListNode::add_to_list(ListNode *head, ListNode *node) {
node->next = tail; node->prev = head;
node->prev = tail->prev; node->next = head->next;
tail->prev->next = node; head->next->prev = node;
tail->prev = node; head->next = node;
} }
bool ListNode::remove_from_list(ListNode *head, bool ListNode::remove_from_list(ListNode *head,
@ -117,7 +117,7 @@ namespace SC_AM_Internal {
if (is_env_status == ANOTHER_MEMCHECK_QUIET_NOT_EXISTS) { if (is_env_status == ANOTHER_MEMCHECK_QUIET_NOT_EXISTS) {
std::clog << " id: " << data->id << std::endl; std::clog << " id: " << data->id << std::endl;
} }
ListNode::add_to_list(malloced_list_tail, data); ListNode::add_to_list(malloced_list_head, data);
} }
((std::recursive_mutex*)recursive_mutex)->unlock(); ((std::recursive_mutex*)recursive_mutex)->unlock();
@ -136,7 +136,7 @@ namespace SC_AM_Internal {
if (is_env_status == ANOTHER_MEMCHECK_QUIET_NOT_EXISTS) { if (is_env_status == ANOTHER_MEMCHECK_QUIET_NOT_EXISTS) {
std::clog << " id: " << data->id << std::endl; std::clog << " id: " << data->id << std::endl;
} }
ListNode::add_to_list(malloced_list_tail, data); ListNode::add_to_list(malloced_list_head, data);
} }
((std::recursive_mutex*)recursive_mutex)->unlock(); ((std::recursive_mutex*)recursive_mutex)->unlock();
@ -161,7 +161,7 @@ namespace SC_AM_Internal {
if (is_env_status == ANOTHER_MEMCHECK_QUIET_NOT_EXISTS) { if (is_env_status == ANOTHER_MEMCHECK_QUIET_NOT_EXISTS) {
std::clog << ", adding id: " << deferred_node->data->id << std::endl; std::clog << ", adding id: " << deferred_node->data->id << std::endl;
} }
ListNode::add_to_list(malloced_list_tail, deferred_node); ListNode::add_to_list(malloced_list_head, deferred_node);
deferred_node = nullptr; deferred_node = nullptr;
} else { } else {
Malloced *data = reinterpret_cast<Malloced*>(real_malloc(sizeof(Malloced))); Malloced *data = reinterpret_cast<Malloced*>(real_malloc(sizeof(Malloced)));
@ -171,7 +171,7 @@ namespace SC_AM_Internal {
if (is_env_status == ANOTHER_MEMCHECK_QUIET_NOT_EXISTS) { if (is_env_status == ANOTHER_MEMCHECK_QUIET_NOT_EXISTS) {
std::clog << ", adding id: " << data->id << std::endl; std::clog << ", adding id: " << data->id << std::endl;
} }
ListNode::add_to_list(malloced_list_tail, data); ListNode::add_to_list(malloced_list_head, data);
} }
} }
@ -204,9 +204,9 @@ namespace SC_AM_Internal {
// it is expected for the OS to reclaim memory from stopped processes and // it is expected for the OS to reclaim memory from stopped processes and
// this function only runs at the end of program execution. // this function only runs at the end of program execution.
std::clog << "List of unfreed memory:\n"; std::clog << "List of unfreed memory:\n";
auto *node = malloced_list_head; auto *node = malloced_list_tail;
while (node->next != nullptr) { while (node->prev != nullptr) {
node = node->next; node = node->prev;
if (node->data) { if (node->data) {
std::clog << " " << node->data->address std::clog << " " << node->data->address

View file

@ -35,8 +35,8 @@ namespace SC_AM_Internal {
struct ListNode { struct ListNode {
ListNode() : next(nullptr), prev(nullptr), data(nullptr) {} ListNode() : next(nullptr), prev(nullptr), data(nullptr) {}
static void add_to_list(ListNode *tail, Malloced *data); static void add_to_list(ListNode *head, Malloced *data);
static void add_to_list(ListNode *tail, ListNode *node); static void add_to_list(ListNode *head, ListNode *node);
/// Returns true if removed. /// Returns true if removed.
static bool remove_from_list(ListNode *head, static bool remove_from_list(ListNode *head,
void *ptr, void *ptr,