2020-02-09 16:00:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-02-09 16:00:00 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-03-12 16:29:37 +00:00
|
|
|
#include <AK/Format.h>
|
2020-02-09 16:00:00 +00:00
|
|
|
#include <AK/Types.h>
|
|
|
|
|
|
|
|
class VirtualAddress {
|
|
|
|
public:
|
2021-02-28 13:42:08 +00:00
|
|
|
VirtualAddress() = default;
|
2021-08-25 03:39:33 +00:00
|
|
|
constexpr explicit VirtualAddress(FlatPtr address)
|
2020-02-09 16:00:00 +00:00
|
|
|
: m_address(address)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
explicit VirtualAddress(void const* address)
|
2020-03-08 09:36:51 +00:00
|
|
|
: m_address((FlatPtr)address)
|
2020-02-09 16:00:00 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-08-25 03:39:33 +00:00
|
|
|
[[nodiscard]] constexpr bool is_null() const { return m_address == 0; }
|
|
|
|
[[nodiscard]] constexpr bool is_page_aligned() const { return (m_address & 0xfff) == 0; }
|
2020-02-09 16:00:00 +00:00
|
|
|
|
2021-08-25 03:39:33 +00:00
|
|
|
[[nodiscard]] constexpr VirtualAddress offset(FlatPtr o) const { return VirtualAddress(m_address + o); }
|
|
|
|
[[nodiscard]] constexpr FlatPtr get() const { return m_address; }
|
2020-03-08 09:36:51 +00:00
|
|
|
void set(FlatPtr address) { m_address = address; }
|
|
|
|
void mask(FlatPtr m) { m_address &= m; }
|
2020-02-09 16:00:00 +00:00
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
bool operator<=(VirtualAddress const& other) const { return m_address <= other.m_address; }
|
|
|
|
bool operator>=(VirtualAddress const& other) const { return m_address >= other.m_address; }
|
|
|
|
bool operator>(VirtualAddress const& other) const { return m_address > other.m_address; }
|
|
|
|
bool operator<(VirtualAddress const& other) const { return m_address < other.m_address; }
|
|
|
|
bool operator==(VirtualAddress const& other) const { return m_address == other.m_address; }
|
|
|
|
bool operator!=(VirtualAddress const& other) const { return m_address != other.m_address; }
|
2020-02-09 16:00:00 +00:00
|
|
|
|
2021-10-31 21:54:39 +00:00
|
|
|
// NOLINTNEXTLINE(readability-make-member-function-const) const VirtualAddress shouldn't be allowed to modify the underlying memory
|
2021-02-14 23:18:35 +00:00
|
|
|
[[nodiscard]] u8* as_ptr() { return reinterpret_cast<u8*>(m_address); }
|
2022-04-01 17:58:27 +00:00
|
|
|
[[nodiscard]] u8 const* as_ptr() const { return reinterpret_cast<u8 const*>(m_address); }
|
2020-02-09 16:00:00 +00:00
|
|
|
|
2021-07-16 23:43:38 +00:00
|
|
|
[[nodiscard]] VirtualAddress page_base() const { return VirtualAddress(m_address & ~(FlatPtr)0xfffu); }
|
2020-02-09 16:00:00 +00:00
|
|
|
|
|
|
|
private:
|
2020-03-08 09:36:51 +00:00
|
|
|
FlatPtr m_address { 0 };
|
2020-02-09 16:00:00 +00:00
|
|
|
};
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
inline VirtualAddress operator-(VirtualAddress const& a, VirtualAddress const& b)
|
2020-02-09 16:00:00 +00:00
|
|
|
{
|
|
|
|
return VirtualAddress(a.get() - b.get());
|
|
|
|
}
|
|
|
|
|
2020-12-25 11:51:11 +00:00
|
|
|
template<>
|
2021-01-09 00:00:22 +00:00
|
|
|
struct AK::Formatter<VirtualAddress> : AK::Formatter<FormatString> {
|
2022-04-01 17:58:27 +00:00
|
|
|
ErrorOr<void> format(FormatBuilder& builder, VirtualAddress const& value)
|
2021-01-09 00:00:22 +00:00
|
|
|
{
|
2022-07-11 17:32:29 +00:00
|
|
|
return AK::Formatter<FormatString>::format(builder, "V{}"sv, value.as_ptr());
|
2021-01-09 00:00:22 +00:00
|
|
|
}
|
2020-12-25 11:51:11 +00:00
|
|
|
};
|