From c851ec6a079ba6928e67099f7b5e5c0bbffe18ca Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Thu, 6 Jun 2024 15:16:42 +0900 Subject: [PATCH] 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 . --- CMakeLists.txt | 2 +- Changelog.md | 7 +++++++ src/another_memcheck.cc | 4 ++++ src/another_memcheck.h | 9 ++++++--- src/another_memcheck_external.cc | 18 ++++++++++++------ 5 files changed, 30 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d974285..e46be10 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/Changelog.md b/Changelog.md index 7bc6a45..108c7b4 100644 --- a/Changelog.md +++ b/Changelog.md @@ -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 diff --git a/src/another_memcheck.cc b/src/another_memcheck.cc index 7e7d4ec..e574ea5 100644 --- a/src/another_memcheck.cc +++ b/src/another_memcheck.cc @@ -9,11 +9,15 @@ namespace SC_AM_Internal { Stats *stats = nullptr; + int is_env_status = 0; Stats *get_init_stats() { Stats *stats = reinterpret_cast( 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; } diff --git a/src/another_memcheck.h b/src/another_memcheck.h index a28d400..a5ea2b9 100644 --- a/src/another_memcheck.h +++ b/src/another_memcheck.h @@ -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); diff --git a/src/another_memcheck_external.cc b/src/another_memcheck_external.cc index 268f666..5e371df 100644 --- a/src/another_memcheck_external.cc +++ b/src/another_memcheck_external.cc @@ -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(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( 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(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); } }