diff --git a/Kernel/Memory/VirtualRangeAllocator.cpp b/Kernel/Memory/VirtualRangeAllocator.cpp index d09bbd85ad..02389c8e1b 100644 --- a/Kernel/Memory/VirtualRangeAllocator.cpp +++ b/Kernel/Memory/VirtualRangeAllocator.cpp @@ -42,13 +42,13 @@ void VirtualRangeAllocator::dump() const } } -void VirtualRangeAllocator::carve_at_iterator(auto& it, VirtualRange const& range) +void VirtualRangeAllocator::carve_from_region(VirtualRange const& from, VirtualRange const& range) { VERIFY(m_lock.is_locked()); - auto remaining_parts = (*it).carve(range); + auto remaining_parts = from.carve(range); VERIFY(remaining_parts.size() >= 1); VERIFY(m_total_range.contains(remaining_parts[0])); - m_available_ranges.remove(it.key()); + m_available_ranges.remove(from.base().get()); m_available_ranges.insert(remaining_parts[0].base().get(), remaining_parts[0]); if (remaining_parts.size() == 2) { VERIFY(m_total_range.contains(remaining_parts[1])); @@ -122,7 +122,7 @@ KResultOr VirtualRangeAllocator::try_allocate_anywhere(size_t size m_available_ranges.remove(it.key()); return allocated_range; } - carve_at_iterator(it, allocated_range); + carve_from_region(*it, allocated_range); return allocated_range; } dmesgln("VirtualRangeAllocator: Failed to allocate anywhere: size={}, alignment={}", size, alignment); @@ -142,18 +142,17 @@ KResultOr VirtualRangeAllocator::try_allocate_specific(VirtualAddr return ENOMEM; SpinlockLocker lock(m_lock); - for (auto it = m_available_ranges.begin(); !it.is_end(); ++it) { - auto& available_range = *it; - if (!available_range.contains(base, size)) - continue; - if (available_range == allocated_range) { - m_available_ranges.remove(it.key()); - return allocated_range; - } - carve_at_iterator(it, allocated_range); + auto available_range = m_available_ranges.find_largest_not_above(base.get()); + if (!available_range) + return ENOMEM; + if (!available_range->contains(allocated_range)) + return ENOMEM; + if (*available_range == allocated_range) { + m_available_ranges.remove(available_range->base().get()); return allocated_range; } - return ENOMEM; + carve_from_region(*available_range, allocated_range); + return allocated_range; } void VirtualRangeAllocator::deallocate(VirtualRange const& range) diff --git a/Kernel/Memory/VirtualRangeAllocator.h b/Kernel/Memory/VirtualRangeAllocator.h index 12cc77a456..9a62b00194 100644 --- a/Kernel/Memory/VirtualRangeAllocator.h +++ b/Kernel/Memory/VirtualRangeAllocator.h @@ -31,7 +31,7 @@ public: bool contains(VirtualRange const& range) const { return m_total_range.contains(range); } private: - void carve_at_iterator(auto&, VirtualRange const&); + void carve_from_region(VirtualRange const& from, VirtualRange const&); RedBlackTree m_available_ranges; VirtualRange m_total_range;