list.h
4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
//===-- list.h --------------------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef SCUDO_LIST_H_
#define SCUDO_LIST_H_
#include "internal_defs.h"
namespace scudo {
// Intrusive POD singly and doubly linked list.
// An object with all zero fields should represent a valid empty list. clear()
// should be called on all non-zero-initialized objects before using.
template <class T> class IteratorBase {
public:
explicit IteratorBase(T *CurrentT) : Current(CurrentT) {}
IteratorBase &operator++() {
Current = Current->Next;
return *this;
}
bool operator!=(IteratorBase Other) const { return Current != Other.Current; }
T &operator*() { return *Current; }
private:
T *Current;
};
template <class T> struct IntrusiveList {
bool empty() const { return Size == 0; }
uptr size() const { return Size; }
T *front() { return First; }
const T *front() const { return First; }
T *back() { return Last; }
const T *back() const { return Last; }
void clear() {
First = Last = nullptr;
Size = 0;
}
typedef IteratorBase<T> Iterator;
typedef IteratorBase<const T> ConstIterator;
Iterator begin() { return Iterator(First); }
Iterator end() { return Iterator(nullptr); }
ConstIterator begin() const { return ConstIterator(First); }
ConstIterator end() const { return ConstIterator(nullptr); }
void checkConsistency() const;
protected:
uptr Size;
T *First;
T *Last;
};
template <class T> void IntrusiveList<T>::checkConsistency() const {
if (Size == 0) {
CHECK_EQ(First, nullptr);
CHECK_EQ(Last, nullptr);
} else {
uptr Count = 0;
for (T *I = First;; I = I->Next) {
Count++;
if (I == Last)
break;
}
CHECK_EQ(this->size(), Count);
CHECK_EQ(Last->Next, nullptr);
}
}
template <class T> struct SinglyLinkedList : public IntrusiveList<T> {
using IntrusiveList<T>::First;
using IntrusiveList<T>::Last;
using IntrusiveList<T>::Size;
using IntrusiveList<T>::empty;
void push_back(T *X) {
X->Next = nullptr;
if (empty())
First = X;
else
Last->Next = X;
Last = X;
Size++;
}
void push_front(T *X) {
if (empty())
Last = X;
X->Next = First;
First = X;
Size++;
}
void pop_front() {
DCHECK(!empty());
First = First->Next;
if (!First)
Last = nullptr;
Size--;
}
void extract(T *Prev, T *X) {
DCHECK(!empty());
DCHECK_NE(Prev, nullptr);
DCHECK_NE(X, nullptr);
DCHECK_EQ(Prev->Next, X);
Prev->Next = X->Next;
if (Last == X)
Last = Prev;
Size--;
}
void append_back(SinglyLinkedList<T> *L) {
DCHECK_NE(this, L);
if (L->empty())
return;
if (empty()) {
*this = *L;
} else {
Last->Next = L->First;
Last = L->Last;
Size += L->size();
}
L->clear();
}
};
template <class T> struct DoublyLinkedList : IntrusiveList<T> {
using IntrusiveList<T>::First;
using IntrusiveList<T>::Last;
using IntrusiveList<T>::Size;
using IntrusiveList<T>::empty;
void push_front(T *X) {
X->Prev = nullptr;
if (empty()) {
Last = X;
} else {
DCHECK_EQ(First->Prev, nullptr);
First->Prev = X;
}
X->Next = First;
First = X;
Size++;
}
// Inserts X before Y.
void insert(T *X, T *Y) {
if (Y == First)
return push_front(X);
T *Prev = Y->Prev;
// This is a hard CHECK to ensure consistency in the event of an intentional
// corruption of Y->Prev, to prevent a potential write-{4,8}.
CHECK_EQ(Prev->Next, Y);
Prev->Next = X;
X->Prev = Prev;
X->Next = Y;
Y->Prev = X;
Size++;
}
void push_back(T *X) {
X->Next = nullptr;
if (empty()) {
First = X;
} else {
DCHECK_EQ(Last->Next, nullptr);
Last->Next = X;
}
X->Prev = Last;
Last = X;
Size++;
}
void pop_front() {
DCHECK(!empty());
First = First->Next;
if (!First)
Last = nullptr;
else
First->Prev = nullptr;
Size--;
}
// The consistency of the adjacent links is aggressively checked in order to
// catch potential corruption attempts, that could yield a mirrored
// write-{4,8} primitive. nullptr checks are deemed less vital.
void remove(T *X) {
T *Prev = X->Prev;
T *Next = X->Next;
if (Prev) {
CHECK_EQ(Prev->Next, X);
Prev->Next = Next;
}
if (Next) {
CHECK_EQ(Next->Prev, X);
Next->Prev = Prev;
}
if (First == X) {
DCHECK_EQ(Prev, nullptr);
First = Next;
} else {
DCHECK_NE(Prev, nullptr);
}
if (Last == X) {
DCHECK_EQ(Next, nullptr);
Last = Prev;
} else {
DCHECK_NE(Next, nullptr);
}
Size--;
}
};
} // namespace scudo
#endif // SCUDO_LIST_H_