Free list allocator#

Different memory allocators have different trade-offs. The free list allocator used here has a fixed total number of lists of free blocks (rather than a linked list or tree). Because of this it will be fast, but will not cope with significant memory fragmentation compared to malloc. It also has a very small minimum allocation compared to malloc. Since this allocator is used with known fixed sizes, the size allocated isn’t stored so it must be passed into the freeListFree function (and freeListRealloc).

This allocator shouldn’t be used directly as there are wrapper functions that keep track of useful debugging information.

Functions#

Functions

void *freeListAlloc(size_t sizeInBlocks)#

Allocate memory.

Allocates a given amount of memory and returns a pointer to the start of that memory. The allocator doesn’t keep track of how much memory has been allocated, so this must be kept for when calling the freeListFree function. Any allocated memory must be freed with freeListFree or reallocated with freeListRealloc. Allocated memory is whatever was previously in memory (it is not zeroed).

Parameters:
  • sizeInBlocks[in] size of the memory in blocks, not bytes

Returns:

pointer to allocated memory

void *freeListRealloc(void *pcMemPtr, size_t oldSizeInBlocks, size_t newSizeInBlocks)#

Reallocate memory.

Increase or decrease the amount of memory of a previous allocation. Since the amount of memory allocated isn’t kept by the allocator, this must be provided along with the new required size. The pointer returned will differ from the one provided and the old pointer will be freed so should no longer be used. Data is copied over to the new location. If the size of the allocation is reduced, then the copy will only be for the new size. If the size is increased then the extra data will be whatever was previously in memory (it is not zeroed).

Parameters:
  • pcMemPtr[in] pointer to the previously allocated memory

  • oldSizeInBlocks[in] size used when this pointer was allocated

  • newSizeInBlocks[in] new required size for the allocation

Returns:

pointer to the allocated memory

void freeListFree(void *pcMemPtr, size_t sizeInBlocks)#

Free allocated memory.

Frees memory previous allocated with freeListAlloc or freeListRealloc. This must be called at some point after memory has been allocated. The allocator does not keep track of how much memory has been allocated, so this must be provided. If the memory has been reallocated with freeListRealloc then the size of the last alloc should be provided, along with the latest pointer allocated.

Parameters:
  • pcMemPtr[in] pointer to the previously allocated memory

  • sizeInBlocks[in] size used when this pointer was allocated

Example#

As with any allocator, allocated memory must be freed.

uint16_t *myMemory = (uint16_t *)freeListAlloc(TO_BLOCKS(8*2));
...
uint16_t *newMemory = (uint16_t *)freeListRealloc(myMemory, TO_BLOCKS(8*2), TO_BLOCKS(16*2));
...
freeListFree(myMemory, TO_BLOCKS(16*2));