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:
parent
e33b13a80b
commit
0e447dc3be
3 changed files with 55 additions and 30 deletions
|
@ -23,13 +23,45 @@ namespace SC_AM_Internal {
|
||||||
Malloced::Malloced() : address(nullptr), size(0) {}
|
Malloced::Malloced() : address(nullptr), size(0) {}
|
||||||
Malloced::Malloced(void *address, std::size_t size) : address(address), size(size) {}
|
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() {
|
void Stats::initialize() {
|
||||||
malloced_list = reinterpret_cast<ListNode*>(real_malloc(sizeof(ListNode)));
|
malloced_list_head = reinterpret_cast<ListNode*>(real_malloc(sizeof(ListNode)));
|
||||||
malloced_list->next = nullptr;
|
malloced_list_tail = reinterpret_cast<ListNode*>(real_malloc(sizeof(ListNode)));
|
||||||
malloced_list->data = nullptr;
|
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) {
|
on_exit([] ([[maybe_unused]] int status, void *ptr) {
|
||||||
const Stats *stats = reinterpret_cast<const Stats*>(ptr);
|
const Stats *stats = reinterpret_cast<const Stats*>(ptr);
|
||||||
stats->print_status();
|
stats->print_status();
|
||||||
|
@ -39,15 +71,10 @@ namespace SC_AM_Internal {
|
||||||
void *Stats::do_malloc(std::size_t size) {
|
void *Stats::do_malloc(std::size_t size) {
|
||||||
void *address = real_malloc(size);
|
void *address = real_malloc(size);
|
||||||
if (address != nullptr) {
|
if (address != nullptr) {
|
||||||
auto *node = malloced_list;
|
Malloced *data = reinterpret_cast<Malloced*>(real_malloc(sizeof(Malloced)));
|
||||||
while (node->next != nullptr) {
|
data->address = address;
|
||||||
node = node->next;
|
data->size = size;
|
||||||
}
|
ListNode::add_to_list(malloced_list_tail, data);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return address;
|
return address;
|
||||||
|
@ -55,17 +82,8 @@ namespace SC_AM_Internal {
|
||||||
|
|
||||||
void Stats::do_free(void *ptr) {
|
void Stats::do_free(void *ptr) {
|
||||||
if (ptr) {
|
if (ptr) {
|
||||||
auto *node = malloced_list;
|
if(!ListNode::remove_from_list(malloced_list_head, ptr)) {
|
||||||
decltype(node) parent_node;
|
std::clog << "WARNING: Attempted free of unknown memory location!\n";
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -75,13 +93,15 @@ 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;
|
auto *node = malloced_list_head;
|
||||||
while (node->next != nullptr) {
|
while (node->next != nullptr) {
|
||||||
node = node->next;
|
node = node->next;
|
||||||
|
|
||||||
|
if (node->data) {
|
||||||
std::clog << " " << node->data->address << ": size " << node->data->size << '\n';
|
std::clog << " " << node->data->address << ": size " << node->data->size << '\n';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// vim: et sw=2 ts=2 sts=2
|
// vim: et sw=2 ts=2 sts=2
|
||||||
|
|
|
@ -24,16 +24,23 @@ namespace SC_AM_Internal {
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ListNode {
|
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 *next;
|
||||||
|
ListNode *prev;
|
||||||
Malloced *data;
|
Malloced *data;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
struct Stats {
|
struct Stats {
|
||||||
Stats();
|
Stats();
|
||||||
|
|
||||||
ListNode *malloced_list;
|
ListNode *malloced_list_head;
|
||||||
|
ListNode *malloced_list_tail;
|
||||||
|
|
||||||
void initialize();
|
void initialize();
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
// Standard library includes.
|
// Standard library includes.
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
/*#include <cstdlib>*/
|
|
||||||
/*#include <cstdio>*/
|
|
||||||
|
|
||||||
// Unix includes.
|
// Unix includes.
|
||||||
#include <dlfcn.h>
|
#include <dlfcn.h>
|
||||||
|
|
Loading…
Reference in a new issue