FreeNOS
|
Memory allocator which uses pools that each manage same-sized objects. More...
#include <PoolAllocator.h>
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... | |
Allocator * | parent () |
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... | |
Pool * | retrievePool (const Size inputSize) |
Find a Pool of sufficient size. More... | |
Pool * | allocatePool (const uint index, const Size objectCount) |
Creates a new Pool instance. More... | |
Result | releasePool (Pool *pool) |
Release Pool instance memory. More... | |
Private Attributes | |
Pool * | m_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 Allocator * | getDefault () |
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... | |
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.
Definition at line 45 of file PoolAllocator.h.
|
private |
Appended in memory after each object.
|
private |
This data structure is prepended in memory before each object.
|
private |
Allocates same-sized objects from a contiguous block of memory.
PoolAllocator::PoolAllocator | ( | Allocator * | parent | ) |
Constructor.
parent | Allocator 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().
|
virtual |
Allocate memory.
args | Contains the requested size and alignment on input. On output, contains the actual allocated address. |
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.
|
private |
Creates a new Pool instance.
index | Index in the Pools array |
objectCount | Allocate for this many of blocks from our parent. |
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().
|
virtual |
Get memory available.
Reimplemented from Allocator.
Definition at line 38 of file PoolAllocator.cpp.
References assert, and calculateUsage().
Calculate minimum object count for a Pool.
objectSize | Size per-object in bytes |
Definition at line 56 of file PoolAllocator.cpp.
References assert, isPowerOfTwo(), and KiloByte.
Referenced by retrievePool().
Calculate object size given the Pool index number.
index | Index number in m_pools |
Definition at line 48 of file PoolAllocator.cpp.
References assert, MaximumPoolSize, and MinimumPoolSize.
Referenced by allocatePool(), and retrievePool().
Determine total memory usage.
totalSize | Total memory in bytes owned by the PoolAllocator. |
totalUsed | Total 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().
|
virtual |
Release memory.
addr | Points to memory previously returned by allocate(). |
Reimplemented from Allocator.
Definition at line 130 of file PoolAllocator.cpp.
References assert, NULL, ObjectSignature, releasePool(), PoolAllocator::ObjectPostfix::signature, Allocator::Success, and ZERO.
|
private |
Release Pool instance memory.
pool | Pool object pointer |
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().
|
private |
Find a Pool of sufficient size.
inputSize | Requested size of object to store |
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().
|
virtual |
Get memory size.
Reimplemented from Allocator.
Definition at line 30 of file PoolAllocator.cpp.
References calculateUsage().
|
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().
|
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().
|
staticprivate |
Minimum power of two for a pool size.
Definition at line 50 of file PoolAllocator.h.
Referenced by calculateObjectSize(), calculateUsage(), and retrievePool().
|
staticprivate |
Signature value is used to detect object corruption/overflows.
Definition at line 56 of file PoolAllocator.h.
Referenced by allocate(), and release().