Go to the documentation of this file.
31 #include "../support/common.hpp"
81 #pragma message("Huge pages not supported")
83 #define MADV_HUGEPAGE 0
87 static constexpr
int PROT = PROT_READ | PROT_WRITE;
91 size_t _size = 0, _capacity = 0;
99 explicit Vector<T, AT>(
const T *data,
size_t length) :
Vector(length) { memcpy(this->data, data, length); }
106 int result = munmap(data, _capacity);
107 assert(result == 0 &&
"mmunmap failed");
117 Vector(
Vector<T, AT> &&oth) : _size(std::exchange(oth._size, 0)), _capacity(std::exchange(oth._capacity, 0)), data(std::exchange(oth.data, nullptr)) {}
154 if (
capacity > _capacity) remap(max(
capacity, _capacity + (_capacity / 2)));
190 data[_size - 1] = elem;
203 std::swap(first._size, second._size);
204 std::swap(first._capacity, second._capacity);
205 std::swap(first.data, second.data);
212 inline const T &
operator[](
size_t i)
const {
return data[i]; };
218 inline size_t size()
const {
return _size; }
226 inline size_t capacity()
const {
return _capacity; }
231 size_t bitCount()
const {
return sizeof(*this) * 8 + _capacity *
sizeof(T) * 8; }
234 static size_t page_aligned(
size_t size) {
236 return ((2 * 1024 * 1024 - 1) | (
size *
sizeof(T) - 1)) + 1;
238 return ((4 * 1024 - 1) | (
size *
sizeof(T) - 1)) + 1;
241 void remap(
size_t size) {
242 if (
size == 0)
return;
248 space =
size *
sizeof(T);
249 mem = _capacity == 0 ? malloc(space) : realloc(data, space);
250 assert(mem != NULL &&
"malloc failed");
252 space = page_aligned(
size);
254 mem = mmap(
nullptr, space,
PROT,
FLAGS, -1, 0);
256 #ifndef MREMAP_MAYMOVE
257 mem = mmap(
nullptr, space,
PROT,
FLAGS, -1, 0);
258 memcpy(mem, data, page_aligned(_capacity));
260 mem = mremap(data, page_aligned(_capacity), space, MREMAP_MAYMOVE, -1, 0);
263 assert(mem != MAP_FAILED &&
"mmap failed");
267 assert(adv == 0 &&
"madvise failed");
271 if (_capacity *
sizeof(T) < space) memset(static_cast<char *>(mem) + _capacity *
sizeof(T), 0, space - _capacity *
sizeof(T));
273 _capacity = space /
sizeof(T);
274 data = static_cast<T *>(mem);
278 const uint64_t nsize = vector.
size();
279 os.write((
char *)&nsize,
sizeof(uint64_t));
280 os.write((
char *)&vector, vector.
size() *
sizeof(T));
286 is.read((
char *)&nsize,
sizeof(uint64_t));
288 is.read((
char *)&vector, vector.
size() *
sizeof(T));
T * operator&() const
Definition: Vector.hpp:209
AllocType
Definition: Vector.hpp:45
Definition: Vector.hpp:57
Definition: Expandable.hpp:37
friend void swap(Vector< T, AT > &first, Vector< T, AT > &second) noexcept
Definition: Vector.hpp:202
Definition: Expandable.hpp:43
Definition: Vector.hpp:49
#define MAP_HUGETLB
Definition: Vector.hpp:82
Vector(Vector< T, AT > &&oth)
Definition: Vector.hpp:117
#define MADV_HUGEPAGE
Definition: Vector.hpp:83
void reserve(size_t capacity)
Definition: Vector.hpp:141
void size(size_t size)
Definition: Vector.hpp:178
Definition: Vector.hpp:47
void grow(size_t capacity)
Definition: Vector.hpp:153
size_t bitCount() const
Definition: Vector.hpp:231
static constexpr int PROT
Definition: Vector.hpp:87
void trimToFit()
Definition: Vector.hpp:132
Vector< T, AT > & operator=(Vector< T, AT > &&oth)
Definition: Vector.hpp:119
size_t size() const
Definition: Vector.hpp:218
void pushBack(T elem)
Definition: Vector.hpp:188
friend std::ostream & operator<<(std::ostream &os, const Vector< T, AT > &vector)
Definition: Vector.hpp:277
void resize(size_t size)
Definition: Vector.hpp:166
T & operator[](size_t i)
Definition: Vector.hpp:215
const T & operator[](size_t i) const
Definition: Vector.hpp:212
size_t capacity() const
Definition: Vector.hpp:226
Definition: Vector.hpp:52
friend std::istream & operator>>(std::istream &is, Vector< T, AT > &vector)
Definition: Vector.hpp:284
Definition: Vector.hpp:78
static constexpr int FLAGS
Definition: Vector.hpp:88
void trim(size_t capacity)
Definition: Vector.hpp:127
T popBack()
Definition: Vector.hpp:200
Vector & operator=(const Vector &)=delete