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 #include "llvm/ADT/Twine.h" 15 #include <cstdint> 16 #ifdef LLVM_ENABLE_EXCEPTIONS 17 #include <stdexcept> 18 #endif 19 using namespace llvm; 20 21 // Check that no bytes are wasted and everything is well-aligned. 22 namespace { 23 // These structures may cause binary compat warnings on AIX. Suppress the 24 // warning since we are only using these types for the static assertions below. 25 #if defined(_AIX) 26 #pragma GCC diagnostic push 27 #pragma GCC diagnostic ignored "-Waix-compat" 28 #endif 29 struct Struct16B { 30 alignas(16) void *X; 31 }; 32 struct Struct32B { 33 alignas(32) void *X; 34 }; 35 #if defined(_AIX) 36 #pragma GCC diagnostic pop 37 #endif 38 } 39 static_assert(sizeof(SmallVector<void *, 0>) == 40 sizeof(unsigned) * 2 + sizeof(void *), 41 "wasted space in SmallVector size 0"); 42 static_assert(alignof(SmallVector<Struct16B, 0>) >= alignof(Struct16B), 43 "wrong alignment for 16-byte aligned T"); 44 static_assert(alignof(SmallVector<Struct32B, 0>) >= alignof(Struct32B), 45 "wrong alignment for 32-byte aligned T"); 46 static_assert(sizeof(SmallVector<Struct16B, 0>) >= alignof(Struct16B), 47 "missing padding for 16-byte aligned T"); 48 static_assert(sizeof(SmallVector<Struct32B, 0>) >= alignof(Struct32B), 49 "missing padding for 32-byte aligned T"); 50 static_assert(sizeof(SmallVector<void *, 1>) == 51 sizeof(unsigned) * 2 + sizeof(void *) * 2, 52 "wasted space in SmallVector size 1"); 53 54 static_assert(sizeof(SmallVector<char, 0>) == 55 sizeof(void *) * 2 + sizeof(void *), 56 "1 byte elements have word-sized type for size and capacity"); 57 58 /// Report that MinSize doesn't fit into this vector's size type. Throws 59 /// std::length_error or calls report_fatal_error. 60 [[noreturn]] static void report_size_overflow(size_t MinSize, size_t MaxSize); 61 static void report_size_overflow(size_t MinSize, size_t MaxSize) { 62 std::string Reason = "SmallVector unable to grow. Requested capacity (" + 63 std::to_string(MinSize) + 64 ") is larger than maximum value for size type (" + 65 std::to_string(MaxSize) + ")"; 66 #ifdef LLVM_ENABLE_EXCEPTIONS 67 throw std::length_error(Reason); 68 #else 69 report_fatal_error(Twine(Reason)); 70 #endif 71 } 72 73 /// Report that this vector is already at maximum capacity. Throws 74 /// std::length_error or calls report_fatal_error. 75 [[noreturn]] static void report_at_maximum_capacity(size_t MaxSize); 76 static void report_at_maximum_capacity(size_t MaxSize) { 77 std::string Reason = 78 "SmallVector capacity unable to grow. Already at maximum size " + 79 std::to_string(MaxSize); 80 #ifdef LLVM_ENABLE_EXCEPTIONS 81 throw std::length_error(Reason); 82 #else 83 report_fatal_error(Twine(Reason)); 84 #endif 85 } 86 87 // Note: Moving this function into the header may cause performance regression. 88 template <class Size_T> 89 static size_t getNewCapacity(size_t MinSize, size_t TSize, size_t OldCapacity) { 90 constexpr size_t MaxSize = std::numeric_limits<Size_T>::max(); 91 92 // Ensure we can fit the new capacity. 93 // This is only going to be applicable when the capacity is 32 bit. 94 if (MinSize > MaxSize) 95 report_size_overflow(MinSize, MaxSize); 96 97 // Ensure we can meet the guarantee of space for at least one more element. 98 // The above check alone will not catch the case where grow is called with a 99 // default MinSize of 0, but the current capacity cannot be increased. 100 // This is only going to be applicable when the capacity is 32 bit. 101 if (OldCapacity == MaxSize) 102 report_at_maximum_capacity(MaxSize); 103 104 // In theory 2*capacity can overflow if the capacity is 64 bit, but the 105 // original capacity would never be large enough for this to be a problem. 106 size_t NewCapacity = 2 * OldCapacity + 1; // Always grow. 107 return std::min(std::max(NewCapacity, MinSize), MaxSize); 108 } 109 110 // Note: Moving this function into the header may cause performance regression. 111 template <class Size_T> 112 void *SmallVectorBase<Size_T>::mallocForGrow(size_t MinSize, size_t TSize, 113 size_t &NewCapacity) { 114 NewCapacity = getNewCapacity<Size_T>(MinSize, TSize, this->capacity()); 115 return llvm::safe_malloc(NewCapacity * TSize); 116 } 117 118 // Note: Moving this function into the header may cause performance regression. 119 template <class Size_T> 120 void SmallVectorBase<Size_T>::grow_pod(void *FirstEl, size_t MinSize, 121 size_t TSize) { 122 size_t NewCapacity = getNewCapacity<Size_T>(MinSize, TSize, this->capacity()); 123 void *NewElts; 124 if (BeginX == FirstEl) { 125 NewElts = safe_malloc(NewCapacity * TSize); 126 127 // Copy the elements over. No need to run dtors on PODs. 128 memcpy(NewElts, this->BeginX, size() * TSize); 129 } else { 130 // If this wasn't grown from the inline copy, grow the allocated space. 131 NewElts = safe_realloc(this->BeginX, NewCapacity * TSize); 132 } 133 134 this->BeginX = NewElts; 135 this->Capacity = NewCapacity; 136 } 137 138 template class llvm::SmallVectorBase<uint32_t>; 139 140 // Disable the uint64_t instantiation for 32-bit builds. 141 // Both uint32_t and uint64_t instantiations are needed for 64-bit builds. 142 // This instantiation will never be used in 32-bit builds, and will cause 143 // warnings when sizeof(Size_T) > sizeof(size_t). 144 #if SIZE_MAX > UINT32_MAX 145 template class llvm::SmallVectorBase<uint64_t>; 146 147 // Assertions to ensure this #if stays in sync with SmallVectorSizeType. 148 static_assert(sizeof(SmallVectorSizeType<char>) == sizeof(uint64_t), 149 "Expected SmallVectorBase<uint64_t> variant to be in use."); 150 #else 151 static_assert(sizeof(SmallVectorSizeType<char>) == sizeof(uint32_t), 152 "Expected SmallVectorBase<uint32_t> variant to be in use."); 153 #endif 154