Skip to content

Commit a96d76f

Browse files
committed
Kernel: Put all VMObjects in an InlineLinkedList instead of a HashTable
Using a HashTable to track "all instances of Foo" is only useful if we actually need to look up entries by some kind of index. And since they are HashTable (not HashMap), the pointer *is* the index. Since we have the pointer, we can just use it directly. Duh. This increase sizeof(VMObject) by two pointers, but removes a global table that had an entry for every VMObject, where the cost was higher. It also avoids all the general hash tabling business when creating or destroying VMObjects. Generally we should do more of this. :^)
1 parent fffd3a6 commit a96d76f

File tree

4 files changed

+32
-14
lines changed

4 files changed

+32
-14
lines changed

Kernel/FileSystem/ProcFS.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -399,17 +399,19 @@ Optional<KBuffer> procfs$self(InodeIdentifier)
399399

400400
Optional<KBuffer> procfs$mm(InodeIdentifier)
401401
{
402-
// FIXME: Implement
403402
InterruptDisabler disabler;
404403
KBufferBuilder builder;
405-
for (auto* vmo : MM.m_vmos) {
406-
builder.appendf("VMO: %p %s(%u): p:%4u\n",
407-
vmo,
408-
vmo->is_anonymous() ? "anon" : "file",
409-
vmo->ref_count(),
410-
vmo->page_count());
411-
}
412-
builder.appendf("VMO count: %u\n", MM.m_vmos.size());
404+
u32 vmobject_count = 0;
405+
MemoryManager::for_each_vmobject([&](auto& vmobject) {
406+
++vmobject_count;
407+
builder.appendf("VMObject: %p %s(%u): p:%4u\n",
408+
&vmobject,
409+
vmobject.is_anonymous() ? "anon" : "file",
410+
vmobject.ref_count(),
411+
vmobject.page_count());
412+
return IterationDecision::Continue;
413+
});
414+
builder.appendf("VMO count: %u\n", vmobject_count);
413415
builder.appendf("Free physical pages: %u\n", MM.user_physical_pages() - MM.user_physical_pages_used());
414416
builder.appendf("Free supervisor physical pages: %u\n", MM.super_physical_pages() - MM.super_physical_pages_used());
415417
return builder.build();

Kernel/VM/MemoryManager.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -753,13 +753,13 @@ bool MemoryManager::validate_user_write(const Process& process, VirtualAddress v
753753
void MemoryManager::register_vmo(VMObject& vmo)
754754
{
755755
InterruptDisabler disabler;
756-
m_vmos.set(&vmo);
756+
m_vmobjects.append(&vmo);
757757
}
758758

759759
void MemoryManager::unregister_vmo(VMObject& vmo)
760760
{
761761
InterruptDisabler disabler;
762-
m_vmos.remove(&vmo);
762+
m_vmobjects.remove(&vmo);
763763
}
764764

765765
void MemoryManager::register_region(Region& region)

Kernel/VM/MemoryManager.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ class MemoryManager {
8080
unsigned super_physical_pages() const { return m_super_physical_pages; }
8181
unsigned super_physical_pages_used() const { return m_super_physical_pages_used; }
8282

83+
template<typename Callback>
84+
static void for_each_vmobject(Callback callback)
85+
{
86+
for (auto* vmobject = MM.m_vmobjects.head(); vmobject; vmobject = vmobject->next()) {
87+
if (callback(*vmobject) == IterationDecision::Break)
88+
break;
89+
}
90+
}
91+
8392
private:
8493
MemoryManager();
8594
~MemoryManager();
@@ -134,10 +143,11 @@ class MemoryManager {
134143
NonnullRefPtrVector<PhysicalRegion> m_user_physical_regions;
135144
NonnullRefPtrVector<PhysicalRegion> m_super_physical_regions;
136145

137-
HashTable<VMObject*> m_vmos;
138146
HashTable<Region*> m_user_regions;
139147
HashTable<Region*> m_kernel_regions;
140148

149+
InlineLinkedList<VMObject> m_vmobjects;
150+
141151
bool m_quickmap_in_use { false };
142152
};
143153

Kernel/VM/VMObject.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
#pragma once
22

3+
#include <AK/FixedArray.h>
4+
#include <AK/InlineLinkedList.h>
35
#include <AK/RefCounted.h>
46
#include <AK/RefPtr.h>
5-
#include <AK/FixedArray.h>
67
#include <AK/Weakable.h>
78
#include <Kernel/Lock.h>
89

910
class Inode;
1011
class PhysicalPage;
1112

1213
class VMObject : public RefCounted<VMObject>
13-
, public Weakable<VMObject> {
14+
, public Weakable<VMObject>
15+
, public InlineLinkedListNode<VMObject> {
1416
friend class MemoryManager;
1517

1618
public:
@@ -27,6 +29,10 @@ class VMObject : public RefCounted<VMObject>
2729

2830
size_t size() const { return m_physical_pages.size() * PAGE_SIZE; }
2931

32+
// For InlineLinkedListNode
33+
VMObject* m_next { nullptr };
34+
VMObject* m_prev { nullptr };
35+
3036
protected:
3137
explicit VMObject(size_t);
3238
explicit VMObject(const VMObject&);

0 commit comments

Comments
 (0)