summaryrefslogtreecommitdiff
path: root/Kernel/Memory/VirtualRangeAllocator.h
blob: b469966d67e39fbf39fa52d6f4e8c291bf07b532 (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
/*
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/RedBlackTree.h>
#include <AK/Traits.h>
#include <Kernel/Locking/Spinlock.h>
#include <Kernel/Memory/VirtualRange.h>

namespace Kernel::Memory {

class VirtualRangeAllocator {
public:
    VirtualRangeAllocator();
    ~VirtualRangeAllocator() = default;

    ErrorOr<void> initialize_with_range(VirtualAddress, size_t);
    ErrorOr<void> initialize_from_parent(VirtualRangeAllocator const&);

    ErrorOr<VirtualRange> try_allocate_anywhere(size_t, size_t alignment = PAGE_SIZE);
    ErrorOr<VirtualRange> try_allocate_specific(VirtualAddress, size_t);
    ErrorOr<VirtualRange> try_allocate_randomized(size_t, size_t alignment);
    void deallocate(VirtualRange const&);

    void dump() const;

    bool contains(VirtualRange const& range) const { return m_total_range.contains(range); }

private:
    void carve_from_region(VirtualRange const& from, VirtualRange const&);

    RedBlackTree<FlatPtr, VirtualRange> m_available_ranges;
    VirtualRange m_total_range;
    mutable Spinlock m_lock;
};

}

namespace AK {
template<>
struct Traits<Kernel::Memory::VirtualRange> : public GenericTraits<Kernel::Memory::VirtualRange> {
    static constexpr bool is_trivial() { return true; }
};
}