forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintrusive-array.hh
337 lines (283 loc) · 10.7 KB
/
intrusive-array.hh
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
* Copyright (C) 2020-present ScyllaDB
*/
/*
* SPDX-License-Identifier: LicenseRef-ScyllaDB-Source-Available-1.0
*/
#pragma once
#include <cassert>
#include <cstddef>
#include <cstdlib>
#include <limits>
#include <utility>
#include "utils/assert.hh"
#include "utils/collection-concepts.hh"
template <typename T>
concept BoundsKeeper = requires (T val, bool bit) {
{ val.is_head() } noexcept -> std::same_as<bool>;
{ val.set_head(bit) } noexcept -> std::same_as<void>;
{ val.is_tail() } noexcept -> std::same_as<bool>;
{ val.set_tail(bit) } noexcept -> std::same_as<void>;
{ val.with_train() } noexcept -> std::same_as<bool>;
{ val.set_train(bit) } noexcept -> std::same_as<void>;
};
/*
* A plain array of T-s that grows and shrinks by constructing a new
* instances. Holds at least one element. Has facilities for sorting
* the elements and for doing "container_of" by the given element
* pointer. LSA-compactible.
*
* Important feature of the array is zero memory overhead -- it doesn't
* keep its size/capacity onboard. The size is calculated each time by
* walking the array of T-s and checking which one of them is the tail
* element. Respectively, the T must keep head/tail flags on itself.
*/
template <typename T>
requires BoundsKeeper<T> && std::is_nothrow_move_constructible_v<T>
class intrusive_array {
// Sanity constant to avoid infinite loops searching for tail
static constexpr int max_len = std::numeric_limits<short int>::max();
union maybe_constructed {
maybe_constructed() { }
~maybe_constructed() { }
T object;
/*
* Train is 1 or more allocated but unoccupied memory slots after
* the tail one. Being unused, this memory keeps the train length.
* An array with the train is marked with the respective flag on
* the 0th element. Train is created by the erase() call and can
* be up to 65535 elements long
*
* Train length is included into the storage_size() to make
* allocator and compaction work correctly, but is not included
* into the number_of_elements(), so the array behaves just like
* there's no train
*
* Respectively both grow and shrink constructors do not carry
* the train (and drop the bit from 0th element) and don't expect
* the memory for the new array to include one
*/
unsigned short train_len;
static_assert(sizeof(T) >= sizeof(unsigned short));
};
maybe_constructed _data[1];
size_t number_of_elements() const noexcept {
for (int i = 0; i < max_len; i++) {
if (_data[i].object.is_tail()) {
return i + 1;
}
}
std::abort();
}
public:
size_t storage_size() const noexcept {
size_t nr = number_of_elements();
if (_data[0].object.with_train()) {
nr += _data[nr].train_len;
}
return nr * sizeof(T);
}
using iterator = T*;
using const_iterator = const T*;
/*
* There are 3 constructing options for the array: initial, grow
* and shrink.
*
* * initial just creates a 1-element array
* * grow -- makes a new one moving all elements from the original
* array and inserting the one (only one) more element at the given
* position
* * shrink -- also makes a new array skipping the not needed
* element while moving them from the original one
*
* In all cases the enough big memory chunk must be provided by the
* caller!
*
* Note, that none of them calls destructors on T-s, unlike vector.
* This is because when the older array is destroyed it has no idea
* about whether or not it was grown/shrunk and thus it destroys
* T-s itself.
*/
// Initial
template <typename... Args>
intrusive_array(Args&&... args) {
new (&_data[0].object) T(std::forward<Args>(args)...);
_data[0].object.set_head(true);
_data[0].object.set_tail(true);
}
// Growing
struct grow_tag {
int add_pos;
};
template <typename... Args>
intrusive_array(intrusive_array& from, grow_tag grow, Args&&... args) {
// The add_pos is strongly _expected_ to be within bounds
int i, off = 0;
bool tail = false;
for (i = 0; !tail; i++) {
if (i == grow.add_pos) {
off = 1;
continue;
}
tail = from._data[i - off].object.is_tail();
new (&_data[i].object) T(std::move(from._data[i - off].object));
}
SCYLLA_ASSERT(grow.add_pos <= i && i < max_len);
new (&_data[grow.add_pos].object) T(std::forward<Args>(args)...);
_data[0].object.set_head(true);
_data[0].object.set_train(false);
if (grow.add_pos == 0) {
_data[1].object.set_head(false);
}
_data[i - off].object.set_tail(true);
if (off == 0) {
_data[i - 1].object.set_tail(false);
}
}
// Shrinking
struct shrink_tag {
int del_pos;
};
intrusive_array(intrusive_array& from, shrink_tag shrink) {
int i, off = 0;
bool tail = false;
for (i = 0; !tail; i++) {
tail = from._data[i].object.is_tail();
if (i == shrink.del_pos) {
off = 1;
} else {
new (&_data[i - off].object) T(std::move(from._data[i].object));
}
}
_data[0].object.set_head(true);
_data[0].object.set_train(false);
_data[i - off - 1].object.set_tail(true);
}
intrusive_array(const intrusive_array& other) = delete;
intrusive_array(intrusive_array&& other) noexcept {
bool tail = false;
int i;
for (i = 0; !tail; i++) {
tail = other._data[i].object.is_tail();
new (&_data[i].object) T(std::move(other._data[i].object));
}
if (_data[0].object.with_train()) {
_data[i].train_len = other._data[i].train_len;
}
}
~intrusive_array() {
bool tail = false;
for (int i = 0; !tail; i++) {
tail = _data[i].object.is_tail();
_data[i].object.~T();
}
}
/*
* Drops the element in-place at position @pos and grows the
* "train". To be used in places where reconstruction is not
* welcome (e.g. because it throws)
*
* Single-elemented array cannot be erased from, just drop it
* altogether if needed
*/
void erase(int pos) noexcept {
SCYLLA_ASSERT(!is_single_element());
SCYLLA_ASSERT(pos < max_len);
bool with_train = _data[0].object.with_train();
bool tail = _data[pos].object.is_tail();
_data[pos].object.~T();
if (tail) {
SCYLLA_ASSERT(pos > 0);
_data[pos - 1].object.set_tail(true);
} else {
while (!tail) {
new (&_data[pos].object) T(std::move(_data[pos + 1].object));
_data[pos + 1].object.~T();
tail = _data[pos++].object.is_tail();
}
_data[0].object.set_head(true);
}
_data[0].object.set_train(true);
unsigned short train_len = with_train ? _data[pos + 1].train_len : 0;
SCYLLA_ASSERT(train_len < max_len);
_data[pos].train_len = train_len + 1;
}
T& operator[](int pos) noexcept { return _data[pos].object; }
const T& operator[](int pos) const noexcept { return _data[pos].object; }
iterator begin() noexcept { return &_data[0].object; }
const_iterator begin() const noexcept { return &_data[0].object; }
const_iterator cbegin() const noexcept { return &_data[0].object; }
iterator end() noexcept { return &_data[number_of_elements()].object; }
const_iterator end() const noexcept { return &_data[number_of_elements()].object; }
const_iterator cend() const noexcept { return &_data[number_of_elements()].object; }
size_t index_of(iterator i) const noexcept { return i - &_data[0].object; }
size_t index_of(const_iterator i) const noexcept { return i - &_data[0].object; }
bool is_single_element() const noexcept { return _data[0].object.is_tail(); }
// A helper for keeping the array sorted
template <typename K, typename Compare>
requires Comparable<K, T, Compare>
const_iterator lower_bound(const K& val, Compare cmp, bool& match) const {
int i = 0;
do {
auto x = cmp(_data[i].object, val);
if (x >= 0) {
match = (x == 0);
break;
}
} while (!_data[i++].object.is_tail());
return &_data[i].object;
}
template <typename K, typename Compare>
requires Comparable<K, T, Compare>
iterator lower_bound(const K& val, Compare cmp, bool& match) {
return const_cast<iterator>(const_cast<const intrusive_array*>(this)->lower_bound(val, std::move(cmp), match));
}
template <typename K, typename Compare>
requires Comparable<K, T, Compare>
const_iterator lower_bound(const K& val, Compare cmp) const {
bool match = false;
return lower_bound(val, cmp, match);
}
template <typename K, typename Compare>
requires Comparable<K, T, Compare>
iterator lower_bound(const K& val, Compare cmp) {
return const_cast<iterator>(const_cast<const intrusive_array*>(this)->lower_bound(val, std::move(cmp)));
}
// And its peer ... just to be used
template <typename K, typename Compare>
requires Comparable<K, T, Compare>
const_iterator upper_bound(const K& val, Compare cmp) const {
int i = 0;
do {
if (cmp(_data[i].object, val) > 0) {
break;
}
} while (!_data[i++].object.is_tail());
return &_data[i].object;
}
template <typename K, typename Compare>
requires Comparable<K, T, Compare>
iterator upper_bound(const K& val, Compare cmp) {
return const_cast<iterator>(const_cast<const intrusive_array*>(this)->upper_bound(val, std::move(cmp)));
}
template <typename Func>
requires Disposer<Func, T>
void for_each(Func&& fn) noexcept {
bool tail = false;
for (int i = 0; !tail; i++) {
tail = _data[i].object.is_tail();
fn(&_data[i].object);
}
}
size_t size() const noexcept { return number_of_elements(); }
static intrusive_array& from_element(T* ptr, int& idx) noexcept {
idx = 0;
while (!ptr->is_head()) {
SCYLLA_ASSERT(idx < max_len); // may the force be with us...
idx++;
ptr--;
}
static_assert(offsetof(intrusive_array, _data[0].object) == 0);
return *reinterpret_cast<intrusive_array*>(ptr);
}
};