MCPcopy Create free account
hub / github.com/FastLED/FastLED / ensure_capacity

Method ensure_capacity

src/fl/stl/deque.h:24–53  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

22 static const fl::size kInitialCapacity = 8;
23
24 void ensure_capacity(fl::size min_capacity) {
25 if (mCapacity >= min_capacity) {
26 return;
27 }
28
29 fl::size new_capacity = mCapacity == 0 ? kInitialCapacity : mCapacity * 2;
30 while (new_capacity < min_capacity) {
31 new_capacity *= 2;
32 }
33
34 T* new_data = static_cast<T*>(mResource->allocate(new_capacity * sizeof(T)));
35 if (!new_data) {
36 return; // Allocation failed
37 }
38
39 // Copy existing elements to new buffer in linear order
40 for (fl::size i = 0; i < mSize; ++i) {
41 fl::size old_idx = (mFront + i) % mCapacity;
42 new (&new_data[i]) T(fl::move(mData[old_idx]));
43 mData[old_idx].~T();
44 }
45
46 if (mData) {
47 mResource->deallocate(mData, mCapacity * sizeof(T));
48 }
49
50 mData = new_data;
51 mCapacity = new_capacity;
52 mFront = 0; // Reset front to 0 after reallocation
53 }
54
55 fl::size get_index(fl::size logical_index) const {
56 return (mFront + logical_index) % mCapacity;

Callers

nothing calls this directly

Calls 2

allocateMethod · 0.45
deallocateMethod · 0.45

Tested by

no test coverage detected