Coverage Report

Created: 2026-05-16 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/serenity/Kernel/Memory/VirtualAddress.h
Line
Count
Source
1
/*
2
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3
 *
4
 * SPDX-License-Identifier: BSD-2-Clause
5
 */
6
7
#pragma once
8
9
#include <AK/Format.h>
10
#include <AK/Types.h>
11
12
class VirtualAddress {
13
public:
14
    VirtualAddress() = default;
15
    constexpr explicit VirtualAddress(FlatPtr address)
16
        : m_address(address)
17
0
    {
18
0
    }
19
20
    explicit VirtualAddress(void const* address)
21
        : m_address((FlatPtr)address)
22
0
    {
23
0
    }
24
25
0
    [[nodiscard]] constexpr bool is_null() const { return m_address == 0; }
26
0
    [[nodiscard]] constexpr bool is_page_aligned() const { return (m_address & 0xfff) == 0; }
27
28
0
    [[nodiscard]] constexpr VirtualAddress offset(FlatPtr o) const { return VirtualAddress(m_address + o); }
29
0
    [[nodiscard]] constexpr FlatPtr get() const { return m_address; }
30
0
    void set(FlatPtr address) { m_address = address; }
31
0
    void mask(FlatPtr m) { m_address &= m; }
32
33
0
    bool operator<=(VirtualAddress const& other) const { return m_address <= other.m_address; }
34
0
    bool operator>=(VirtualAddress const& other) const { return m_address >= other.m_address; }
35
0
    bool operator>(VirtualAddress const& other) const { return m_address > other.m_address; }
36
0
    bool operator<(VirtualAddress const& other) const { return m_address < other.m_address; }
37
0
    bool operator==(VirtualAddress const& other) const { return m_address == other.m_address; }
38
0
    bool operator!=(VirtualAddress const& other) const { return m_address != other.m_address; }
39
40
    // NOLINTNEXTLINE(readability-make-member-function-const) const VirtualAddress shouldn't be allowed to modify the underlying memory
41
0
    [[nodiscard]] u8* as_ptr() { return reinterpret_cast<u8*>(m_address); }
42
0
    [[nodiscard]] u8 const* as_ptr() const { return reinterpret_cast<u8 const*>(m_address); }
43
44
0
    [[nodiscard]] VirtualAddress page_base() const { return VirtualAddress(m_address & ~(FlatPtr)0xfffu); }
45
46
private:
47
    FlatPtr m_address { 0 };
48
};
49
50
inline VirtualAddress operator-(VirtualAddress const& a, VirtualAddress const& b)
51
0
{
52
0
    return VirtualAddress(a.get() - b.get());
53
0
}
54
55
template<>
56
struct AK::Formatter<VirtualAddress> : AK::Formatter<FormatString> {
57
    ErrorOr<void> format(FormatBuilder& builder, VirtualAddress const& value)
58
0
    {
59
0
        return AK::Formatter<FormatString>::format(builder, "V{}"sv, value.as_ptr());
60
0
    }
61
};