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