summaryrefslogtreecommitdiff
path: root/Kernel/Memory/VirtualRange.cpp
blob: eea79f785a077f50126af054aaccd98b8b8eb191 (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
51
52
53
54
55
56
57
58
59
60
/*
 * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
 * Copyright (c) 2021, Leon Albrecht <leon2002.la@gmail.com>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/Vector.h>
#include <Kernel/Memory/MemoryManager.h>
#include <Kernel/Memory/VirtualRange.h>

namespace Kernel::Memory {

Vector<VirtualRange, 2> VirtualRange::carve(VirtualRange const& taken) const
{
    VERIFY((taken.size() % PAGE_SIZE) == 0);

    Vector<VirtualRange, 2> parts;
    if (taken == *this)
        return {};
    if (taken.base() > base())
        parts.append({ base(), taken.base().get() - base().get() });
    if (taken.end() < end())
        parts.append({ taken.end(), end().get() - taken.end().get() });
    return parts;
}

bool VirtualRange::intersects(VirtualRange const& other) const
{
    auto a = *this;
    auto b = other;

    if (a.base() > b.base())
        swap(a, b);

    return a.base() < b.end() && b.base() < a.end();
}

VirtualRange VirtualRange::intersect(VirtualRange const& other) const
{
    if (*this == other) {
        return *this;
    }
    auto new_base = max(base(), other.base());
    auto new_end = min(end(), other.end());
    VERIFY(new_base < new_end);
    return VirtualRange(new_base, (new_end - new_base).get());
}

ErrorOr<VirtualRange> VirtualRange::expand_to_page_boundaries(FlatPtr address, size_t size)
{
    if ((address + size) < address)
        return EINVAL;

    auto base = VirtualAddress { address }.page_base();
    auto end = TRY(page_round_up(address + size));
    return VirtualRange { base, end - base.get() };
}

}