34 if (!tracking_enabled_.load()) {
38 std::lock_guard<std::mutex> lock(allocations_mutex_);
43 info.
file = file ? file :
"unknown";
45 info.
function =
function ? function :
"unknown";
46 info.
timestamp = std::chrono::steady_clock::now();
48 allocations_[ptr] = info;
66 if (!tracking_enabled_.load()) {
70 std::lock_guard<std::mutex> lock(allocations_mutex_);
72 auto it = allocations_.find(ptr);
73 if (it != allocations_.end()) {
74 size_t size = it->second.size;
75 allocations_.erase(it);
88 std::lock_guard<std::mutex> lock(allocations_mutex_);
90 std::vector<AllocationInfo> result;
91 result.reserve(allocations_.size());
93 for (
const auto& pair : allocations_) {
94 result.push_back(pair.second);
111 std::lock_guard<std::mutex> lock(allocations_mutex_);
112 allocations_.clear();
129 std::cout <<
"\n=== Memory Tracker Report ===" << std::endl;
130 std::cout <<
"Total allocations: " << stats.total_allocations << std::endl;
131 std::cout <<
"Total deallocations: " << stats.total_deallocations << std::endl;
132 std::cout <<
"Current allocations: " << stats.current_allocations << std::endl;
133 std::cout <<
"Peak allocations: " << stats.peak_allocations << std::endl;
134 std::cout <<
"Total bytes allocated: " << stats.total_bytes_allocated << std::endl;
135 std::cout <<
"Total bytes deallocated: " << stats.total_bytes_deallocated << std::endl;
136 std::cout <<
"Current bytes allocated: " << stats.current_bytes_allocated << std::endl;
137 std::cout <<
"Peak bytes allocated: " << stats.peak_bytes_allocated << std::endl;
138 std::cout <<
"Current active allocations: " << current_allocations.size() << std::endl;
140 if (!current_allocations.empty()) {
141 std::cout <<
"\n=== Current Allocations ===" << std::endl;
142 for (
const auto& alloc : current_allocations) {
143 std::cout <<
"Ptr: " << alloc.ptr <<
", Size: " << alloc.size <<
", File: " << alloc.file <<
":" << alloc.line
144 <<
", Function: " << alloc.function << std::endl;
152 if (leaked_allocations.empty()) {
153 std::cout <<
"\n=== No Memory Leaks Detected ===" << std::endl;
157 std::cout <<
"\n=== Memory Leak Report ===" << std::endl;
158 std::cout <<
"Found " << leaked_allocations.size() <<
" potential memory leaks:" << std::endl;
160 size_t total_leaked_bytes = 0;
161 for (
const auto& alloc : leaked_allocations) {
162 std::cout <<
"Leaked: " << alloc.size <<
" bytes at " << alloc.ptr <<
" allocated in " << alloc.file <<
":"
163 << alloc.line <<
" (" << alloc.function <<
")" << std::endl;
164 total_leaked_bytes += alloc.size;
167 std::cout <<
"Total leaked bytes: " << total_leaked_bytes << std::endl;
174 std::ostringstream oss;
175 oss <<
"\n=== Memory Tracker Report ===\n";
176 oss <<
"Total allocations: " << stats.total_allocations <<
"\n";
177 oss <<
"Total deallocations: " << stats.total_deallocations <<
"\n";
178 oss <<
"Current allocations: " << stats.current_allocations <<
"\n";
179 oss <<
"Peak allocations: " << stats.peak_allocations <<
"\n";
180 oss <<
"Total bytes allocated: " << stats.total_bytes_allocated <<
"\n";
181 oss <<
"Total bytes deallocated: " << stats.total_bytes_deallocated <<
"\n";
182 oss <<
"Current bytes allocated: " << stats.current_bytes_allocated <<
"\n";
183 oss <<
"Peak bytes allocated: " << stats.peak_bytes_allocated <<
"\n";
184 oss <<
"Current active allocations: " << current_allocations.size();
188 if (!current_allocations.empty()) {
189 std::ostringstream alloc_oss;
190 alloc_oss <<
"\n=== Current Allocations ===\n";
191 for (
const auto& alloc : current_allocations) {
192 alloc_oss <<
"Ptr: " << alloc.ptr <<
", Size: " << alloc.size <<
", File: " << alloc.file <<
":" << alloc.line
193 <<
", Function: " << alloc.function <<
"\n";
202 if (leaked_allocations.empty()) {
203 UNILINK_LOG_INFO(
"memory_tracker",
"leak_check",
"No Memory Leaks Detected");
207 std::ostringstream oss;
208 oss <<
"\n=== Memory Leak Report ===\n";
209 oss <<
"Found " << leaked_allocations.size() <<
" potential memory leaks:\n";
211 size_t total_leaked_bytes = 0;
212 for (
const auto& alloc : leaked_allocations) {
213 oss <<
"Leaked: " << alloc.size <<
" bytes at " << alloc.ptr <<
" allocated in " << alloc.file <<
":" << alloc.line
214 <<
" (" << alloc.function <<
")\n";
215 total_leaked_bytes += alloc.size;
218 oss <<
"Total leaked bytes: " << total_leaked_bytes;
224 : file_(file), line_(line), function_(function) {}
Memory allocation tracker for debugging and monitoring.
void track_allocation(void *ptr, size_t size, const char *file, int line, const char *function)
bool is_tracking_enabled() const
static MemoryTracker & instance()
void clear_tracking_data()
std::vector< AllocationInfo > get_leaked_allocations() const
MemoryStats get_stats() const
std::vector< AllocationInfo > get_current_allocations() const
void log_memory_report() const
void print_leak_report() const
void log_leak_report() const
void enable_tracking(bool enable=true)
void track_deallocation(void *ptr)
void print_memory_report() const
ScopedMemoryTracker(const char *file, int line, const char *function)
void track_allocation(void *ptr, size_t size)
void track_deallocation(void *ptr)
#define UNILINK_LOG_INFO(component, operation, message)
#define UNILINK_LOG_ERROR(component, operation, message)
std::chrono::steady_clock::time_point timestamp
size_t current_bytes_allocated
size_t total_deallocations
size_t total_bytes_allocated
size_t total_bytes_deallocated
size_t peak_bytes_allocated
size_t current_allocations