Skip to content

feat: add linear allocator implementation#3132

Open
B33Boy wants to merge 7 commits intoTheAlgorithms:masterfrom
B33Boy:master
Open

feat: add linear allocator implementation#3132
B33Boy wants to merge 7 commits intoTheAlgorithms:masterfrom
B33Boy:master

Conversation

@B33Boy
Copy link
Copy Markdown

@B33Boy B33Boy commented Apr 28, 2026

Description of Change

I've implemented a simple linear (bump) allocator under a new memory/ subdirectory, intended to house memory management algorithms (e.g. pool, stack, free list allocators).

Description of Implementation

A linear allocator pre-allocates a fixed block of memory and hands it out to the user sequentially. It consists of a buffer and an offset. This offset gets bumped forward after each allocation, and adheres to memory alignment requirements.

My implementation manages a buffer that is stack-allocated via a templated parameter. It is faster than malloc because allocation is O(1), there are no locks used, and there is no external memory fragmentation (memory is contiguous and the only waste is padding due to alignment). The tradeoff is losing the ability to have individual de-allocations. Futhermore, the lifetime is tied to the scope of usage, so it is best suited for scenarios where you are working with objects that share the same lifetime.

Why is this educational?

My implementation exposes the mechanics of allocators such as:

  • how a simple linear allocator works
  • how alignment works, and why it matters
  • usage of C++ functions alignof, alignas

Checklist

  • Added description of change
  • Added file name matches File name guidelines
  • Added tests and example, test must pass
  • Added documentation so that the program is self-explanatory and educational - Doxygen guidelines
  • Relevant documentation/comments is changed or added
  • PR title follows semantic commit guidelines
  • Search previous suggestions before making a new one, as yours may be a duplicate.
  • I acknowledge that all my contributions will be made under the project's license.

Notes: Add a linear allocator implementation under new memory/ subdirectory

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant