MCPcopy Create free account
hub / github.com/OpenStarbound/OpenStarbound / reserve

Method reserve

source/core/StarSmallVector.hpp:205–235  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

203
204template <typename Element, size_t MaxStackSize>
205void SmallVector<Element, MaxStackSize>::reserve(size_t newCapacity) {
206 size_t oldCapacity = m_capacity - m_begin;
207 if (newCapacity > oldCapacity) {
208 newCapacity = max(oldCapacity * 2, newCapacity);
209 auto newMem = (Element*)Star::malloc(newCapacity * sizeof(Element));
210 if (!newMem)
211 throw MemoryException::format("Could not set new SmallVector capacity {}\n", newCapacity);
212
213 size_t size = m_end - m_begin;
214 auto oldMem = m_begin;
215 auto oldHeapAllocated = isHeapAllocated();
216
217 // We assume that move constructors can never throw.
218 for (size_t i = 0; i < size; ++i) {
219 new (&newMem[i]) Element(std::move(oldMem[i]));
220 }
221
222 m_begin = newMem;
223 m_end = m_begin + size;
224 m_capacity = m_begin + newCapacity;
225
226 auto freeOldMem = finally([=]() {
227 if (oldHeapAllocated)
228 Star::free(oldMem, oldCapacity * sizeof(Element));
229 });
230
231 for (size_t i = 0; i < size; ++i) {
232 oldMem[i].~Element();
233 }
234 }
235}
236
237template <typename Element, size_t MaxStackSize>
238auto SmallVector<Element, MaxStackSize>::at(size_t i) -> reference {

Callers

nothing calls this directly

Calls 4

finallyFunction · 0.85
mallocFunction · 0.70
formatFunction · 0.70
freeFunction · 0.70

Tested by

no test coverage detected