18 : total_allocations_(0), total_deallocations_(0),
19 total_used_bytes_(0), total_allocated_bytes_(0) {
40 void* data = std::malloc(size);
79 size_t aligned_size = size + alignment - 1;
83 uintptr_t addr =
reinterpret_cast<uintptr_t
>(ptr);
84 uintptr_t aligned_addr = (addr + alignment - 1) & ~(alignment - 1);
85 return reinterpret_cast<void*
>(aligned_addr);
102 block.in_use =
false;
105 block.in_use =
false;
108 block.in_use =
false;
111 block.in_use =
false;
122 std::vector<MemoryBlock>* pools[] = {
126 if (pool_index >= 4) {
130 auto& pool = *pools[pool_index];
133 auto it = std::find_if(pool.begin(), pool.end(),
134 [](
const MemoryBlock& block) { return !block.in_use; });
136 return (it != pool.end()) ? &(*it) :
nullptr;
140 size_t block_size,
size_t count) {
143 for (
size_t i = 0; i < count; ++i) {
144 void* data = std::malloc(block_size);
146 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_
Main namespace for the application.