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