1 //===- llvm/ADT/SmallVector.cpp - 'Normally small' vectors ----------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the SmallVector class. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/ADT/SmallVector.h" 15 using namespace llvm; 16 17 // Check that no bytes are wasted. 18 static_assert(sizeof(SmallVector<void *, 1>) == 19 sizeof(unsigned) * 2 + sizeof(void *) * 2, 20 "wasted space in SmallVector size 1; missing EBO?"); 21 22 /// grow_pod - This is an implementation of the grow() method which only works 23 /// on POD-like datatypes and is out of line to reduce code duplication. 24 void SmallVectorBase::grow_pod(void *FirstEl, size_t MinCapacity, 25 size_t TSize) { 26 // Ensure we can fit the new capacity in 32 bits. 27 if (MinCapacity > UINT32_MAX) 28 report_bad_alloc_error("SmallVector capacity overflow during allocation"); 29 30 size_t NewCapacity = 2 * capacity() + 1; // Always grow. 31 NewCapacity = 32 std::min(std::max(NewCapacity, MinCapacity), size_t(UINT32_MAX)); 33 34 void *NewElts; 35 if (BeginX == FirstEl) { 36 NewElts = safe_malloc(NewCapacity * TSize); 37 38 // Copy the elements over. No need to run dtors on PODs. 39 memcpy(NewElts, this->BeginX, size() * TSize); 40 } else { 41 // If this wasn't grown from the inline copy, grow the allocated space. 42 NewElts = safe_realloc(this->BeginX, NewCapacity * TSize); 43 } 44 45 this->BeginX = NewElts; 46 this->Capacity = NewCapacity; 47 } 48