ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

This class provides a pool of memory 'chunks' of identical size. More...

#include <memorypool.h>

Inheritance diagram for Common::MemoryPool:
Collaboration diagram for Common::MemoryPool:

List of all members.

Classes

struct  Page

Public Member Functions

 MemoryPool (size_t chunkSize)
 Constructor for a memory pool with the given chunk size.
 ~MemoryPool ()
void * allocChunk ()
 Allocate a new chunk from the memory pool.
void freeChunk (void *ptr)
 Return a chunk to the memory pool.
void freeUnusedPages ()
 Perform garbage collection.
size_t getChunkSize () const
 Return the chunk size used by this memory pool.

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.

Definition at line 42 of file memorypool.h.


Constructor & Destructor Documentation

Common::MemoryPool::MemoryPool ( const MemoryPool  )  [protected]
Common::MemoryPool::MemoryPool ( size_t  chunkSize  )  [explicit]

Constructor for a memory pool with the given chunk size.

Parameters:
chunkSize the chunk size of this memory pool

Definition at line 43 of file memorypool.cpp.

Common::MemoryPool::~MemoryPool (  ) 

Definition at line 51 of file memorypool.cpp.


Member Function Documentation

void Common::MemoryPool::addPageToPool ( const Page page  )  [protected]

Definition at line 81 of file memorypool.cpp.

void * Common::MemoryPool::allocChunk (  ) 

Allocate a new chunk from the memory pool.

Definition at line 98 of file memorypool.cpp.

void Common::MemoryPool::allocPage (  )  [protected]

Definition at line 62 of file memorypool.cpp.

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.

Definition at line 109 of file memorypool.cpp.

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.

Definition at line 120 of file memorypool.cpp.

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

Return the chunk size used by this memory pool.

Definition at line 96 of file memorypool.h.

bool Common::MemoryPool::isPointerInPage ( void *  ptr,
const Page page 
) [protected]

Definition at line 116 of file memorypool.cpp.

MemoryPool& Common::MemoryPool::operator= ( const MemoryPool  )  [protected]

Member Data Documentation

const size_t Common::MemoryPool::_chunkSize [protected]

Definition at line 52 of file memorypool.h.

Definition at line 55 of file memorypool.h.

void* Common::MemoryPool::_next [protected]

Definition at line 54 of file memorypool.h.

Definition at line 53 of file memorypool.h.


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


Generated on Sat Jan 19 2019 05:05:53 for ResidualVM by doxygen 1.7.1
curved edge   curved edge