Refactorings

Internally use doubly-linked list instead of singly-linked list. Also
minor fixes related to changing use to doubly-linked list.

Removed unnecessary comments.

Move list add/remove code to designated functions.
This commit is contained in:
Stephen Seo 2024-06-04 15:08:33 +09:00
parent e33b13a80b
commit 0e447dc3be
3 changed files with 55 additions and 30 deletions

View file

@ -23,13 +23,45 @@ namespace SC_AM_Internal {
Malloced::Malloced() : address(nullptr), size(0) {}
Malloced::Malloced(void *address, std::size_t size) : address(address), size(size) {}
Stats::Stats() : malloced_list(nullptr) {
void ListNode::add_to_list(ListNode *tail, Malloced *data) {
ListNode *new_node = reinterpret_cast<ListNode*>(real_malloc(sizeof(ListNode)));
new_node->next = tail;
new_node->prev = tail->prev;
tail->prev->next = new_node;
tail->prev = new_node;
new_node->data = data;
}
bool ListNode::remove_from_list(ListNode *head, void *ptr) {
ListNode *node = head;
while (node != nullptr) {
node = node->next;
if (node->data && node->data->address == ptr) {
node->prev->next = node->next;
node->next->prev = node->prev;
real_free(node->data);
real_free(node);
return true;
}
}
return false;
}
Stats::Stats() : malloced_list_head(nullptr), malloced_list_tail(nullptr) {
}
void Stats::initialize() {
malloced_list = reinterpret_cast<ListNode*>(real_malloc(sizeof(ListNode)));
malloced_list->next = nullptr;
malloced_list->data = nullptr;
malloced_list_head = reinterpret_cast<ListNode*>(real_malloc(sizeof(ListNode)));
malloced_list_tail = reinterpret_cast<ListNode*>(real_malloc(sizeof(ListNode)));
malloced_list_head->next = malloced_list_tail;
malloced_list_head->prev = nullptr;
malloced_list_tail->next = nullptr;
malloced_list_tail->prev = malloced_list_head;
malloced_list_head->data = nullptr;
malloced_list_tail->data = nullptr;
on_exit([] ([[maybe_unused]] int status, void *ptr) {
const Stats *stats = reinterpret_cast<const Stats*>(ptr);
stats->print_status();
@ -39,15 +71,10 @@ namespace SC_AM_Internal {
void *Stats::do_malloc(std::size_t size) {
void *address = real_malloc(size);
if (address != nullptr) {
auto *node = malloced_list;
while (node->next != nullptr) {
node = node->next;
}
node->next = reinterpret_cast<ListNode*>(real_malloc(sizeof(ListNode)));
node->next->next = nullptr;
node->next->data = reinterpret_cast<Malloced*>(real_malloc(sizeof(Malloced)));
node->next->data->address = address;
node->next->data->size = size;
Malloced *data = reinterpret_cast<Malloced*>(real_malloc(sizeof(Malloced)));
data->address = address;
data->size = size;
ListNode::add_to_list(malloced_list_tail, data);
}
return address;
@ -55,17 +82,8 @@ namespace SC_AM_Internal {
void Stats::do_free(void *ptr) {
if (ptr) {
auto *node = malloced_list;
decltype(node) parent_node;
while (node->next != nullptr) {
parent_node = node;
node = node->next;
if (node->data && node->data->address == ptr) {
real_free(node->data);
parent_node->next = node->next;
real_free(node);
break;
}
if(!ListNode::remove_from_list(malloced_list_head, ptr)) {
std::clog << "WARNING: Attempted free of unknown memory location!\n";
}
}
}
@ -75,11 +93,13 @@ 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;
auto *node = malloced_list_head;
while (node->next != nullptr) {
node = node->next;
std::clog << " " << node->data->address << ": size " << node->data->size << '\n';
if (node->data) {
std::clog << " " << node->data->address << ": size " << node->data->size << '\n';
}
}
}
}

View file

@ -24,16 +24,23 @@ namespace SC_AM_Internal {
};
struct ListNode {
ListNode() : next(nullptr), data(nullptr) {}
ListNode() : next(nullptr), prev(nullptr), data(nullptr) {}
static void add_to_list(ListNode *tail, Malloced *data);
/// Returns true if removed.
static bool remove_from_list(ListNode *head, void *ptr);
ListNode *next;
ListNode *prev;
Malloced *data;
};
struct Stats {
Stats();
ListNode *malloced_list;
ListNode *malloced_list_head;
ListNode *malloced_list_tail;
void initialize();

View file

@ -1,7 +1,5 @@
// Standard library includes.
#include <iostream>
/*#include <cstdlib>*/
/*#include <cstdio>*/
// Unix includes.
#include <dlfcn.h>