CUGL 2.3
Cornell University Game Library
|
#include <CUGreedyFreeList.h>
Public Member Functions | |
GreedyFreeList () | |
~GreedyFreeList () | |
bool | init (size_t capacity) |
bool | init (size_t capacity, bool expand) |
T * | malloc () |
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 T * | getPreallocated () const |
T * | malloc () |
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 |
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.
|
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.
|
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.
|
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.
capacity | the number of objects to preallocate |
|
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.
capacity | the number of objects to preallocate |
|
inline |
This inherited initializer is disabled
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.
|
protected |
Tracks all of the memory that has been allocated, allowing forceable recycling