Files
core_zero/tjp/core/debug/Allocations.h
Timothy Prepscius 996151a56a flatten 20260225
2026-02-25 12:43:06 -05:00

59 lines
1.2 KiB
C++
Executable File

// License: Modified MIT (NON-AI)
// See the LICENSE file in the root directory for license information.
// Copyright 2025 Timothy Prepscius
#pragma once
#include "Debug.h"
#include <typeinfo>
#include "../containers/Map.h"
#include "../string/String.h"
#include "../threads/Lock.hpp"
#include "../algorithm/ExecuteOnDestruct.hpp"
#include "../rtti/RTTI.hpp"
namespace tjp::core::debug {
struct Allocations
{
struct Allocation {
int count = 0;
} ;
Mutex mutex;
Map<String, Allocation> allocations;
void onAllocation (const type_index &t);
void onDeallocation (const type_index &t);
String log ();
static Allocations *shared();
} ;
} // namespace
#if defined(_DEBUG)
#define ALLOC_RECORD(x) \
tjp::core::debug::Allocations::shared()->onAllocation(type_id(*x))
#define DEALLOC_RECORD(x) \
tjp::core::debug::Allocations::shared()->onDeallocation(type_id(*x))
#define SCOPE_RECORD(x) \
struct __ALLOC__##x __alloc__ {}; \
ALLOC_RECORD(&__alloc_); \
tjp::core::ExecuteOnDestruct __dealloc__([&]() { DEALLOC_RECORD(&__alloc__)); \
#define ALLOC_LOG() \
sLogRelease("alloc", tjp::core::debug::Allocations::shared()->log())
#else
#define ALLOC_RECORD(x)
#define DEALLOC_RECORD(x)
#define SCOPE_RECORD(x)
#define ALLOC_LOG()
#endif