ladybird/Kernel/VM/PhysicalRegion.h
Andreas Kling 379bcd26e4 Kernel: Avoid O(n) loop over zones when allocating from PhysicalRegion
We now keep all the PhysicalZones on one of two intrusive lists within
the PhysicalRegion.

The "usable" list contains all zones that can be allocated from,
and the "full" list contains all zones with no free pages.
2021-07-13 23:08:45 +02:00

55 lines
1.4 KiB
C++

/*
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Bitmap.h>
#include <AK/NonnullOwnPtrVector.h>
#include <AK/Optional.h>
#include <AK/OwnPtr.h>
#include <Kernel/VM/PhysicalPage.h>
#include <Kernel/VM/PhysicalZone.h>
namespace Kernel {
class PhysicalRegion {
AK_MAKE_ETERNAL
public:
static OwnPtr<PhysicalRegion> try_create(PhysicalAddress lower, PhysicalAddress upper)
{
return adopt_own_if_nonnull(new PhysicalRegion { lower, upper });
}
~PhysicalRegion();
void initialize_zones();
PhysicalAddress lower() const { return m_lower; }
PhysicalAddress upper() const { return m_upper; }
unsigned size() const { return m_pages; }
bool contains(PhysicalAddress paddr) const { return paddr >= m_lower && paddr <= m_upper; }
OwnPtr<PhysicalRegion> try_take_pages_from_beginning(unsigned);
RefPtr<PhysicalPage> take_free_page();
NonnullRefPtrVector<PhysicalPage> take_contiguous_free_pages(size_t count);
void return_page(PhysicalAddress);
private:
PhysicalRegion(PhysicalAddress lower, PhysicalAddress upper);
NonnullOwnPtrVector<PhysicalZone> m_zones;
PhysicalZone::List m_usable_zones;
PhysicalZone::List m_full_zones;
PhysicalAddress m_lower;
PhysicalAddress m_upper;
unsigned m_pages { 0 };
};
}