Compare commits
No commits in common. "f8540d8cde755d7d5ba96fe23aab5c46c851bf4b" and "d4c47f871bb101f093cba5d93f4cdb6f42f49edf" have entirely different histories.
f8540d8cde
...
d4c47f871b
3 changed files with 30 additions and 24 deletions
|
@ -1,12 +1,5 @@
|
||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
## Version 2.6
|
|
||||||
|
|
||||||
Use a more robust mutex (by using `std::recursive_mutex` instead of
|
|
||||||
`pthread_mutex`).
|
|
||||||
|
|
||||||
Minor refactorings.
|
|
||||||
|
|
||||||
## Version 2.5
|
## Version 2.5
|
||||||
|
|
||||||
Added support for realloc().
|
Added support for realloc().
|
||||||
|
|
|
@ -3,7 +3,6 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <mutex>
|
|
||||||
|
|
||||||
// Local includes.
|
// Local includes.
|
||||||
#include "another_memcheck.h"
|
#include "another_memcheck.h"
|
||||||
|
@ -76,7 +75,7 @@ namespace SC_AM_Internal {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Stats::Stats() : malloced_list_head(nullptr), malloced_list_tail(nullptr), deferred_node(nullptr), recursive_mutex(nullptr) {
|
Stats::Stats() : malloced_list_head(nullptr), malloced_list_tail(nullptr), deferred_node(nullptr), pthread_mutex{.__align=0} {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Stats::initialize() {
|
void Stats::initialize() {
|
||||||
|
@ -89,9 +88,9 @@ namespace SC_AM_Internal {
|
||||||
malloced_list_head->data = nullptr;
|
malloced_list_head->data = nullptr;
|
||||||
malloced_list_tail->data = nullptr;
|
malloced_list_tail->data = nullptr;
|
||||||
|
|
||||||
recursive_mutex = real_malloc(sizeof(std::recursive_mutex));
|
deferred_node = nullptr;
|
||||||
void *unused = new(recursive_mutex) std::recursive_mutex{};
|
|
||||||
(void)unused;
|
pthread_mutex_init(&pthread_mutex, nullptr);
|
||||||
|
|
||||||
on_exit([] ([[maybe_unused]] int status, void *ptr) {
|
on_exit([] ([[maybe_unused]] int status, void *ptr) {
|
||||||
Stats *stats = reinterpret_cast<Stats*>(ptr);
|
Stats *stats = reinterpret_cast<Stats*>(ptr);
|
||||||
|
@ -101,14 +100,15 @@ namespace SC_AM_Internal {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Stats::cleanup() {
|
void Stats::cleanup() {
|
||||||
using std_recursive_mutex = std::recursive_mutex;
|
pthread_mutex_destroy(&pthread_mutex);
|
||||||
((std::recursive_mutex*)recursive_mutex)->~std_recursive_mutex();
|
|
||||||
real_free(recursive_mutex);
|
|
||||||
// TODO maybe cleanup list, but it is the end of the program.
|
// TODO maybe cleanup list, but it is the end of the program.
|
||||||
}
|
}
|
||||||
|
|
||||||
void *Stats::do_malloc(std::size_t size) {
|
void *Stats::do_malloc(std::size_t size) {
|
||||||
((std::recursive_mutex*)recursive_mutex)->lock();
|
if(int ret = pthread_mutex_lock(&pthread_mutex); ret == EINVAL) {
|
||||||
|
std::clog << "ERROR: pthread mutex not properly initialized!\n";
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
void *address = real_malloc(size);
|
void *address = real_malloc(size);
|
||||||
if (address != nullptr) {
|
if (address != nullptr) {
|
||||||
|
@ -122,12 +122,15 @@ namespace SC_AM_Internal {
|
||||||
ListNode::add_to_list(malloced_list_tail, data);
|
ListNode::add_to_list(malloced_list_tail, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
((std::recursive_mutex*)recursive_mutex)->unlock();
|
pthread_mutex_unlock(&pthread_mutex);
|
||||||
return address;
|
return address;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *Stats::do_calloc(std::size_t n, std::size_t size) {
|
void *Stats::do_calloc(std::size_t n, std::size_t size) {
|
||||||
((std::recursive_mutex*)recursive_mutex)->lock();
|
if(int ret = pthread_mutex_lock(&pthread_mutex); ret == EINVAL) {
|
||||||
|
std::clog << "ERROR: pthread mutex not properly initialized!\n";
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
void *address = real_calloc(n, size);
|
void *address = real_calloc(n, size);
|
||||||
if (address != nullptr) {
|
if (address != nullptr) {
|
||||||
|
@ -141,12 +144,15 @@ namespace SC_AM_Internal {
|
||||||
ListNode::add_to_list(malloced_list_tail, data);
|
ListNode::add_to_list(malloced_list_tail, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
((std::recursive_mutex*)recursive_mutex)->unlock();
|
pthread_mutex_unlock(&pthread_mutex);
|
||||||
return address;
|
return address;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *Stats::do_realloc(void *ptr, std::size_t size) {
|
void *Stats::do_realloc(void *ptr, std::size_t size) {
|
||||||
((std::recursive_mutex*)recursive_mutex)->lock();
|
if(int ret = pthread_mutex_lock(&pthread_mutex); ret == EINVAL) {
|
||||||
|
std::clog << "ERROR: pthread mutex not properly initialized!\n";
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
if (ptr) {
|
if (ptr) {
|
||||||
if (!ListNode::remove_from_list(malloced_list_head, ptr, this)) {
|
if (!ListNode::remove_from_list(malloced_list_head, ptr, this)) {
|
||||||
|
@ -177,13 +183,16 @@ namespace SC_AM_Internal {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
((std::recursive_mutex*)recursive_mutex)->unlock();
|
pthread_mutex_unlock(&pthread_mutex);
|
||||||
return address;
|
return address;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Stats::do_free(void *ptr) {
|
bool Stats::do_free(void *ptr) {
|
||||||
bool result = false;
|
bool result = false;
|
||||||
((std::recursive_mutex*)recursive_mutex)->lock();
|
if(int ret = pthread_mutex_lock(&pthread_mutex); ret == EINVAL) {
|
||||||
|
std::clog << "ERROR: pthread mutex not properly initialized!\n";
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
if (ptr) {
|
if (ptr) {
|
||||||
if (!ListNode::remove_from_list(malloced_list_head, ptr)) {
|
if (!ListNode::remove_from_list(malloced_list_head, ptr)) {
|
||||||
|
@ -194,7 +203,7 @@ namespace SC_AM_Internal {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
((std::recursive_mutex*)recursive_mutex)->unlock();
|
pthread_mutex_unlock(&pthread_mutex);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
#ifndef SEODISPARATE_COM_ANOTHER_MEMCHECK_H
|
#ifndef SEODISPARATE_COM_ANOTHER_MEMCHECK_H
|
||||||
#define SEODISPARATE_COM_ANOTHER_MEMCHECK_H
|
#define SEODISPARATE_COM_ANOTHER_MEMCHECK_H
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
#include <pthread.h>
|
||||||
|
}
|
||||||
|
|
||||||
namespace SC_AM_Internal {
|
namespace SC_AM_Internal {
|
||||||
// Forward declaration.
|
// Forward declaration.
|
||||||
struct Stats;
|
struct Stats;
|
||||||
|
@ -52,7 +56,7 @@ namespace SC_AM_Internal {
|
||||||
ListNode *malloced_list_head;
|
ListNode *malloced_list_head;
|
||||||
ListNode *malloced_list_tail;
|
ListNode *malloced_list_tail;
|
||||||
ListNode *deferred_node;
|
ListNode *deferred_node;
|
||||||
void *recursive_mutex;
|
pthread_mutex_t pthread_mutex;
|
||||||
|
|
||||||
void initialize();
|
void initialize();
|
||||||
void cleanup();
|
void cleanup();
|
||||||
|
|
Loading…
Reference in a new issue