LLVM  9.0.0svn
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 using namespace llvm;
15 
16 // Check that no bytes are wasted and everything is well-aligned.
17 namespace {
18 struct Struct16B {
19  alignas(16) void *X;
20 };
21 struct Struct32B {
22  alignas(32) void *X;
23 };
24 }
25 static_assert(sizeof(SmallVector<void *, 0>) ==
26  sizeof(unsigned) * 2 + sizeof(void *),
27  "wasted space in SmallVector size 0");
28 static_assert(alignof(SmallVector<Struct16B, 0>) >= alignof(Struct16B),
29  "wrong alignment for 16-byte aligned T");
30 static_assert(alignof(SmallVector<Struct32B, 0>) >= alignof(Struct32B),
31  "wrong alignment for 32-byte aligned T");
32 static_assert(sizeof(SmallVector<Struct16B, 0>) >= alignof(Struct16B),
33  "missing padding for 16-byte aligned T");
34 static_assert(sizeof(SmallVector<Struct32B, 0>) >= alignof(Struct32B),
35  "missing padding for 32-byte aligned T");
36 static_assert(sizeof(SmallVector<void *, 1>) ==
37  sizeof(unsigned) * 2 + sizeof(void *) * 2,
38  "wasted space in SmallVector size 1");
39 
40 /// grow_pod - This is an implementation of the grow() method which only works
41 /// on POD-like datatypes and is out of line to reduce code duplication.
42 void SmallVectorBase::grow_pod(void *FirstEl, size_t MinCapacity,
43  size_t TSize) {
44  // Ensure we can fit the new capacity in 32 bits.
45  if (MinCapacity > UINT32_MAX)
46  report_bad_alloc_error("SmallVector capacity overflow during allocation");
47 
48  size_t NewCapacity = 2 * capacity() + 1; // Always grow.
49  NewCapacity =
50  std::min(std::max(NewCapacity, MinCapacity), size_t(UINT32_MAX));
51 
52  void *NewElts;
53  if (BeginX == FirstEl) {
54  NewElts = safe_malloc(NewCapacity * TSize);
55 
56  // Copy the elements over. No need to run dtors on PODs.
57  memcpy(NewElts, this->BeginX, size() * TSize);
58  } else {
59  // If this wasn't grown from the inline copy, grow the allocated space.
60  NewElts = safe_realloc(this->BeginX, NewCapacity * TSize);
61  }
62 
63  this->BeginX = NewElts;
64  this->Capacity = NewCapacity;
65 }
void grow_pod(void *FirstEl, size_t MinCapacity, 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:42
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
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:40
void report_bad_alloc_error(const char *Reason, bool GenCrashDiag=true)
Reports a bad alloc error, calling any user defined bad alloc error handler.
LLVM_ATTRIBUTE_RETURNS_NONNULL void * safe_malloc(size_t Sz)
Definition: MemAlloc.h:25
auto size(R &&Range, typename std::enable_if< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr) -> decltype(std::distance(Range.begin(), Range.end()))
Get the size of a range.
Definition: STLExtras.h:1166
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:841