1 //===- llvm/ADT/SmallVector.cpp - 'Normally small' vectors ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the SmallVector class.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ADT/SmallVector.h"
17 // Check that no bytes are wasted and everything is well-aligned.
26 static_assert(sizeof(SmallVector
<void *, 0>) ==
27 sizeof(unsigned) * 2 + sizeof(void *),
28 "wasted space in SmallVector size 0");
29 static_assert(alignof(SmallVector
<Struct16B
, 0>) >= alignof(Struct16B
),
30 "wrong alignment for 16-byte aligned T");
31 static_assert(alignof(SmallVector
<Struct32B
, 0>) >= alignof(Struct32B
),
32 "wrong alignment for 32-byte aligned T");
33 static_assert(sizeof(SmallVector
<Struct16B
, 0>) >= alignof(Struct16B
),
34 "missing padding for 16-byte aligned T");
35 static_assert(sizeof(SmallVector
<Struct32B
, 0>) >= alignof(Struct32B
),
36 "missing padding for 32-byte aligned T");
37 static_assert(sizeof(SmallVector
<void *, 1>) ==
38 sizeof(unsigned) * 2 + sizeof(void *) * 2,
39 "wasted space in SmallVector size 1");
41 /// grow_pod - This is an implementation of the grow() method which only works
42 /// on POD-like datatypes and is out of line to reduce code duplication.
43 void SmallVectorBase::grow_pod(void *FirstEl
, size_t MinCapacity
,
45 // Ensure we can fit the new capacity in 32 bits.
46 if (MinCapacity
> UINT32_MAX
)
47 report_bad_alloc_error("SmallVector capacity overflow during allocation");
49 size_t NewCapacity
= 2 * capacity() + 1; // Always grow.
51 std::min(std::max(NewCapacity
, MinCapacity
), size_t(UINT32_MAX
));
54 if (BeginX
== FirstEl
) {
55 NewElts
= safe_malloc(NewCapacity
* TSize
);
57 // Copy the elements over. No need to run dtors on PODs.
58 memcpy(NewElts
, this->BeginX
, size() * TSize
);
60 // If this wasn't grown from the inline copy, grow the allocated space.
61 NewElts
= safe_realloc(this->BeginX
, NewCapacity
* TSize
);
64 this->BeginX
= NewElts
;
65 this->Capacity
= NewCapacity
;