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-04-03 13:13:07 +00:00
|
|
|
#pragma once
|
|
|
|
|
2020-12-22 06:21:58 +00:00
|
|
|
#include <AK/HashTable.h>
|
2019-08-08 08:43:44 +00:00
|
|
|
#include <AK/InlineLinkedList.h>
|
2019-06-21 16:58:45 +00:00
|
|
|
#include <AK/RefCounted.h>
|
2019-08-07 16:06:17 +00:00
|
|
|
#include <AK/RefPtr.h>
|
2020-09-07 09:53:54 +00:00
|
|
|
#include <AK/Vector.h>
|
2019-05-28 09:53:16 +00:00
|
|
|
#include <AK/Weakable.h>
|
2019-04-03 13:13:07 +00:00
|
|
|
#include <Kernel/Lock.h>
|
|
|
|
|
2020-02-16 00:27:42 +00:00
|
|
|
namespace Kernel {
|
|
|
|
|
2019-04-03 13:13:07 +00:00
|
|
|
class Inode;
|
|
|
|
class PhysicalPage;
|
|
|
|
|
2020-12-22 06:21:58 +00:00
|
|
|
class VMObjectDeletedHandler {
|
|
|
|
public:
|
2021-02-28 13:42:08 +00:00
|
|
|
virtual ~VMObjectDeletedHandler() = default;
|
2020-12-22 06:21:58 +00:00
|
|
|
virtual void vmobject_deleted(VMObject&) = 0;
|
|
|
|
};
|
|
|
|
|
2019-06-21 13:29:31 +00:00
|
|
|
class VMObject : public RefCounted<VMObject>
|
2019-08-08 08:43:44 +00:00
|
|
|
, public Weakable<VMObject>
|
|
|
|
, public InlineLinkedListNode<VMObject> {
|
2019-04-03 13:13:07 +00:00
|
|
|
friend class MemoryManager;
|
2019-11-03 23:45:33 +00:00
|
|
|
friend class Region;
|
2019-05-28 09:53:16 +00:00
|
|
|
|
2019-04-03 13:13:07 +00:00
|
|
|
public:
|
2019-08-07 16:06:17 +00:00
|
|
|
virtual ~VMObject();
|
2019-04-03 13:13:07 +00:00
|
|
|
|
2020-09-05 03:12:25 +00:00
|
|
|
virtual RefPtr<VMObject> clone() = 0;
|
2019-04-03 13:13:07 +00:00
|
|
|
|
2019-08-07 16:06:17 +00:00
|
|
|
virtual bool is_anonymous() const { return false; }
|
|
|
|
virtual bool is_inode() const { return false; }
|
2020-03-01 10:08:28 +00:00
|
|
|
virtual bool is_shared_inode() const { return false; }
|
|
|
|
virtual bool is_private_inode() const { return false; }
|
2020-03-07 17:24:41 +00:00
|
|
|
virtual bool is_contiguous() const { return false; }
|
2019-04-03 13:13:07 +00:00
|
|
|
|
2019-08-07 18:12:50 +00:00
|
|
|
size_t page_count() const { return m_physical_pages.size(); }
|
2020-09-07 09:53:54 +00:00
|
|
|
const Vector<RefPtr<PhysicalPage>>& physical_pages() const { return m_physical_pages; }
|
|
|
|
Vector<RefPtr<PhysicalPage>>& physical_pages() { return m_physical_pages; }
|
2019-04-03 13:13:07 +00:00
|
|
|
|
2019-08-07 18:12:50 +00:00
|
|
|
size_t size() const { return m_physical_pages.size() * PAGE_SIZE; }
|
2019-04-03 13:13:07 +00:00
|
|
|
|
2020-02-28 19:58:57 +00:00
|
|
|
virtual const char* class_name() const = 0;
|
|
|
|
|
2019-08-08 08:43:44 +00:00
|
|
|
// For InlineLinkedListNode
|
|
|
|
VMObject* m_next { nullptr };
|
|
|
|
VMObject* m_prev { nullptr };
|
|
|
|
|
2021-01-03 23:58:50 +00:00
|
|
|
ALWAYS_INLINE void ref_region() { m_regions_count++; }
|
|
|
|
ALWAYS_INLINE void unref_region() { m_regions_count--; }
|
|
|
|
ALWAYS_INLINE bool is_shared_by_multiple_regions() const { return m_regions_count > 1; }
|
2021-01-02 19:03:14 +00:00
|
|
|
|
2020-12-22 06:21:58 +00:00
|
|
|
void register_on_deleted_handler(VMObjectDeletedHandler& handler)
|
|
|
|
{
|
|
|
|
m_on_deleted.set(&handler);
|
|
|
|
}
|
|
|
|
void unregister_on_deleted_handler(VMObjectDeletedHandler& handler)
|
|
|
|
{
|
|
|
|
m_on_deleted.remove(&handler);
|
|
|
|
}
|
|
|
|
|
2019-08-07 16:06:17 +00:00
|
|
|
protected:
|
2021-02-26 12:32:43 +00:00
|
|
|
VMObject();
|
2019-08-07 18:12:50 +00:00
|
|
|
explicit VMObject(size_t);
|
2019-08-07 16:06:17 +00:00
|
|
|
explicit VMObject(const VMObject&);
|
2019-04-03 13:13:07 +00:00
|
|
|
|
2019-05-28 09:53:16 +00:00
|
|
|
template<typename Callback>
|
|
|
|
void for_each_region(Callback);
|
2019-04-03 13:13:07 +00:00
|
|
|
|
2020-09-07 09:53:54 +00:00
|
|
|
Vector<RefPtr<PhysicalPage>> m_physical_pages;
|
2019-12-09 18:12:38 +00:00
|
|
|
Lock m_paging_lock { "VMObject" };
|
2019-08-07 16:06:17 +00:00
|
|
|
|
2020-09-05 21:52:14 +00:00
|
|
|
mutable SpinLock<u8> m_lock;
|
|
|
|
|
2019-08-07 16:06:17 +00:00
|
|
|
private:
|
|
|
|
VMObject& operator=(const VMObject&) = delete;
|
|
|
|
VMObject& operator=(VMObject&&) = delete;
|
|
|
|
VMObject(VMObject&&) = delete;
|
2021-01-02 19:03:14 +00:00
|
|
|
|
2021-01-03 23:58:50 +00:00
|
|
|
Atomic<u32, AK::MemoryOrder::memory_order_relaxed> m_regions_count { 0 };
|
2020-12-22 06:21:58 +00:00
|
|
|
HashTable<VMObjectDeletedHandler*> m_on_deleted;
|
|
|
|
SpinLock<u8> m_on_deleted_lock;
|
2019-04-03 13:13:07 +00:00
|
|
|
};
|
2020-02-16 00:27:42 +00:00
|
|
|
|
|
|
|
}
|