Skip to content

Commit

Permalink
Added chaining
Browse files Browse the repository at this point in the history
  • Loading branch information
pbentes committed Dec 20, 2024
1 parent 8eff0e6 commit cc4125a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
14 changes: 12 additions & 2 deletions Arena.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ typedef struct Arena {
void* buffer;
void* index;
size_t size;
Arena* next;
} Arena;

Arena* arenaCreate(size_t size = ARENA_SIZE);
Expand All @@ -33,6 +34,7 @@ void arenaDestroy(Arena* arena);
throw std::bad_alloc();
}
arena->index = arena->buffer;
arena->next = nullptr;

return arena;
}
Expand All @@ -41,17 +43,25 @@ void arenaDestroy(Arena* arena);
void* ret = arena->index;

arena->index = static_cast<char*>(arena->index) + size;
if (arena->index > static_cast<char*>(arena->buffer) + arena->size)
return nullptr;
if (arena->index > static_cast<char*>(arena->buffer) + arena->size) {
arena->next = arenaCreate(arena->size);
return arenaAlloc(arena->next, size);
}

return ret;
}

void arenaClear(Arena* arena) {
arena->index = arena->buffer;

if(arena->next)
arenaDestroy(arena->next);
}

void arenaDestroy(Arena* arena) {
if(arena->next)
arenaDestroy(arena->next);

if(arena) {
free(arena->buffer);
free(arena);
Expand Down
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
[![Build](https://github.com/pbentes/Arena/actions/workflows/build.yml/badge.svg)](https://github.com/pbentes/Arena/actions/workflows/build.yml)

# C-Like C++ implementation of a memory arena
This is a very simple, header only, C-Like C++ implementation of a memory arena

Work in progress. Its not fancy.

## Todo

- 🟩 Create Memory Arena
- 🟩 Allocate
- 🟨 Clear
- 🟩 Clear
- 🟩 Destroy
- πŸŸ₯ Free
- πŸŸ₯ Add a free list
- πŸŸ₯ Add chaining
- πŸŸ₯ Add debug utilities
- 🟩 Add chaining

0 comments on commit cc4125a

Please sign in to comment.