Compare commits

..

No commits in common. "3169bc0458610fd92aeb2330b112b4842cf2985b" and "8f1bf7a5a82d1b70e800a08e9d231caf9031f891" have entirely different histories.

4 changed files with 10 additions and 38 deletions

View file

@ -1,10 +1,5 @@
# Changelog # Changelog
## Version 2.3
Added ids to each chunk of allocated memory to make it easier to keep track of
which allocated memory refers to which unfreed memory.
## Version 2.2 ## Version 2.2
Minor refactoring: remove unused constexpr declarations in header. Minor refactoring: remove unused constexpr declarations in header.

View file

@ -10,7 +10,6 @@
namespace SC_AM_Internal { namespace SC_AM_Internal {
Stats *stats = nullptr; Stats *stats = nullptr;
int is_env_status = 0; int is_env_status = 0;
unsigned long long Malloced::count = 0;
Stats *get_init_stats() { Stats *get_init_stats() {
Stats *stats = reinterpret_cast<SC_AM_Internal::Stats*>( Stats *stats = reinterpret_cast<SC_AM_Internal::Stats*>(
@ -26,8 +25,8 @@ namespace SC_AM_Internal {
void *(*real_calloc)(std::size_t, std::size_t) = nullptr; void *(*real_calloc)(std::size_t, std::size_t) = nullptr;
void (*real_free)(void*) = nullptr; void (*real_free)(void*) = nullptr;
Malloced::Malloced() : address(nullptr), size(0), id(0) {} Malloced::Malloced() : address(nullptr), size(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) {}
void ListNode::add_to_list(ListNode *tail, Malloced *data) { void ListNode::add_to_list(ListNode *tail, Malloced *data) {
ListNode *new_node = reinterpret_cast<ListNode*>(real_malloc(sizeof(ListNode))); ListNode *new_node = reinterpret_cast<ListNode*>(real_malloc(sizeof(ListNode)));
@ -44,9 +43,6 @@ namespace SC_AM_Internal {
while (node != nullptr) { while (node != nullptr) {
node = node->next; node = node->next;
if (node->data && node->data->address == ptr) { if (node->data && node->data->address == ptr) {
if (is_env_status == ANOTHER_MEMCHECK_QUIET_NOT_EXISTS) {
std::clog << " id: " << node->data->id << std::endl;
}
node->prev->next = node->next; node->prev->next = node->next;
node->next->prev = node->prev; node->next->prev = node->prev;
real_free(node->data); real_free(node->data);
@ -96,10 +92,6 @@ namespace SC_AM_Internal {
Malloced *data = reinterpret_cast<Malloced*>(real_malloc(sizeof(Malloced))); Malloced *data = reinterpret_cast<Malloced*>(real_malloc(sizeof(Malloced)));
data->address = address; data->address = address;
data->size = size; data->size = size;
data->id = data->count++;
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_tail, data);
} }
@ -118,10 +110,6 @@ namespace SC_AM_Internal {
Malloced *data = reinterpret_cast<Malloced*>(real_malloc(sizeof(Malloced))); Malloced *data = reinterpret_cast<Malloced*>(real_malloc(sizeof(Malloced)));
data->address = address; data->address = address;
data->size = n * size; data->size = n * size;
data->id = data->count++;
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_tail, data);
} }
@ -129,23 +117,19 @@ namespace SC_AM_Internal {
return address; return address;
} }
bool Stats::do_free(void *ptr) { void Stats::do_free(void *ptr) {
bool result = false;
if(int ret = pthread_mutex_lock(&pthread_mutex); ret == EINVAL) { if(int ret = pthread_mutex_lock(&pthread_mutex); ret == EINVAL) {
std::clog << "ERROR: pthread mutex not properly initialized!\n"; std::clog << "ERROR: pthread mutex not properly initialized!\n";
return result; return;
} }
if (ptr) { if (ptr) {
if(!ListNode::remove_from_list(malloced_list_head, ptr)) { if(!ListNode::remove_from_list(malloced_list_head, ptr)) {
std::clog << "WARNING: Attempted free of unknown memory location!\n"; std::clog << "WARNING: Attempted free of unknown memory location!\n";
} else {
result = true;
} }
} }
pthread_mutex_unlock(&pthread_mutex); pthread_mutex_unlock(&pthread_mutex);
return result;
} }
void Stats::print_status() const { void Stats::print_status() const {
@ -158,9 +142,7 @@ namespace SC_AM_Internal {
node = node->next; node = node->next;
if (node->data) { if (node->data) {
std::clog << " " << node->data->address std::clog << " " << node->data->address << ": size " << node->data->size << '\n';
<< ": size " << node->data->size
<< " id " << node->data->id << '\n';
} }
} }
} }

View file

@ -29,8 +29,6 @@ namespace SC_AM_Internal {
void* address; void* address;
std::size_t size; std::size_t size;
unsigned long long id;
static unsigned long long count;
}; };
struct ListNode { struct ListNode {
@ -58,8 +56,7 @@ namespace SC_AM_Internal {
void *do_malloc(std::size_t size); void *do_malloc(std::size_t size);
void *do_calloc(std::size_t n, std::size_t size); void *do_calloc(std::size_t n, std::size_t size);
/// true on success. void do_free(void *ptr);
bool do_free(void *ptr);
void print_status() const; void print_status() const;
}; };

View file

@ -18,7 +18,7 @@ extern "C" {
} }
if (SC_AM_Internal::is_env_status == SC_AM_Internal::ANOTHER_MEMCHECK_QUIET_NOT_EXISTS) { if (SC_AM_Internal::is_env_status == SC_AM_Internal::ANOTHER_MEMCHECK_QUIET_NOT_EXISTS) {
std::clog << "attempting to malloc size: " << size; std::clog << "attempting to malloc size: " << size << "...\n";
} }
return SC_AM_Internal::stats->do_malloc(size); return SC_AM_Internal::stats->do_malloc(size);
@ -35,7 +35,7 @@ extern "C" {
} }
if (SC_AM_Internal::is_env_status == SC_AM_Internal::ANOTHER_MEMCHECK_QUIET_NOT_EXISTS) { if (SC_AM_Internal::is_env_status == SC_AM_Internal::ANOTHER_MEMCHECK_QUIET_NOT_EXISTS) {
std::clog << "attempting to calloc size: " << size; std::clog << "attempting to calloc size: " << size << "...\n";
} }
return SC_AM_Internal::stats->do_calloc(n, size); return SC_AM_Internal::stats->do_calloc(n, size);
@ -51,12 +51,10 @@ extern "C" {
} }
if (SC_AM_Internal::is_env_status == SC_AM_Internal::ANOTHER_MEMCHECK_QUIET_NOT_EXISTS) { if (SC_AM_Internal::is_env_status == SC_AM_Internal::ANOTHER_MEMCHECK_QUIET_NOT_EXISTS) {
std::clog << "attempting to free"; std::clog << "attempting to free...\n";
} }
if (!SC_AM_Internal::stats->do_free(ptr)) { return SC_AM_Internal::stats->do_free(ptr);
std::clog << std::endl;
}
} }
} }