Introduction

One of my many projects that I work on from time to time is the sstdlib, which is a pure C implementation of common data structures that I use. The inspiration for this is stb but built by me from the ground up. So I know exactly how it all works under the hood.

Last time, I implemented Dynamic and Heterogeneous Vectors in C, which was a nice little exercise for it. The problem with this was that I was allocating this on the heap. I made it work with an arena allocator (which I implemented in Some more pointer magic with Memory Arenas, by the way), but it still needs work. More specifically, I need to design a generic allocator interface for it to be able to use different types of allocation strategies - normal heap allocations, arena allocations, and what we are writing today - pool allocations.

Difference between Arena Allocations and Pool Allocations

The terminology for these types of allocators is ambiguous. But for the purpose of this article, the difference is the following:

Arena allocations may result in external fragmentation over time.

Say this is the initial memory layout of the memory arena

Initial memory layout of the arena

And say we don't need Segment 2, so we free it.

Arena after freeing segment 2, leaving a hole

Now comes a problem.

To fit another substantially large segment, we need to shift segment 3 and 4 to the left OR track the amount of space the new segment needs in order to fit in that memory. Either way, this is an expensive operation to perform.

Pool allocators provide fixed-size segments

Unlike arena allocators, pool allocators have their memory divided in fixed segments.

Pool allocator divided into fixed-size segments

So if we free segment 2, then we can track it by pushing it onto a stack. The next operation that requires free memory, will pop off the segment from the top of the stack and use that. This makes sure that we don't have external fragmentation.

However, since the segments are of fixed size, we make a trade-off of internal fragmentation for knowing the size before allocation.

Implementation

This is what the segment/block looks like. The next pointer points to the next block.

typedef struct block_t {
    int data;
    struct block_t *next;
} Block;

This is the structure that tracks the pool allocator.

typedef struct pool_t {
    Block *pool;
    Block *free_list; 
    size_t count;
    size_t pool_size;
} Pool;

These are the functions that we'll need.

void pool_printer(Pool *pool);
Pool *pool_create(size_t pool_size);
Block *block_allocate(Pool *pool);
void block_free(Pool *pool, Block *block);
void pool_free(Pool *pool);

Creating the pool

Here is what this function does in short:

Pool *pool_create(size_t pool_size)
{
    Pool *pool_ptr = malloc(sizeof(Pool));

    pool_ptr->pool = malloc(sizeof(Block) * pool_size);

    pool_ptr->free_list = NULL;

    for (size_t i = 0; i < pool_size; i++) {

        if (i == 0) {
            pool_ptr->pool[i].next = NULL;
            pool_ptr->free_list = &pool_ptr->pool[i];
            continue;
        }

        pool_ptr->pool[i].next = pool_ptr->free_list;

        pool_ptr->free_list = &pool_ptr->pool[i];
    }

    pool_ptr->count = pool_size;
    pool_ptr->pool_size = pool_size;

    return pool_ptr;
}

Allocate block

Block *block_allocate(Pool *pool) 
{
    if (pool->count == 0) return NULL;

    Block *tmp = pool->free_list;
    pool->free_list = pool->free_list->next;
    pool->count--;

    return tmp;
}

Free the block

void block_free(Pool *pool, Block *block)
{
    block->data = 0;
    block->next = pool->free_list;
    pool->free_list = block;
    pool->count++;
}

Free the pool

void pool_free(Pool *pool)
{
    assert(pool != NULL);
    assert(pool->pool != NULL);

    pool->free_list = NULL;
    pool->count = 0;

    free(pool->pool);
    free(pool);
}

Conclusion

This pool allocator is useful if I have vectors of fixed data types.

That way it's easier to reallocate more space if necessary, and not worry about external fragmentation.

The source code can be found at https://github.com/syswraith/pool_allocator.