From 87b54a82c77bf811e9e9497cfb6037ac1a8ecde3 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 17 May 2019 04:32:08 +0200 Subject: Kernel: Let Region keep a Range internally. --- Kernel/Process.cpp | 6 +++--- Kernel/VM/MemoryManager.cpp | 2 +- Kernel/VM/RangeAllocator.h | 2 ++ Kernel/VM/Region.cpp | 21 +++++++++------------ Kernel/VM/Region.h | 21 +++++++++++---------- 5 files changed, 26 insertions(+), 26 deletions(-) (limited to 'Kernel') diff --git a/Kernel/Process.cpp b/Kernel/Process.cpp index bb1f0caee0..0275cc83c5 100644 --- a/Kernel/Process.cpp +++ b/Kernel/Process.cpp @@ -79,7 +79,7 @@ Region* Process::allocate_region(LinearAddress laddr, size_t size, String&& name else 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()); if (commit) m_regions.last()->commit(); @@ -97,7 +97,7 @@ Region* Process::allocate_file_backed_region(LinearAddress laddr, size_t size, R else 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()); 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; 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()); return m_regions.last().ptr(); } diff --git a/Kernel/VM/MemoryManager.cpp b/Kernel/VM/MemoryManager.cpp index 5ef89cfa0b..b26cca4c09 100644 --- a/Kernel/VM/MemoryManager.cpp +++ b/Kernel/VM/MemoryManager.cpp @@ -404,7 +404,7 @@ RetainPtr MemoryManager::allocate_kernel_region(size_t size, String&& na ASSERT(!(size % PAGE_SIZE)); auto range = m_range_allocator.allocate_anywhere(size); 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); // FIXME: It would be cool if these could zero-fill on demand instead. region->commit(); diff --git a/Kernel/VM/RangeAllocator.h b/Kernel/VM/RangeAllocator.h index bd1575ef4e..a2f1575bdc 100644 --- a/Kernel/VM/RangeAllocator.h +++ b/Kernel/VM/RangeAllocator.h @@ -17,6 +17,8 @@ public: size_t size() const { return m_size; } 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); } bool operator==(const Range& other) const diff --git a/Kernel/VM/Region.cpp b/Kernel/VM/Region.cpp index 27b0d5ebff..e1cd53dbdc 100644 --- a/Kernel/VM/Region.cpp +++ b/Kernel/VM/Region.cpp @@ -4,10 +4,9 @@ #include #include -Region::Region(LinearAddress a, size_t s, String&& n, bool r, bool w, bool cow) - : m_laddr(a) - , m_size(s) - , m_vmo(VMObject::create_anonymous(s)) +Region::Region(const Range& range, String&& n, bool r, bool w, bool cow) + : m_range(range) + , m_vmo(VMObject::create_anonymous(size())) , m_name(move(n)) , m_readable(r) , 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); } -Region::Region(LinearAddress a, size_t s, RetainPtr&& inode, String&& n, bool r, bool w) - : m_laddr(a) - , m_size(s) +Region::Region(const Range& range, RetainPtr&& inode, String&& n, bool r, bool w) + : m_range(range) , m_vmo(VMObject::create_file_backed(move(inode))) , m_name(move(n)) , m_readable(r) @@ -29,9 +27,8 @@ Region::Region(LinearAddress a, size_t s, RetainPtr&& inode, String&& n, MM.register_region(*this); } -Region::Region(LinearAddress a, size_t s, Retained&& vmo, size_t offset_in_vmo, String&& n, bool r, bool w, bool cow) - : m_laddr(a) - , m_size(s) +Region::Region(const Range& range, Retained&& vmo, size_t offset_in_vmo, String&& n, bool r, bool w, bool cow) + : m_range(range) , m_offset_in_vmo(offset_in_vmo) , m_vmo(move(vmo)) , m_name(move(n)) @@ -83,7 +80,7 @@ Retained Region::clone() laddr().get()); #endif // 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 @@ -96,7 +93,7 @@ Retained Region::clone() // Set up a COW region. The parent (this) region becomes COW as well! m_cow_map.fill(true); 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() diff --git a/Kernel/VM/Region.h b/Kernel/VM/Region.h index 305a35a6fb..806fa6460c 100644 --- a/Kernel/VM/Region.h +++ b/Kernel/VM/Region.h @@ -3,6 +3,7 @@ #include #include #include +#include class Inode; class VMObject; @@ -10,13 +11,13 @@ class VMObject; class Region : public Retainable { friend class MemoryManager; public: - Region(LinearAddress, size_t, String&&, bool r, bool w, bool cow = false); - Region(LinearAddress, size_t, Retained&&, size_t offset_in_vmo, String&&, bool r, bool w, bool cow = false); - Region(LinearAddress, size_t, RetainPtr&&, String&&, bool r, bool w); + Region(const Range&, String&&, bool r, bool w, bool cow = false); + Region(const Range&, Retained&&, size_t offset_in_vmo, String&&, bool r, bool w, bool cow = false); + Region(const Range&, RetainPtr&&, String&&, bool r, bool w); ~Region(); - LinearAddress laddr() const { return m_laddr; } - size_t size() const { return m_size; } + LinearAddress laddr() const { return m_range.base(); } + size_t size() const { return m_range.size(); } bool is_readable() const { return m_readable; } bool is_writable() const { return m_writable; } String name() const { return m_name; } @@ -30,14 +31,15 @@ public: void set_shared(bool shared) { m_shared = shared; } Retained clone(); + 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 { - return (laddr - m_laddr).get() / PAGE_SIZE; + return (laddr - m_range.base()).get() / PAGE_SIZE; } size_t first_page_index() const @@ -52,7 +54,7 @@ public: size_t page_count() const { - return m_size / PAGE_SIZE; + return size() / PAGE_SIZE; } bool page_in(); @@ -82,8 +84,7 @@ public: private: RetainPtr m_page_directory; - LinearAddress m_laddr; - size_t m_size { 0 }; + Range m_range; size_t m_offset_in_vmo { 0 }; Retained m_vmo; String m_name; -- cgit v1.2.3