ladybird/Kernel/Memory/VirtualRangeAllocator.h
Andreas Kling 02a95a196f Kernel: Use AddressSpace region tree for range allocation
This patch stops using VirtualRangeAllocator in AddressSpace and instead
looks for holes in the region tree when allocating VM space.

There are many benefits:

- VirtualRangeAllocator is non-intrusive and would call kmalloc/kfree
  when used. This new solution is allocation-free. This was a source
  of unpleasant MM/kmalloc deadlocks.

- We consolidate authority on what the address space looks like in a
  single place. Previously, we had both the range allocator *and* the
  region tree both being used to determine if an address was valid.
  Now there is only the region tree.

- Deallocation of VM when splitting regions is no longer complicated,
  as we don't need to keep two separate trees in sync.
2022-04-03 21:51:58 +02:00

50 lines
1.4 KiB
C++

/*
* 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); }
VirtualRange total_range() const { return m_total_range; }
private:
ErrorOr<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; }
};
}