Cornell Cocos
Cornell Extensions to Cocos2d
Public Member Functions | Protected Attributes | List of all members
GreedyFreeList< T > Class Template Reference

#include <CUGreedyFreeList.h>

Inheritance diagram for GreedyFreeList< T >:
FreeList< T >

Public Member Functions

 GreedyFreeList (size_t capacity)
 
 ~GreedyFreeList ()
 
virtual T * alloc () override
 
- Public Member Functions inherited from FreeList< T >
 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
 
- Protected Attributes inherited from FreeList< T >
size_t _allocated
 
size_t _released
 
size_t _peaksize
 
T * _prealloc
 
size_t _capacity
 
queue< T * > _freeobjs
 
bool _expandable
 
queue< T * > _expansion
 

Detailed Description

template<class T>
class GreedyFreeList< T >

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.

Constructor & Destructor Documentation

template<class T >
GreedyFreeList< T >::GreedyFreeList ( size_t  capacity)
inline

Creates a new free list with the given capacity.

As greedy free lists are not expandable, the capacity must be non-zero.

Parameters
capacitythe number of objects to preallocate
template<class T >
GreedyFreeList< T >::~GreedyFreeList ( )
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.

Member Function Documentation

template<class T >
T * GreedyFreeList< T >::alloc ( )
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.

Returns
a pointer to a newly allocated T object.

Reimplemented from FreeList< T >.

Member Data Documentation

template<class T >
queue<T*> GreedyFreeList< T >::_allocation
protected

Tracks all of the memory that has been allocated, allowing forceable recycling


The documentation for this class was generated from the following file: