MCPcopy Create free account
hub / github.com/DanielChappuis/reactphysics3d / reserve

Method reserve

include/reactphysics3d/containers/Array.h:250–282  ·  view source on GitHub ↗

Allocate memory for a given number of elements

Source from the content-addressed store, hash-verified

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) {

Callers

nothing calls this directly

Calls 2

allocateMethod · 0.45
releaseMethod · 0.45

Tested by

no test coverage detected