1 //===- llvm/ADT/SmallVector.cpp - 'Normally small' vectors ----------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements the SmallVector class. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/ADT/SmallVector.h" 14 using namespace llvm; 15 16 // Check that no bytes are wasted and everything is well-aligned. 17 namespace { 18 struct Struct16B { 19 alignas(16) void *X; 20 }; 21 struct Struct32B { 22 alignas(32) void *X; 23 }; 24 } 25 static_assert(sizeof(SmallVector<void *, 0>) == 26 sizeof(unsigned) * 2 + sizeof(void *), 27 "wasted space in SmallVector size 0"); 28 static_assert(alignof(SmallVector<Struct16B, 0>) >= alignof(Struct16B), 29 "wrong alignment for 16-byte aligned T"); 30 static_assert(alignof(SmallVector<Struct32B, 0>) >= alignof(Struct32B), 31 "wrong alignment for 32-byte aligned T"); 32 static_assert(sizeof(SmallVector<Struct16B, 0>) >= alignof(Struct16B), 33 "missing padding for 16-byte aligned T"); 34 static_assert(sizeof(SmallVector<Struct32B, 0>) >= alignof(Struct32B), 35 "missing padding for 32-byte aligned T"); 36 static_assert(sizeof(SmallVector<void *, 1>) == 37 sizeof(unsigned) * 2 + sizeof(void *) * 2, 38 "wasted space in SmallVector size 1"); 39 40 static_assert(sizeof(SmallVector<char, 0>) == 41 sizeof(void *) * 2 + sizeof(void *), 42 "1 byte elements have word-sized type for size and capacity"); 43 44 // Note: Moving this function into the header may cause performance regression. 45 template <class Size_T> 46 void SmallVectorBase<Size_T>::grow_pod(void *FirstEl, size_t MinCapacity, 47 size_t TSize) { 48 // Ensure we can fit the new capacity. 49 // This is only going to be applicable when the capacity is 32 bit. 50 if (MinCapacity > SizeTypeMax()) 51 report_bad_alloc_error("SmallVector capacity overflow during allocation"); 52 53 // Ensure we can meet the guarantee of space for at least one more element. 54 // The above check alone will not catch the case where grow is called with a 55 // default MinCapacity of 0, but the current capacity cannot be increased. 56 // This is only going to be applicable when the capacity is 32 bit. 57 if (capacity() == SizeTypeMax()) 58 report_bad_alloc_error("SmallVector capacity unable to grow"); 59 60 // In theory 2*capacity can overflow if the capacity is 64 bit, but the 61 // original capacity would never be large enough for this to be a problem. 62 size_t NewCapacity = 2 * capacity() + 1; // Always grow. 63 NewCapacity = std::min(std::max(NewCapacity, MinCapacity), SizeTypeMax()); 64 65 void *NewElts; 66 if (BeginX == FirstEl) { 67 NewElts = safe_malloc(NewCapacity * TSize); 68 69 // Copy the elements over. No need to run dtors on PODs. 70 memcpy(NewElts, this->BeginX, size() * TSize); 71 } else { 72 // If this wasn't grown from the inline copy, grow the allocated space. 73 NewElts = safe_realloc(this->BeginX, NewCapacity * TSize); 74 } 75 76 this->BeginX = NewElts; 77 this->Capacity = NewCapacity; 78 } 79 80 template class llvm::SmallVectorBase<uint32_t>; 81 82 // Disable the uint64_t instantiation for 32-bit builds. 83 // Both uint32_t and uint64_t instantations are needed for 64-bit builds. 84 // This instantiation will never be used in 32-bit builds, and will cause 85 // warnings when sizeof(Size_T) > sizeof(size_t). 86 #if SIZE_MAX > UINT32_MAX 87 template class llvm::SmallVectorBase<uint64_t>; 88 89 // Assertions to ensure this #if stays in sync with SmallVectorSizeType. 90 static_assert(sizeof(SmallVectorSizeType<char>) == sizeof(uint64_t), 91 "Expected SmallVectorBase<uint64_t> variant to be in use."); 92 #else 93 static_assert(sizeof(SmallVectorSizeType<char>) == sizeof(uint32_t), 94 "Expected SmallVectorBase<uint32_t> variant to be in use."); 95 #endif 96