blob: 36f0a81f6b00deb0c3fa8b3e816e95dc7ddda95b [file] [log] [blame]
Chris Lattner4ea86c42009-12-16 08:44:24 +00001//===- llvm/ADT/SmallVector.cpp - 'Normally small' vectors ----------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Chris Lattner4ea86c42009-12-16 08:44:24 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the SmallVector class.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/ADT/SmallVector.h"
14using namespace llvm;
15
Duncan P. N. Exon Smithab55bc42018-07-24 11:32:13 +000016// Check that no bytes are wasted and everything is well-aligned.
17namespace {
18struct Struct16B {
19 alignas(16) void *X;
20};
21struct Struct32B {
22 alignas(32) void *X;
23};
24}
25static_assert(sizeof(SmallVector<void *, 0>) ==
26 sizeof(unsigned) * 2 + sizeof(void *),
27 "wasted space in SmallVector size 0");
28static_assert(alignof(SmallVector<Struct16B, 0>) >= alignof(Struct16B),
29 "wrong alignment for 16-byte aligned T");
30static_assert(alignof(SmallVector<Struct32B, 0>) >= alignof(Struct32B),
31 "wrong alignment for 32-byte aligned T");
32static_assert(sizeof(SmallVector<Struct16B, 0>) >= alignof(Struct16B),
33 "missing padding for 16-byte aligned T");
34static_assert(sizeof(SmallVector<Struct32B, 0>) >= alignof(Struct32B),
35 "missing padding for 32-byte aligned T");
Duncan P. N. Exon Smithc03b04d2018-07-20 00:44:58 +000036static_assert(sizeof(SmallVector<void *, 1>) ==
37 sizeof(unsigned) * 2 + sizeof(void *) * 2,
Duncan P. N. Exon Smithab55bc42018-07-24 11:32:13 +000038 "wasted space in SmallVector size 1");
Duncan P. N. Exon Smithf4c82cf2018-06-23 18:39:44 +000039
Chris Lattner4ea86c42009-12-16 08:44:24 +000040/// 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.
Duncan P. N. Exon Smithc03b04d2018-07-20 00:44:58 +000042void SmallVectorBase::grow_pod(void *FirstEl, size_t MinCapacity,
Richard Smith24f09cc2012-08-22 00:11:07 +000043 size_t TSize) {
Duncan P. N. Exon Smithc03b04d2018-07-20 00:44:58 +000044 // 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));
Benjamin Kramer4e36e5b2010-06-08 11:44:30 +000051
52 void *NewElts;
Richard Smith24f09cc2012-08-22 00:11:07 +000053 if (BeginX == FirstEl) {
Duncan P. N. Exon Smithc03b04d2018-07-20 00:44:58 +000054 NewElts = safe_malloc(NewCapacity * TSize);
Benjamin Kramer4e36e5b2010-06-08 11:44:30 +000055
56 // Copy the elements over. No need to run dtors on PODs.
Duncan P. N. Exon Smithc03b04d2018-07-20 00:44:58 +000057 memcpy(NewElts, this->BeginX, size() * TSize);
Benjamin Kramer4e36e5b2010-06-08 11:44:30 +000058 } else {
59 // If this wasn't grown from the inline copy, grow the allocated space.
Duncan P. N. Exon Smithc03b04d2018-07-20 00:44:58 +000060 NewElts = safe_realloc(this->BeginX, NewCapacity * TSize);
Benjamin Kramer4e36e5b2010-06-08 11:44:30 +000061 }
62
Chris Lattner4ea86c42009-12-16 08:44:24 +000063 this->BeginX = NewElts;
Duncan P. N. Exon Smithc03b04d2018-07-20 00:44:58 +000064 this->Capacity = NewCapacity;
Chris Lattner4ea86c42009-12-16 08:44:24 +000065}