Kernel/Memory: Introduce a method to allocate TypedMapping on the heap

This will be used later on to allocate such structure on the heap when
it is necessary to do so.
This commit is contained in:
Liav A 2022-09-23 14:47:16 +03:00 committed by Linus Groh
commit 6bafbd64e2
Notes: sideshowbarker 2024-07-17 06:41:51 +09:00

View file

@ -6,7 +6,9 @@
#pragma once #pragma once
#include <AK/NonnullOwnPtr.h>
#include <AK/StringView.h> #include <AK/StringView.h>
#include <AK/Try.h>
#include <Kernel/Memory/MemoryManager.h> #include <Kernel/Memory/MemoryManager.h>
namespace Kernel::Memory { namespace Kernel::Memory {
@ -24,6 +26,17 @@ struct TypedMapping {
size_t offset { 0 }; size_t offset { 0 };
}; };
template<typename T>
static ErrorOr<NonnullOwnPtr<TypedMapping<T>>> adopt_new_nonnull_own_typed_mapping(PhysicalAddress paddr, size_t length, Region::Access access = Region::Access::Read)
{
auto mapping_length = TRY(page_round_up(paddr.offset_in_page() + length));
auto region = TRY(MM.allocate_kernel_region(paddr.page_base(), mapping_length, {}, access));
auto table = TRY(adopt_nonnull_own_or_enomem(new (nothrow) Memory::TypedMapping<T>()));
table->region = move(region);
table->offset = paddr.offset_in_page();
return table;
}
template<typename T> template<typename T>
static ErrorOr<TypedMapping<T>> map_typed(PhysicalAddress paddr, size_t length, Region::Access access = Region::Access::Read) static ErrorOr<TypedMapping<T>> map_typed(PhysicalAddress paddr, size_t length, Region::Access access = Region::Access::Read)
{ {