From d4ba15571105723e2bf8120e5cf8ca440261029c Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 8 Feb 2019 16:40:48 +0100 Subject: Kernel: Break retain cycle between Inode and VMObject. There's no need for an Inode to keep its corresponding VMObject alive. Obviously there are huge benefits to keeping a filesystem cache, but leaking everything is hardly the right strategy. :^) --- Kernel/FileSystem.cpp | 4 ++-- Kernel/FileSystem.h | 5 +++-- Kernel/MemoryManager.cpp | 6 ++---- Kernel/MemoryManager.h | 3 ++- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Kernel/FileSystem.cpp b/Kernel/FileSystem.cpp index 953ef55abe..75e6a83e04 100644 --- a/Kernel/FileSystem.cpp +++ b/Kernel/FileSystem.cpp @@ -148,7 +148,7 @@ void FS::sync() } } -void Inode::set_vmo(RetainPtr&& vmo) +void Inode::set_vmo(VMObject& vmo) { - m_vmo = move(vmo); + m_vmo = vmo.make_weak_ptr(); } diff --git a/Kernel/FileSystem.h b/Kernel/FileSystem.h index 58a24d25cb..eab654ac51 100644 --- a/Kernel/FileSystem.h +++ b/Kernel/FileSystem.h @@ -14,6 +14,7 @@ #include #include #include +#include static const dword mepoch = 476763780; @@ -103,7 +104,7 @@ public: void will_be_destroyed(); - void set_vmo(RetainPtr&&); + void set_vmo(VMObject&); VMObject* vmo() { return m_vmo.ptr(); } const VMObject* vmo() const { return m_vmo.ptr(); } @@ -118,7 +119,7 @@ protected: private: FS& m_fs; unsigned m_index { 0 }; - RetainPtr m_vmo; + WeakPtr m_vmo; bool m_metadata_dirty { false }; }; diff --git a/Kernel/MemoryManager.cpp b/Kernel/MemoryManager.cpp index 7f70203959..76dacee97c 100644 --- a/Kernel/MemoryManager.cpp +++ b/Kernel/MemoryManager.cpp @@ -667,7 +667,7 @@ RetainPtr VMObject::create_file_backed(RetainPtr&& inode) if (inode->vmo()) return static_cast(inode->vmo()); auto vmo = adopt(*new VMObject(move(inode))); - vmo->inode()->set_vmo(vmo.ptr()); + vmo->inode()->set_vmo(*vmo); return vmo; } @@ -732,10 +732,8 @@ VMObject::VMObject(RetainPtr&& inode) VMObject::~VMObject() { - if (m_inode) { + if (m_inode) ASSERT(m_inode->vmo() == this); - m_inode->set_vmo(nullptr); - } MM.unregister_vmo(*this); } diff --git a/Kernel/MemoryManager.h b/Kernel/MemoryManager.h index b5974c3d67..a7243d5b97 100644 --- a/Kernel/MemoryManager.h +++ b/Kernel/MemoryManager.h @@ -10,6 +10,7 @@ #include #include #include +#include #include #define PAGE_ROUND_UP(x) ((((dword)(x)) + PAGE_SIZE-1) & (~(PAGE_SIZE-1))) @@ -77,7 +78,7 @@ private: HashMap> m_physical_pages; }; -class VMObject : public Retainable { +class VMObject : public Retainable, public Weakable { friend class MemoryManager; public: static RetainPtr create_file_backed(RetainPtr&&); -- cgit v1.2.3