CUGL 2.1
Cornell University Game Library
Public Member Functions | Static Public Member Functions | Protected Attributes | List of all members
cugl::GreedyFreeList< T > Class Template Reference

#include <CUGreedyFreeList.h>

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

Public Member Functions

 GreedyFreeList ()
 
 ~GreedyFreeList ()
 
bool init (size_t capacity)
 
bool init (size_t capacity, bool expand)
 
Tmalloc ()
 
- Public Member Functions inherited from cugl::FreeList< T >
 FreeList ()
 
 ~FreeList ()
 
void dispose ()
 
bool init (size_t capacity)
 
bool init (size_t capacity, bool expand)
 
size_t getAvailable () const
 
size_t getCapacity () const
 
size_t getUsage () const
 
size_t getPeakUsage () const
 
bool isExpandable () const
 
const TgetPreallocated () const
 
Tmalloc ()
 
void free (T *obj)
 
virtual void clear ()
 

Static Public Member Functions

static std::shared_ptr< GreedyFreeList< T > > alloc (size_t capacity)
 
- Static Public Member Functions inherited from cugl::FreeList< T >
static std::shared_ptr< FreeList< T > > alloc (size_t capacity=0, bool expand=false)
 

Protected Attributes

std::queue< T * > _allocation
 
- Protected Attributes inherited from cugl::FreeList< T >
size_t _allocated
 
size_t _released
 
size_t _peaksize
 
T_prealloc
 
size_t _capacity
 
std::queue< T * > _freeobjs
 
bool _expandable
 
std::queue< T * > _expansion
 

Detailed Description

template<class T>
class cugl::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 all have the method

void reset();

This method resets the object when it is recycled. It is like a destructor, except that the object is not actually deleted. 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.

A free list is not an all-purpose memory allocator. It is restricted to a single class. It should only be used for specialized applications.

WARNING: Templates cannot support virual methods. Therefore it is unsafe to downcast a GreedyFreeList pointer to a FreeList pointer.

Constructor & Destructor Documentation

◆ GreedyFreeList()

template<class T >
cugl::GreedyFreeList< T >::GreedyFreeList ( )
inline

Creates a new greedy free list with no capacity.

You must initialize this greedy free list before use.

NEVER USE A CONSTRUCTOR WITH NEW. If you want to allocate a free list on the heap, use one of the static constructors instead.

◆ ~GreedyFreeList()

template<class T >
cugl::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

◆ alloc()

template<class T >
static std::shared_ptr<GreedyFreeList<T> > cugl::GreedyFreeList< T >::alloc ( size_t  capacity)
inlinestatic

Returns a newly allocated greedy free list with the given capacity.

As it is not expandable, it will never allocate any objects beyond those preallocated in this constructor. Hence the capacity must be non-zero.

Parameters
capacitythe number of objects to preallocate
Returns
a newly allocated greedy free list with the given capacity.

◆ init() [1/2]

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

Initializes a greedy free list with the given capacity

As it is not expandable, it will never allocate any objects beyond those preallocated in this constructor. Hence the capacity must be non-zero.

Parameters
capacitythe number of objects to preallocate
Returns
true if initialization was successful.

◆ init() [2/2]

template<class T >
bool cugl::GreedyFreeList< T >::init ( size_t  capacity,
bool  expand 
)
inline

This inherited initializer is disabled

◆ malloc()

template<class T >
T * cugl::GreedyFreeList< T >::malloc ( )

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.

Member Data Documentation

◆ _allocation

template<class T >
std::queue<T*> cugl::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: