VirtualRangeAllocator.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/RedBlackTree.h>
  8. #include <AK/Traits.h>
  9. #include <Kernel/Locking/Spinlock.h>
  10. #include <Kernel/Memory/VirtualRange.h>
  11. namespace Kernel::Memory {
  12. class VirtualRangeAllocator {
  13. public:
  14. VirtualRangeAllocator();
  15. ~VirtualRangeAllocator() = default;
  16. void initialize_with_range(VirtualAddress, size_t);
  17. void initialize_from_parent(VirtualRangeAllocator const&);
  18. Optional<VirtualRange> allocate_anywhere(size_t, size_t alignment = PAGE_SIZE);
  19. Optional<VirtualRange> allocate_specific(VirtualAddress, size_t);
  20. Optional<VirtualRange> allocate_randomized(size_t, size_t alignment);
  21. void deallocate(VirtualRange const&);
  22. void dump() const;
  23. bool contains(VirtualRange const& range) const { return m_total_range.contains(range); }
  24. private:
  25. void carve_at_iterator(auto&, VirtualRange const&);
  26. RedBlackTree<FlatPtr, VirtualRange> m_available_ranges;
  27. VirtualRange m_total_range;
  28. mutable Spinlock<u8> m_lock;
  29. };
  30. }
  31. namespace AK {
  32. template<>
  33. struct Traits<Kernel::Memory::VirtualRange> : public GenericTraits<Kernel::Memory::VirtualRange> {
  34. static constexpr bool is_trivial() { return true; }
  35. };
  36. }