Max OS 0.3
Loading...
Searching...
No Matches
bump.h
1//
2// Created by 98max on 8/07/2026.
3//
4
5#ifndef MAXOS_ALLOCATOR_BUMP_H
6#define MAXOS_ALLOCATOR_BUMP_H
7
8#include <cstddef>
9#include <cstdint>
10
11#include <libcommon/assert.h>
12
14
15namespace MaxOS::memory::allocator {
16
17
18 constexpr uint16_t EXPECTED_CANARY = 0xC0DE;
19 constexpr size_t CHUNK_ALIGNMENT = 0x10;
20
25 typedef struct alignas(CHUNK_ALIGNMENT) ChunkHeader {
26
27 uint16_t canary;
28
31
32 uint8_t allocated;
33 size_t size;
34
35 bool is_valid();
36 ChunkHeader* prev_chunk();
37 ChunkHeader* next_chunk();
38
40
41 constexpr size_t CHUNK_HEADER_SIZE = sizeof(chunk_header_t);
42
48
49 private:
50 chunk_header_t* m_first_memory_chunk = nullptr;
51 chunk_header_t* m_last_memory_chunk = nullptr;
52
53 void* expand_heap(size_t size) final;
54 virtual void* allocate_extra_space(size_t requested_size, size_t& actual_size);
55
56 bool m_setup = false;
57
58
59 public:
61 virtual ~BumpAllocator();
62
63 void setup_region(uintptr_t address, size_t length) final;
64
65 void* allocate(size_t size) final;
66 void unallocate(void* pointer) final;
67
68 size_t amount_used() final;
69 size_t align(size_t size) final;
70
71 };
72
73}
74
75#endif //MAXOS_ALLOCATOR_BUMP_H
needs breif
Basic doubly linked list bump memory allocator.
Definition bump.h:47
virtual ~BumpAllocator()
Destructs a Bump memory allocator.
Generic memory allocator type.
Definition allocator.h:23
A span of memory in the heap, can be allocated or free. Used as a node in a doubly linked list.
Definition bump.h:25
ChunkHeader * next
Pointer to the chunk after this one in the list.
Definition bump.h:29
uint8_t allocated
Whether this chunk is in use or can be allocated.
Definition bump.h:32
ChunkHeader * prev
Pointer to the chunk before this one in the list.
Definition bump.h:30
size_t size
The size of this span of memory (not including the MemoryChunk struct itself)
Definition bump.h:33
uint16_t canary
Trip value to detect buffer overflows.
Definition bump.h:27