Max OS 0.3
Loading...
Searching...
No Matches
memorymanagement.cpp
Go to the documentation of this file.
1
10#include <common/logger.h>
11#include <processes/scheduler.h>
12
13using namespace MaxOS;
14using namespace MaxOS::memory;
15using namespace MaxOS::memory::allocator;
16using namespace MaxOS::common;
17using namespace MaxOS::processes;
18using namespace MaxOS::system;
19
26 : m_virtual_memory_manager(virtual_memory_manager)
27{
28
29 // Create the VMM if not provided
30 if(m_virtual_memory_manager == nullptr)
31 m_virtual_memory_manager = new VirtualMemoryManager();
32
33 // Set up the first chunk of memory
34 auto address = (uintptr_t)m_virtual_memory_manager->allocate(PAGE_SIZE, 0);
35 setup_region(address, PAGE_SIZE);
36
37}
38
43
44 // Free the VMM (if this is not the kernel memory manager)
45 if(m_virtual_memory_manager != nullptr)
46 delete m_virtual_memory_manager;
47
48}
49
56void* BumpMemoryManager::allocate_extra_space(size_t requested_size, size_t& actual_size) {
57
58 // Create a new chunk of memory
59 auto* chunk = m_virtual_memory_manager->allocate(requested_size, actual_size, PRESENT | WRITE | NO_EXECUTE);
60 ASSERT(chunk != nullptr, "Out of memory - kernel cannot allocate any more memory");
61
62 return chunk;
63
64}
65
66
73
74 return m_virtual_memory_manager;
75}
76
77
84
85 // Make sure there is a manager
86 if(manager == nullptr)
87 return;
88
89 // Switch the address space
90 asm volatile("mov %0, %%cr3"::"r"((uint64_t) manager->m_virtual_memory_manager->pml4_root_address_physical()) : "memory");
91
92}
93
101{
102
103 // First memory manager is the kernel memory manager
104 if(s_kernel_memory_manager == nullptr)
106
107}
108
113
114 // Remove the kernel reference to this
115 if(s_kernel_memory_manager == this)
116 s_kernel_memory_manager = nullptr;
117}
118
125void* MemoryManager::malloc(size_t size) {
126
128}
129
136void* MemoryManager::kmalloc(size_t size) {
137
138 // Make sure there is a kernel memory manager
139 if(s_kernel_memory_manager == nullptr)
140 return nullptr;
141
142 return s_kernel_memory_manager->allocate(size);
143}
144
150void MemoryManager::free(void* pointer) {
151
153}
154
160void MemoryManager::kfree(void* pointer) {
161
162 // Make sure there is a kernel memory manager
163 if(s_kernel_memory_manager == nullptr)
164 return;
165
167}
168
169//Redefine the default object functions with memory orientated ones (defaults disabled in makefile)
Basic doubly linked list bump memory manager.
BumpMemoryManager(VirtualMemoryManager *virtual_memory_manager=nullptr)
Constructs a new BumpMemoryManager.
static void switch_active_memory_manager(BumpMemoryManager *manager)
Switches the active memory manager.
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.
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.
Manages the virtual memory of the system and provides functions to allocate and free memory in the vi...
Definition virtual.h:104
void * allocate(size_t size, size_t flags)
Allocate a new chunk of virtual memory.
Definition virtual.cpp:113
uint64_t * pml4_root_address_physical()
Get the physical address of the PML4 root.
Definition virtual.cpp:476
void unallocate(void *pointer) final
Frees a block of memory.
Definition bump.cpp:212
void * allocate(size_t size) final
Allocates a block of memory.
Definition bump.cpp:144
void setup_region(uintptr_t address, size_t length) final
Setup a region to be bump memory managed, creates a free chunk represent the whole region.
Definition bump.cpp:122
static Process * current_process()
Gets the process on the currently executing core.
memory::MemoryManager * memory_manager
The manager for memory used by this process.
Definition process.h:121
Defines a Logger class for logging messages with different severity levels to multiple output streams...
Defines a MemoryManager class for handling memory allocation and deallocation.
constexpr uint64_t PAGE_SIZE
The size of a page (4KB)
Definition physical.h:92
Defines a GlobalScheduler and Scheduler for managing processes and threads.