| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
#pragma once |
| 16 |
#if TRACY_ENABLE && TRACY_MEMORY_USAGE |
| 17 |
#include "tracy/Tracy.hpp" |
| 18 |
#else |
| 19 |
#define TracyAllocS(ptr, size, n) (void)0 |
| 20 |
#define TracyFreeS(ptr, n) (void)0 |
| 21 |
#endif |
| 22 |
#include <vector> |
| 23 |
|
| 24 |
#include "manifold/vec_view.h" |
| 25 |
#include "parallel.h" |
| 26 |
|
| 27 |
namespace manifold { |
| 28 |
|
| 29 |
#if (MANIFOLD_PAR == 1) |
| 30 |
extern tbb::task_arena gc_arena; |
| 31 |
#endif |
| 32 |
|
| 33 |
template <typename T> |
| 34 |
class Vec; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
template <typename T> |
| 45 |
class Vec : public VecView<T> { |
| 46 |
public: |
| 47 |
Vec() {} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
Vec(size_t size) : VecView<T>() { |
| 53 |
reserve(size); |
| 54 |
this->size_ = size; |
| 55 |
} |
| 56 |
|
| 57 |
Vec(size_t size, T val) : VecView<T>() { resize(size, val); } |
| 58 |
|
| 59 |
Vec(const Vec<T>& vec) : VecView<T>() { *this = Vec(vec.view()); } |
| 60 |
|
| 61 |
Vec(const VecView<const T>& vec) : VecView<T>() { |
| 62 |
this->size_ = vec.size(); |
| 63 |
this->capacity_ = this->size_; |
| 64 |
auto policy = autoPolicy(this->size_); |
| 65 |
if (this->size_ != 0) { |
| 66 |
this->ptr_ = reinterpret_cast<T*>(malloc(this->size_ * sizeof(T))); |
| 67 |
ASSERT(this->ptr_ != nullptr, std::bad_alloc()); |
| 68 |
TracyAllocS(this->ptr_, this->size_ * sizeof(T), 3); |
| 69 |
copy(policy, vec.begin(), vec.end(), this->ptr_); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
Vec(const std::vector<T>& vec) : VecView<T>() { |
| 74 |
this->size_ = vec.size(); |
| 75 |
this->capacity_ = this->size_; |
| 76 |
auto policy = autoPolicy(this->size_); |
| 77 |
if (this->size_ != 0) { |
| 78 |
this->ptr_ = reinterpret_cast<T*>(malloc(this->size_ * sizeof(T))); |
| 79 |
ASSERT(this->ptr_ != nullptr, std::bad_alloc()); |
| 80 |
TracyAllocS(this->ptr_, this->size_ * sizeof(T), 3); |
| 81 |
copy(policy, vec.begin(), vec.end(), this->ptr_); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
Vec(Vec<T>&& vec) : VecView<T>() { |
| 86 |
this->ptr_ = vec.ptr_; |
| 87 |
this->size_ = vec.size_; |
| 88 |
capacity_ = vec.capacity_; |
| 89 |
vec.ptr_ = nullptr; |
| 90 |
vec.size_ = 0; |
| 91 |
vec.capacity_ = 0; |
| 92 |
} |
| 93 |
|
| 94 |
operator VecView<T>() { return {this->ptr_, this->size_}; } |
| 95 |
operator VecView<const T>() const { return {this->ptr_, this->size_}; } |
| 96 |
|
| 97 |
~Vec() { |
| 98 |
if (this->ptr_ != nullptr) { |
| 99 |
free_async(this->ptr_, capacity_); |
| 100 |
} |
| 101 |
this->ptr_ = nullptr; |
| 102 |
this->size_ = 0; |
| 103 |
capacity_ = 0; |
| 104 |
} |
| 105 |
|
| 106 |
Vec<T>& operator=(const Vec<T>& other) { |
| 107 |
if (&other == this) return *this; |
| 108 |
if (this->ptr_ != nullptr) { |
| 109 |
free_async(this->ptr_, capacity_); |
| 110 |
} |
| 111 |
this->size_ = other.size_; |
| 112 |
capacity_ = other.size_; |
| 113 |
if (this->size_ != 0) { |
| 114 |
this->ptr_ = reinterpret_cast<T*>(malloc(this->size_ * sizeof(T))); |
| 115 |
ASSERT(this->ptr_ != nullptr, std::bad_alloc()); |
| 116 |
TracyAllocS(this->ptr_, this->size_ * sizeof(T), 3); |
| 117 |
manifold::copy(other.begin(), other.end(), this->ptr_); |
| 118 |
} |
| 119 |
return *this; |
| 120 |
} |
| 121 |
|
| 122 |
Vec<T>& operator=(Vec<T>&& other) { |
| 123 |
if (&other == this) return *this; |
| 124 |
if (this->ptr_ != nullptr) { |
| 125 |
free_async(this->ptr_, capacity_); |
| 126 |
} |
| 127 |
this->size_ = other.size_; |
| 128 |
capacity_ = other.capacity_; |
| 129 |
this->ptr_ = other.ptr_; |
| 130 |
other.ptr_ = nullptr; |
| 131 |
other.size_ = 0; |
| 132 |
other.capacity_ = 0; |
| 133 |
return *this; |
| 134 |
} |
| 135 |
|
| 136 |
operator VecView<T>() const { return {this->ptr_, this->size_}; } |
| 137 |
|
| 138 |
void swap(Vec<T>& other) { |
| 139 |
std::swap(this->ptr_, other.ptr_); |
| 140 |
std::swap(this->size_, other.size_); |
| 141 |
std::swap(capacity_, other.capacity_); |
| 142 |
} |
| 143 |
|
| 144 |
inline void push_back(const T& val) { |
| 145 |
if (this->size_ >= capacity_) { |
| 146 |
|
| 147 |
T val_copy = val; |
| 148 |
reserve(capacity_ == 0 ? 128 : capacity_ * 2); |
| 149 |
this->ptr_[this->size_++] = val_copy; |
| 150 |
return; |
| 151 |
} |
| 152 |
this->ptr_[this->size_++] = val; |
| 153 |
} |
| 154 |
|
| 155 |
inline void extend(size_t n) { |
| 156 |
if (this->size_ + n >= capacity_) |
| 157 |
reserve(capacity_ == 0 ? 128 : std::max(capacity_ * 2, this->size_ + n)); |
| 158 |
this->size_ += n; |
| 159 |
} |
| 160 |
|
| 161 |
void reserve(size_t n) { |
| 162 |
if (n > capacity_) { |
| 163 |
T* newBuffer = reinterpret_cast<T*>(malloc(n * sizeof(T))); |
| 164 |
ASSERT(newBuffer != nullptr, std::bad_alloc()); |
| 165 |
TracyAllocS(newBuffer, n * sizeof(T), 3); |
| 166 |
if (this->size_ > 0) |
| 167 |
manifold::copy(autoPolicy(this->size_), this->ptr_, |
| 168 |
this->ptr_ + this->size_, newBuffer); |
| 169 |
if (this->ptr_ != nullptr) { |
| 170 |
free_async(this->ptr_, capacity_); |
| 171 |
} |
| 172 |
this->ptr_ = newBuffer; |
| 173 |
capacity_ = n; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
void resize(size_t newSize, T val = T()) { |
| 178 |
bool shrink = this->size_ > 2 * newSize && this->size_ > 16; |
| 179 |
reserve(newSize); |
| 180 |
if (this->size_ < newSize) { |
| 181 |
fill(autoPolicy(newSize - this->size_), this->ptr_ + this->size_, |
| 182 |
this->ptr_ + newSize, val); |
| 183 |
} |
| 184 |
this->size_ = newSize; |
| 185 |
if (shrink) shrink_to_fit(); |
| 186 |
} |
| 187 |
|
| 188 |
void resize_nofill(size_t newSize) { |
| 189 |
bool shrink = this->size_ > 2 * newSize && this->size_ > 16; |
| 190 |
reserve(newSize); |
| 191 |
this->size_ = newSize; |
| 192 |
if (shrink) shrink_to_fit(); |
| 193 |
} |
| 194 |
|
| 195 |
void pop_back() { resize_nofill(this->size_ - 1); } |
| 196 |
|
| 197 |
void clear(bool shrink = true) { |
| 198 |
this->size_ = 0; |
| 199 |
if (shrink) shrink_to_fit(); |
| 200 |
} |
| 201 |
|
| 202 |
void shrink_to_fit() { |
| 203 |
T* newBuffer = nullptr; |
| 204 |
if (this->size_ > 0) { |
| 205 |
newBuffer = reinterpret_cast<T*>(malloc(this->size_ * sizeof(T))); |
| 206 |
ASSERT(newBuffer != nullptr, std::bad_alloc()); |
| 207 |
TracyAllocS(newBuffer, this->size_ * sizeof(T), 3); |
| 208 |
manifold::copy(this->ptr_, this->ptr_ + this->size_, newBuffer); |
| 209 |
} |
| 210 |
if (this->ptr_ != nullptr) { |
| 211 |
free_async(this->ptr_, capacity_); |
| 212 |
} |
| 213 |
this->ptr_ = newBuffer; |
| 214 |
capacity_ = this->size_; |
| 215 |
} |
| 216 |
|
| 217 |
size_t capacity() const { return capacity_; } |
| 218 |
|
| 219 |
private: |
| 220 |
size_t capacity_ = 0; |
| 221 |
|
| 222 |
static_assert(std::is_trivially_destructible<T>::value); |
| 223 |
|
| 224 |
static void free_async(T* ptr, size_t size) { |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
constexpr size_t ASYNC_FREE_THRESHOLD = 1 << 18; |
| 230 |
TracyFreeS(ptr, 3); |
| 231 |
#if defined(__has_feature) |
| 232 |
#if !__has_feature(address_sanitizer) |
| 233 |
#if (MANIFOLD_PAR == 1) |
| 234 |
if (size * sizeof(T) > ASYNC_FREE_THRESHOLD) |
| 235 |
gc_arena.enqueue([ptr]() { free(ptr); }); |
| 236 |
else |
| 237 |
#endif |
| 238 |
#endif |
| 239 |
#endif |
| 240 |
free(ptr); |
| 241 |
} |
| 242 |
}; |
| 243 |
} |