From 882732a3ad8bceab94e928f77958f8b7c2bba5e6 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Wed, 26 Jun 2024 14:01:51 +0900 Subject: [PATCH] Add ids to allocated memory to be printed later --- src/another_memcheck.cc | 28 +++++++++++++++++++++++----- src/another_memcheck.h | 5 ++++- src/another_memcheck_external.cc | 10 ++++++---- 3 files changed, 33 insertions(+), 10 deletions(-) diff --git a/src/another_memcheck.cc b/src/another_memcheck.cc index e574ea5..448141b 100644 --- a/src/another_memcheck.cc +++ b/src/another_memcheck.cc @@ -10,6 +10,7 @@ namespace SC_AM_Internal { Stats *stats = nullptr; int is_env_status = 0; + unsigned long long Malloced::count = 0; Stats *get_init_stats() { Stats *stats = reinterpret_cast( @@ -25,8 +26,8 @@ namespace SC_AM_Internal { void *(*real_calloc)(std::size_t, std::size_t) = nullptr; void (*real_free)(void*) = nullptr; - Malloced::Malloced() : address(nullptr), size(0) {} - Malloced::Malloced(void *address, std::size_t size) : address(address), size(size) {} + 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) { ListNode *new_node = reinterpret_cast(real_malloc(sizeof(ListNode))); @@ -43,6 +44,9 @@ namespace SC_AM_Internal { while (node != nullptr) { node = node->next; 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->next->prev = node->prev; real_free(node->data); @@ -92,6 +96,10 @@ namespace SC_AM_Internal { Malloced *data = reinterpret_cast(real_malloc(sizeof(Malloced))); data->address = address; 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); } @@ -110,6 +118,10 @@ namespace SC_AM_Internal { Malloced *data = reinterpret_cast(real_malloc(sizeof(Malloced))); data->address = address; 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); } @@ -117,19 +129,23 @@ namespace SC_AM_Internal { return address; } - void Stats::do_free(void *ptr) { + bool Stats::do_free(void *ptr) { + bool result = false; if(int ret = pthread_mutex_lock(&pthread_mutex); ret == EINVAL) { std::clog << "ERROR: pthread mutex not properly initialized!\n"; - return; + return result; } if (ptr) { if(!ListNode::remove_from_list(malloced_list_head, ptr)) { std::clog << "WARNING: Attempted free of unknown memory location!\n"; + } else { + result = true; } } pthread_mutex_unlock(&pthread_mutex); + return result; } void Stats::print_status() const { @@ -142,7 +158,9 @@ namespace SC_AM_Internal { 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 + << " id " << node->data->id << '\n'; } } } diff --git a/src/another_memcheck.h b/src/another_memcheck.h index a5ea2b9..aae9a9e 100644 --- a/src/another_memcheck.h +++ b/src/another_memcheck.h @@ -29,6 +29,8 @@ namespace SC_AM_Internal { void* address; std::size_t size; + unsigned long long id; + static unsigned long long count; }; struct ListNode { @@ -56,7 +58,8 @@ namespace SC_AM_Internal { void *do_malloc(std::size_t size); void *do_calloc(std::size_t n, std::size_t size); - void do_free(void *ptr); + /// true on success. + bool do_free(void *ptr); void print_status() const; }; diff --git a/src/another_memcheck_external.cc b/src/another_memcheck_external.cc index 5e371df..5bed1b8 100644 --- a/src/another_memcheck_external.cc +++ b/src/another_memcheck_external.cc @@ -18,7 +18,7 @@ extern "C" { } if (SC_AM_Internal::is_env_status == SC_AM_Internal::ANOTHER_MEMCHECK_QUIET_NOT_EXISTS) { - std::clog << "attempting to malloc size: " << size << "...\n"; + std::clog << "attempting to malloc size: " << 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) { - std::clog << "attempting to calloc size: " << size << "...\n"; + std::clog << "attempting to calloc size: " << size; } return SC_AM_Internal::stats->do_calloc(n, size); @@ -51,10 +51,12 @@ extern "C" { } if (SC_AM_Internal::is_env_status == SC_AM_Internal::ANOTHER_MEMCHECK_QUIET_NOT_EXISTS) { - std::clog << "attempting to free...\n"; + std::clog << "attempting to free"; } - return SC_AM_Internal::stats->do_free(ptr); + if (!SC_AM_Internal::stats->do_free(ptr)) { + std::clog << std::endl; + } } }