Kernel: Let Region keep a Range internally.

This commit is contained in:
Andreas Kling 2019-05-17 04:32:08 +02:00
parent 4a6fcfbacf
commit 87b54a82c7
Notes: sideshowbarker 2024-07-19 14:05:18 +09:00
5 changed files with 26 additions and 26 deletions

View file

@ -79,7 +79,7 @@ Region* Process::allocate_region(LinearAddress laddr, size_t size, String&& name
else else
range = m_range_allocator.allocate_specific(laddr, size); range = m_range_allocator.allocate_specific(laddr, size);
m_regions.append(adopt(*new Region(range.base(), range.size(), move(name), is_readable, is_writable))); m_regions.append(adopt(*new Region(range, move(name), is_readable, is_writable)));
MM.map_region(*this, *m_regions.last()); MM.map_region(*this, *m_regions.last());
if (commit) if (commit)
m_regions.last()->commit(); m_regions.last()->commit();
@ -97,7 +97,7 @@ Region* Process::allocate_file_backed_region(LinearAddress laddr, size_t size, R
else else
range = m_range_allocator.allocate_specific(laddr, size); range = m_range_allocator.allocate_specific(laddr, size);
m_regions.append(adopt(*new Region(range.base(), range.size(), move(inode), move(name), is_readable, is_writable))); m_regions.append(adopt(*new Region(range, move(inode), move(name), is_readable, is_writable)));
MM.map_region(*this, *m_regions.last()); MM.map_region(*this, *m_regions.last());
return m_regions.last().ptr(); return m_regions.last().ptr();
} }
@ -115,7 +115,7 @@ Region* Process::allocate_region_with_vmo(LinearAddress laddr, size_t size, Reta
offset_in_vmo &= PAGE_MASK; offset_in_vmo &= PAGE_MASK;
size = ceil_div(size, PAGE_SIZE) * PAGE_SIZE; size = ceil_div(size, PAGE_SIZE) * PAGE_SIZE;
m_regions.append(adopt(*new Region(range.base(), range.size(), move(vmo), offset_in_vmo, move(name), is_readable, is_writable))); m_regions.append(adopt(*new Region(range, move(vmo), offset_in_vmo, move(name), is_readable, is_writable)));
MM.map_region(*this, *m_regions.last()); MM.map_region(*this, *m_regions.last());
return m_regions.last().ptr(); return m_regions.last().ptr();
} }

View file

@ -404,7 +404,7 @@ RetainPtr<Region> MemoryManager::allocate_kernel_region(size_t size, String&& na
ASSERT(!(size % PAGE_SIZE)); ASSERT(!(size % PAGE_SIZE));
auto range = m_range_allocator.allocate_anywhere(size); auto range = m_range_allocator.allocate_anywhere(size);
ASSERT(range.is_valid()); ASSERT(range.is_valid());
auto region = adopt(*new Region(range.base(), range.size(), move(name), true, true, false)); auto region = adopt(*new Region(range, move(name), true, true, false));
MM.map_region_at_address(*m_kernel_page_directory, *region, range.base(), false); MM.map_region_at_address(*m_kernel_page_directory, *region, range.base(), false);
// FIXME: It would be cool if these could zero-fill on demand instead. // FIXME: It would be cool if these could zero-fill on demand instead.
region->commit(); region->commit();

View file

@ -17,6 +17,8 @@ public:
size_t size() const { return m_size; } size_t size() const { return m_size; }
bool is_valid() const { return !m_base.is_null(); } bool is_valid() const { return !m_base.is_null(); }
bool contains(LinearAddress laddr) const { return laddr >= base() && laddr < end(); }
LinearAddress end() const { return m_base.offset(m_size); } LinearAddress end() const { return m_base.offset(m_size); }
bool operator==(const Range& other) const bool operator==(const Range& other) const

View file

@ -4,10 +4,9 @@
#include <Kernel/Process.h> #include <Kernel/Process.h>
#include <Kernel/Thread.h> #include <Kernel/Thread.h>
Region::Region(LinearAddress a, size_t s, String&& n, bool r, bool w, bool cow) Region::Region(const Range& range, String&& n, bool r, bool w, bool cow)
: m_laddr(a) : m_range(range)
, m_size(s) , m_vmo(VMObject::create_anonymous(size()))
, m_vmo(VMObject::create_anonymous(s))
, m_name(move(n)) , m_name(move(n))
, m_readable(r) , m_readable(r)
, m_writable(w) , m_writable(w)
@ -17,9 +16,8 @@ Region::Region(LinearAddress a, size_t s, String&& n, bool r, bool w, bool cow)
MM.register_region(*this); MM.register_region(*this);
} }
Region::Region(LinearAddress a, size_t s, RetainPtr<Inode>&& inode, String&& n, bool r, bool w) Region::Region(const Range& range, RetainPtr<Inode>&& inode, String&& n, bool r, bool w)
: m_laddr(a) : m_range(range)
, m_size(s)
, m_vmo(VMObject::create_file_backed(move(inode))) , m_vmo(VMObject::create_file_backed(move(inode)))
, m_name(move(n)) , m_name(move(n))
, m_readable(r) , m_readable(r)
@ -29,9 +27,8 @@ Region::Region(LinearAddress a, size_t s, RetainPtr<Inode>&& inode, String&& n,
MM.register_region(*this); MM.register_region(*this);
} }
Region::Region(LinearAddress a, size_t s, Retained<VMObject>&& vmo, size_t offset_in_vmo, String&& n, bool r, bool w, bool cow) Region::Region(const Range& range, Retained<VMObject>&& vmo, size_t offset_in_vmo, String&& n, bool r, bool w, bool cow)
: m_laddr(a) : m_range(range)
, m_size(s)
, m_offset_in_vmo(offset_in_vmo) , m_offset_in_vmo(offset_in_vmo)
, m_vmo(move(vmo)) , m_vmo(move(vmo))
, m_name(move(n)) , m_name(move(n))
@ -83,7 +80,7 @@ Retained<Region> Region::clone()
laddr().get()); laddr().get());
#endif #endif
// Create a new region backed by the same VMObject. // Create a new region backed by the same VMObject.
return adopt(*new Region(laddr(), size(), m_vmo.copy_ref(), m_offset_in_vmo, String(m_name), m_readable, m_writable)); return adopt(*new Region(m_range, m_vmo.copy_ref(), m_offset_in_vmo, String(m_name), m_readable, m_writable));
} }
#ifdef MM_DEBUG #ifdef MM_DEBUG
@ -96,7 +93,7 @@ Retained<Region> Region::clone()
// Set up a COW region. The parent (this) region becomes COW as well! // Set up a COW region. The parent (this) region becomes COW as well!
m_cow_map.fill(true); m_cow_map.fill(true);
MM.remap_region(current->process().page_directory(), *this); MM.remap_region(current->process().page_directory(), *this);
return adopt(*new Region(laddr(), size(), m_vmo->clone(), m_offset_in_vmo, String(m_name), m_readable, m_writable, true)); return adopt(*new Region(m_range, m_vmo->clone(), m_offset_in_vmo, String(m_name), m_readable, m_writable, true));
} }
int Region::commit() int Region::commit()

View file

@ -3,6 +3,7 @@
#include <AK/AKString.h> #include <AK/AKString.h>
#include <AK/Bitmap.h> #include <AK/Bitmap.h>
#include <Kernel/VM/PageDirectory.h> #include <Kernel/VM/PageDirectory.h>
#include <Kernel/VM/RangeAllocator.h>
class Inode; class Inode;
class VMObject; class VMObject;
@ -10,13 +11,13 @@ class VMObject;
class Region : public Retainable<Region> { class Region : public Retainable<Region> {
friend class MemoryManager; friend class MemoryManager;
public: public:
Region(LinearAddress, size_t, String&&, bool r, bool w, bool cow = false); Region(const Range&, String&&, bool r, bool w, bool cow = false);
Region(LinearAddress, size_t, Retained<VMObject>&&, size_t offset_in_vmo, String&&, bool r, bool w, bool cow = false); Region(const Range&, Retained<VMObject>&&, size_t offset_in_vmo, String&&, bool r, bool w, bool cow = false);
Region(LinearAddress, size_t, RetainPtr<Inode>&&, String&&, bool r, bool w); Region(const Range&, RetainPtr<Inode>&&, String&&, bool r, bool w);
~Region(); ~Region();
LinearAddress laddr() const { return m_laddr; } LinearAddress laddr() const { return m_range.base(); }
size_t size() const { return m_size; } size_t size() const { return m_range.size(); }
bool is_readable() const { return m_readable; } bool is_readable() const { return m_readable; }
bool is_writable() const { return m_writable; } bool is_writable() const { return m_writable; }
String name() const { return m_name; } String name() const { return m_name; }
@ -30,14 +31,15 @@ public:
void set_shared(bool shared) { m_shared = shared; } void set_shared(bool shared) { m_shared = shared; }
Retained<Region> clone(); Retained<Region> clone();
bool contains(LinearAddress laddr) const bool contains(LinearAddress laddr) const
{ {
return laddr >= m_laddr && laddr < m_laddr.offset(size()); return m_range.contains(laddr);
} }
unsigned page_index_from_address(LinearAddress laddr) const unsigned page_index_from_address(LinearAddress laddr) const
{ {
return (laddr - m_laddr).get() / PAGE_SIZE; return (laddr - m_range.base()).get() / PAGE_SIZE;
} }
size_t first_page_index() const size_t first_page_index() const
@ -52,7 +54,7 @@ public:
size_t page_count() const size_t page_count() const
{ {
return m_size / PAGE_SIZE; return size() / PAGE_SIZE;
} }
bool page_in(); bool page_in();
@ -82,8 +84,7 @@ public:
private: private:
RetainPtr<PageDirectory> m_page_directory; RetainPtr<PageDirectory> m_page_directory;
LinearAddress m_laddr; Range m_range;
size_t m_size { 0 };
size_t m_offset_in_vmo { 0 }; size_t m_offset_in_vmo { 0 };
Retained<VMObject> m_vmo; Retained<VMObject> m_vmo;
String m_name; String m_name;