A freelist is a way of retrieving some kind of resource in an efficient manner. Usually a freelist is used when a memory allocation is needed, but searching for a free block should be fast. Freelists can be used inside of general purpose allocators, or embedded directly into an optimized algorithm.
Lets say we have an array of elements, where each element is 16 bytes of memory. Our array has 32 elements. The program that arrays resides in needs to request 16 byte elements, use them, and later give them back; we have allocation of elements and deallocation of elements.
The order the elements are allocated is not related in any way to order of deallocation. In order for deallocation to be a fast operation the 16 byte element needs to be in a state such that a future allocation be handed this element to be reused.
A singly linked list can be used to hold onto all unused and free elements. Since the elements are 16 bytes each this is more than enough memory to store a pointer, or integer index, which points to the next block in the free list. We can use the null pointer, or a -1 index to signify the end of the freelist.
Allocating and deallocating can now look like:
// Can be integer index, or more complex handle
void* Alloc( )
if ( freeList )
Element* e = freeList;
freeList = freeList->next;
void Free( void* element )
freeList->next = element;
freeList = element;
Setting up the memory* will take some work. Each element needs to be linked together somehow, like through a pointer or integer index. If no more elements are available then more arrays of size 32 can be allocated — this means our memory is being managed with the style of a “paged allocator”, where each array can be thought of as a page.
The freelist is an important concept that can be embedded ad-hoc into more complex algorithms. Often times it is important for little pieces of software to expose a very tiny C-like interface, usually just a function or two. Having these softwares self-contain their own internal freelists is one way to achieve a simple interface.
Example of Hiding the Freelist
For example say we are computing the convex hull of a point-set through the Quick Hull algorithm. The hypothetical algorithm exposes an interface like this:
// Computes number of bytes needed to run algorithm
int ComputeMemoryBound( int vertCount );
// memory points to ComputeMemoryBound( n ) bytes of free space allocated by the user
// Output an array of Vec2s written to memory
// returns the number of output Vec2s
int QHull( Vec3* points, int vertCount, void* memory );
This QHull function does no explicit memory allocation and forces the user to allocate an appropriate amount of memory to work with. The bounds of this memory (how big it needs to be for the algorithm’s worst case scenario) is calculated by the ComputeMemoryBound function.
Inside of QHull often times the hull is expanded and many new faces are allocated. These faces are held on a free list. Once new faces are made, old ones are deleted. These deleted faces are pushed onto the free list. This continues until the algorithm concludes, and the user does not need to know about the details of the embedded memory management of the freelist.
Convex hull about to expand to point P. The white faces will be deleted. The see-through faces will be allocated.
A convex hull fully expanded to point P. All old faces were deleted.
The above images were found at this address: http://www.eecs.tufts.edu/~mhorn01/comp163/algorithm.html