yaze 0.2.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
memory_tracker.h
Go to the documentation of this file.
1#ifndef YAZE_APP_CORE_PLATFORM_MEMORY_TRACKER_H
2#define YAZE_APP_CORE_PLATFORM_MEMORY_TRACKER_H
3
4#include <SDL.h>
5
6#include <cstddef>
7#include <mutex>
8#include <string>
9#include <unordered_map>
10
11namespace yaze {
12namespace core {
13
15 public:
17 static MemoryTracker instance;
18 return instance;
19 }
20
21 void TrackAllocation(const void* ptr, size_t size, const char* type) {
22 std::lock_guard<std::mutex> lock(mutex_);
23 allocations_[ptr] = {size, type};
24 total_allocated_ += size;
25 }
26
27 void TrackDeallocation(const void* ptr) {
28 std::lock_guard<std::mutex> lock(mutex_);
29 auto it = allocations_.find(ptr);
30 if (it != allocations_.end()) {
31 total_allocated_ -= it->second.size;
32 allocations_.erase(it);
33 }
34 }
35
36 size_t GetTotalAllocated() const {
37 std::lock_guard<std::mutex> lock(mutex_);
38 return total_allocated_;
39 }
40
41 void DumpAllocations() const {
42 std::lock_guard<std::mutex> lock(mutex_);
43 SDL_Log("Memory allocations: %zu bytes in %zu allocations",
45
46 std::unordered_map<std::string, size_t> type_counts;
47 for (const auto& pair : allocations_) {
48 type_counts[pair.second.type] += pair.second.size;
49 }
50
51 for (const auto& pair : type_counts) {
52 SDL_Log(" %s: %zu bytes", pair.first.c_str(), pair.second);
53 }
54 }
55
56 // Check if the memory was freed by another reference
57 bool IsFreed(const void* ptr) const {
58 std::lock_guard<std::mutex> lock(mutex_);
59 return allocations_.find(ptr) == allocations_.end();
60 }
61
62 private:
63 MemoryTracker() = default;
64
66 size_t size;
67 const char* type;
68 };
69
70 std::unordered_map<const void*, AllocationInfo> allocations_;
71 size_t total_allocated_ = 0;
72 mutable std::mutex mutex_;
73};
74
75} // namespace core
76} // namespace yaze
77
78#endif // YAZE_APP_CORE_PLATFORM_MEMORY_TRACKER_H
static MemoryTracker & GetInstance()
void TrackDeallocation(const void *ptr)
bool IsFreed(const void *ptr) const
size_t GetTotalAllocated() const
void TrackAllocation(const void *ptr, size_t size, const char *type)
std::unordered_map< const void *, AllocationInfo > allocations_
Main namespace for the application.
Definition controller.cc:18