13using namespace MaxOS::memory;
14using namespace MaxOS::common;
15using namespace MaxOS::system;
23: m_virtual_memory_manager(vmm) {
26 if(m_virtual_memory_manager ==
nullptr)
35 m_first_memory_chunk->
prev =
nullptr;
36 m_first_memory_chunk->
next =
nullptr;
38 m_last_memory_chunk = m_first_memory_chunk;
53 delete m_virtual_memory_manager;
119 result = expand_heap(size);
131 extra->allocated =
false;
137 if(
extra->next !=
nullptr)
147 if(
result == m_last_memory_chunk)
148 m_last_memory_chunk =
extra;
189 if(pointer ==
nullptr)
198 chunk->allocated =
false;
201 if(
chunk->prev !=
nullptr && !
chunk->prev->allocated) {
208 if(
chunk->next !=
nullptr)
217 if(
chunk->next !=
nullptr && !
chunk->next->allocated) {
225 if(
chunk->next !=
nullptr)
237MemoryChunk* MemoryManager::expand_heap(
size_t size) {
241 ASSERT(
chunk !=
nullptr,
"Out of memory - kernel cannot allocate any more memory");
248 chunk->allocated =
false;
250 chunk->next =
nullptr;
254 chunk->prev = m_last_memory_chunk;
255 m_last_memory_chunk =
chunk;
259 if(!
chunk->prev->allocated)
306 asm volatile(
"mov %0, %%cr3"::
"r"((
uint64_t)
manager->m_virtual_memory_manager->pml4_root_address_physical()) :
"memory");
319 return m_virtual_memory_manager;
331void*
operator new(
size_t size)
throw() {
343void*
operator new[](
size_t size)
throw() {
356void*
operator new(
size_t size,
void* pointer) {
368void*
operator new[](
size_t size,
void* pointer) {
379void operator delete(
void* pointer) {
390void operator delete[](
void* pointer) {
402void operator delete(
void* pointer,
size_t) {
414void operator delete[](
void* pointer,
size_t size) {
Stores the left, top, width and height of a rectangle.
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.
MemoryManager(VirtualMemoryManager *virtual_memory_manager=nullptr)
Construct a new Memory Manager object. Will switch the pml4 to use the calling process's page tables.
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...
void * allocate(size_t size, size_t flags)
Allocate a new chunk of virtual memory.
Defines a Logger class for logging messages with different severity levels to multiple output streams...
#define ASSERT(condition, format,...)
If the specified condition is not met then the kernel will crash with the specified message.
Defines a MemoryManager class for handling memory allocation and deallocation.
constexpr size_t CHUNK_ALIGNMENT
How many bytes the chunks should be a multiple of (round up to this)
constexpr uint64_t PAGE_SIZE
The size of a page (4KB)
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.