Kernel/Memory: Move some VirtualRange functions inline

This will allow turning this class into a templated base class.
Don't move expand_to_page_boundaries() inline since it will only be
implemented by one subclass. Additionally, this function uses
page_round_up() from MemoryManager.h. Including this header in
VirtualRange.h would result in a circular include.
This commit is contained in:
Sönke Holz authored and Sönke Holz committed 2026-09-18 17:25:42 +02:00
1 parent 3c4fe04b2b
commit 25538bf0f0
2 files changed
+35 -40

No files matched your search

-37
View File
@@ -5,48 +5,11 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Vector.h>
#include <Kernel/Memory/MemoryManager.h>
#include <Kernel/Memory/VirtualRange.h>
namespace Kernel::Memory {
Vector<VirtualRange, 2> VirtualRange::carve(VirtualRange const& taken) const
{
VERIFY((taken.size() % PAGE_SIZE) == 0);
Vector<VirtualRange, 2> parts;
if (taken == *this)
return {};
if (taken.base() > base())
parts.unchecked_append({ base(), taken.base().get() - base().get() });
if (taken.end() < end())
parts.unchecked_append({ taken.end(), end().get() - taken.end().get() });
return parts;
}
bool VirtualRange::intersects(VirtualRange const& other) const
{
auto a = *this;
auto b = other;
if (a.base() > b.base())
swap(a, b);
return a.base() < b.end() && b.base() < a.end();
}
VirtualRange VirtualRange::intersect(VirtualRange const& other) const
{
if (*this == other) {
return *this;
}
auto new_base = max(base(), other.base());
auto new_end = min(end(), other.end());
VERIFY(new_base < new_end);
return VirtualRange(new_base, (new_end - new_base).get());
}
ErrorOr<VirtualRange> VirtualRange::expand_to_page_boundaries(FlatPtr address, size_t size)
{
if ((address + size) < address)
+35 -3
View File
@@ -8,6 +8,7 @@
#pragma once
#include <AK/Error.h>
#include <AK/Vector.h>
#include <Kernel/Memory/VirtualAddress.h>
namespace Kernel::Memory {
@@ -46,10 +47,41 @@ public:
return contains(other.base(), other.size());
}
Vector<VirtualRange, 2> carve(VirtualRange const&) const;
VirtualRange intersect(VirtualRange const&) const;
Vector<VirtualRange, 2> carve(VirtualRange const& taken) const
{
VERIFY((taken.size() % PAGE_SIZE) == 0);
bool intersects(VirtualRange const&) const;
Vector<VirtualRange, 2> parts;
if (taken == *this)
return {};
if (taken.base() > base())
parts.unchecked_append({ base(), taken.base().get() - base().get() });
if (taken.end() < end())
parts.unchecked_append({ taken.end(), end().get() - taken.end().get() });
return parts;
}
VirtualRange intersect(VirtualRange const& other) const
{
if (*this == other) {
return *this;
}
auto new_base = max(base(), other.base());
auto new_end = min(end(), other.end());
VERIFY(new_base < new_end);
return VirtualRange(new_base, (new_end - new_base).get());
}
bool intersects(VirtualRange const& other) const
{
auto a = *this;
auto b = other;
if (a.base() > b.base())
swap(a, b);
return a.base() < b.end() && b.base() < a.end();
}
static ErrorOr<VirtualRange> expand_to_page_boundaries(FlatPtr address, size_t size);