LLVM  12.0.0git
SmallVector.cpp
Go to the documentation of this file.
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 template <class Size_T>
50  std::string Reason = "SmallVector unable to grow. Requested capacity (" +
51  std::to_string(MinSize) +
52  ") is larger than maximum value for size type (" +
53  std::to_string(SizeTypeMax()) + ")";
54 #ifdef LLVM_ENABLE_EXCEPTIONS
55  throw std::length_error(Reason);
56 #else
57  report_fatal_error(Reason);
58 #endif
59 }
60 
62  std::string Reason =
63  "SmallVector capacity unable to grow. Already at maximum size " +
64  std::to_string(SizeTypeMax());
65 #ifdef LLVM_ENABLE_EXCEPTIONS
66  throw std::length_error(Reason);
67 #else
68  report_fatal_error(Reason);
69 #endif
70 }
71 
72 // Note: Moving this function into the header may cause performance regression.
73 template <class Size_T>
74 void SmallVectorBase<Size_T>::grow_pod(void *FirstEl, size_t MinSize,
75  size_t TSize) {
76  // Ensure we can fit the new capacity.
77  // This is only going to be applicable when the capacity is 32 bit.
78  if (MinSize > SizeTypeMax())
79  report_size_overflow(MinSize);
80 
81  // Ensure we can meet the guarantee of space for at least one more element.
82  // The above check alone will not catch the case where grow is called with a
83  // default MinSize of 0, but the current capacity cannot be increased.
84  // This is only going to be applicable when the capacity is 32 bit.
85  if (capacity() == SizeTypeMax())
86  report_at_maximum_capacity();
87 
88  // In theory 2*capacity can overflow if the capacity is 64 bit, but the
89  // original capacity would never be large enough for this to be a problem.
90  size_t NewCapacity = 2 * capacity() + 1; // Always grow.
91  NewCapacity = std::min(std::max(NewCapacity, MinSize), SizeTypeMax());
92 
93  void *NewElts;
94  if (BeginX == FirstEl) {
95  NewElts = safe_malloc(NewCapacity * TSize);
96 
97  // Copy the elements over. No need to run dtors on PODs.
98  memcpy(NewElts, this->BeginX, size() * TSize);
99  } else {
100  // If this wasn't grown from the inline copy, grow the allocated space.
101  NewElts = safe_realloc(this->BeginX, NewCapacity * TSize);
102  }
103 
104  this->BeginX = NewElts;
105  this->Capacity = NewCapacity;
106 }
107 
108 template class llvm::SmallVectorBase<uint32_t>;
109 
110 // Disable the uint64_t instantiation for 32-bit builds.
111 // Both uint32_t and uint64_t instantiations are needed for 64-bit builds.
112 // This instantiation will never be used in 32-bit builds, and will cause
113 // warnings when sizeof(Size_T) > sizeof(size_t).
114 #if SIZE_MAX > UINT32_MAX
115 template class llvm::SmallVectorBase<uint64_t>;
116 
117 // Assertions to ensure this #if stays in sync with SmallVectorSizeType.
118 static_assert(sizeof(SmallVectorSizeType<char>) == sizeof(uint64_t),
119  "Expected SmallVectorBase<uint64_t> variant to be in use.");
120 #else
121 static_assert(sizeof(SmallVectorSizeType<char>) == sizeof(uint32_t),
122  "Expected SmallVectorBase<uint32_t> variant to be in use.");
123 #endif
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
static LLVM_ATTRIBUTE_NORETURN void report_at_maximum_capacity()
Report that this vector is already at maximum capacity.
Definition: SmallVector.cpp:61
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:140
This class represents lattice values for constants.
Definition: AllocatorList.h:23
LLVM_ATTRIBUTE_RETURNS_NONNULL void * safe_realloc(void *Ptr, size_t Sz)
Definition: MemAlloc.h:52
Expected< ExpressionValue > min(const ExpressionValue &Lhs, const ExpressionValue &Rhs)
Definition: FileCheck.cpp:339
void grow_pod(void *FirstEl, size_t MinSize, size_t TSize)
This is an implementation of the grow() method which only works on POD-like data types and is out of ...
Definition: SmallVector.cpp:74
LLVM_ATTRIBUTE_RETURNS_NONNULL void * safe_malloc(size_t Sz)
Definition: MemAlloc.h:25
Align max(MaybeAlign Lhs, Align Rhs)
Definition: Alignment.h:350
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1116
This is all the stuff common to all SmallVectors.
Definition: SmallVector.h:45
const std::string to_string(const T &Value)
Definition: ScopedPrinter.h:61
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition: STLExtras.h:1479
typename std::conditional< sizeof(T)< 4 &&sizeof(void *) >=8, uint64_t, uint32_t >::type SmallVectorSizeType
Definition: SmallVector.h:95
static LLVM_ATTRIBUTE_NORETURN void report_size_overflow(size_t MinSize)
Report that MinSize doesn't fit into this vector's size type.
Definition: SmallVector.cpp:49