forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanaged_vector.hh
247 lines (228 loc) · 7.22 KB
/
managed_vector.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
/*
* Copyright (C) 2015-present ScyllaDB
*/
/*
* SPDX-License-Identifier: LicenseRef-ScyllaDB-Source-Available-1.0
*/
#pragma once
#include <array>
#include <type_traits>
#include "utils/allocation_strategy.hh"
template<typename T, unsigned InternalSize = 0, typename SizeType = size_t>
requires std::is_nothrow_move_constructible_v<T>
class managed_vector {
public:
using value_type = T;
using size_type = SizeType;
using iterator = T*;
using const_iterator = const T*;
private:
struct external {
managed_vector* _backref;
T _data[0];
external(external&& other) noexcept : _backref(other._backref) {
for (unsigned i = 0; i < _backref->size(); i++) {
new (_data + i) T(std::move(other._data[i]));
other._data[i].~T();
}
_backref->_data = _data;
}
size_t storage_size() const noexcept {
return sizeof(*this) + sizeof(T) * _backref->_capacity;
}
};
union maybe_constructed {
maybe_constructed() { }
~maybe_constructed() { }
T object;
};
private:
std::array<maybe_constructed, InternalSize> _internal;
size_type _size = 0;
size_type _capacity = InternalSize;
T* _data = reinterpret_cast<T*>(_internal.data());
friend class external;
private:
bool is_external() const {
return _data != reinterpret_cast<const T*>(_internal.data());
}
external* get_external() {
auto ptr = reinterpret_cast<char*>(_data) - offsetof(external, _data);
return reinterpret_cast<external*>(ptr);
}
void maybe_grow(size_type new_size) {
if (new_size <= _capacity) {
return;
}
auto new_capacity = std::max({ _capacity + std::min(_capacity, size_type(1024)), new_size, size_type(InternalSize + 8) });
reserve(new_capacity);
}
public:
// Clears the vector and ensures that the destructor will not have to free any memory so
// that the object can be destroyed under any allocator.
void clear_and_release() noexcept {
clear();
if (is_external()) {
current_allocator().free(get_external(), get_external()->storage_size());
_data = reinterpret_cast<T*>(_internal.data());
}
}
public:
managed_vector() = default;
managed_vector(const managed_vector& other) {
reserve(other._size);
try {
for (const auto& v : other) {
push_back(v);
}
} catch (...) {
clear_and_release();
throw;
}
}
managed_vector(managed_vector&& other) noexcept : _size(other._size), _capacity(other._capacity) {
if (other.is_external()) {
_data = other._data;
other._data = reinterpret_cast<T*>(other._internal.data());
get_external()->_backref = this;
} else {
for (unsigned i = 0; i < _size; i++) {
new (_data + i) T(std::move(other._data[i]));
other._data[i].~T();
}
}
other._size = 0;
other._capacity = InternalSize;
}
managed_vector& operator=(const managed_vector& other) {
if (this != &other) {
managed_vector tmp(other);
this->~managed_vector();
new (this) managed_vector(std::move(tmp));
}
return *this;
}
managed_vector& operator=(managed_vector&& other) noexcept {
if (this != &other) {
this->~managed_vector();
new (this) managed_vector(std::move(other));
}
return *this;
}
~managed_vector() {
clear_and_release();
}
T& at(size_type pos) {
if (pos >= _size) {
throw std::out_of_range("out of range");
}
return operator[](pos);
}
const T& at(size_type pos) const {
if (pos >= _size) {
throw std::out_of_range("out of range");
}
return operator[](pos);
}
T& operator[](size_type pos) noexcept {
return _data[pos];
}
const T& operator[](size_type pos) const noexcept {
return _data[pos];
}
T& front() noexcept { return *_data; }
const T& front() const noexcept { return *_data; }
T& back() noexcept { return _data[_size - 1]; }
const T& back() const noexcept { return _data[_size - 1]; }
T* data() noexcept { return _data; }
const T* data() const noexcept { return _data; }
iterator begin() noexcept { return _data; }
const_iterator begin() const noexcept { return _data; }
const_iterator cbegin() const noexcept { return _data; }
iterator end() noexcept { return _data + _size; }
const_iterator end() const noexcept { return _data + _size; }
const_iterator cend() const noexcept { return _data + _size; }
bool empty() const noexcept { return !_size; }
size_type size() const noexcept { return _size; }
size_type capacity() const noexcept { return _capacity; }
void clear() {
while (_size) {
pop_back();
}
}
void reserve(size_type new_capacity) {
if (new_capacity <= _capacity) {
return;
}
auto ptr = current_allocator().alloc<external>(sizeof(external) + sizeof(T) * new_capacity);
auto ext = static_cast<external*>(ptr);
ext->_backref = this;
T* data_ptr = ext->_data;
for (unsigned i = 0; i < _size; i++) {
new (data_ptr + i) T(std::move(_data[i]));
_data[i].~T();
}
if (is_external()) {
current_allocator().free(get_external(), get_external()->storage_size());
}
_data = data_ptr;
_capacity = new_capacity;
}
iterator erase(iterator it) {
std::move(it + 1, end(), it);
_data[_size - 1].~T();
_size--;
return it;
}
void push_back(const T& value) {
emplace_back(value);
}
void push_back(T&& value) {
emplace_back(std::move(value));
}
template<typename... Args>
T& emplace_back(Args&&... args) {
maybe_grow(_size + 1);
T* elem = new (_data + _size) T(std::forward<Args>(args)...);
_size++;
return *elem;
}
void pop_back() {
_data[_size - 1].~T();
_size--;
}
void resize(size_type new_size) {
maybe_grow(new_size);
while (_size > new_size) {
pop_back();
}
while (_size < new_size) {
emplace_back();
}
}
void resize(size_type new_size, const T& value) {
maybe_grow(new_size);
while (_size > new_size) {
pop_back();
}
while (_size < new_size) {
push_back(value);
}
}
// Returns the amount of external memory used to hold inserted items.
// Ignores reserved space.
size_t used_space_external_memory_usage() const {
if (is_external()) {
return sizeof(external) + _size * sizeof(T);
}
return 0;
}
// Returns the amount of external memory used to hold inserted items.
// Takes into account reserved space.
size_t external_memory_usage() const {
if (is_external()) {
return sizeof(external) + _capacity * sizeof(T);
}
return 0;
}
};