forked from meemknight/pinnedVector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PinnedVector.h
376 lines (291 loc) · 7.88 KB
/
PinnedVector.h
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#pragma once
/////////////////////////////////////////////
//#define PINNED_VECTOR_BOUNDS_CHECK
// It checks if you access outside the array
/////////////////////////////////////////////
#ifdef _DEBUG
#define PINNED_VECTOR_BOUNDS_CHECK
#endif
//(not working yet)
/////////////////////////////////////////////////////////////////////////////
//#define PINNED_VECTOR_MEMORY_CHECK
//
// this checks if you access memory that was freed.
// It does not release memory, but rather invalidates it, so it would
// fail on access. Can consume a lot of memory due to this reason so use it
// only to debug, and it can crash quickly if you allocate and deallocate
// often with this vector
/////////////////////////////////////////////////////////////////////////////
#ifdef PINNED_VECTOR_BOUNDS_CHECK
//you can use your own assert function here if you want.
//it is used to check if the vector gets out of bounds
#define PINNED_VECTOR_ASSERT(x) assert(x)
#else
#define PINNED_VECTOR_ASSERT(x)
#endif
//you can use your own assert function here if you want.
//it is used to check if the allocations fail
#define PINNED_VECTOR_ALLOCATION_FAILED_ASSERT(x) assert(x)
////////////////////////////////////////////////////////////////////////////////////////////////
//code
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
#define PINNED_VECTOR_WIN
#elif defined(__linux__) && !defined(__ANDROID__)
#define PINNED_VECTOR_LINUX
#else
#error pinned vector supports only windows and linux
#endif
#ifdef PINNED_VECTOR_WIN
#include <Windows.h>
#else
#include <sys/mman.h>
#include <stdio.h>
#endif
#include <assert.h>
#include <type_traits>
#include "stdint.h"
#ifdef PINNED_VECTOR_WIN
#define PINNED_VECTOR_RESERVE_MEMORY(size)\
VirtualAlloc(0, (size), MEM_RESERVE, PAGE_READWRITE)
#define PINNED_VECTOR_COMMIT_MEMORY(beg, size)\
VirtualAlloc((beg), (size), MEM_COMMIT, PAGE_READWRITE)
#define PINNED_VECTOR_FREE_MEMORY(beg)\
VirtualFree((beg), 0, MEM_RELEASE)
#else
//#define PINNED_VECTOR_RESERVE_MEMORY(size) mmap(nullptr, (size), PROT_NONE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0)
#define PINNED_VECTOR_RESERVE_MEMORY(size) mmap(nullptr, (size), PROT_READ|PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0)
//#define PINNED_VECTOR_RESERVE_MEMORY(size) malloc(size)
//#define PINNED_VECTOR_COMMIT_MEMORY(beg, size) true
//(mprotect((beg), (size), PROT_READ|PROT_WRITE)==0) && (mlock((beg), (size)) == 0)
#define PINNED_VECTOR_COMMIT_MEMORY(beg, size) \
(mprotect((beg), (size), PROT_READ|PROT_WRITE)==0, 1)
//(mprotect((beg), (size), PROT_READ|PROT_WRITE)==0) && (mlock((beg), (size)) == 0 && (((unsigned char*)beg)[size-1] = 0, 1))
/* dangerous af but works so whatever */
#define maxElCount UINT64_MAX
#define PINNED_VECTOR_FREE_MEMORY(beg) munmap((beg), maxElCount*sizeof(T))
//#define PINNED_VECTOR_FREE_MEMORY(beg) ::free((void*)beg)
#endif
//maxElCount is the maximum number of ELEMENTS that the vector can store.
template <class T>
struct PinnedVector
{
T *beg_ = 0;
unsigned int size_ = 0;
typedef T *iterator;
typedef const T *constIterator;
iterator begin() { return &((T *)beg_)[0]; }
constIterator begin() const { return &((T *)beg_)[0]; }
iterator end() { return &((T *)beg_)[size_]; }
constIterator end() const { return &((T *)beg_)[size_]; }
explicit PinnedVector(size_t maxElementCount = 1000)
{
initializeArena(maxElementCount);
}
PinnedVector(PinnedVector &&other)
noexcept {
this->beg_ = other.beg_;
this->size_ = other.size_;
other.size_ = 0;
other.beg_ = nullptr;
//?
other.initializeArena();
}
PinnedVector(PinnedVector &other)
{
initializeArena();
noConstructorCallResize(other.size_);
memcpy(beg_, other.beg_, size_ * sizeof(T));
}
void initializeArena(size_t maxElementCount);
void noConstructorCallResize(size_t elementCount);
void resize(size_t elementCount);
[[maybe_unused]] void reserve(size_t elementCount) const;
[[nodiscard]] size_t size()const { return size_; }
[[maybe_unused]] [[nodiscard]] bool empty() const
{
return (size_ == 0);
}
[[maybe_unused]] T *data();
PinnedVector &operator= (const PinnedVector &other)
{
if (this == &other)
{
return *this;
}
free();
this->noConstructorCallResize(other.size_);
memcpy(beg_, other.beg_, size_ * sizeof(T));
return *this;
}
PinnedVector &operator= (PinnedVector &&other)
noexcept {
if (this == &other)
{
return *this;
}
free();
this(std::move(other));
return *this;
}
T &operator[] (unsigned int index)
{
PINNED_VECTOR_ASSERT(index < size_)
return static_cast<T *>(beg_)[index];
}
T operator[] (unsigned int index) const
{
PINNED_VECTOR_ASSERT(index < size_)
return static_cast<T *>(beg_)[index];
}
[[maybe_unused]] void push_back(const T &el);
void push_back(T &&el);
void pop_back();
T &top()
{
return (*this)[size_ - 1];
}
[[maybe_unused]] const T &top() const
{
return (*this)[size_ - 1];
}
void clear();
void free();
~PinnedVector();
};
template<class T>
inline void PinnedVector<T>::initializeArena(size_t maxElementCount)
{
if (beg_)
{
free();
}
beg_ = (T *)PINNED_VECTOR_RESERVE_MEMORY(sizeof(T) * maxElementCount);
PINNED_VECTOR_ALLOCATION_FAILED_ASSERT(beg_ != 0 && beg_ != (void *)-1);
}
template<class T>
inline void PinnedVector<T>::noConstructorCallResize(size_t elementCount)
{
//if (!beg_)
//{
// initializeArena();
//}
if (elementCount > size_)
{
auto check = PINNED_VECTOR_COMMIT_MEMORY((beg_), (elementCount * sizeof(T)));
PINNED_VECTOR_ALLOCATION_FAILED_ASSERT(check);
size_ = elementCount;
}
else
{
size_ = elementCount;
}
}
template<class T>
inline void PinnedVector<T>::resize(size_t elementCount)
{
//if (!beg_)
//{
// initializeArena();
//}
if (elementCount > size_)
{
auto check = PINNED_VECTOR_COMMIT_MEMORY((beg_), (elementCount * sizeof(T)));
PINNED_VECTOR_ALLOCATION_FAILED_ASSERT(check);
for (int i = size_; i < elementCount; i++)
{
new(&beg_[i])T{};
}
}
else
{
if constexpr (!std::is_trivially_destructible_v<T>)
{
for (size_t i = elementCount; i < size_; i++)
{
beg_[i].~T();
}
}
}
size_ = elementCount;
}
template<class T>
[[maybe_unused]] inline void PinnedVector<T>::reserve(const size_t elementCount) const {
auto check = PINNED_VECTOR_COMMIT_MEMORY((beg_), (elementCount * sizeof(T)));
PINNED_VECTOR_ALLOCATION_FAILED_ASSERT(check);
}
template<class T>
[[maybe_unused]] inline T *PinnedVector<T>::data()
{
return beg_;
}
template<class T>
[[maybe_unused]] inline void PinnedVector<T>::push_back(const T &el)
{
noConstructorCallResize(size_ + 1);
//new(&beg_[size_ - 1])T(el);
new(&beg_[size_ - 1])T(std::forward<T>(el));
}
template<class T>
inline void PinnedVector<T>::push_back(T &&el)
{
noConstructorCallResize(size_ + 1);
//new(&beg_[size_ - 1])T(el);
new(&beg_[size_ - 1])T(std::forward<T>(el));
}
template<class T>
inline void PinnedVector<T>::pop_back()
{
if constexpr (!std::is_trivially_destructible<T>::value)
{
beg_[size_ - 1].~T();
}
noConstructorCallResize(size_ - 1);
}
template<class T>
inline void PinnedVector<T>::clear()
{
if constexpr (!std::is_trivially_destructible<T>::value)
{
for (int i = 0; i < size_; i++)
{
beg_[i].~T();
}
}
size_ = 0;
}
//Todo: add commited size_
template<class T>
inline void PinnedVector<T>::free()
{
if constexpr (!std::is_trivially_destructible<T>::value)
{
for (int i = 0; i < size_; i++)
{
beg_[i].~T();
}
}
#ifdef PINNED_VECTOR_MEMORY_CHECK
if (beg_)
{
//VirtualFree(beg, maxElCount * sizeof(T), MEM_DECOMMIT);
DWORD last = {};
VirtualProtect(beg_, sizeof(T) * size_, PAGE_NOACCESS, &last);
}
#else
if (beg_)
{
PINNED_VECTOR_FREE_MEMORY(beg_);
}
size_ = 0;
#endif // PINNED_VECTOR_MEMORY_CHECK
}
template<class T>
inline PinnedVector<T>::~PinnedVector()
{
free();
}
#undef PINNED_VECTOR_WIN
#undef PINNED_VECTOR_LINUX
#undef PINNED_VECTOR_RESERVE_MEMORY
#undef PINNED_VECTOR_COMMIT_MEMORY
#undef PINNED_VECTOR_FREE_MEMORY