Cleanup of Stats struct

This commit is contained in:
Stephen Seo 2024-11-12 16:10:10 +09:00
parent 4d3d6c9168
commit 33d3adba3f
2 changed files with 16 additions and 11 deletions

View file

@ -19,7 +19,7 @@ namespace SC_AM_Internal {
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(); [[maybe_unused]] void *unused = new(stats) Stats{};
is_env_status = getenv("ANOTHER_MEMCHECK_QUIET") != nullptr is_env_status = getenv("ANOTHER_MEMCHECK_QUIET") != nullptr
? ANOTHER_MEMCHECK_QUIET_EXISTS ? ANOTHER_MEMCHECK_QUIET_EXISTS
: ANOTHER_MEMCHECK_QUIET_NOT_EXISTS; : ANOTHER_MEMCHECK_QUIET_NOT_EXISTS;
@ -80,9 +80,6 @@ namespace SC_AM_Internal {
} }
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), recursive_mutex(nullptr) {
}
void Stats::initialize() {
malloced_list_head = reinterpret_cast<ListNode*>(real_malloc(sizeof(ListNode))); malloced_list_head = reinterpret_cast<ListNode*>(real_malloc(sizeof(ListNode)));
malloced_list_tail = reinterpret_cast<ListNode*>(real_malloc(sizeof(ListNode))); malloced_list_tail = reinterpret_cast<ListNode*>(real_malloc(sizeof(ListNode)));
malloced_list_head->next = malloced_list_tail; malloced_list_head->next = malloced_list_tail;
@ -95,13 +92,12 @@ namespace SC_AM_Internal {
deferred_node = nullptr; deferred_node = nullptr;
recursive_mutex = real_malloc(sizeof(std::recursive_mutex)); recursive_mutex = real_malloc(sizeof(std::recursive_mutex));
void *unused = new(recursive_mutex) std::recursive_mutex{}; [[maybe_unused]] void *unused = new(recursive_mutex) std::recursive_mutex{};
(void)unused;
std::atexit(exit_handler_stats); std::atexit(exit_handler_stats);
} }
void Stats::cleanup() { Stats::~Stats() {
using std_recursive_mutex = std::recursive_mutex; using std_recursive_mutex = std::recursive_mutex;
((std::recursive_mutex*)recursive_mutex)->~std_recursive_mutex(); ((std::recursive_mutex*)recursive_mutex)->~std_recursive_mutex();
real_free(recursive_mutex); real_free(recursive_mutex);
@ -221,7 +217,8 @@ namespace SC_AM_Internal {
void exit_handler_stats() { void exit_handler_stats() {
if (SC_AM_Internal::stats != nullptr) { if (SC_AM_Internal::stats != nullptr) {
SC_AM_Internal::stats->print_status(); SC_AM_Internal::stats->print_status();
SC_AM_Internal::stats->cleanup(); using SCS_AM_INTERNAL_Stats = SC_AM_Internal::Stats;
SC_AM_Internal::stats->~SCS_AM_INTERNAL_Stats();
SC_AM_Internal::stats = nullptr; SC_AM_Internal::stats = nullptr;
} }
} }

View file

@ -1,6 +1,8 @@
#ifndef SEODISPARATE_COM_ANOTHER_MEMCHECK_H #ifndef SEODISPARATE_COM_ANOTHER_MEMCHECK_H
#define SEODISPARATE_COM_ANOTHER_MEMCHECK_H #define SEODISPARATE_COM_ANOTHER_MEMCHECK_H
#include <cstdint>
namespace SC_AM_Internal { namespace SC_AM_Internal {
// Forward declaration. // Forward declaration.
struct Stats; struct Stats;
@ -48,15 +50,21 @@ namespace SC_AM_Internal {
struct Stats { struct Stats {
Stats(); Stats();
~Stats();
// Disable copy.
Stats(Stats &other) = delete;
Stats& operator=(Stats &other) = delete;
// Disable move.
Stats(Stats &&other) = delete;
Stats& operator=(Stats &&other) = delete;
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; void *recursive_mutex;
void initialize();
void cleanup();
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_realloc(void*, std::size_t size); void *do_realloc(void*, std::size_t size);