[Zion] Actually free memory pages when a MemoryObject goes out of scope
This commit is contained in:
parent
344e84c313
commit
4d1846a7d5
8 changed files with 63 additions and 21 deletions
|
|
@ -14,7 +14,7 @@ glcr::RefPtr<AddressSpace> AddressSpace::ForRoot() {
|
|||
}
|
||||
|
||||
AddressSpace::AddressSpace() {
|
||||
cr3_ = phys_mem::AllocatePage();
|
||||
cr3_ = phys_mem::AllocateAndZeroPage();
|
||||
InitializePml4(cr3_);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -20,7 +20,13 @@ MemoryObject::MemoryObject(uint64_t size) : size_(size) {
|
|||
}
|
||||
}
|
||||
|
||||
MemoryObject::~MemoryObject() { dbgln("Memory Object Freed"); }
|
||||
MemoryObject::~MemoryObject() {
|
||||
for (uint64_t page : phys_page_list_) {
|
||||
if (page != 0) {
|
||||
phys_mem::FreePage(page);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t MemoryObject::PhysicalPageAtOffset(uint64_t offset) {
|
||||
if (offset > size_) {
|
||||
|
|
@ -70,6 +76,12 @@ uint64_t MemoryObject::PageNumberToPhysAddr(uint64_t page_num) {
|
|||
return *iter;
|
||||
}
|
||||
|
||||
FixedMemoryObject::~FixedMemoryObject() {
|
||||
if (should_free_) {
|
||||
phys_mem::FreePages(physical_addr_, num_pages());
|
||||
}
|
||||
}
|
||||
|
||||
glcr::ErrorOr<glcr::RefPtr<MemoryObject>> FixedMemoryObject::Duplicate(
|
||||
uint64_t offset, uint64_t length) {
|
||||
if (offset + length > size()) {
|
||||
|
|
@ -77,5 +89,6 @@ glcr::ErrorOr<glcr::RefPtr<MemoryObject>> FixedMemoryObject::Duplicate(
|
|||
}
|
||||
|
||||
return glcr::StaticCastRefPtr<MemoryObject>(
|
||||
glcr::MakeRefCounted<FixedMemoryObject>(physical_addr_ + offset, length));
|
||||
glcr::MakeRefCounted<FixedMemoryObject>(physical_addr_ + offset, length,
|
||||
false));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ class MemoryObject : public KernelObject {
|
|||
~MemoryObject();
|
||||
|
||||
uint64_t size() { return size_; }
|
||||
uint64_t num_pages() { return size_ / 0x1000; }
|
||||
uint64_t num_pages() { return ((size_ - 1) / 0x1000) + 1; }
|
||||
|
||||
uint64_t PhysicalPageAtOffset(uint64_t offset);
|
||||
|
||||
|
|
@ -58,14 +58,20 @@ class MemoryObject : public KernelObject {
|
|||
class FixedMemoryObject : public MemoryObject {
|
||||
public:
|
||||
// FIXME: Validate that this is 4k aligned.
|
||||
FixedMemoryObject(uint64_t physical_addr, uint64_t size)
|
||||
: MemoryObject(size, true), physical_addr_(physical_addr) {}
|
||||
// Create a new class object for should free.
|
||||
FixedMemoryObject(uint64_t physical_addr, uint64_t size, bool should_free)
|
||||
: MemoryObject(size, true),
|
||||
physical_addr_(physical_addr),
|
||||
should_free_(should_free) {}
|
||||
|
||||
~FixedMemoryObject();
|
||||
|
||||
virtual glcr::ErrorOr<glcr::RefPtr<MemoryObject>> Duplicate(
|
||||
uint64_t offset, uint64_t length) override;
|
||||
|
||||
private:
|
||||
uint64_t physical_addr_;
|
||||
bool should_free_;
|
||||
|
||||
uint64_t PageNumberToPhysAddr(uint64_t page_num) override {
|
||||
return physical_addr_ + (0x1000 * page_num);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue