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) cmake_minimum_required(VERSION 3.7)
project(AnotherMemCheck) project(AnotherMemCheck)
set(AnotherMemCheck_VERSION 2.1) set(AnotherMemCheck_VERSION 2.2)
set(AnotherMemCheck_SOVERSION 2) set(AnotherMemCheck_SOVERSION 2)
set(AnotherMemCheck_SOURCES set(AnotherMemCheck_SOURCES

View file

@ -1,5 +1,12 @@
# Changelog # 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 ## Verison 2.1
Previous implementation did not fully account for total size of a call to Previous implementation did not fully account for total size of a call to

View file

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

View file

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

View file

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