Kernel: Add kmalloc helper APIs
This commit is contained in:
parent
5f63ea8f8a
commit
f0b18da881
|
@ -13,3 +13,4 @@ void* kmalloc(size_t size, size_t align, bool force_identity_map = false);
|
|||
void kfree(void*);
|
||||
|
||||
BAN::Optional<Kernel::paddr_t> kmalloc_paddr_of(Kernel::vaddr_t);
|
||||
BAN::Optional<Kernel::vaddr_t> kmalloc_vaddr_of(Kernel::paddr_t);
|
||||
|
|
|
@ -418,12 +418,29 @@ void kfree(void* address)
|
|||
|
||||
}
|
||||
|
||||
static bool is_kmalloc_vaddr(Kernel::vaddr_t vaddr)
|
||||
{
|
||||
using namespace Kernel;
|
||||
if (vaddr < reinterpret_cast<vaddr_t>(s_kmalloc_storage))
|
||||
return false;
|
||||
if (vaddr >= reinterpret_cast<vaddr_t>(s_kmalloc_storage) + sizeof(s_kmalloc_storage))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
BAN::Optional<Kernel::paddr_t> kmalloc_paddr_of(Kernel::vaddr_t vaddr)
|
||||
{
|
||||
using namespace Kernel;
|
||||
|
||||
if ((vaddr_t)s_kmalloc_storage <= vaddr && vaddr < (vaddr_t)s_kmalloc_storage + sizeof(s_kmalloc_storage))
|
||||
return vaddr - KERNEL_OFFSET + g_boot_info.kernel_paddr;
|
||||
|
||||
if (!is_kmalloc_vaddr(vaddr))
|
||||
return {};
|
||||
return vaddr - KERNEL_OFFSET + g_boot_info.kernel_paddr;
|
||||
}
|
||||
|
||||
BAN::Optional<Kernel::vaddr_t> kmalloc_vaddr_of(Kernel::paddr_t paddr)
|
||||
{
|
||||
using namespace Kernel;
|
||||
const vaddr_t vaddr = paddr + KERNEL_OFFSET - g_boot_info.kernel_paddr;
|
||||
if (!is_kmalloc_vaddr(vaddr))
|
||||
return {};
|
||||
return vaddr;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue