Skip to content

eyedoteye/C-Memory-Allocators

Repository files navigation

C-Memory-Allocators

A pair of memory allocators, stack & list, allowing for a more rapid and spacially conservant way of distributing memory within a program. This project was made with video games in mind, but can be leveraged for use in any program where the maximum memory need is pre-defined.

Stack Allocator

This allocator works by assigning memory via a stack-like structure. This implementation places the overhead data of each allocated chunk within the alignment padding.

void InitializeStack(stack *Stack, size_t Size);

void* AllocateSpaceOnStack_(stack *Stack, size_t Size, short Alignment);
#define AllocateSpaceOnStack(Stack, Type) \
        AllocateSpaceOnStack_(Stack, sizeof(Type), alignof(Type))

void DeallocateSpaceOnStack_(stack *Stack, size_t Size);
#define DeallocateSpaceOnStack(Stack, Type) \
        DeallocateSpaceOnStack_(Stack, sizeof(Type))

List Allocator

This allocator works by assigning memory via a list-like structure. This implementation stores the offset from the aligned header and the aligned data as a char right before the data itself. This saves space while ensuring all data is aligned.

void InitializeList(list *List, size_t Size);

void* AllocateSpaceOnList_(list *List, size_t Size, unsigned char Alignment);
#define AllocateSpaceOnList(List, Type) \
        AllocateSpaceOnList_(List, sizeof(Type), alignof(Type))
        
void DeallocateSpaceOnList(list *List, void* Address);

Notes

This repository includes a tester.
!Note that although the allocators themselves are platform agnostic, the tester is windows only.

Tester Build

A pre-built binary is available within the build folder.

./build/test.exe

To build a new set of binaries on a Windows environment, first run either the 64-bit or 32-bit vcvarsall.bat, then ./build.bat.

Tester Usage

Usage: ./build/test.exe [option <value>]

The available options are as follows:

-v          Print memory allocation information.
-a <#####>  Specify allocation size.
-t <#####>  Specify test count.

Motivation

I have to thank my good friend Mariano for encouraging and coercing me to explore game programming at a deeper level (seriously, check him out, he's super cool). I'm ever thankful of the time we spent scratching our heads and drawing advanced rectangles on white boards.

About

Allocation techniques for malloc replacement

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published