2021-02-25 11:42:49 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2021-02-26 11:16:03 +01:00
|
|
|
* Copyright (c) 2021, Leon Albrecht <leon2002.la@gmail.com>
|
2021-02-25 11:42:49 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-02-25 11:42:49 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2021-09-04 21:04:10 +00:00
|
|
|
#include <Kernel/API/KResult.h>
|
2021-02-25 11:42:49 +01:00
|
|
|
#include <Kernel/VirtualAddress.h>
|
|
|
|
|
|
2021-08-06 13:49:36 +02:00
|
|
|
namespace Kernel::Memory {
|
2021-02-25 11:42:49 +01:00
|
|
|
|
2021-08-06 13:54:48 +02:00
|
|
|
class VirtualRange {
|
|
|
|
|
friend class VirtualRangeAllocator;
|
2021-02-25 11:42:49 +01:00
|
|
|
|
|
|
|
|
public:
|
2021-08-06 13:54:48 +02:00
|
|
|
VirtualRange() = delete;
|
|
|
|
|
VirtualRange(VirtualAddress base, size_t size)
|
2021-02-25 11:42:49 +01:00
|
|
|
: m_base(base)
|
|
|
|
|
, m_size(size)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VirtualAddress base() const { return m_base; }
|
|
|
|
|
size_t size() const { return m_size; }
|
|
|
|
|
bool is_valid() const { return !m_base.is_null(); }
|
|
|
|
|
|
|
|
|
|
bool contains(VirtualAddress vaddr) const { return vaddr >= base() && vaddr < end(); }
|
|
|
|
|
|
|
|
|
|
VirtualAddress end() const { return m_base.offset(m_size); }
|
|
|
|
|
|
2021-08-06 13:54:48 +02:00
|
|
|
bool operator==(VirtualRange const& other) const
|
2021-02-25 11:42:49 +01:00
|
|
|
{
|
|
|
|
|
return m_base == other.m_base && m_size == other.m_size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool contains(VirtualAddress base, size_t size) const
|
|
|
|
|
{
|
|
|
|
|
if (base.offset(size) < base)
|
|
|
|
|
return false;
|
|
|
|
|
return base >= m_base && base.offset(size) <= end();
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-06 13:54:48 +02:00
|
|
|
bool contains(VirtualRange const& other) const
|
2021-02-25 11:42:49 +01:00
|
|
|
{
|
|
|
|
|
return contains(other.base(), other.size());
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-06 13:54:48 +02:00
|
|
|
Vector<VirtualRange, 2> carve(VirtualRange const&) const;
|
|
|
|
|
VirtualRange intersect(VirtualRange const&) const;
|
2021-02-25 11:42:49 +01:00
|
|
|
|
2021-08-06 13:54:48 +02:00
|
|
|
static KResultOr<VirtualRange> expand_to_page_boundaries(FlatPtr address, size_t size);
|
2021-05-28 11:03:21 +02:00
|
|
|
|
2021-02-25 11:42:49 +01:00
|
|
|
private:
|
|
|
|
|
VirtualAddress m_base;
|
|
|
|
|
size_t m_size { 0 };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|
2021-07-06 20:37:44 -06:00
|
|
|
|
|
|
|
|
template<>
|
2021-08-06 13:54:48 +02:00
|
|
|
struct AK::Formatter<Kernel::Memory::VirtualRange> : Formatter<FormatString> {
|
|
|
|
|
void format(FormatBuilder& builder, Kernel::Memory::VirtualRange value)
|
2021-07-06 20:37:44 -06:00
|
|
|
{
|
2021-07-21 19:53:38 +02:00
|
|
|
return Formatter<FormatString>::format(builder, "{} - {} (size {:p})", value.base().as_ptr(), value.base().offset(value.size() - 1).as_ptr(), value.size());
|
2021-07-06 20:37:44 -06:00
|
|
|
}
|
|
|
|
|
};
|