From df0d861bfcc4a85577d60fb318583f44ed6f76b1 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Wed, 13 Nov 2024 14:31:45 +0900 Subject: [PATCH 1/2] Add malloc'd info to head of list, not tail Malloc'd memory tends to be deallocated in reverse order of allocation, so it would make more sense to push new entries to the head, since removals traverse the list from the head-to-tail. --- src/another_memcheck.cc | 34 +++++++++++++++++----------------- src/another_memcheck.h | 4 ++-- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/another_memcheck.cc b/src/another_memcheck.cc index a0f5cd8..50204cf 100644 --- a/src/another_memcheck.cc +++ b/src/another_memcheck.cc @@ -35,21 +35,21 @@ namespace SC_AM_Internal { Malloced::Malloced() : address(nullptr), size(0), 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(real_malloc(sizeof(ListNode))); - new_node->next = tail; - new_node->prev = tail->prev; - tail->prev->next = new_node; - tail->prev = new_node; + new_node->prev = head; + new_node->next = head->next; + head->next->prev = new_node; + head->next = new_node; new_node->data = data; } - void ListNode::add_to_list(ListNode *tail, ListNode *node) { - node->next = tail; - node->prev = tail->prev; - tail->prev->next = node; - tail->prev = node; + void ListNode::add_to_list(ListNode *head, ListNode *node) { + node->prev = head; + node->next = head->next; + head->next->prev = node; + head->next = node; } bool ListNode::remove_from_list(ListNode *head, @@ -117,7 +117,7 @@ namespace SC_AM_Internal { if (is_env_status == ANOTHER_MEMCHECK_QUIET_NOT_EXISTS) { 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(); @@ -136,7 +136,7 @@ namespace SC_AM_Internal { if (is_env_status == ANOTHER_MEMCHECK_QUIET_NOT_EXISTS) { 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(); @@ -161,7 +161,7 @@ namespace SC_AM_Internal { if (is_env_status == ANOTHER_MEMCHECK_QUIET_NOT_EXISTS) { 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; } else { Malloced *data = reinterpret_cast(real_malloc(sizeof(Malloced))); @@ -171,7 +171,7 @@ namespace SC_AM_Internal { if (is_env_status == ANOTHER_MEMCHECK_QUIET_NOT_EXISTS) { 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 // this function only runs at the end of program execution. std::clog << "List of unfreed memory:\n"; - auto *node = malloced_list_head; - while (node->next != nullptr) { - node = node->next; + auto *node = malloced_list_tail; + while (node->prev != nullptr) { + node = node->prev; if (node->data) { std::clog << " " << node->data->address diff --git a/src/another_memcheck.h b/src/another_memcheck.h index a4bebc3..8209f6e 100644 --- a/src/another_memcheck.h +++ b/src/another_memcheck.h @@ -35,8 +35,8 @@ namespace SC_AM_Internal { struct ListNode { ListNode() : next(nullptr), prev(nullptr), data(nullptr) {} - static void add_to_list(ListNode *tail, Malloced *data); - static void add_to_list(ListNode *tail, ListNode *node); + static void add_to_list(ListNode *head, Malloced *data); + static void add_to_list(ListNode *head, ListNode *node); /// Returns true if removed. static bool remove_from_list(ListNode *head, void *ptr, -- 2.47.0 From 5417870cb57fcd9c106e734376aa645d90d095ac Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Wed, 13 Nov 2024 15:47:52 +0900 Subject: [PATCH 2/2] Update Changelog.md --- Changelog.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Changelog.md b/Changelog.md index f307edd..03ae886 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,5 +1,9 @@ # Changelog +## Upcoming Changes + +Internal refactorings. + ## Version 2.9 Add checking/stat-printing of when NULL pointers are free'd. -- 2.47.0