ScummVM API documentation
Common::MemoryPool Class Reference

#include <memorypool.h>

Inheritance diagram for Common::MemoryPool:
Common::FixedSizeMemoryPool< CHUNK_SIZE, NUM_INTERNAL_CHUNKS > Common::FixedSizeMemoryPool< CHUNK_SIZE, 0 > Common::FixedSizeMemoryPool< sizeof(Common::HashMap::Node), NUM_INTERNAL_CHUNKS > Common::FixedSizeMemoryPool< sizeof(Common::XMLParser::ParserNode), NUM_INTERNAL_CHUNKS > Common::FixedSizeMemoryPool< sizeof(PendingEvent), 32 > Common::FixedSizeMemoryPool< sizeof(T), NUM_INTERNAL_CHUNKS > Common::ObjectPool< Common::HashMap::Node, HASHMAP_MEMORYPOOL_SIZE > Common::ObjectPool< Common::XMLParser::ParserNode, 8 > Common::ObjectPool< PendingEvent > Common::ObjectPool< T, NUM_INTERNAL_CHUNKS >

Classes

struct  Page
 

Public Member Functions

 MemoryPool (size_t chunkSize)
 
void * allocChunk ()
 
void freeChunk (void *ptr)
 
void freeUnusedPages ()
 
size_t getChunkSize () const
 

Protected Member Functions

 MemoryPool (const MemoryPool &)
 
MemoryPooloperator= (const MemoryPool &)
 
void allocPage ()
 
void addPageToPool (const Page &page)
 
bool isPointerInPage (void *ptr, const Page &page)
 

Protected Attributes

const size_t _chunkSize
 
Array< Page_pages
 
void * _next
 
size_t _chunksPerPage
 

Detailed Description

This class provides a pool of memory 'chunks' of identical size. The size of a chunk is determined when creating the memory pool.

Using a memory pool may yield better performance and memory usage when allocating and deallocating many memory blocks of equal size. E.g. the Common::String class uses a memory pool for the refCount variables (each the size of an int) it allocates for each string instance.

Constructor & Destructor Documentation

◆ MemoryPool()

Common::MemoryPool::MemoryPool ( size_t  chunkSize)
explicit

Constructor for a memory pool with the given chunk size.

Parameters
chunkSizethe chunk size of this memory pool

Member Function Documentation

◆ allocChunk()

void* Common::MemoryPool::allocChunk ( )

Allocate a new chunk from the memory pool.

◆ freeChunk()

void Common::MemoryPool::freeChunk ( void *  ptr)

Return a chunk to the memory pool. The given pointer must have been obtained from calling the allocChunk() method of the very same MemoryPool instance. Passing any other pointer (e.g. to a chunk from another MemoryPool, or a malloc'ed memory block) will lead to undefined behavior and may result in a crash (if you are lucky) or in silent data corruption.

◆ freeUnusedPages()

void Common::MemoryPool::freeUnusedPages ( )

Perform garbage collection. The memory pool stores all the chunks it manages in memory 'pages' obtained via the classic memory allocation APIs (i.e. malloc/free). Ordinarily, once a page has been allocated, it won't be released again during the life time of the memory pool. The exception is when this method is called.

◆ getChunkSize()

size_t Common::MemoryPool::getChunkSize ( ) const
inline

Return the chunk size used by this memory pool.


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