blob: 1070c6672edcec422476b15a0837a1cdf6bc157c [file] [log] [blame]
Chris Lattner4ea86c42009-12-16 08:44:24 +00001//===- llvm/ADT/SmallVector.cpp - 'Normally small' vectors ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the SmallVector class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/ADT/SmallVector.h"
15using namespace llvm;
16
Duncan P. N. Exon Smithab55bc42018-07-24 11:32:13 +000017// Check that no bytes are wasted and everything is well-aligned.
18namespace {
19struct Struct16B {
20 alignas(16) void *X;
21};
22struct Struct32B {
23 alignas(32) void *X;
24};
25}
26static_assert(sizeof(SmallVector<void *, 0>) ==
27 sizeof(unsigned) * 2 + sizeof(void *),
28 "wasted space in SmallVector size 0");
29static_assert(alignof(SmallVector<Struct16B, 0>) >= alignof(Struct16B),
30 "wrong alignment for 16-byte aligned T");
31static_assert(alignof(SmallVector<Struct32B, 0>) >= alignof(Struct32B),
32 "wrong alignment for 32-byte aligned T");
33static_assert(sizeof(SmallVector<Struct16B, 0>) >= alignof(Struct16B),
34 "missing padding for 16-byte aligned T");
35static_assert(sizeof(SmallVector<Struct32B, 0>) >= alignof(Struct32B),
36 "missing padding for 32-byte aligned T");
Duncan P. N. Exon Smithc03b04d2018-07-20 00:44:58 +000037static_assert(sizeof(SmallVector<void *, 1>) ==
38 sizeof(unsigned) * 2 + sizeof(void *) * 2,
Duncan P. N. Exon Smithab55bc42018-07-24 11:32:13 +000039 "wasted space in SmallVector size 1");
Duncan P. N. Exon Smithf4c82cf2018-06-23 18:39:44 +000040
Chris Lattner4ea86c42009-12-16 08:44:24 +000041/// 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.
Duncan P. N. Exon Smithc03b04d2018-07-20 00:44:58 +000043void SmallVectorBase::grow_pod(void *FirstEl, size_t MinCapacity,
Richard Smith24f09cc2012-08-22 00:11:07 +000044 size_t TSize) {
Duncan P. N. Exon Smithc03b04d2018-07-20 00:44:58 +000045 // 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");
48
49 size_t NewCapacity = 2 * capacity() + 1; // Always grow.
50 NewCapacity =
51 std::min(std::max(NewCapacity, MinCapacity), size_t(UINT32_MAX));
Benjamin Kramer4e36e5b2010-06-08 11:44:30 +000052
53 void *NewElts;
Richard Smith24f09cc2012-08-22 00:11:07 +000054 if (BeginX == FirstEl) {
Duncan P. N. Exon Smithc03b04d2018-07-20 00:44:58 +000055 NewElts = safe_malloc(NewCapacity * TSize);
Benjamin Kramer4e36e5b2010-06-08 11:44:30 +000056
57 // Copy the elements over. No need to run dtors on PODs.
Duncan P. N. Exon Smithc03b04d2018-07-20 00:44:58 +000058 memcpy(NewElts, this->BeginX, size() * TSize);
Benjamin Kramer4e36e5b2010-06-08 11:44:30 +000059 } else {
60 // If this wasn't grown from the inline copy, grow the allocated space.
Duncan P. N. Exon Smithc03b04d2018-07-20 00:44:58 +000061 NewElts = safe_realloc(this->BeginX, NewCapacity * TSize);
Benjamin Kramer4e36e5b2010-06-08 11:44:30 +000062 }
63
Chris Lattner4ea86c42009-12-16 08:44:24 +000064 this->BeginX = NewElts;
Duncan P. N. Exon Smithc03b04d2018-07-20 00:44:58 +000065 this->Capacity = NewCapacity;
Chris Lattner4ea86c42009-12-16 08:44:24 +000066}