ladybird/Kernel/Memory/VMObject.cpp
Andreas Kling 7979b5a8bb Kernel: Port VMObject to ListedRefCounted
The VMObject class now manages its own instance list (it was previously
a member of MemoryManager.) Removal from the list is done safely on the
last unref(), closing a race window in the previous implementation.

Note that VMObject::all_instances() now has its own lock instead of
using the global MM lock.
2021-08-17 01:21:47 +02:00

44 lines
1,016 B
C++

/*
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Singleton.h>
#include <Kernel/Memory/MemoryManager.h>
#include <Kernel/Memory/VMObject.h>
namespace Kernel::Memory {
static Singleton<SpinLockProtectedValue<VMObject::AllInstancesList>> s_all_instances;
SpinLockProtectedValue<VMObject::AllInstancesList>& VMObject::all_instances()
{
return s_all_instances;
}
VMObject::VMObject(VMObject const& other)
: m_physical_pages(other.m_physical_pages)
{
all_instances().with([&](auto& list) { list.append(*this); });
}
VMObject::VMObject(size_t size)
: m_physical_pages(ceil_div(size, static_cast<size_t>(PAGE_SIZE)))
{
all_instances().with([&](auto& list) { list.append(*this); });
}
VMObject::~VMObject()
{
{
ScopedSpinLock lock(m_on_deleted_lock);
for (auto& it : m_on_deleted)
it->vmobject_deleted(*this);
m_on_deleted.clear();
}
VERIFY(m_regions.is_empty());
}
}