1
0
forked from 0ad/0ad

remove Bucket allocator (superseded by upcoming Arena/Pool with expandable Storage policies)

This was SVN commit r10049.
This commit is contained in:
janwas 2011-08-21 10:30:35 +00:00
parent 1c2472e67a
commit c26a369762
2 changed files with 43 additions and 28 deletions

View File

@ -35,6 +35,49 @@
const size_t bucketSize = 4000;
template<typename T, class Allocator = Allocator_Heap, size_t bucketSize = pageSize>
class Buckets
{
public:
// (must round up because freelist stores pointers inside objects)
static const size_t objectSize = ROUND_UP(sizeof(T), sizeof(intptr_t));
Buckets(size_t maxObjects)
: storage(maxObjects*objectSize)
{
}
size_t RemainingObjects()
{
return (storage.MaxCapacity() - end) / objectSize;
}
T* Allocate()
{
void* p = mem_freelist_Detach(freelist);
if(p)
{
ASSERT(Contains(p));
return (T*)p;
}
return (T*)StorageAppend(storage, end, objectSize);
}
void Deallocate(T* p)
{
ASSERT(Contains(p));
mem_freelist_AddToFront(freelist, p);
}
private:
Storage storage;
size_t end;
void* freelist;
};
Status bucket_create(Bucket* b, size_t el_size)
{
b->freelist = mem_freelist_Sentinel();
@ -131,19 +174,3 @@ void* bucket_fast_alloc(Bucket* b)
b->pos += b->el_size;
return ret;
}
void bucket_free(Bucket* b, void* el)
{
if(b->el_size == 0)
{
DEBUG_WARN_ERR(ERR::LOGIC); // cannot free variable-size items
return;
}
mem_freelist_AddToFront(b->freelist, el);
// note: checking if <el> was actually allocated from <b> is difficult:
// it may not be in the currently open bucket, so we'd have to
// iterate over the list - too much work.
}

View File

@ -99,16 +99,4 @@ LIB_API void* bucket_alloc(Bucket* b, size_t size);
LIB_API void* bucket_fast_alloc(Bucket* b);
/**
* make an entry available for reuse in the given Bucket.
*
* this is not allowed if created for variable-size elements.
* rationale: avoids having to pass el_size here and compare with size when
* allocating; also prevents fragmentation and leaking memory.
*
* @param b Bucket*
* @param el entry allocated via bucket_alloc.
**/
LIB_API void bucket_free(Bucket* b, void* el);
#endif // #ifndef INCLUDED_ALLOCATORS_BUCKET