18 : total_allocations_(0),
19 total_deallocations_(0),
21 total_allocated_bytes_(0) {
43 for (
auto& block : *pool) {
44 std::free(block.data);
57 void* data = std::malloc(size);
102 size_t aligned_size = size + alignment - 1;
106 uintptr_t addr =
reinterpret_cast<uintptr_t
>(ptr);
107 uintptr_t aligned_addr = (addr + alignment - 1) & ~(alignment - 1);
108 return reinterpret_cast<void*
>(aligned_addr);
125 block.in_use =
false;
128 block.in_use =
false;
131 block.in_use =
false;
134 block.in_use =
false;
148 if (pool_index >= 4) {
152 auto& pool = *pools[pool_index];
156 std::find_if(pool.begin(), pool.end(),
157 [](
const MemoryBlock& block) { return !block.in_use; });
159 return (it != pool.end()) ? &(*it) :
nullptr;
163 size_t block_size,
size_t count) {
166 for (
size_t i = 0; i < count; ++i) {
167 void* data = std::malloc(block_size);
169 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_