[Zion] Move the physical memory manager to the LinkedList class.

This commit is contained in:
Drew Galbraith 2023-11-15 14:44:16 -08:00
parent 92d8a02291
commit 8e95a11907
2 changed files with 44 additions and 32 deletions

View File

@ -16,6 +16,23 @@ class LinkedList {
bool empty() const { return size_ == 0; } bool empty() const { return size_ == 0; }
uint64_t size() const { return size_; } uint64_t size() const { return size_; }
void PushFront(const T& item) {
ListItem* new_item = new ListItem{
.item = item,
.next = front_,
};
front_ = new_item;
size_++;
}
void PushFront(T&& item) {
ListItem* new_item = new ListItem{
.item = glcr::Move(item),
.next = front_,
};
front_ = new_item;
size_++;
}
void PushBack(const T& item) { void PushBack(const T& item) {
ListItem* new_item = new ListItem{ ListItem* new_item = new ListItem{
.item = item, .item = item,
@ -42,7 +59,8 @@ class LinkedList {
return ret; return ret;
} }
T& PeekFront() const { return front_->item; } T& PeekFront() { return front_->item; }
const T& PeekFront() const { return front_->item; }
struct ListItem { struct ListItem {
T item; T item;

View File

@ -1,5 +1,7 @@
#include "memory/physical_memory.h" #include "memory/physical_memory.h"
#include <glacier/container/linked_list.h>
#include "boot/boot_info.h" #include "boot/boot_info.h"
#include "debug/debug.h" #include "debug/debug.h"
@ -53,21 +55,20 @@ class PhysicalMemoryManager {
} }
uint64_t AllocatePage() { uint64_t AllocatePage() {
if (front_ == nullptr) { if (memory_blocks.size() == 0) {
panic("No available memory regions."); panic("No available memory regions.");
} }
if (front_->num_pages == 0) { while (memory_blocks.PeekFront().num_pages == 0) {
panic("Bad state, empty memory block."); memory_blocks.PopFront();
} }
uint64_t page = front_->base; MemBlock& block = memory_blocks.PeekFront();
front_->base += 0x1000; uint64_t page = block.base;
front_->num_pages--; block.base += 0x1000;
if (front_->num_pages == 0) { block.num_pages--;
MemBlock* temp = front_; if (block.num_pages == 0) {
front_ = front_->next; memory_blocks.PopFront();
delete temp;
} }
#if K_PHYS_DEBUG #if K_PHYS_DEBUG
dbgln("Single {x}", page); dbgln("Single {x}", page);
@ -75,33 +76,28 @@ class PhysicalMemoryManager {
return page; return page;
} }
uint64_t AllocateContinuous(uint64_t num_pages) { uint64_t AllocateContinuous(uint64_t num_pages) {
if (front_ == nullptr) { if (memory_blocks.size() == 0) {
panic("No available memory regions."); panic("No available memory regions.");
} }
if (front_->num_pages == 0) { MemBlock& block = memory_blocks.PeekFront();
if (block.num_pages == 0) {
panic("Bad state, empty memory block."); panic("Bad state, empty memory block.");
} }
MemBlock* block = front_; auto iter = memory_blocks.begin();
while (block != nullptr && block->num_pages < num_pages) { while (iter != memory_blocks.end() && iter->num_pages < num_pages) {
dbgln("Skipping block of size {} seeking {}", block->num_pages, dbgln("Skipping block of size {} seeking {}", iter->num_pages, num_pages);
num_pages); iter = iter.next();
block = block->next;
} }
if (block == nullptr) { if (iter == memory_blocks.end()) {
panic("No memory regions to allocate"); panic("No memory regions to allocate");
} }
uint64_t page = front_->base; uint64_t page = iter->base;
front_->base += num_pages * 0x1000; iter->base += num_pages * 0x1000;
front_->num_pages -= num_pages; iter->num_pages -= num_pages;
if (front_->num_pages == 0) {
MemBlock* temp = front_;
front_ = front_->next;
delete temp;
}
#if K_PHYS_DEBUG #if K_PHYS_DEBUG
dbgln("Continuous {x}:{}", page, num_pages); dbgln("Continuous {x}:{}", page, num_pages);
#endif #endif
@ -111,20 +107,18 @@ class PhysicalMemoryManager {
private: private:
void AddMemoryRegion(uint64_t base, uint64_t size) { void AddMemoryRegion(uint64_t base, uint64_t size) {
MemBlock* block = new MemBlock{ MemBlock block{
.next = front_,
.base = base, .base = base,
.num_pages = size >> 12, .num_pages = size >> 12,
}; };
front_ = block; memory_blocks.PushFront(block);
} }
struct MemBlock { struct MemBlock {
MemBlock* next = nullptr;
uint64_t base = 0; uint64_t base = 0;
uint64_t num_pages = 0; uint64_t num_pages = 0;
}; };
MemBlock* front_ = nullptr; glcr::LinkedList<MemBlock> memory_blocks;
}; };
static PhysicalMemoryManager* gPmm = nullptr; static PhysicalMemoryManager* gPmm = nullptr;