yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
yaze::gfx::MemoryPool Class Reference

High-performance memory pool allocator for graphics data. More...

#include <memory_pool.h>

Classes

struct  MemoryBlock
 

Public Member Functions

void * Allocate (size_t size)
 Allocate memory block of specified size.
 
void Deallocate (void *ptr)
 Deallocate memory block.
 
void * AllocateAligned (size_t size, size_t alignment)
 Allocate memory block aligned to specified boundary.
 
std::pair< size_t, size_t > GetMemoryStats () const
 Get memory usage statistics.
 
std::pair< size_t, size_t > GetAllocationStats () const
 Get allocation statistics.
 
void Clear ()
 Clear all allocated blocks (for cleanup)
 

Static Public Member Functions

static MemoryPoolGet ()
 

Private Member Functions

 MemoryPool ()
 
 ~MemoryPool ()
 
MemoryBlockFindFreeBlock (size_t size)
 
void InitializeBlockPool (std::vector< MemoryBlock > &pool, size_t block_size, size_t count)
 
size_t GetPoolIndex (size_t size) const
 

Private Attributes

std::vector< MemoryBlocksmall_blocks_
 
std::vector< MemoryBlockmedium_blocks_
 
std::vector< MemoryBlocklarge_blocks_
 
std::vector< MemoryBlockhuge_blocks_
 
std::unordered_map< void *, MemoryBlock * > allocated_blocks_
 
size_t total_allocations_
 
size_t total_deallocations_
 
size_t total_used_bytes_
 
size_t total_allocated_bytes_
 

Static Private Attributes

static constexpr size_t kSmallBlockSize = 1024
 
static constexpr size_t kMediumBlockSize
 
static constexpr size_t kLargeBlockSize
 
static constexpr size_t kHugeBlockSize
 

Detailed Description

High-performance memory pool allocator for graphics data.

The MemoryPool class provides efficient memory management for graphics operations in the YAZE ROM hacking editor. It reduces memory fragmentation and allocation overhead through pre-allocated memory blocks.

Key Features:

  • Pre-allocated memory blocks for common graphics sizes
  • O(1) allocation and deallocation
  • Automatic block size management
  • Memory usage tracking and statistics
  • Thread-safe operations

Performance Optimizations:

  • Eliminates malloc/free overhead for graphics data
  • Reduces memory fragmentation
  • Fast allocation for common sizes (8x8, 16x16, 32x32 tiles)
  • Automatic block reuse and recycling

ROM Hacking Specific:

  • Optimized for SNES tile sizes (8x8, 16x16)
  • Support for graphics sheet buffers (128x128, 256x256)
  • Efficient palette data allocation
  • Tile cache memory management

Definition at line 38 of file memory_pool.h.

Constructor & Destructor Documentation

◆ MemoryPool()

yaze::gfx::MemoryPool::MemoryPool ( )
private

◆ ~MemoryPool()

yaze::gfx::MemoryPool::~MemoryPool ( )
private

Definition at line 30 of file memory_pool.cc.

References Clear().

Here is the call graph for this function:

Member Function Documentation

◆ Get()

◆ Allocate()

void * yaze::gfx::MemoryPool::Allocate ( size_t  size)

Allocate memory block of specified size.

Parameters
sizeSize in bytes
Returns
Pointer to allocated memory block

Definition at line 34 of file memory_pool.cc.

References allocated_blocks_, yaze::gfx::MemoryPool::MemoryBlock::data, FindFreeBlock(), yaze::gfx::MemoryPool::MemoryBlock::in_use, yaze::gfx::MemoryPool::MemoryBlock::size, total_allocations_, and total_used_bytes_.

Referenced by yaze::gfx::PoolAllocator< T >::allocate(), and AllocateAligned().

Here is the call graph for this function:

◆ Deallocate()

void yaze::gfx::MemoryPool::Deallocate ( void *  ptr)

Deallocate memory block.

Parameters
ptrPointer to memory block to deallocate

Definition at line 55 of file memory_pool.cc.

References allocated_blocks_, yaze::gfx::MemoryPool::MemoryBlock::in_use, yaze::gfx::MemoryPool::MemoryBlock::size, total_deallocations_, and total_used_bytes_.

Referenced by yaze::gfx::PoolAllocator< T >::deallocate().

◆ AllocateAligned()

void * yaze::gfx::MemoryPool::AllocateAligned ( size_t  size,
size_t  alignment 
)

Allocate memory block aligned to specified boundary.

Parameters
sizeSize in bytes
alignmentAlignment boundary (must be power of 2)
Returns
Pointer to aligned memory block

Definition at line 76 of file memory_pool.cc.

References Allocate().

Here is the call graph for this function:

◆ GetMemoryStats()

std::pair< size_t, size_t > yaze::gfx::MemoryPool::GetMemoryStats ( ) const

◆ GetAllocationStats()

std::pair< size_t, size_t > yaze::gfx::MemoryPool::GetAllocationStats ( ) const

Get allocation statistics.

Returns
Pair of (allocations, deallocations)

Definition at line 95 of file memory_pool.cc.

References total_allocations_, and total_deallocations_.

◆ Clear()

void yaze::gfx::MemoryPool::Clear ( )

Clear all allocated blocks (for cleanup)

Definition at line 99 of file memory_pool.cc.

References allocated_blocks_, huge_blocks_, large_blocks_, medium_blocks_, small_blocks_, and total_used_bytes_.

Referenced by ~MemoryPool().

◆ FindFreeBlock()

MemoryBlock * yaze::gfx::MemoryPool::FindFreeBlock ( size_t  size)
private

Definition at line 118 of file memory_pool.cc.

References GetPoolIndex(), huge_blocks_, large_blocks_, medium_blocks_, and small_blocks_.

Referenced by Allocate().

Here is the call graph for this function:

◆ InitializeBlockPool()

void yaze::gfx::MemoryPool::InitializeBlockPool ( std::vector< MemoryBlock > &  pool,
size_t  block_size,
size_t  count 
)
private

Definition at line 139 of file memory_pool.cc.

Referenced by MemoryPool().

◆ GetPoolIndex()

size_t yaze::gfx::MemoryPool::GetPoolIndex ( size_t  size) const
private

Definition at line 151 of file memory_pool.cc.

References kHugeBlockSize, kLargeBlockSize, kMediumBlockSize, and kSmallBlockSize.

Referenced by FindFreeBlock().

Member Data Documentation

◆ kSmallBlockSize

constexpr size_t yaze::gfx::MemoryPool::kSmallBlockSize = 1024
staticconstexprprivate

Definition at line 95 of file memory_pool.h.

Referenced by GetPoolIndex(), and MemoryPool().

◆ kMediumBlockSize

constexpr size_t yaze::gfx::MemoryPool::kMediumBlockSize
staticconstexprprivate
Initial value:
=
4096

Definition at line 96 of file memory_pool.h.

Referenced by GetPoolIndex(), and MemoryPool().

◆ kLargeBlockSize

constexpr size_t yaze::gfx::MemoryPool::kLargeBlockSize
staticconstexprprivate
Initial value:
=
16384

Definition at line 98 of file memory_pool.h.

Referenced by GetPoolIndex(), and MemoryPool().

◆ kHugeBlockSize

constexpr size_t yaze::gfx::MemoryPool::kHugeBlockSize
staticconstexprprivate
Initial value:
=
65536

Definition at line 100 of file memory_pool.h.

Referenced by GetPoolIndex(), and MemoryPool().

◆ small_blocks_

std::vector<MemoryBlock> yaze::gfx::MemoryPool::small_blocks_
private

Definition at line 104 of file memory_pool.h.

Referenced by Clear(), FindFreeBlock(), and MemoryPool().

◆ medium_blocks_

std::vector<MemoryBlock> yaze::gfx::MemoryPool::medium_blocks_
private

Definition at line 105 of file memory_pool.h.

Referenced by Clear(), FindFreeBlock(), and MemoryPool().

◆ large_blocks_

std::vector<MemoryBlock> yaze::gfx::MemoryPool::large_blocks_
private

Definition at line 106 of file memory_pool.h.

Referenced by Clear(), FindFreeBlock(), and MemoryPool().

◆ huge_blocks_

std::vector<MemoryBlock> yaze::gfx::MemoryPool::huge_blocks_
private

Definition at line 107 of file memory_pool.h.

Referenced by Clear(), FindFreeBlock(), and MemoryPool().

◆ allocated_blocks_

std::unordered_map<void*, MemoryBlock*> yaze::gfx::MemoryPool::allocated_blocks_
private

Definition at line 110 of file memory_pool.h.

Referenced by Allocate(), Clear(), and Deallocate().

◆ total_allocations_

size_t yaze::gfx::MemoryPool::total_allocations_
private

Definition at line 111 of file memory_pool.h.

Referenced by Allocate(), and GetAllocationStats().

◆ total_deallocations_

size_t yaze::gfx::MemoryPool::total_deallocations_
private

Definition at line 112 of file memory_pool.h.

Referenced by Deallocate(), and GetAllocationStats().

◆ total_used_bytes_

size_t yaze::gfx::MemoryPool::total_used_bytes_
private

Definition at line 113 of file memory_pool.h.

Referenced by Allocate(), Clear(), Deallocate(), and GetMemoryStats().

◆ total_allocated_bytes_

size_t yaze::gfx::MemoryPool::total_allocated_bytes_
private

Definition at line 114 of file memory_pool.h.

Referenced by GetMemoryStats(), and MemoryPool().


The documentation for this class was generated from the following files: