yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
memory_pool.h
Go to the documentation of this file.
1#ifndef YAZE_APP_GFX_MEMORY_POOL_H
2#define YAZE_APP_GFX_MEMORY_POOL_H
3
4#include <cstdint>
5#include <memory>
6#include <unordered_map>
7#include <vector>
8
9namespace yaze {
10namespace gfx {
11
39 public:
40 static MemoryPool& Get();
41
47 void* Allocate(size_t size);
48
53 void Deallocate(void* ptr);
54
61 void* AllocateAligned(size_t size, size_t alignment);
62
67 std::pair<size_t, size_t> GetMemoryStats() const;
68
73 std::pair<size_t, size_t> GetAllocationStats() const;
74
78 void Clear();
79
80 struct MemoryBlock {
81 void* data;
82 size_t size;
83 bool in_use;
84 size_t alignment;
85
86 MemoryBlock(void* d, size_t s, size_t a = 0)
87 : data(d), size(s), in_use(false), alignment(a) {}
88 };
89
90 private:
91 MemoryPool();
93
94 // Block size categories for common graphics operations
95 static constexpr size_t kSmallBlockSize = 1024; // 8x8 tiles, small palettes
96 static constexpr size_t kMediumBlockSize =
97 4096; // 16x16 tiles, medium graphics
98 static constexpr size_t kLargeBlockSize =
99 16384; // 32x32 tiles, large graphics
100 static constexpr size_t kHugeBlockSize =
101 65536; // Graphics sheets, large buffers
102
103 // Pre-allocated block pools
104 std::vector<MemoryBlock> small_blocks_;
105 std::vector<MemoryBlock> medium_blocks_;
106 std::vector<MemoryBlock> large_blocks_;
107 std::vector<MemoryBlock> huge_blocks_;
108
109 // Allocation tracking
110 std::unordered_map<void*, MemoryBlock*> allocated_blocks_;
115
116 // Helper methods
117 MemoryBlock* FindFreeBlock(size_t size);
118 void InitializeBlockPool(std::vector<MemoryBlock>& pool, size_t block_size,
119 size_t count);
120 size_t GetPoolIndex(size_t size) const;
121};
122
127template <typename T>
129 public:
130 using value_type = T;
131 using pointer = T*;
132 using const_pointer = const T*;
133 using reference = T&;
134 using const_reference = const T&;
135 using size_type = std::size_t;
136 using difference_type = std::ptrdiff_t;
137
138 PoolAllocator() = default;
139 template <typename U>
141
143 return static_cast<pointer>(MemoryPool::Get().Allocate(n * sizeof(T)));
144 }
145
147
148 template <typename U>
149 bool operator==(const PoolAllocator<U>&) const {
150 return true;
151 }
152
153 template <typename U>
154 bool operator!=(const PoolAllocator<U>&) const {
155 return false;
156 }
157};
158
159} // namespace gfx
160} // namespace yaze
161
162#endif // YAZE_APP_GFX_MEMORY_POOL_H
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_
RAII wrapper for memory pool allocations.
void deallocate(pointer p, size_type)
pointer allocate(size_type n)
bool operator!=(const PoolAllocator< U > &) const
PoolAllocator(const PoolAllocator< U > &)
std::ptrdiff_t difference_type
bool operator==(const PoolAllocator< U > &) const
Main namespace for the application.
MemoryBlock(void *d, size_t s, size_t a=0)
Definition memory_pool.h:86