Kernel/Memory: Added a function to first a suitable guest address at which to allocate a region of a given size.

This commit is contained in:
Subv
2018-10-10 18:59:27 -05:00
parent 5fb8854a11
commit 9ff787bf61
2 changed files with 30 additions and 0 deletions

View File

@@ -119,6 +119,28 @@ ResultVal<VMManager::VMAHandle> VMManager::MapMemoryBlock(VAddr target,
return MakeResult<VMAHandle>(MergeAdjacent(vma_handle));
}
ResultVal<VAddr> VMManager::FindFreeRegion(u32 size) {
VAddr base = GetAddressSpaceBaseAddress();
// Find the first Free VMA.
VMAHandle vma_handle = std::find_if(vma_map.begin(), vma_map.end(), [&](const auto& vma) {
if (vma.second.type != VMAType::Free)
return false;
VAddr vma_end = vma.second.base + vma.second.size;
return vma_end > base && vma_end >= base + size;
});
if (vma_handle == vma_map.end()) {
// TODO(Subv): Find the correct error code here.
return ResultCode(-1);
}
VAddr target = std::max(base, vma_handle->second.base);
return MakeResult<VAddr>(target);
}
ResultVal<VMManager::VMAHandle> VMManager::MapBackingMemory(VAddr target, u8* memory, u64 size,
MemoryState state) {
ASSERT(memory != nullptr);

View File

@@ -147,6 +147,14 @@ public:
ResultVal<VMAHandle> MapMemoryBlock(VAddr target, std::shared_ptr<std::vector<u8>> block,
std::size_t offset, u64 size, MemoryState state);
/**
* Finds the first free address that can hold a region of the desired size.
*
* @param size Size of the desired region.
* @returns The found free address.
*/
ResultVal<VAddr> FindFreeRegion(u32 size);
/**
* Maps an unmanaged host memory pointer at a given address.
*