RegionTree.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Error.h>
  8. #include <AK/IntrusiveRedBlackTree.h>
  9. #include <Kernel/Locking/Spinlock.h>
  10. #include <Kernel/Memory/Region.h>
  11. #include <Kernel/Memory/VirtualAddress.h>
  12. #include <Kernel/Memory/VirtualRange.h>
  13. namespace Kernel::Memory {
  14. enum class RandomizeVirtualAddress {
  15. No,
  16. Yes,
  17. };
  18. // RegionTree represents a virtual address space.
  19. // It is used by MemoryManager for kernel VM and by AddressSpace for user VM.
  20. // Regions are stored in an intrusive data structure and there are no allocations when interacting with it.
  21. class RegionTree {
  22. AK_MAKE_NONCOPYABLE(RegionTree);
  23. AK_MAKE_NONMOVABLE(RegionTree);
  24. public:
  25. explicit RegionTree(VirtualRange total_range)
  26. : m_total_range(total_range)
  27. {
  28. }
  29. ~RegionTree();
  30. auto& regions() { return m_regions; }
  31. auto const& regions() const { return m_regions; }
  32. VirtualRange total_range() const { return m_total_range; }
  33. ErrorOr<void> place_anywhere(Region&, RandomizeVirtualAddress, size_t size, size_t alignment = PAGE_SIZE);
  34. ErrorOr<void> place_specifically(Region&, VirtualRange const&);
  35. void delete_all_regions_assuming_they_are_unmapped();
  36. bool remove(Region&);
  37. Region* find_region_containing(VirtualAddress);
  38. Region* find_region_containing(VirtualRange);
  39. private:
  40. ErrorOr<VirtualRange> allocate_range_anywhere(size_t size, size_t alignment = PAGE_SIZE);
  41. ErrorOr<VirtualRange> allocate_range_specific(VirtualAddress base, size_t size);
  42. ErrorOr<VirtualRange> allocate_range_randomized(size_t size, size_t alignment = PAGE_SIZE);
  43. IntrusiveRedBlackTree<&Region::m_tree_node> m_regions;
  44. VirtualRange const m_total_range;
  45. };
  46. }