2021-02-25 13:42:49 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2021-02-26 13:16:03 +03:00
|
|
|
* Copyright (c) 2021, Leon Albrecht <leon2002.la@gmail.com>
|
2021-02-25 13:42:49 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-02-25 13:42:49 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
#include <AK/Error.h>
|
2023-02-24 20:54:30 +03:00
|
|
|
#include <Kernel/Memory/VirtualAddress.h>
|
2021-02-25 13:42:49 +03:00
|
|
|
|
2021-08-06 14:49:36 +03:00
|
|
|
namespace Kernel::Memory {
|
2021-02-25 13:42:49 +03:00
|
|
|
|
2021-08-06 14:54:48 +03:00
|
|
|
class VirtualRange {
|
2021-02-25 13:42:49 +03:00
|
|
|
public:
|
2021-08-06 14:54:48 +03:00
|
|
|
VirtualRange() = delete;
|
|
|
|
VirtualRange(VirtualAddress base, size_t size)
|
2021-02-25 13:42:49 +03: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 14:54:48 +03:00
|
|
|
bool operator==(VirtualRange const& other) const
|
2021-02-25 13:42:49 +03: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 14:54:48 +03:00
|
|
|
bool contains(VirtualRange const& other) const
|
2021-02-25 13:42:49 +03:00
|
|
|
{
|
|
|
|
return contains(other.base(), other.size());
|
|
|
|
}
|
|
|
|
|
2021-08-06 14:54:48 +03:00
|
|
|
Vector<VirtualRange, 2> carve(VirtualRange const&) const;
|
|
|
|
VirtualRange intersect(VirtualRange const&) const;
|
2021-02-25 13:42:49 +03:00
|
|
|
|
2022-04-02 21:01:29 +03:00
|
|
|
bool intersects(VirtualRange const&) const;
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
static ErrorOr<VirtualRange> expand_to_page_boundaries(FlatPtr address, size_t size);
|
2021-05-28 12:03:21 +03:00
|
|
|
|
2021-02-25 13:42:49 +03:00
|
|
|
private:
|
|
|
|
VirtualAddress m_base;
|
|
|
|
size_t m_size { 0 };
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
2021-07-07 05:37:44 +03:00
|
|
|
|
|
|
|
template<>
|
2021-08-06 14:54:48 +03:00
|
|
|
struct AK::Formatter<Kernel::Memory::VirtualRange> : Formatter<FormatString> {
|
2021-11-16 03:15:21 +03:00
|
|
|
ErrorOr<void> format(FormatBuilder& builder, Kernel::Memory::VirtualRange value)
|
2021-07-07 05:37:44 +03:00
|
|
|
{
|
2022-07-11 20:32:29 +03:00
|
|
|
return Formatter<FormatString>::format(builder, "{} - {} (size {:p})"sv, value.base().as_ptr(), value.base().offset(value.size() - 1).as_ptr(), value.size());
|
2021-07-07 05:37:44 +03:00
|
|
|
}
|
|
|
|
};
|