43 #ifndef __CU_FREE_LIST_H__
44 #define __CU_FREE_LIST_H__
50 #pragma mark FreeList Template
112 #pragma mark Constructors
122 FreeList() : _allocated(0), _released(0), _peaksize(0), _prealloc(nullptr),
123 _capacity(0), _expandable(false) { }
142 if (_prealloc !=
nullptr) {
158 CUAssertLog(capacity,
"An unexapandable free list must have non-zero capacity");
160 _capacity = capacity;
163 _prealloc = (_capacity > 0 ?
new T[
_capacity] :
nullptr);
179 bool init(
size_t capacity,
bool expand) {
180 CUAssertLog(capacity || expand,
"The free list must be expandable or have capacity non-zero");
181 _expandable = expand;
182 _capacity = capacity;
185 _prealloc = (_capacity > 0 ?
new T[
_capacity] :
nullptr);
201 static std::shared_ptr<FreeList<T>>
alloc(
size_t capacity=0,
bool expand=
false) {
202 std::shared_ptr<FreeList<T>> result = std::make_shared<FreeList<T>>();
203 return (result->init(capacity,expand) ? result :
nullptr);
207 #pragma mark Accessors
216 size_t getAvailable()
const {
return (_capacity-(
long)_allocated)+_freeobjs.size(); }
268 #pragma mark Memory Managment
304 virtual void clear();
309 #pragma mark Method Implementations
324 if (!_freeobjs.empty()) {
325 result = _freeobjs.front();
328 }
else if (_allocated < _capacity) {
329 result = &_prealloc[_allocated];
331 }
else if (_expandable) {
333 _expansion.push(result);
336 _peaksize = (_peaksize < getUsage() ? getUsage() : _peaksize);
356 CCASSERT(obj !=
nullptr,
"Attempt to free null pointer");
372 while (!_expansion.empty()) {
373 T* a = _expansion.front();
379 for(
int ii =0; ii < _capacity; ii++) {
380 _prealloc[ii].reset();
384 while (!_expansion.empty()) {
size_t _peaksize
Definition: CUFreeList.h:97
bool isExpandable() const
Definition: CUFreeList.h:256
size_t getCapacity() const
Definition: CUFreeList.h:226
Definition: CUFreeList.h:90
size_t getUsage() const
Definition: CUFreeList.h:236
FreeList()
Definition: CUFreeList.h:122
size_t _allocated
Definition: CUFreeList.h:93
size_t getAvailable() const
Definition: CUFreeList.h:216
void free(T *obj)
Definition: CUFreeList.h:355
static std::shared_ptr< FreeList< T > > alloc(size_t capacity=0, bool expand=false)
Definition: CUFreeList.h:201
bool _expandable
Definition: CUFreeList.h:108
std::queue< T * > _freeobjs
Definition: CUFreeList.h:105
std::queue< T * > _expansion
Definition: CUFreeList.h:110
size_t _released
Definition: CUFreeList.h:95
T * malloc()
Definition: CUFreeList.h:322
size_t getPeakUsage() const
Definition: CUFreeList.h:246
const T * getPreallocated() const
Definition: CUFreeList.h:265
void dispose()
Definition: CUFreeList.h:140
virtual void clear()
Definition: CUFreeList.h:370
~FreeList()
Definition: CUFreeList.h:131
bool init(size_t capacity, bool expand)
Definition: CUFreeList.h:179
Definition: CUAnimationNode.h:52
size_t _capacity
Definition: CUFreeList.h:102
T * _prealloc
Definition: CUFreeList.h:100
bool init(size_t capacity)
Definition: CUFreeList.h:157