Check for ANOTHER_MEMCHECK_QUIET env var, refactor

Now, if ANOTHER_MEMCHECK_QUIET environment variable is defined, do not
print output when malloc/calloc/free is called.

Minor refactoring: remove unused constexpr declarations in header.

Bump version to 2.2 .

Update Changelog.md .
This commit is contained in:
Stephen Seo 2024-06-06 15:16:42 +09:00
parent dcc9ae38c5
commit c851ec6a07
5 changed files with 30 additions and 10 deletions

View file

@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.7)
project(AnotherMemCheck)
set(AnotherMemCheck_VERSION 2.1)
set(AnotherMemCheck_VERSION 2.2)
set(AnotherMemCheck_SOVERSION 2)
set(AnotherMemCheck_SOURCES

View file

@ -1,5 +1,12 @@
# Changelog
## Version 2.2
Minor refactoring: remove unused constexpr declarations in header.
Change implementation to not print when malloc/calloc/free is called if the
"ANOTHER_MEMCHECK_QUIET" environment variable is defined.
## Verison 2.1
Previous implementation did not fully account for total size of a call to

View file

@ -9,11 +9,15 @@
namespace SC_AM_Internal {
Stats *stats = nullptr;
int is_env_status = 0;
Stats *get_init_stats() {
Stats *stats = reinterpret_cast<SC_AM_Internal::Stats*>(
real_malloc(sizeof(Stats)));
stats->initialize();
is_env_status = getenv("ANOTHER_MEMCHECK_QUIET") != nullptr
? ANOTHER_MEMCHECK_QUIET_EXISTS
: ANOTHER_MEMCHECK_QUIET_NOT_EXISTS;
return stats;
}

View file

@ -11,10 +11,13 @@ namespace SC_AM_Internal {
extern Stats *stats;
Stats *get_init_stats();
constexpr int ANOTHER_MEMCHECK_QUIET_EXISTS = 1;
constexpr int ANOTHER_MEMCHECK_QUIET_NOT_EXISTS = 2;
constexpr unsigned int DATA_BLOCK_SIZE = 24;
constexpr unsigned int DATA_ITEMS_SIZE = 500;
/// 0 If unknown, 1 if env "ANOTHER_MEMCHECK_QUIET" exists, 2 if not.
extern int is_env_status;
Stats *get_init_stats();
extern void *(*real_malloc)(std::size_t size);
extern void *(*real_calloc)(std::size_t n, std::size_t size);

View file

@ -9,8 +9,6 @@
extern "C" {
void *malloc(std::size_t size) {
std::clog << "attempting to malloc size: " << size << "...\n";
if (SC_AM_Internal::real_malloc == nullptr) {
SC_AM_Internal::real_malloc = reinterpret_cast<void*(*)(std::size_t)>(dlsym(RTLD_NEXT, "malloc"));
}
@ -19,12 +17,14 @@ extern "C" {
SC_AM_Internal::stats = SC_AM_Internal::get_init_stats();
}
if (SC_AM_Internal::is_env_status == SC_AM_Internal::ANOTHER_MEMCHECK_QUIET_NOT_EXISTS) {
std::clog << "attempting to malloc size: " << size << "...\n";
}
return SC_AM_Internal::stats->do_malloc(size);
}
void *calloc(std::size_t n, std::size_t size) {
std::clog << "attempting to calloc size: " << size << "...\n";
if (SC_AM_Internal::real_calloc == nullptr) {
SC_AM_Internal::real_calloc = reinterpret_cast<void*(*)(std::size_t, std::size_t)>(
dlsym(RTLD_NEXT, "calloc"));
@ -34,12 +34,14 @@ extern "C" {
SC_AM_Internal::stats = SC_AM_Internal::get_init_stats();
}
if (SC_AM_Internal::is_env_status == SC_AM_Internal::ANOTHER_MEMCHECK_QUIET_NOT_EXISTS) {
std::clog << "attempting to calloc size: " << size << "...\n";
}
return SC_AM_Internal::stats->do_calloc(n, size);
}
void free(void *ptr) {
std::clog << "attempting to free...\n";
if (SC_AM_Internal::real_free == nullptr) {
SC_AM_Internal::real_free = reinterpret_cast<void(*)(void*)>(dlsym(RTLD_NEXT, "free"));
}
@ -48,6 +50,10 @@ extern "C" {
SC_AM_Internal::stats = SC_AM_Internal::get_init_stats();
}
if (SC_AM_Internal::is_env_status == SC_AM_Internal::ANOTHER_MEMCHECK_QUIET_NOT_EXISTS) {
std::clog << "attempting to free...\n";
}
return SC_AM_Internal::stats->do_free(ptr);
}
}