Max OS 0.3
Loading...
Searching...
No Matches
memorymanagement.h
Go to the documentation of this file.
1
9#ifndef MAXOS_SYSTEM_MEMORYMANAGEMENT_H
10#define MAXOS_SYSTEM_MEMORYMANAGEMENT_H
11
12#include <cstddef>
13#include <cstdint>
14#include <system/multiboot.h>
15#include <memory/virtual.h>
16
17
18namespace MaxOS::memory {
19
24 struct MemoryChunk {
25
28
29 bool allocated;
30 size_t size;
31
32 };
33
34 constexpr size_t CHUNK_ALIGNMENT = 0x10;
35
41
42 private:
43 MemoryChunk* m_first_memory_chunk;
44 MemoryChunk* m_last_memory_chunk;
45
46 VirtualMemoryManager* m_virtual_memory_manager;
47
48 MemoryChunk* expand_heap(size_t size);
49
50 public:
51 inline static MemoryManager* s_current_memory_manager = nullptr;
52 inline static MemoryManager* s_kernel_memory_manager = nullptr;
53
54 explicit MemoryManager(VirtualMemoryManager* virtual_memory_manager = nullptr);
56
57 // Public Memory Management
58 static void* malloc(size_t size);
59 static void free(void* pointer);
60
61 // Kernel Memory Management
62 static void* kmalloc(size_t size);
63 static void kfree(void* pointer);
64
65 // Internal Memory Management
66 void* handle_malloc(size_t size);
67 void handle_free(void* pointer);
69
70 // Utility Functions
71 size_t memory_used();
72 static size_t align(size_t size);
73 static void switch_active_memory_manager(MemoryManager* manager);
74 };
75}
76
77
78void* operator new(size_t size) throw();
79void* operator new[](size_t size) throw();
80
81//Placement New
82void* operator new(size_t size, void* pointer);
83void* operator new[](size_t size, void* pointer);
84
85void operator delete(void* pointer);
86void operator delete[](void* pointer);
87
88void operator delete(void* pointer, size_t size);
89void operator delete[](void* pointer, size_t size);
90
91#endif //MAXOS_SYSTEM_MEMORYMANAGEMENT_H
Handles memory allocation and deallocation.
void * handle_malloc(size_t size)
Allocates a block of memory.
static MemoryManager * s_current_memory_manager
The memory manager for the current process.
static size_t align(size_t size)
Aligns the size to the chunk alignment.
void handle_free(void *pointer)
Frees a block of memory.
static void free(void *pointer)
Frees a block of memory using the current memory manager.
static MemoryManager * s_kernel_memory_manager
The memory manager for any kernel processes and all kernel allocations.
static void * malloc(size_t size)
Allocates a block of memory in the current USERSPACE heap.
static void * kmalloc(size_t size)
Allocates a block of memory in the KERNEL space.
size_t memory_used()
Returns the amount of memory used.
static void kfree(void *pointer)
Frees a block of memory using the kernel memory manager.
~MemoryManager()
Destroy the Memory Manager object, frees the VMM if not the kernel memory manager.
static void switch_active_memory_manager(MemoryManager *manager)
Switches the active memory manager.
VirtualMemoryManager * vmm()
Manages the virtual memory of the system and provides functions to allocate and free memory in the vi...
Definition virtual.h:96
constexpr size_t CHUNK_ALIGNMENT
How many bytes the chunks should be a multiple of (round up to this)
A span of memory in the heap, can be allocated or free. Used as a node in a doubly linked list.
MemoryChunk * prev
Pointer to the chunk before this one in the list.
MemoryChunk * next
Pointer to the chunk after this one in the list.
size_t size
The size of this span of memory (not including the MemoryChunk struct itself)
bool allocated
Whether this chunk is in use or can be allocated.
Defines a VirtualMemoryManager class for managing virtual memory allocation and mapping to physical m...