24 #include <unordered_map>
50 size_t total_allocations{0};
51 size_t total_deallocations{0};
52 size_t current_allocations{0};
53 size_t peak_allocations{0};
54 size_t total_bytes_allocated{0};
55 size_t total_bytes_deallocated{0};
56 size_t current_bytes_allocated{0};
57 size_t peak_bytes_allocated{0};
64 void track_allocation(
void* ptr,
size_t size,
const char* file,
int line,
const char*
function);
65 void track_deallocation(
void* ptr);
68 MemoryStats get_stats()
const;
69 std::vector<AllocationInfo> get_current_allocations()
const;
70 std::vector<AllocationInfo> get_leaked_allocations()
const;
73 void enable_tracking(
bool enable =
true);
74 void disable_tracking();
75 bool is_tracking_enabled()
const;
78 void clear_tracking_data();
81 void print_memory_report()
const;
82 void print_leak_report()
const;
85 void log_memory_report()
const;
86 void log_leak_report()
const;
95 mutable std::mutex allocations_mutex_;
96 std::unordered_map<void*, AllocationInfo> allocations_;
98 std::atomic<bool> tracking_enabled_{
true};
109 void track_allocation(
void* ptr,
size_t size);
110 void track_deallocation(
void* ptr);
115 const char* function_;
122 #ifdef UNILINK_ENABLE_MEMORY_TRACKING
123 #define MEMORY_TRACK_ALLOCATION(ptr, size) \
124 unilink::memory::MemoryTracker::instance().track_allocation(ptr, size, __FILE__, __LINE__, __FUNCTION__)
126 #define MEMORY_TRACK_DEALLOCATION(ptr) unilink::memory::MemoryTracker::instance().track_deallocation(ptr)
128 #define MEMORY_TRACK_SCOPE() unilink::memory::ScopedMemoryTracker _mem_tracker(__FILE__, __LINE__, __FUNCTION__)
130 #define MEMORY_TRACK_ALLOCATION(ptr, size) ((void)0)
131 #define MEMORY_TRACK_DEALLOCATION(ptr) ((void)0)
132 #define MEMORY_TRACK_SCOPE() ((void)0)
Memory allocation tracker for debugging and monitoring.
RAII helper for automatic memory tracking.
std::chrono::steady_clock::time_point timestamp