FreeNOS
Data Structures | Public Member Functions | Private Types | Private Member Functions | Private Attributes | Static Private Attributes
PoolAllocator Class Reference

Memory allocator which uses pools that each manage same-sized objects. More...

#include <PoolAllocator.h>

Inheritance diagram for PoolAllocator:
Allocator

Data Structures

struct  ObjectPostfix
 Appended in memory after each object. More...
 
struct  ObjectPrefix
 This data structure is prepended in memory before each object. More...
 
struct  Pool
 Allocates same-sized objects from a contiguous block of memory. More...
 

Public Member Functions

 PoolAllocator (Allocator *parent)
 Constructor. More...
 
virtual Size size () const
 Get memory size. More...
 
virtual Size available () const
 Get memory available. More...
 
virtual Result allocate (Range &args)
 Allocate memory. More...
 
virtual Result release (const Address addr)
 Release memory. More...
 
- Public Member Functions inherited from Allocator
 Allocator ()
 Default class constructor. More...
 
 Allocator (const Range range)
 Class constructor with Range input. More...
 
virtual ~Allocator ()
 Class destructor. More...
 
void setParent (Allocator *parent)
 Set parent allocator. More...
 
Allocatorparent ()
 Get parent Allocator. More...
 
Address base () const
 Get memory base address for allocations. More...
 
Size alignment () const
 Get memory alignment in bytes for allocations. More...
 

Private Types

typedef PoolAllocator::Pool Pool
 Allocates same-sized objects from a contiguous block of memory. More...
 
typedef struct PoolAllocator::ObjectPrefix ObjectPrefix
 This data structure is prepended in memory before each object. More...
 
typedef struct PoolAllocator::ObjectPostfix ObjectPostfix
 Appended in memory after each object. More...
 

Private Member Functions

Size calculateObjectSize (const Size index) const
 Calculate object size given the Pool index number. More...
 
Size calculateObjectCount (const Size objectSize) const
 Calculate minimum object count for a Pool. More...
 
void calculateUsage (Size &totalSize, Size &totalUsed) const
 Determine total memory usage. More...
 
PoolretrievePool (const Size inputSize)
 Find a Pool of sufficient size. More...
 
PoolallocatePool (const uint index, const Size objectCount)
 Creates a new Pool instance. More...
 
Result releasePool (Pool *pool)
 Release Pool instance memory. More...
 

Private Attributes

Poolm_pools [MaximumPoolSize+1]
 Array of memory pools. More...
 

Static Private Attributes

static const Size MinimumPoolSize = 2
 Minimum power of two for a pool size. More...
 
static const Size MaximumPoolSize = 27
 Maximum power of two size a pool can be (128MiB). More...
 
static const u32 ObjectSignature = 0xF7312A56
 Signature value is used to detect object corruption/overflows. More...
 

Additional Inherited Members

- Public Types inherited from Allocator
enum  Result {
  Success = 0, InvalidAddress, InvalidSize, InvalidAlignment,
  OutOfMemory
}
 Allocation results. More...
 
typedef struct Allocator::Range Range
 Describes a range of memory. More...
 
- Static Public Member Functions inherited from Allocator
static void setDefault (Allocator *alloc)
 Makes the given Allocator the default. More...
 
static AllocatorgetDefault ()
 Retrieve the currently default Allocator. More...
 
- Protected Member Functions inherited from Allocator
Address aligned (const Address addr, const Size boundary) const
 Align memory address. More...
 

Detailed Description

Memory allocator which uses pools that each manage same-sized objects.

Allocates memory from pools each having the size of a power of two. Each pool is pre-allocated and has a bitmap representing free blocks.

Todo:
While this Allocator works well, its performance can be improved. The BitAllocator contains a BitArray that scans its internal array for "free bits". If the caller is unfortunate, the whole array needs to be scanned, adding overhead. The Linux kernel uses a buddy allocator, that basically combines a bit array and a linked list for optimal performance.

Definition at line 45 of file PoolAllocator.h.

Member Typedef Documentation

◆ ObjectPostfix

Appended in memory after each object.

◆ ObjectPrefix

This data structure is prepended in memory before each object.

◆ Pool

Allocates same-sized objects from a contiguous block of memory.

Constructor & Destructor Documentation

◆ PoolAllocator()

PoolAllocator::PoolAllocator ( Allocator parent)

Constructor.

Parameters
parentAllocator for obtaining new memory to manage

Definition at line 23 of file PoolAllocator.cpp.

References assert, m_pools, NULL, Allocator::parent(), MemoryBlock::set(), and Allocator::setParent().

Member Function Documentation

◆ allocate()

Allocator::Result PoolAllocator::allocate ( Allocator::Range args)
virtual

Allocate memory.

Parameters
argsContains the requested size and alignment on input. On output, contains the actual allocated address.
Returns
Result value.

Reimplemented from Allocator.

Definition at line 87 of file PoolAllocator.cpp.

References Allocator::Range::address, Allocator::aligned(), Allocator::Range::alignment, Allocator::InvalidAlignment, Allocator::InvalidSize, ObjectSignature, Allocator::OutOfMemory, PoolAllocator::ObjectPrefix::pool, retrievePool(), PoolAllocator::ObjectPrefix::signature, PoolAllocator::ObjectPostfix::signature, Allocator::Range::size, Allocator::Success, and ZERO.

◆ allocatePool()

PoolAllocator::Pool * PoolAllocator::allocatePool ( const uint  index,
const Size  objectCount 
)
private

Creates a new Pool instance.

Parameters
indexIndex in the Pools array
objectCountAllocate for this many of blocks from our parent.
Returns
Pointer to a Pool object on success, ZERO on failure.

Definition at line 212 of file PoolAllocator.cpp.

References Allocator::Range::address, Allocator::aligned(), Allocator::Range::alignment, Allocator::allocate(), assert, calculateObjectSize(), PoolAllocator::Pool::index, m_pools, PoolAllocator::Pool::next, NULL, Allocator::parent(), PoolAllocator::Pool::prev, Allocator::Range::size, Allocator::Success, and ZERO.

Referenced by retrievePool().

◆ available()

Size PoolAllocator::available ( ) const
virtual

Get memory available.

Returns
Size of memory available by the PoolAllocator.

Reimplemented from Allocator.

Definition at line 38 of file PoolAllocator.cpp.

References assert, and calculateUsage().

◆ calculateObjectCount()

Size PoolAllocator::calculateObjectCount ( const Size  objectSize) const
private

Calculate minimum object count for a Pool.

Parameters
objectSizeSize per-object in bytes
Returns
Minimum number of objects to allocate

Definition at line 56 of file PoolAllocator.cpp.

References assert, isPowerOfTwo(), and KiloByte.

Referenced by retrievePool().

◆ calculateObjectSize()

Size PoolAllocator::calculateObjectSize ( const Size  index) const
private

Calculate object size given the Pool index number.

Parameters
indexIndex number in m_pools
Returns
Size in bytes

Definition at line 48 of file PoolAllocator.cpp.

References assert, MaximumPoolSize, and MinimumPoolSize.

Referenced by allocatePool(), and retrievePool().

◆ calculateUsage()

void PoolAllocator::calculateUsage ( Size totalSize,
Size totalUsed 
) const
private

Determine total memory usage.

Parameters
totalSizeTotal memory in bytes owned by the PoolAllocator.
totalUsedTotal memory in bytes actually used.

Definition at line 67 of file PoolAllocator.cpp.

References m_pools, MaximumPoolSize, MinimumPoolSize, and NULL.

Referenced by available(), and size().

◆ release()

Allocator::Result PoolAllocator::release ( const Address  addr)
virtual

Release memory.

Parameters
addrPoints to memory previously returned by allocate().
Returns
Result value.
See also
allocate

Reimplemented from Allocator.

Definition at line 130 of file PoolAllocator.cpp.

References assert, NULL, ObjectSignature, releasePool(), PoolAllocator::ObjectPostfix::signature, Allocator::Success, and ZERO.

◆ releasePool()

Allocator::Result PoolAllocator::releasePool ( Pool pool)
private

Release Pool instance memory.

Parameters
poolPool object pointer
Returns
Result value.

Definition at line 272 of file PoolAllocator.cpp.

References PoolAllocator::Pool::index, m_pools, PoolAllocator::Pool::next, NULL, Allocator::parent(), PoolAllocator::Pool::prev, Allocator::release(), and Allocator::Success.

Referenced by release().

◆ retrievePool()

PoolAllocator::Pool * PoolAllocator::retrievePool ( const Size  inputSize)
private

Find a Pool of sufficient size.

Parameters
inputSizeRequested size of object to store
Returns
Pool object pointer on success or NULL on failure.

Definition at line 165 of file PoolAllocator.cpp.

References allocatePool(), BitAllocator::available(), calculateObjectCount(), calculateObjectSize(), m_pools, MaximumPoolSize, MinimumPoolSize, PoolAllocator::Pool::next, and ZERO.

Referenced by allocate().

◆ size()

Size PoolAllocator::size ( ) const
virtual

Get memory size.

Returns
Size of memory owned by the PoolAllocator.

Reimplemented from Allocator.

Definition at line 30 of file PoolAllocator.cpp.

References calculateUsage().

Field Documentation

◆ m_pools

Pool* PoolAllocator::m_pools[MaximumPoolSize+1]
private

Array of memory pools.

Index represents the power of two.

Definition at line 201 of file PoolAllocator.h.

Referenced by allocatePool(), calculateUsage(), PoolAllocator(), releasePool(), and retrievePool().

◆ MaximumPoolSize

const Size PoolAllocator::MaximumPoolSize = 27
staticprivate

Maximum power of two size a pool can be (128MiB).

Definition at line 53 of file PoolAllocator.h.

Referenced by calculateObjectSize(), calculateUsage(), and retrievePool().

◆ MinimumPoolSize

const Size PoolAllocator::MinimumPoolSize = 2
staticprivate

Minimum power of two for a pool size.

Definition at line 50 of file PoolAllocator.h.

Referenced by calculateObjectSize(), calculateUsage(), and retrievePool().

◆ ObjectSignature

const u32 PoolAllocator::ObjectSignature = 0xF7312A56
staticprivate

Signature value is used to detect object corruption/overflows.

Definition at line 56 of file PoolAllocator.h.

Referenced by allocate(), and release().


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