From 8e95a11907a94bb92cc87554d53abfe8e2b78349 Mon Sep 17 00:00:00 2001 From: Drew Galbraith Date: Wed, 15 Nov 2023 14:44:16 -0800 Subject: [PATCH] [Zion] Move the physical memory manager to the LinkedList class. --- lib/glacier/container/linked_list.h | 20 ++++++++++- zion/memory/physical_memory.cpp | 56 +++++++++++++---------------- 2 files changed, 44 insertions(+), 32 deletions(-) diff --git a/lib/glacier/container/linked_list.h b/lib/glacier/container/linked_list.h index ad420ef..eda0a74 100644 --- a/lib/glacier/container/linked_list.h +++ b/lib/glacier/container/linked_list.h @@ -16,6 +16,23 @@ class LinkedList { bool empty() const { return size_ == 0; } 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) { ListItem* new_item = new ListItem{ .item = item, @@ -42,7 +59,8 @@ class LinkedList { return ret; } - T& PeekFront() const { return front_->item; } + T& PeekFront() { return front_->item; } + const T& PeekFront() const { return front_->item; } struct ListItem { T item; diff --git a/zion/memory/physical_memory.cpp b/zion/memory/physical_memory.cpp index c51495a..c8308e5 100644 --- a/zion/memory/physical_memory.cpp +++ b/zion/memory/physical_memory.cpp @@ -1,5 +1,7 @@ #include "memory/physical_memory.h" +#include + #include "boot/boot_info.h" #include "debug/debug.h" @@ -53,21 +55,20 @@ class PhysicalMemoryManager { } uint64_t AllocatePage() { - if (front_ == nullptr) { + if (memory_blocks.size() == 0) { panic("No available memory regions."); } - if (front_->num_pages == 0) { - panic("Bad state, empty memory block."); + while (memory_blocks.PeekFront().num_pages == 0) { + memory_blocks.PopFront(); } - uint64_t page = front_->base; - front_->base += 0x1000; - front_->num_pages--; - if (front_->num_pages == 0) { - MemBlock* temp = front_; - front_ = front_->next; - delete temp; + MemBlock& block = memory_blocks.PeekFront(); + uint64_t page = block.base; + block.base += 0x1000; + block.num_pages--; + if (block.num_pages == 0) { + memory_blocks.PopFront(); } #if K_PHYS_DEBUG dbgln("Single {x}", page); @@ -75,33 +76,28 @@ class PhysicalMemoryManager { return page; } uint64_t AllocateContinuous(uint64_t num_pages) { - if (front_ == nullptr) { + if (memory_blocks.size() == 0) { 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."); } - MemBlock* block = front_; - while (block != nullptr && block->num_pages < num_pages) { - dbgln("Skipping block of size {} seeking {}", block->num_pages, - num_pages); - block = block->next; + auto iter = memory_blocks.begin(); + while (iter != memory_blocks.end() && iter->num_pages < num_pages) { + dbgln("Skipping block of size {} seeking {}", iter->num_pages, num_pages); + iter = iter.next(); } - if (block == nullptr) { + if (iter == memory_blocks.end()) { panic("No memory regions to allocate"); } - uint64_t page = front_->base; - front_->base += num_pages * 0x1000; - front_->num_pages -= num_pages; - if (front_->num_pages == 0) { - MemBlock* temp = front_; - front_ = front_->next; - delete temp; - } + uint64_t page = iter->base; + iter->base += num_pages * 0x1000; + iter->num_pages -= num_pages; #if K_PHYS_DEBUG dbgln("Continuous {x}:{}", page, num_pages); #endif @@ -111,20 +107,18 @@ class PhysicalMemoryManager { private: void AddMemoryRegion(uint64_t base, uint64_t size) { - MemBlock* block = new MemBlock{ - .next = front_, + MemBlock block{ .base = base, .num_pages = size >> 12, }; - front_ = block; + memory_blocks.PushFront(block); } struct MemBlock { - MemBlock* next = nullptr; uint64_t base = 0; uint64_t num_pages = 0; }; - MemBlock* front_ = nullptr; + glcr::LinkedList memory_blocks; }; static PhysicalMemoryManager* gPmm = nullptr;