Luna/kernel/src/memory/AddressSpace.h

63 lines
1.6 KiB
C
Raw Normal View History

#pragma once
#include "arch/MMU.h"
#include <luna/LinkedList.h>
#include <luna/OwnedPtr.h>
#include <luna/Result.h>
class VMRegion : LinkedListNode<VMRegion>
{
public:
u64 start;
u64 end;
usize count;
bool used { true };
bool persistent { false };
int flags { 0 };
int prot { 0 };
u64 shmid;
void cleanup_shared();
};
class AddressSpace
{
public:
AddressSpace();
~AddressSpace();
AddressSpace& operator=(AddressSpace&& other);
Result<u64> alloc_region(usize count, int prot, int flags, u64 shmid = 0, bool persistent = false);
Result<bool> test_and_alloc_region(u64 address, usize count, int prot, int flags, u64 shmid = 0,
bool persistent = false)
{
return set_region(address, count, true, prot, flags, shmid, persistent);
}
Result<bool> free_region(u64 address, usize count)
{
return set_region(address, count, false, 0, 0, 0, false);
}
static Result<OwnedPtr<AddressSpace>> try_create();
Result<OwnedPtr<AddressSpace>> clone();
2023-03-18 22:45:48 +00:00
PageDirectory* page_directory() const
{
return m_directory;
}
private:
Result<bool> set_region(u64 address, usize count, bool used, int prot, int flags, u64 shmid, bool persistent);
Result<void> create_default_region();
Result<void> create_null_region();
void try_merge_region_with_neighbors(VMRegion* region);
void merge_contiguous_regions(VMRegion* a, VMRegion* b);
Result<VMRegion*> split_region(VMRegion* parent, u64 boundary);
LinkedList<VMRegion> m_regions;
PageDirectory* m_directory;
};