summaryrefslogtreecommitdiff
path: root/Kernel/VM/VirtualAddress.h
blob: 273053d431aa3516a6656d26c01d1d9b846c1ef7 (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
#pragma once

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

class VirtualAddress {
public:
    VirtualAddress() {}
    explicit VirtualAddress(u32 address)
        : m_address(address)
    {
    }

    bool is_null() const { return m_address == 0; }
    bool is_page_aligned() const { return (m_address & 0xfff) == 0; }

    VirtualAddress offset(u32 o) const { return VirtualAddress(m_address + o); }
    u32 get() const { return m_address; }
    void set(u32 address) { m_address = address; }
    void mask(u32 m) { m_address &= m; }

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

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

    VirtualAddress page_base() const { return VirtualAddress(m_address & 0xfffff000); }

private:
    u32 m_address { 0 };
};

inline VirtualAddress operator-(const VirtualAddress& a, const VirtualAddress& b)
{
    return VirtualAddress(a.get() - b.get());
}

inline const LogStream& operator<<(const LogStream& stream, VirtualAddress value)
{
    return stream << 'V' << value.as_ptr();
}