Allocate memory for a given number of elements
| 248 | |
| 249 | /// Allocate memory for a given number of elements |
| 250 | void reserve(uint64 capacity) { |
| 251 | |
| 252 | if (capacity <= mCapacity) return; |
| 253 | |
| 254 | // Make sure capacity is an integral multiple of alignment |
| 255 | capacity = std::ceil(capacity / float(GLOBAL_ALIGNMENT)) * GLOBAL_ALIGNMENT; |
| 256 | |
| 257 | // Allocate memory for the new array |
| 258 | void* newMemory = mAllocator.allocate(capacity * sizeof(T)); |
| 259 | T* destination = static_cast<T*>(newMemory); |
| 260 | |
| 261 | if (mBuffer != nullptr) { |
| 262 | |
| 263 | if (mSize > 0) { |
| 264 | |
| 265 | // Copy the elements to the new allocated memory location |
| 266 | std::uninitialized_copy(mBuffer, mBuffer + mSize, destination); |
| 267 | |
| 268 | // Destruct the previous items |
| 269 | for (uint64 i=0; i<mSize; i++) { |
| 270 | mBuffer[i].~T(); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | // Release the previously allocated memory |
| 275 | mAllocator.release(mBuffer, mCapacity * sizeof(T)); |
| 276 | } |
| 277 | |
| 278 | mBuffer = destination; |
| 279 | assert(mBuffer != nullptr); |
| 280 | |
| 281 | mCapacity = capacity; |
| 282 | } |
| 283 | |
| 284 | /// Add an element into the array |
| 285 | void add(const T& element) { |