2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2019-06-11 11:13:02 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Bitmap.h>
|
2020-05-08 18:15:01 +00:00
|
|
|
#include <AK/NonnullRefPtrVector.h>
|
2020-03-07 17:24:41 +00:00
|
|
|
#include <AK/Optional.h>
|
2020-02-16 00:27:42 +00:00
|
|
|
#include <AK/RefCounted.h>
|
2019-06-11 11:13:02 +00:00
|
|
|
#include <Kernel/VM/PhysicalPage.h>
|
|
|
|
|
2020-02-16 00:27:42 +00:00
|
|
|
namespace Kernel {
|
|
|
|
|
2019-06-21 13:29:31 +00:00
|
|
|
class PhysicalRegion : public RefCounted<PhysicalRegion> {
|
2019-06-11 11:13:02 +00:00
|
|
|
AK_MAKE_ETERNAL
|
|
|
|
|
|
|
|
public:
|
2019-06-21 16:37:47 +00:00
|
|
|
static NonnullRefPtr<PhysicalRegion> create(PhysicalAddress lower, PhysicalAddress upper);
|
2021-02-28 13:42:08 +00:00
|
|
|
~PhysicalRegion() = default;
|
2019-06-11 11:13:02 +00:00
|
|
|
|
|
|
|
void expand(PhysicalAddress lower, PhysicalAddress upper);
|
|
|
|
unsigned finalize_capacity();
|
|
|
|
|
|
|
|
PhysicalAddress lower() const { return m_lower; }
|
|
|
|
PhysicalAddress upper() const { return m_upper; }
|
|
|
|
unsigned size() const { return m_pages; }
|
2020-09-08 05:18:38 +00:00
|
|
|
unsigned used() const { return m_used - m_recently_returned.size(); }
|
|
|
|
unsigned free() const { return m_pages - m_used + m_recently_returned.size(); }
|
2020-08-22 03:49:50 +00:00
|
|
|
bool contains(const PhysicalPage& page) const { return page.paddr() >= m_lower && page.paddr() <= m_upper; }
|
2019-06-11 11:13:02 +00:00
|
|
|
|
2019-06-21 16:37:47 +00:00
|
|
|
RefPtr<PhysicalPage> take_free_page(bool supervisor);
|
2020-12-06 05:49:24 +00:00
|
|
|
NonnullRefPtrVector<PhysicalPage> take_contiguous_free_pages(size_t count, bool supervisor, size_t physical_alignment = PAGE_SIZE);
|
2020-09-08 05:18:38 +00:00
|
|
|
void return_page(const PhysicalPage& page);
|
2019-06-11 11:13:02 +00:00
|
|
|
|
|
|
|
private:
|
2020-12-06 05:49:24 +00:00
|
|
|
unsigned find_contiguous_free_pages(size_t count, size_t physical_alignment = PAGE_SIZE);
|
|
|
|
Optional<unsigned> find_and_allocate_contiguous_range(size_t count, unsigned alignment = 1);
|
2020-09-08 05:18:38 +00:00
|
|
|
Optional<unsigned> find_one_free_page();
|
|
|
|
void free_page_at(PhysicalAddress addr);
|
2020-03-07 17:24:41 +00:00
|
|
|
|
2019-06-11 11:13:02 +00:00
|
|
|
PhysicalRegion(PhysicalAddress lower, PhysicalAddress upper);
|
|
|
|
|
|
|
|
PhysicalAddress m_lower;
|
|
|
|
PhysicalAddress m_upper;
|
|
|
|
unsigned m_pages { 0 };
|
|
|
|
unsigned m_used { 0 };
|
|
|
|
Bitmap m_bitmap;
|
2020-09-08 05:18:38 +00:00
|
|
|
size_t m_free_hint { 0 };
|
|
|
|
Vector<PhysicalAddress, 256> m_recently_returned;
|
2019-06-11 11:13:02 +00:00
|
|
|
};
|
2020-02-16 00:27:42 +00:00
|
|
|
|
|
|
|
}
|