summaryrefslogtreecommitdiff
path: root/Kernel/PhysicalAddress.h
blob: 7c0a2efcd2e81377a5317316a6e4e8fe91641065 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Format.h>
#include <AK/Types.h>

class PhysicalAddress {
public:
    PhysicalAddress() = default;
    explicit PhysicalAddress(FlatPtr address)
        : m_address(address)
    {
    }

    [[nodiscard]] PhysicalAddress offset(FlatPtr o) const { return PhysicalAddress(m_address + o); }
    [[nodiscard]] FlatPtr get() const { return m_address; }
    void set(FlatPtr address) { m_address = address; }
    void mask(FlatPtr m) { m_address &= m; }

    [[nodiscard]] bool is_null() const { return m_address == 0; }

    [[nodiscard]] u8* as_ptr() { return reinterpret_cast<u8*>(m_address); }
    [[nodiscard]] const u8* as_ptr() const { return reinterpret_cast<const u8*>(m_address); }

    [[nodiscard]] PhysicalAddress page_base() const { return PhysicalAddress(m_address & 0xfffff000); }
    [[nodiscard]] FlatPtr offset_in_page() const { return PhysicalAddress(m_address & 0xfff).get(); }

    bool operator==(const PhysicalAddress& other) const { return m_address == other.m_address; }
    bool operator!=(const PhysicalAddress& other) const { return m_address != other.m_address; }
    bool operator>(const PhysicalAddress& other) const { return m_address > other.m_address; }
    bool operator>=(const PhysicalAddress& other) const { return m_address >= other.m_address; }
    bool operator<(const PhysicalAddress& other) const { return m_address < other.m_address; }
    bool operator<=(const PhysicalAddress& other) const { return m_address <= other.m_address; }

private:
    FlatPtr m_address { 0 };
};

template<>
struct AK::Formatter<PhysicalAddress> : AK::Formatter<FormatString> {
    void format(FormatBuilder& builder, PhysicalAddress value)
    {
        return AK::Formatter<FormatString>::format(builder, "P{}", value.as_ptr());
    }
};