The Pedigree Project 0.1
MappingList.h
1/* Copyright (c) 2026, Pedigree Developers. */
2#ifndef VFS_MAPPING_LIST_H
3#define VFS_MAPPING_LIST_H
4
5#include "pedigree/kernel/utilities/List.h"
6#include "pedigree/kernel/utilities/Tree.h"
7
11template <class Object>
13 public:
14 using Iterator = typename List<Object*>::Iterator;
15 using ReverseIterator = typename List<Object*>::ReverseIterator;
16
17 MappingList() = default;
18
19 size_t count() const {
20 return m_Objects.count();
21 }
22 Iterator begin() {
23 return m_Objects.begin();
24 }
25 Iterator end() {
26 return m_Objects.end();
27 }
28 ReverseIterator rbegin() {
29 return m_Objects.rbegin();
30 }
31 ReverseIterator rend() {
32 return m_Objects.rend();
33 }
34
35 bool tryPushBack(Object* object) {
36 assert(object && !m_HasReservation);
37 if (!reserveBack(object->address()))
38 return false;
39 publishBack(object);
40 return true;
41 }
42
44 bool reserveBack(uintptr_t address) {
45 assert(!m_HasReservation);
46 m_LastMatch = nullptr;
47 if (m_Index.contains(address) || !m_Index.tryInsert(address, nullptr))
48 return false;
49 if (!m_Objects.tryPushBack(nullptr)) {
50 m_Index.remove(address);
51 return false;
52 }
53 m_ReservedAddress = address;
54 m_HasReservation = true;
55 return true;
56 }
57
58 void publishBack(Object* object) {
59 assert(m_HasReservation && object && object->address() == m_ReservedAddress);
60 m_LastMatch = nullptr;
61 Object** slot = m_Index.find(m_ReservedAddress);
62 assert(slot && !*slot);
63 *slot = object;
64 *m_Objects.rbegin() = object;
65 m_HasReservation = false;
66 }
67
68 Object* popBack() {
69 m_LastMatch = nullptr;
70 Object* object = m_Objects.popBack();
71 if (m_HasReservation) {
72 assert(!object);
73 m_Index.remove(m_ReservedAddress);
74 m_HasReservation = false;
75 } else if (object) {
76 m_Index.remove(object->address());
77 }
78 return object;
79 }
80
81 Iterator erase(Iterator& it) {
82 assert(!m_HasReservation && *it);
83 m_LastMatch = nullptr;
84 m_Index.remove((*it)->address());
85 return m_Objects.erase(it);
86 }
87 ReverseIterator erase(ReverseIterator& it) {
88 assert(!m_HasReservation && *it);
89 m_LastMatch = nullptr;
90 m_Index.remove((*it)->address());
91 return m_Objects.erase(it);
92 }
93
94 Object* find(uintptr_t address, size_t* objectVisits = nullptr) const {
95 if (objectVisits) {
96 *objectVisits = 0;
97 }
98 // Adjacent page faults usually stay in one mapping. Lookup and mutation
99 // remain serialized by the caller, keeping this non-owning pointer live.
100 if (m_LastMatch) {
101 if (objectVisits) {
102 ++*objectVisits;
103 }
104 if (m_LastMatch->matches(address)) {
105 return m_LastMatch;
106 }
107 }
108 uintptr_t start;
109 Object* object;
110 if (!m_Index.floorBound(address, start, object))
111 return nullptr;
112 // A split can reenter before publishing its reserved suffix. The source
113 // still owns that range until the split changes its length.
114 if (!object && (!start || !m_Index.floorBound(start - 1, start, object)))
115 return nullptr;
116 if (!object)
117 return nullptr;
118 if (objectVisits) {
119 ++*objectVisits;
120 }
121 if (!object->matches(address)) {
122 return nullptr;
123 }
124 m_LastMatch = object;
125 return object;
126 }
127
128 private:
129 NOT_COPYABLE_OR_ASSIGNABLE(MappingList);
130
131 List<Object*> m_Objects;
133 mutable Object* m_LastMatch = nullptr;
134 uintptr_t m_ReservedAddress = 0;
135 bool m_HasReservation = false;
136};
137
138#endif
An iterator applicable for many data structures.
Definition Iterator.h:41
Definition List.h:61
Iterator begin()
Definition List.h:122
ReverseIterator rend()
Definition List.h:152
ReverseIterator rbegin()
Definition List.h:142
::Iterator< T, node_t > Iterator
Definition List.h:67
Iterator end()
Definition List.h:132
bool reserveBack(uintptr_t address)
Definition MappingList.h:44
A key/value dictionary.
Definition Tree.h:33
E * find(const K &key)
Definition Tree.h:208
bool tryInsert(const K &key, const E &value)
Definition Tree.h:169
void remove(const K &key)
Definition Tree.h:301
bool contains(const K &key) const
Definition Tree.h:287
bool floorBound(const K &key, K &foundKey, E &foundValue) const
Definition Tree.h:263
Iterator erase(Iterator &Iter)
Definition List.h:352
bool tryPushBack(const T &value)
Definition List.h:246
size_t count() const
Definition List.h:212
T popBack()
Definition List.h:278