Add ids to allocated memory to be printed later
This commit is contained in:
parent
8f1bf7a5a8
commit
882732a3ad
3 changed files with 33 additions and 10 deletions
|
@ -10,6 +10,7 @@
|
||||||
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*>(
|
||||||
|
@ -25,8 +26,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) {}
|
Malloced::Malloced() : address(nullptr), size(0), id(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), id(0) {}
|
||||||
|
|
||||||
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)));
|
||||||
|
@ -43,6 +44,9 @@ 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);
|
||||||
|
@ -92,6 +96,10 @@ 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -110,6 +118,10 @@ 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -117,19 +129,23 @@ namespace SC_AM_Internal {
|
||||||
return address;
|
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) {
|
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;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
||||||
|
@ -142,7 +158,9 @@ namespace SC_AM_Internal {
|
||||||
node = node->next;
|
node = node->next;
|
||||||
|
|
||||||
if (node->data) {
|
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';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,6 +29,8 @@ 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 {
|
||||||
|
@ -56,7 +58,8 @@ 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);
|
||||||
void do_free(void *ptr);
|
/// true on success.
|
||||||
|
bool do_free(void *ptr);
|
||||||
|
|
||||||
void print_status() const;
|
void print_status() const;
|
||||||
};
|
};
|
||||||
|
|
|
@ -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 << "...\n";
|
std::clog << "attempting to malloc size: " << size;
|
||||||
}
|
}
|
||||||
|
|
||||||
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 << "...\n";
|
std::clog << "attempting to calloc size: " << size;
|
||||||
}
|
}
|
||||||
|
|
||||||
return SC_AM_Internal::stats->do_calloc(n, 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) {
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue