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

Method grow_to

src/fl/stl/basic_vector.cpp.hpp:58–95  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

56}
57
58void vector_basic::grow_to(fl::size new_capacity) {
59 // PATH 1: Try in-place reallocate if on heap and trivially copyable
60 if (!isInline() && mArray && !mOps) {
61 void* result = mResource->reallocate(mArray, mCapacity * mElementSize,
62 new_capacity * mElementSize);
63 if (result) {
64 mArray = result;
65 mCapacity = new_capacity;
66 return;
67 }
68 }
69
70 // PATH 2: Allocate new buffer, move elements, free old
71 FASTLED_ASSERT(mResource != nullptr, "memory_resource is null");
72 fl::size alloc_bytes = new_capacity * mElementSize;
73 FASTLED_ASSERT(alloc_bytes > 0, "zero allocation in grow_to");
74 void* new_buf = mResource->allocate(alloc_bytes);
75 FASTLED_ASSERT(new_buf != nullptr, "allocation failed in grow_to");
76 if (!new_buf) return;
77
78 // Move existing elements to new buffer
79 if (mSize > 0 && mArray) {
80 if (mOps) {
81 mOps->uninitialized_move_n(new_buf, mArray, mSize);
82 mOps->destroy_n(mArray, mSize);
83 } else {
84 trivial_copy(new_buf, mArray, mSize);
85 }
86 }
87
88 // Free old buffer (only if it's on the heap, not inline)
89 if (!isInline() && mArray) {
90 mResource->deallocate(mArray, mCapacity * mElementSize);
91 }
92
93 mArray = new_buf;
94 mCapacity = new_capacity;
95}
96
97void vector_basic::reserve_impl(fl::size n) {
98 if (n > mCapacity) {

Callers

nothing calls this directly

Calls 3

reallocateMethod · 0.45
allocateMethod · 0.45
deallocateMethod · 0.45

Tested by

no test coverage detected