From c26a369762879dcaad8f507fedb5f9fd708c5f20 Mon Sep 17 00:00:00 2001 From: janwas Date: Sun, 21 Aug 2011 10:30:35 +0000 Subject: [PATCH] remove Bucket allocator (superseded by upcoming Arena/Pool with expandable Storage policies) This was SVN commit r10049. --- source/lib/allocators/bucket.cpp | 59 +++++++++++++++++++++++--------- source/lib/allocators/bucket.h | 12 ------- 2 files changed, 43 insertions(+), 28 deletions(-) diff --git a/source/lib/allocators/bucket.cpp b/source/lib/allocators/bucket.cpp index 5326a51048..4228d38226 100644 --- a/source/lib/allocators/bucket.cpp +++ b/source/lib/allocators/bucket.cpp @@ -35,6 +35,49 @@ const size_t bucketSize = 4000; +template +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 was actually allocated from is difficult: - // it may not be in the currently open bucket, so we'd have to - // iterate over the list - too much work. -} diff --git a/source/lib/allocators/bucket.h b/source/lib/allocators/bucket.h index 80ccacda60..e03ab517cd 100644 --- a/source/lib/allocators/bucket.h +++ b/source/lib/allocators/bucket.h @@ -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