2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2020-03-22 03:12:45 +03:00
|
|
|
#include <Kernel/Heap/kmalloc.h>
|
2021-08-06 11:45:34 +03:00
|
|
|
#include <Kernel/Memory/MemoryManager.h>
|
|
|
|
#include <Kernel/Memory/PhysicalPage.h>
|
2019-04-03 16:13:07 +03:00
|
|
|
|
2021-08-06 14:49:36 +03:00
|
|
|
namespace Kernel::Memory {
|
2020-02-16 03:27:42 +03:00
|
|
|
|
2021-07-17 14:30:47 +03:00
|
|
|
NonnullRefPtr<PhysicalPage> PhysicalPage::create(PhysicalAddress paddr, MayReturnToFreeList may_return_to_freelist)
|
2019-04-03 16:13:07 +03:00
|
|
|
{
|
2021-07-08 04:50:05 +03:00
|
|
|
auto& physical_page_entry = MM.get_physical_page_entry(paddr);
|
2021-07-12 23:52:17 +03:00
|
|
|
return adopt_ref(*new (&physical_page_entry.allocated.physical_page) PhysicalPage(may_return_to_freelist));
|
2019-04-03 16:13:07 +03:00
|
|
|
}
|
|
|
|
|
2021-07-17 14:30:47 +03:00
|
|
|
PhysicalPage::PhysicalPage(MayReturnToFreeList may_return_to_freelist)
|
2019-04-03 16:13:07 +03:00
|
|
|
: m_may_return_to_freelist(may_return_to_freelist)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-07-08 04:50:05 +03:00
|
|
|
PhysicalAddress PhysicalPage::paddr() const
|
|
|
|
{
|
|
|
|
return MM.get_physical_address(*this);
|
|
|
|
}
|
|
|
|
|
2021-07-08 05:28:51 +03:00
|
|
|
void PhysicalPage::free_this()
|
2019-04-03 16:13:07 +03:00
|
|
|
{
|
2021-07-12 23:52:17 +03:00
|
|
|
auto paddr = MM.get_physical_address(*this);
|
2021-07-17 14:30:47 +03:00
|
|
|
if (m_may_return_to_freelist == MayReturnToFreeList::Yes) {
|
2021-07-12 23:52:17 +03:00
|
|
|
auto& this_as_freelist_entry = MM.get_physical_page_entry(paddr).freelist;
|
2021-07-08 05:28:51 +03:00
|
|
|
this->~PhysicalPage(); // delete in place
|
2021-07-12 23:52:17 +03:00
|
|
|
this_as_freelist_entry.next_index = -1;
|
|
|
|
this_as_freelist_entry.prev_index = -1;
|
2021-07-12 00:12:32 +03:00
|
|
|
MM.deallocate_physical_page(paddr);
|
2021-07-08 05:28:51 +03:00
|
|
|
} else {
|
|
|
|
this->~PhysicalPage(); // delete in place
|
|
|
|
}
|
2019-04-03 16:13:07 +03:00
|
|
|
}
|
2020-02-16 03:27:42 +03:00
|
|
|
|
|
|
|
}
|