32 Size totalSize, totalUsed;
40 Size totalSize, totalUsed;
43 assert(totalUsed <= totalSize);
45 return totalSize - totalUsed;
76 totalSize +=
sizeof(
Pool);
77 totalSize += pool->bitmapSize;
78 totalSize += pool->size();
80 totalUsed +=
sizeof(
Pool);
81 totalUsed += pool->bitmapSize;
82 totalUsed += (pool->size() - pool->available());
97 else if (inputSize == 0)
153 Result result = prefix->pool->release(actualAddr);
157 if (prefix->pool->available() == prefix->pool->size())
168 Size index, nPools = 1, objectSize = 0;
176 if (requestedSize <= objectSize)
181 if (requestedSize > objectSize)
194 for (pool =
m_pools[index]; pool; pool = pool->
next, nPools++)
201 else if (!pool->
next)
215 const Size requestBitmapSize = objectCount;
216 const Size requestPayloadSize = objectCount * objectSize;
217 const Size requestTotalSize =
aligned(
sizeof(
Pool) + requestBitmapSize + requestPayloadSize,
sizeof(
u32));
224 alloc_args.
size = requestTotalSize;
236 Size actualObjectCount = (alloc_args.
size -
sizeof(
Pool) - requestBitmapSize) / objectSize;
237 Size actualPayloadSize = 0;
238 Size actualBitmapSize = 0;
239 Size actualTotalSize = 0;
241 assert(actualObjectCount >= objectCount);
243 while (actualObjectCount >= objectCount)
245 actualPayloadSize = actualObjectCount * objectSize;
246 actualBitmapSize =
aligned(actualObjectCount,
sizeof(
u32));
247 actualTotalSize =
sizeof(
Pool) + actualBitmapSize + actualPayloadSize;
249 if (actualTotalSize <= alloc_args.
size)
257 const Allocator::Range range = { bitmapAddr + actualBitmapSize, actualPayloadSize,
sizeof(
u32) };
260 pool =
new (alloc_args.
address)
Pool(range, objectSize, actualBitmapSize, (
u8 *) bitmapAddr);
282 if (prevPool !=
NULL)
284 prevPool->
next = nextPool;
287 if (nextPool !=
NULL)
289 nextPool->
prev = prevPool;
virtual Result release(const Address addr)
Release memory.
Allocator * parent()
Get parent Allocator.
virtual Result allocate(Range &range)
Allocate memory.
void setParent(Allocator *parent)
Set parent allocator.
Result
Allocation results.
Address aligned(const Address addr, const Size boundary) const
Align memory address.
virtual Size available() const
Get available memory.
static void * set(void *dest, int ch, unsigned count)
Fill memory with a constant byte.
static const Size MaximumPoolSize
Maximum power of two size a pool can be (128MiB).
Size calculateObjectCount(const Size objectSize) const
Calculate minimum object count for a Pool.
static const Size MinimumPoolSize
Minimum power of two for a pool size.
Pool * m_pools[MaximumPoolSize+1]
Array of memory pools.
PoolAllocator(Allocator *parent)
Constructor.
virtual Result allocate(Range &args)
Allocate memory.
Size calculateObjectSize(const Size index) const
Calculate object size given the Pool index number.
virtual Size size() const
Get memory size.
void calculateUsage(Size &totalSize, Size &totalUsed) const
Determine total memory usage.
static const u32 ObjectSignature
Signature value is used to detect object corruption/overflows.
virtual Size available() const
Get memory available.
Pool * retrievePool(const Size inputSize)
Find a Pool of sufficient size.
Result releasePool(Pool *pool)
Release Pool instance memory.
Pool * allocatePool(const uint index, const Size objectCount)
Creates a new Pool instance.
virtual Result release(const Address addr)
Release memory.
#define assert(exp)
Insert program diagnostics.
#define NULL
NULL means zero.
unsigned int u32
Unsigned 32-bit number.
unsigned long Address
A memory address.
bool isPowerOfTwo(unsigned number)
Check if a number is power of two.
#define KiloByte(v)
Convert kilobytes to bytes.
unsigned int Size
Any sane size indicator cannot go negative.
unsigned char u8
Unsigned 8-bit number.
Describes a range of memory.
Size alignment
Alignment in bytes or ZERO for default alignment.
Address address
Starting address of the memory range.
Size size
Amount of memory in bytes.
Appended in memory after each object.
u32 signature
Filled with a fixed value to detect corruption/overflows.
This data structure is prepended in memory before each object.
Pool * pool
Points to the Pool instance where this object belongs to.
u32 signature
Filled with a fixed value to detect corruption/overflows.
Allocates same-sized objects from a contiguous block of memory.
Pool * next
Points to the next pool of this size (if any).
Pool * prev
Points to the previous pool of this size (if any).
Size index
Index number in the m_pools array where this Pool is stored.