![]() |
Cornell Cocos
Cornell Extensions to Cocos2d
|
#include <CUGreedyFreeList.h>
Public Member Functions | |
GreedyFreeList (size_t capacity) | |
~GreedyFreeList () | |
virtual T * | alloc () override |
![]() | |
FreeList (size_t capacity=0, bool expand=false) | |
~FreeList () | |
size_t | getAvailable () const |
size_t | getCapacity () const |
size_t | getUsage () const |
size_t | getPeakUsage () const |
bool | isExpandable () const |
T * | getPreallocated () |
void | free (T *obj) |
virtual void | clear () |
Protected Attributes | |
queue< T * > | _allocation |
![]() | |
size_t | _allocated |
size_t | _released |
size_t | _peaksize |
T * | _prealloc |
size_t | _capacity |
queue< T * > | _freeobjs |
bool | _expandable |
queue< T * > | _expansion |
Template for a free list class with aggressive recycling.
This free list is not expandable, and never allocates memory beyond the preallocated capacity. Instead, if you attempt to allocate beyond the capacity, it will immediately recycle the oldest allocated object, even if it is not freed.
This sounds a bit unsafe. In order to use it safely, object pointers have to be prepared to be working with a reset object at any given time. In particular, it is designed for particle systems, where the particles are managed by a set that does not permit duplicates. That way, an allocation of a forceably recycled object will only appear once in the list.
In order to work properly, the objects allocated must be a class with the method
void reset();
The class does not have to formally subclass anything or implement an interface (C++ does not work that way). It just has to have this method. In addition, the class must have a default constructor with no arguments. You should have an init() method if you need to initialize the object after allocation.
This class owns all memory that it allocates. When the free list is deleted, all of the objects that it allocated will be deleted also.
|
inline |
Creates a new free list with the given capacity.
As greedy free lists are not expandable, the capacity must be non-zero.
capacity | the number of objects to preallocate |
|
inline |
Deletes this free list, releasing all memory.
A free list is the owner of all memory it allocates. Any object allocated by this free list will be deleted and unsafe to access.
|
overridevirtual |
Returns a pointer to a newly allocated T object.
If there are any objects on the free list, it will recycle them. Next, if there are any preallocated objects, it will use one of those. Finally, it will forceably recycle the oldest allocated object.
Reimplemented from FreeList< T >.
|
protected |
Tracks all of the memory that has been allocated, allowing forceable recycling