yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
memory_pool.cc
Go to the documentation of this file.
2
3#include <algorithm>
4#include <cstdlib>
5#include <cstring>
6
7namespace yaze {
8namespace gfx {
9
11
13 static MemoryPool instance;
14 return instance;
15}
16
18 : total_allocations_(0),
19 total_deallocations_(0),
20 total_used_bytes_(0),
21 total_allocated_bytes_(0) {
22 // Initialize block pools with common graphics sizes
24 100); // 100KB for small tiles
26 50); // 200KB for medium tiles
28 20); // 320KB for large tiles
30 10); // 640KB for graphics sheets
31
33 (20 * kLargeBlockSize) + (10 * kHugeBlockSize);
34}
35
39
40void* MemoryPool::Allocate(size_t size) {
42
43 MemoryBlock* block = FindFreeBlock(size);
44 if (!block) {
45 // Fallback to system malloc if no pool block available
46 void* data = std::malloc(size);
47 if (data) {
48 total_used_bytes_ += size;
49 allocated_blocks_[data] = nullptr; // Mark as system allocated
50 }
51 return data;
52 }
53
54 block->in_use = true;
55 total_used_bytes_ += block->size;
56 allocated_blocks_[block->data] = block;
57
58 return block->data;
59}
60
61void MemoryPool::Deallocate(void* ptr) {
62 if (!ptr)
63 return;
64
66
67 auto it = allocated_blocks_.find(ptr);
68 if (it == allocated_blocks_.end()) {
69 // System allocated, use free
70 std::free(ptr);
71 return;
72 }
73
74 MemoryBlock* block = it->second;
75 if (block) {
76 block->in_use = false;
77 total_used_bytes_ -= block->size;
78 }
79
80 allocated_blocks_.erase(it);
81}
82
83void* MemoryPool::AllocateAligned(size_t size, size_t alignment) {
84 // For simplicity, allocate extra space and align manually
85 // In a production system, you'd want more sophisticated alignment handling
86 size_t aligned_size = size + alignment - 1;
87 void* ptr = Allocate(aligned_size);
88
89 if (ptr) {
90 uintptr_t addr = reinterpret_cast<uintptr_t>(ptr);
91 uintptr_t aligned_addr = (addr + alignment - 1) & ~(alignment - 1);
92 return reinterpret_cast<void*>(aligned_addr);
93 }
94
95 return nullptr;
96}
97
98std::pair<size_t, size_t> MemoryPool::GetMemoryStats() const {
100}
101
102std::pair<size_t, size_t> MemoryPool::GetAllocationStats() const {
104}
105
107 // Reset all blocks to unused state
108 for (auto& block : small_blocks_) {
109 block.in_use = false;
110 }
111 for (auto& block : medium_blocks_) {
112 block.in_use = false;
113 }
114 for (auto& block : large_blocks_) {
115 block.in_use = false;
116 }
117 for (auto& block : huge_blocks_) {
118 block.in_use = false;
119 }
120
121 allocated_blocks_.clear();
123}
124
126 // Determine which pool to use based on size
127 size_t pool_index = GetPoolIndex(size);
128
129 std::vector<MemoryBlock>* pools[] = {&small_blocks_, &medium_blocks_,
131
132 if (pool_index >= 4) {
133 return nullptr; // Size too large for any pool
134 }
135
136 auto& pool = *pools[pool_index];
137
138 // Find first unused block
139 auto it =
140 std::find_if(pool.begin(), pool.end(),
141 [](const MemoryBlock& block) { return !block.in_use; });
142
143 return (it != pool.end()) ? &(*it) : nullptr;
144}
145
146void MemoryPool::InitializeBlockPool(std::vector<MemoryBlock>& pool,
147 size_t block_size, size_t count) {
148 pool.reserve(count);
149
150 for (size_t i = 0; i < count; ++i) {
151 void* data = std::malloc(block_size);
152 if (data) {
153 pool.emplace_back(data, block_size);
154 }
155 }
156}
157
158size_t MemoryPool::GetPoolIndex(size_t size) const {
159 if (size <= kSmallBlockSize)
160 return 0;
161 if (size <= kMediumBlockSize)
162 return 1;
163 if (size <= kLargeBlockSize)
164 return 2;
165 if (size <= kHugeBlockSize)
166 return 3;
167 return 4; // Too large for any pool
168}
169
170} // namespace gfx
171} // namespace yaze
High-performance memory pool allocator for graphics data.
Definition memory_pool.h:38
static constexpr size_t kSmallBlockSize
Definition memory_pool.h:95
void Deallocate(void *ptr)
Deallocate memory block.
size_t GetPoolIndex(size_t size) const
static constexpr size_t kHugeBlockSize
void * Allocate(size_t size)
Allocate memory block of specified size.
void * AllocateAligned(size_t size, size_t alignment)
Allocate memory block aligned to specified boundary.
static constexpr size_t kLargeBlockSize
Definition memory_pool.h:98
std::vector< MemoryBlock > medium_blocks_
static constexpr size_t kMediumBlockSize
Definition memory_pool.h:96
std::unordered_map< void *, MemoryBlock * > allocated_blocks_
std::vector< MemoryBlock > huge_blocks_
std::vector< MemoryBlock > small_blocks_
MemoryBlock * FindFreeBlock(size_t size)
void Clear()
Clear all allocated blocks (for cleanup)
std::pair< size_t, size_t > GetAllocationStats() const
Get allocation statistics.
std::pair< size_t, size_t > GetMemoryStats() const
Get memory usage statistics.
static MemoryPool & Get()
void InitializeBlockPool(std::vector< MemoryBlock > &pool, size_t block_size, size_t count)
std::vector< MemoryBlock > large_blocks_