10b57cec5SDimitry Andric //===- llvm/ADT/SmallVector.cpp - 'Normally small' vectors ----------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file implements the SmallVector class.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
145ffd83dbSDimitry Andric #include <cstdint>
15*af732203SDimitry Andric #ifdef LLVM_ENABLE_EXCEPTIONS
16*af732203SDimitry Andric #include <stdexcept>
17*af732203SDimitry Andric #endif
180b57cec5SDimitry Andric using namespace llvm;
190b57cec5SDimitry Andric 
200b57cec5SDimitry Andric // Check that no bytes are wasted and everything is well-aligned.
210b57cec5SDimitry Andric namespace {
220b57cec5SDimitry Andric struct Struct16B {
230b57cec5SDimitry Andric   alignas(16) void *X;
240b57cec5SDimitry Andric };
250b57cec5SDimitry Andric struct Struct32B {
260b57cec5SDimitry Andric   alignas(32) void *X;
270b57cec5SDimitry Andric };
280b57cec5SDimitry Andric }
290b57cec5SDimitry Andric static_assert(sizeof(SmallVector<void *, 0>) ==
300b57cec5SDimitry Andric                   sizeof(unsigned) * 2 + sizeof(void *),
310b57cec5SDimitry Andric               "wasted space in SmallVector size 0");
320b57cec5SDimitry Andric static_assert(alignof(SmallVector<Struct16B, 0>) >= alignof(Struct16B),
330b57cec5SDimitry Andric               "wrong alignment for 16-byte aligned T");
340b57cec5SDimitry Andric static_assert(alignof(SmallVector<Struct32B, 0>) >= alignof(Struct32B),
350b57cec5SDimitry Andric               "wrong alignment for 32-byte aligned T");
360b57cec5SDimitry Andric static_assert(sizeof(SmallVector<Struct16B, 0>) >= alignof(Struct16B),
370b57cec5SDimitry Andric               "missing padding for 16-byte aligned T");
380b57cec5SDimitry Andric static_assert(sizeof(SmallVector<Struct32B, 0>) >= alignof(Struct32B),
390b57cec5SDimitry Andric               "missing padding for 32-byte aligned T");
400b57cec5SDimitry Andric static_assert(sizeof(SmallVector<void *, 1>) ==
410b57cec5SDimitry Andric                   sizeof(unsigned) * 2 + sizeof(void *) * 2,
420b57cec5SDimitry Andric               "wasted space in SmallVector size 1");
430b57cec5SDimitry Andric 
445ffd83dbSDimitry Andric static_assert(sizeof(SmallVector<char, 0>) ==
455ffd83dbSDimitry Andric                   sizeof(void *) * 2 + sizeof(void *),
465ffd83dbSDimitry Andric               "1 byte elements have word-sized type for size and capacity");
475ffd83dbSDimitry Andric 
48*af732203SDimitry Andric /// Report that MinSize doesn't fit into this vector's size type. Throws
49*af732203SDimitry Andric /// std::length_error or calls report_fatal_error.
50*af732203SDimitry Andric LLVM_ATTRIBUTE_NORETURN
51*af732203SDimitry Andric static void report_size_overflow(size_t MinSize, size_t MaxSize);
report_size_overflow(size_t MinSize,size_t MaxSize)52*af732203SDimitry Andric static void report_size_overflow(size_t MinSize, size_t MaxSize) {
53*af732203SDimitry Andric   std::string Reason = "SmallVector unable to grow. Requested capacity (" +
54*af732203SDimitry Andric                        std::to_string(MinSize) +
55*af732203SDimitry Andric                        ") is larger than maximum value for size type (" +
56*af732203SDimitry Andric                        std::to_string(MaxSize) + ")";
57*af732203SDimitry Andric #ifdef LLVM_ENABLE_EXCEPTIONS
58*af732203SDimitry Andric   throw std::length_error(Reason);
59*af732203SDimitry Andric #else
60*af732203SDimitry Andric   report_fatal_error(Reason);
61*af732203SDimitry Andric #endif
62*af732203SDimitry Andric }
63*af732203SDimitry Andric 
64*af732203SDimitry Andric /// Report that this vector is already at maximum capacity. Throws
65*af732203SDimitry Andric /// std::length_error or calls report_fatal_error.
66*af732203SDimitry Andric LLVM_ATTRIBUTE_NORETURN static void report_at_maximum_capacity(size_t MaxSize);
report_at_maximum_capacity(size_t MaxSize)67*af732203SDimitry Andric static void report_at_maximum_capacity(size_t MaxSize) {
68*af732203SDimitry Andric   std::string Reason =
69*af732203SDimitry Andric       "SmallVector capacity unable to grow. Already at maximum size " +
70*af732203SDimitry Andric       std::to_string(MaxSize);
71*af732203SDimitry Andric #ifdef LLVM_ENABLE_EXCEPTIONS
72*af732203SDimitry Andric   throw std::length_error(Reason);
73*af732203SDimitry Andric #else
74*af732203SDimitry Andric   report_fatal_error(Reason);
75*af732203SDimitry Andric #endif
76*af732203SDimitry Andric }
77*af732203SDimitry Andric 
785ffd83dbSDimitry Andric // Note: Moving this function into the header may cause performance regression.
795ffd83dbSDimitry Andric template <class Size_T>
getNewCapacity(size_t MinSize,size_t TSize,size_t OldCapacity)80*af732203SDimitry Andric static size_t getNewCapacity(size_t MinSize, size_t TSize, size_t OldCapacity) {
81*af732203SDimitry Andric   constexpr size_t MaxSize = std::numeric_limits<Size_T>::max();
82*af732203SDimitry Andric 
835ffd83dbSDimitry Andric   // Ensure we can fit the new capacity.
845ffd83dbSDimitry Andric   // This is only going to be applicable when the capacity is 32 bit.
85*af732203SDimitry Andric   if (MinSize > MaxSize)
86*af732203SDimitry Andric     report_size_overflow(MinSize, MaxSize);
870b57cec5SDimitry Andric 
885ffd83dbSDimitry Andric   // Ensure we can meet the guarantee of space for at least one more element.
895ffd83dbSDimitry Andric   // The above check alone will not catch the case where grow is called with a
90*af732203SDimitry Andric   // default MinSize of 0, but the current capacity cannot be increased.
915ffd83dbSDimitry Andric   // This is only going to be applicable when the capacity is 32 bit.
92*af732203SDimitry Andric   if (OldCapacity == MaxSize)
93*af732203SDimitry Andric     report_at_maximum_capacity(MaxSize);
945ffd83dbSDimitry Andric 
955ffd83dbSDimitry Andric   // In theory 2*capacity can overflow if the capacity is 64 bit, but the
965ffd83dbSDimitry Andric   // original capacity would never be large enough for this to be a problem.
97*af732203SDimitry Andric   size_t NewCapacity = 2 * OldCapacity + 1; // Always grow.
98*af732203SDimitry Andric   return std::min(std::max(NewCapacity, MinSize), MaxSize);
99*af732203SDimitry Andric }
1000b57cec5SDimitry Andric 
101*af732203SDimitry Andric // Note: Moving this function into the header may cause performance regression.
102*af732203SDimitry Andric template <class Size_T>
mallocForGrow(size_t MinSize,size_t TSize,size_t & NewCapacity)103*af732203SDimitry Andric void *SmallVectorBase<Size_T>::mallocForGrow(size_t MinSize, size_t TSize,
104*af732203SDimitry Andric                                              size_t &NewCapacity) {
105*af732203SDimitry Andric   NewCapacity = getNewCapacity<Size_T>(MinSize, TSize, this->capacity());
106*af732203SDimitry Andric   return llvm::safe_malloc(NewCapacity * TSize);
107*af732203SDimitry Andric }
108*af732203SDimitry Andric 
109*af732203SDimitry Andric // Note: Moving this function into the header may cause performance regression.
110*af732203SDimitry Andric template <class Size_T>
grow_pod(void * FirstEl,size_t MinSize,size_t TSize)111*af732203SDimitry Andric void SmallVectorBase<Size_T>::grow_pod(void *FirstEl, size_t MinSize,
112*af732203SDimitry Andric                                        size_t TSize) {
113*af732203SDimitry Andric   size_t NewCapacity = getNewCapacity<Size_T>(MinSize, TSize, this->capacity());
1140b57cec5SDimitry Andric   void *NewElts;
1150b57cec5SDimitry Andric   if (BeginX == FirstEl) {
1160b57cec5SDimitry Andric     NewElts = safe_malloc(NewCapacity * TSize);
1170b57cec5SDimitry Andric 
1180b57cec5SDimitry Andric     // Copy the elements over.  No need to run dtors on PODs.
1190b57cec5SDimitry Andric     memcpy(NewElts, this->BeginX, size() * TSize);
1200b57cec5SDimitry Andric   } else {
1210b57cec5SDimitry Andric     // If this wasn't grown from the inline copy, grow the allocated space.
1220b57cec5SDimitry Andric     NewElts = safe_realloc(this->BeginX, NewCapacity * TSize);
1230b57cec5SDimitry Andric   }
1240b57cec5SDimitry Andric 
1250b57cec5SDimitry Andric   this->BeginX = NewElts;
1260b57cec5SDimitry Andric   this->Capacity = NewCapacity;
1270b57cec5SDimitry Andric }
1285ffd83dbSDimitry Andric 
1295ffd83dbSDimitry Andric template class llvm::SmallVectorBase<uint32_t>;
1305ffd83dbSDimitry Andric 
1315ffd83dbSDimitry Andric // Disable the uint64_t instantiation for 32-bit builds.
132*af732203SDimitry Andric // Both uint32_t and uint64_t instantiations are needed for 64-bit builds.
1335ffd83dbSDimitry Andric // This instantiation will never be used in 32-bit builds, and will cause
1345ffd83dbSDimitry Andric // warnings when sizeof(Size_T) > sizeof(size_t).
1355ffd83dbSDimitry Andric #if SIZE_MAX > UINT32_MAX
1365ffd83dbSDimitry Andric template class llvm::SmallVectorBase<uint64_t>;
1375ffd83dbSDimitry Andric 
1385ffd83dbSDimitry Andric // Assertions to ensure this #if stays in sync with SmallVectorSizeType.
1395ffd83dbSDimitry Andric static_assert(sizeof(SmallVectorSizeType<char>) == sizeof(uint64_t),
1405ffd83dbSDimitry Andric               "Expected SmallVectorBase<uint64_t> variant to be in use.");
1415ffd83dbSDimitry Andric #else
1425ffd83dbSDimitry Andric static_assert(sizeof(SmallVectorSizeType<char>) == sizeof(uint32_t),
1435ffd83dbSDimitry Andric               "Expected SmallVectorBase<uint32_t> variant to be in use.");
1445ffd83dbSDimitry Andric #endif
145