-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_tracker.h
More file actions
410 lines (347 loc) · 15.9 KB
/
memory_tracker.h
File metadata and controls
410 lines (347 loc) · 15.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/**************************************************************************/
/* memory_tracker.h */
/**************************************************************************/
/* Independent Memory Management Module */
/* Memory tracking and statistics */
/**************************************************************************/
#pragma once
#include "platform_defines.h"
#include "error_handling.h"
#include "thread_safe.h"
#include "memory_config.h"
#include <unordered_map>
#include <string>
#include <mutex>
// Forward declarations
template<typename Config>
class MemoryTracker;
// Allocation information for detailed tracking
struct AllocationInfo {
memory_size_t size;
const char* file;
int line;
const char* function;
memory_uint64_t timestamp;
memory_uint64_t allocation_id;
AllocationInfo() = default;
AllocationInfo(memory_size_t s, const char* f, int l, const char* func, memory_uint64_t ts, memory_uint64_t id)
: size(s), file(f), line(l), function(func), timestamp(ts), allocation_id(id) {}
};
// Memory statistics
struct MemoryStats {
memory_uint64_t total_allocated = 0;
memory_uint64_t total_freed = 0;
memory_uint64_t current_usage = 0;
memory_uint64_t peak_usage = 0;
memory_uint64_t allocation_count = 0;
memory_uint64_t deallocation_count = 0;
memory_uint64_t reallocation_count = 0;
void reset() {
total_allocated = 0;
total_freed = 0;
current_usage = 0;
peak_usage = 0;
allocation_count = 0;
deallocation_count = 0;
reallocation_count = 0;
}
};
// No tracking implementation
template<typename Config>
class MemoryTracker {
static_assert(Config::TRACKING_LEVEL == MemoryTrackingLevel::NONE, "Use specialized tracker for non-none tracking");
public:
using CounterType = SafeNumeric<memory_uint64_t, ThreadSafetyPolicy::NONE>;
static MEMORY_ALWAYS_INLINE void track_allocation([[maybe_unused]] memory_size_t size, [[maybe_unused]] const char* file = nullptr, [[maybe_unused]] int line = 0, [[maybe_unused]] const char* function = nullptr) {
// No-op
}
static MEMORY_ALWAYS_INLINE void track_deallocation([[maybe_unused]] memory_size_t size, [[maybe_unused]] const char* file = nullptr, [[maybe_unused]] int line = 0, [[maybe_unused]] const char* function = nullptr) {
// No-op
}
static MEMORY_ALWAYS_INLINE void track_reallocation([[maybe_unused]] memory_size_t old_size, [[maybe_unused]] memory_size_t new_size, [[maybe_unused]] const char* file = nullptr, [[maybe_unused]] int line = 0, [[maybe_unused]] const char* function = nullptr) {
// No-op
}
static MEMORY_ALWAYS_INLINE memory_uint64_t get_current_usage() {
return 0;
}
static MEMORY_ALWAYS_INLINE memory_uint64_t get_peak_usage() {
return 0;
}
static MEMORY_ALWAYS_INLINE memory_uint64_t get_allocation_count() {
return 0;
}
static MEMORY_ALWAYS_INLINE MemoryStats get_stats() {
return MemoryStats{};
}
static MEMORY_ALWAYS_INLINE void reset_stats() {
// No-op
}
static MEMORY_ALWAYS_INLINE void dump_allocations() {
// No-op
}
};
// Basic tracking implementation - template specialization
template<typename Config>
class BasicMemoryTracker {
static_assert(Config::TRACKING_LEVEL == MemoryTrackingLevel::BASIC, "Use for basic tracking only");
public:
using CounterType = SafeNumeric<memory_uint64_t, Config::THREAD_POLICY>;
private:
static CounterType current_usage_;
static CounterType peak_usage_;
static CounterType allocation_count_;
static CounterType deallocation_count_;
static CounterType reallocation_count_;
public:
static MEMORY_ALWAYS_INLINE void track_allocation(memory_size_t size, [[maybe_unused]] const char* file = nullptr, [[maybe_unused]] int line = 0, [[maybe_unused]] const char* function = nullptr) {
if constexpr (Config::ENABLE_TRACKING) {
memory_uint64_t new_usage = current_usage_.add(size);
peak_usage_.exchange_if_greater(new_usage);
allocation_count_.increment();
}
}
static MEMORY_ALWAYS_INLINE void track_deallocation(memory_size_t size, [[maybe_unused]] const char* file = nullptr, [[maybe_unused]] int line = 0, [[maybe_unused]] const char* function = nullptr) {
if constexpr (Config::ENABLE_TRACKING) {
current_usage_.sub(size);
deallocation_count_.increment();
}
}
static MEMORY_ALWAYS_INLINE void track_reallocation(memory_size_t old_size, memory_size_t new_size, [[maybe_unused]] const char* file = nullptr, [[maybe_unused]] int line = 0, [[maybe_unused]] const char* function = nullptr) {
if constexpr (Config::ENABLE_TRACKING) {
if (new_size > old_size) {
memory_uint64_t new_usage = current_usage_.add(new_size - old_size);
peak_usage_.exchange_if_greater(new_usage);
} else if (old_size > new_size) {
current_usage_.sub(old_size - new_size);
}
reallocation_count_.increment();
}
}
static MEMORY_ALWAYS_INLINE memory_uint64_t get_current_usage() {
if constexpr (Config::ENABLE_TRACKING) {
return current_usage_.get();
} else {
return 0;
}
}
static MEMORY_ALWAYS_INLINE memory_uint64_t get_peak_usage() {
if constexpr (Config::ENABLE_TRACKING) {
return peak_usage_.get();
} else {
return 0;
}
}
static MEMORY_ALWAYS_INLINE memory_uint64_t get_allocation_count() {
if constexpr (Config::ENABLE_TRACKING) {
return allocation_count_.get();
} else {
return 0;
}
}
static MemoryStats get_stats() {
if constexpr (Config::ENABLE_TRACKING) {
MemoryStats stats;
stats.current_usage = current_usage_.get();
stats.peak_usage = peak_usage_.get();
stats.allocation_count = allocation_count_.get();
stats.deallocation_count = deallocation_count_.get();
stats.reallocation_count = reallocation_count_.get();
stats.total_allocated = stats.allocation_count; // Simplified
stats.total_freed = stats.deallocation_count; // Simplified
return stats;
} else {
return MemoryStats{};
}
}
static void reset_stats() {
if constexpr (Config::ENABLE_TRACKING) {
current_usage_.set(0);
peak_usage_.set(0);
allocation_count_.set(0);
deallocation_count_.set(0);
reallocation_count_.set(0);
}
}
static void dump_allocations() {
// Basic tracking doesn't store individual allocations
if constexpr (Config::ENABLE_TRACKING) {
MemoryStats stats = get_stats();
// Use error handler to output stats
std::string message = "Memory Stats - Current: " + std::to_string(stats.current_usage) +
" Peak: " + std::to_string(stats.peak_usage) +
" Allocs: " + std::to_string(stats.allocation_count);
_memory_report_error(MemoryErrorType::MEM_WARNING, MEMORY_FUNCTION_STR, __FILE__, __LINE__, message.c_str());
}
}
};
// Detailed tracking implementation
template<typename Config>
class DetailedMemoryTracker {
static_assert(Config::TRACKING_LEVEL == MemoryTrackingLevel::DETAILED, "Use for detailed tracking only");
public:
using CounterType = SafeNumeric<memory_uint64_t, Config::THREAD_POLICY>;
private:
static CounterType current_usage_;
static CounterType peak_usage_;
static CounterType allocation_count_;
static CounterType deallocation_count_;
static CounterType reallocation_count_;
static CounterType next_allocation_id_;
// Thread-safe allocation tracking
static std::mutex allocations_mutex_;
static std::unordered_map<void*, AllocationInfo> allocations_;
public:
static void track_allocation(memory_size_t size, [[maybe_unused]] const char* file = nullptr, [[maybe_unused]] int line = 0, [[maybe_unused]] const char* function = nullptr) {
if constexpr (Config::ENABLE_TRACKING) {
memory_uint64_t new_usage = current_usage_.add(size);
peak_usage_.exchange_if_greater(new_usage);
allocation_count_.increment();
// Store allocation info if detailed tracking is enabled
if constexpr (Config::TRACKING_LEVEL == MemoryTrackingLevel::DETAILED) {
[[maybe_unused]] memory_uint64_t alloc_id = next_allocation_id_.increment();
// Note: We would need the actual pointer to store this properly
// This is a simplified version
}
}
}
static void track_allocation_with_ptr(void* ptr, memory_size_t size, const char* file = nullptr, int line = 0, const char* function = nullptr) {
if constexpr (Config::ENABLE_TRACKING && Config::TRACKING_LEVEL == MemoryTrackingLevel::DETAILED) {
memory_uint64_t new_usage = current_usage_.add(size);
peak_usage_.exchange_if_greater(new_usage);
allocation_count_.increment();
memory_uint64_t alloc_id = next_allocation_id_.increment();
memory_uint64_t timestamp = 0; // Would need actual timestamp implementation
std::lock_guard<std::mutex> lock(allocations_mutex_);
allocations_[ptr] = AllocationInfo(size, file, line, function, timestamp, alloc_id);
}
}
static void track_deallocation(memory_size_t size, [[maybe_unused]] const char* file = nullptr, [[maybe_unused]] int line = 0, [[maybe_unused]] const char* function = nullptr) {
if constexpr (Config::ENABLE_TRACKING) {
current_usage_.sub(size);
deallocation_count_.increment();
}
}
static void track_deallocation_with_ptr(void* ptr, [[maybe_unused]] const char* file = nullptr, [[maybe_unused]] int line = 0, [[maybe_unused]] const char* function = nullptr) {
if constexpr (Config::ENABLE_TRACKING && Config::TRACKING_LEVEL == MemoryTrackingLevel::DETAILED) {
std::lock_guard<std::mutex> lock(allocations_mutex_);
auto it = allocations_.find(ptr);
if (it != allocations_.end()) {
current_usage_.sub(it->second.size);
allocations_.erase(it);
}
deallocation_count_.increment();
}
}
static void track_reallocation(memory_size_t old_size, memory_size_t new_size, [[maybe_unused]] const char* file = nullptr, [[maybe_unused]] int line = 0, [[maybe_unused]] const char* function = nullptr) {
if constexpr (Config::ENABLE_TRACKING) {
if (new_size > old_size) {
memory_uint64_t new_usage = current_usage_.add(new_size - old_size);
peak_usage_.exchange_if_greater(new_usage);
} else if (old_size > new_size) {
current_usage_.sub(old_size - new_size);
}
reallocation_count_.increment();
}
}
static memory_uint64_t get_current_usage() {
if constexpr (Config::ENABLE_TRACKING) {
return current_usage_.get();
} else {
return 0;
}
}
static memory_uint64_t get_peak_usage() {
if constexpr (Config::ENABLE_TRACKING) {
return peak_usage_.get();
} else {
return 0;
}
}
static memory_uint64_t get_allocation_count() {
if constexpr (Config::ENABLE_TRACKING) {
return allocation_count_.get();
} else {
return 0;
}
}
static MemoryStats get_stats() {
if constexpr (Config::ENABLE_TRACKING) {
MemoryStats stats;
stats.current_usage = current_usage_.get();
stats.peak_usage = peak_usage_.get();
stats.allocation_count = allocation_count_.get();
stats.deallocation_count = deallocation_count_.get();
stats.reallocation_count = reallocation_count_.get();
stats.total_allocated = stats.allocation_count; // Simplified
stats.total_freed = stats.deallocation_count; // Simplified
return stats;
} else {
return MemoryStats{};
}
}
static void reset_stats() {
if constexpr (Config::ENABLE_TRACKING) {
current_usage_.set(0);
peak_usage_.set(0);
allocation_count_.set(0);
deallocation_count_.set(0);
reallocation_count_.set(0);
next_allocation_id_.set(0);
if constexpr (Config::TRACKING_LEVEL == MemoryTrackingLevel::DETAILED) {
std::lock_guard<std::mutex> lock(allocations_mutex_);
allocations_.clear();
}
}
}
static void dump_allocations() {
if constexpr (Config::ENABLE_TRACKING && Config::TRACKING_LEVEL == MemoryTrackingLevel::DETAILED) {
std::lock_guard<std::mutex> lock(allocations_mutex_);
for (const auto& [ptr, info] : allocations_) {
std::string message = "Leak: " + std::to_string(info.size) + " bytes at " +
(info.file ? info.file : "unknown") + ":" + std::to_string(info.line);
_memory_report_error(MemoryErrorType::WARNING, info.function ? info.function : "unknown",
info.file ? info.file : "unknown", info.line, message.c_str());
}
}
}
};
// Static member definitions would go in a .cpp file
// For header-only implementation, we use inline static
template<typename Config>
inline typename BasicMemoryTracker<Config>::CounterType BasicMemoryTracker<Config>::current_usage_{};
template<typename Config>
inline typename BasicMemoryTracker<Config>::CounterType BasicMemoryTracker<Config>::peak_usage_{};
template<typename Config>
inline typename BasicMemoryTracker<Config>::CounterType BasicMemoryTracker<Config>::allocation_count_{};
template<typename Config>
inline typename BasicMemoryTracker<Config>::CounterType BasicMemoryTracker<Config>::deallocation_count_{};
template<typename Config>
inline typename BasicMemoryTracker<Config>::CounterType BasicMemoryTracker<Config>::reallocation_count_{};
// Static member definitions for DetailedMemoryTracker
template<typename Config>
inline typename DetailedMemoryTracker<Config>::CounterType DetailedMemoryTracker<Config>::current_usage_{};
template<typename Config>
inline typename DetailedMemoryTracker<Config>::CounterType DetailedMemoryTracker<Config>::peak_usage_{};
template<typename Config>
inline typename DetailedMemoryTracker<Config>::CounterType DetailedMemoryTracker<Config>::allocation_count_{};
template<typename Config>
inline typename DetailedMemoryTracker<Config>::CounterType DetailedMemoryTracker<Config>::deallocation_count_{};
template<typename Config>
inline typename DetailedMemoryTracker<Config>::CounterType DetailedMemoryTracker<Config>::reallocation_count_{};
template<typename Config>
inline typename DetailedMemoryTracker<Config>::CounterType DetailedMemoryTracker<Config>::next_allocation_id_{};
template<typename Config>
inline std::mutex DetailedMemoryTracker<Config>::allocations_mutex_{};
template<typename Config>
inline std::unordered_map<void*, AllocationInfo> DetailedMemoryTracker<Config>::allocations_{};
// Type selection based on configuration
template<typename Config>
using MemoryTrackerType = std::conditional_t<
Config::TRACKING_LEVEL == MemoryTrackingLevel::NONE,
MemoryTracker<Config>,
std::conditional_t<
Config::TRACKING_LEVEL == MemoryTrackingLevel::BASIC,
BasicMemoryTracker<Config>,
DetailedMemoryTracker<Config>
>
>;