27 #include "precompiled.h"
148 return current < successor;
169 template<
class InsertPolicy>
176 if(InsertPolicy::ShouldInsertBefore(freedBlock, successor))
181 freedBlock->
next = successor;
183 successor->
prev = freedBlock;
196 if(freedBlock->Size() >= minSize)
220 size_t freeBlocks = 0, freeBytes = 0;
224 freedBlock->Validate(
id);
226 freeBytes += freedBlock->Size();
231 freedBlock->Validate(
id);
233 freeBytes += freedBlock->Size();
289 m_bitmap |= Bit<uintptr_t>(sizeClass);
299 const size_t minSizeClass =
SizeClass(minSize);
300 size_t sizeClassBits =
m_bitmap & ((~size_t(0)) << minSizeClass);
304 sizeClassBits &= ~size;
305 const size_t sizeClass =
SizeClass(size);
325 m_bitmap &= ~Bit<uintptr_t>(sizeClass);
341 size_t freeBlocks = 0;
349 size_t freeBytes = 0;
367 return (x & -(intptr_t)x);
439 if(p == beginningOfPool)
452 return precedingBlock;
457 if(p+size == endOfPool)
465 return followingBlock;
498 u8*
const p = (
u8*)freedBlock;
601 ENSURE(x1 == x2 && x2 == x3);
702 p -= precedingBlock->
Size();
703 size += precedingBlock->
Size();
712 size += followingBlock->
Size();
721 size_t leftoverSize = 0;
728 leftoverSize = freedBlock->
Size() - size;
748 : impl(new
Impl(poolSize))
754 return impl->Reset();
759 return impl->Allocate(size);
764 return impl->Deallocate((
u8*)p, size);
769 return impl->Validate();
void OnAddToFreelist(size_t size)
size_t FreeBlocks() const
void Validate(uintptr_t id) const
void RemoveTags(FreedBlock *freedBlock)
void * pool_alloc(Pool *p, size_t size)
Dole out memory from the pool.
FreedBlock * PrecedingBlock(u8 *p, u8 *beginningOfPool) const
void OnDeallocate(size_t size)
u64 m_totalAllocatedBlocks
RangeList m_rangeLists[numRangeLists]
size_t FreeBlocks() const
static FreedBlock * Footer(FreedBlock *freedBlock)
void Insert(FreedBlock *freedBlock)
Status pool_create(Pool *p, size_t max_size, size_t el_size)
Ready Pool for use.
size_t FreeBlocks() const
static const uintptr_t s_footerId
void OnRemoveFromFreelist(size_t size)
void OnAllocate(size_t size)
void Remove(FreedBlock *freedBlock)
static bool ShouldInsertBefore(FreedBlock *current, FreedBlock *successor)
#define ENSURE(expr)
ensure the expression <expr> evaluates to non-zero.
u64 m_totalAllocatedBytes
void Validate(uintptr_t id) const
warn if any invariant doesn't hold.
void Validate(FreedBlock *freedBlock) const
size_t FreeBlocks() const
bool IsFreedBlock(uintptr_t id) const
static const size_t numRangeLists
allocator design parameters:
void Insert(FreedBlock *freedBlock)
void Validate(uintptr_t id) const
FreedBlock * Find(size_t minSize)
FreedBlock * Find(size_t minSize)
static size_t SizeClass(size_t size)
static const uintptr_t s_headerId
u64 m_currentExtantBlocks
void pool_free_all(Pool *p)
"free" all user allocations that ensued from the given Pool.
size_t ceil_log2(T x)
ceil(log2(x))
static uintptr_t ValueOfLeastSignificantOneBit(uintptr_t x)
FreedBlock(uintptr_t id, size_t size)
Status pool_destroy(Pool *p)
free all memory (address space + physical) that constitutes the given Pool.
void Remove(FreedBlock *freedBlock)
#define cassert(expr)
Compile-time assertion.
u64 m_totalDeallocatedBytes
FreedBlock * WriteTags(u8 *p, size_t size)
FreedBlock * FollowingBlock(u8 *p, size_t size, u8 *endOfPool) const
u64 m_totalDeallocatedBlocks
bool pool_contains(const Pool *p, void *el)
indicate whether a pointer was allocated from the given pool.