18 : total_allocations_(0),
19 total_deallocations_(0),
21 total_allocated_bytes_(0) {
46 void* data = std::malloc(size);
86 size_t aligned_size = size + alignment - 1;
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);
109 block.in_use =
false;
112 block.in_use =
false;
115 block.in_use =
false;
118 block.in_use =
false;
132 if (pool_index >= 4) {
136 auto& pool = *pools[pool_index];
140 std::find_if(pool.begin(), pool.end(),
141 [](
const MemoryBlock& block) { return !block.in_use; });
143 return (it != pool.end()) ? &(*it) :
nullptr;
147 size_t block_size,
size_t count) {
150 for (
size_t i = 0; i < count; ++i) {
151 void* data = std::malloc(block_size);
153 pool.emplace_back(data, block_size);
High-performance memory pool allocator for graphics data.
size_t total_allocated_bytes_
static constexpr size_t kSmallBlockSize
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
std::vector< MemoryBlock > medium_blocks_
static constexpr size_t kMediumBlockSize
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()
size_t total_allocations_
size_t total_deallocations_
void InitializeBlockPool(std::vector< MemoryBlock > &pool, size_t block_size, size_t count)
std::vector< MemoryBlock > large_blocks_