Chris Lattner | 4ea86c4 | 2009-12-16 08:44:24 +0000 | [diff] [blame] | 1 | //===- 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" |
| 15 | using namespace llvm; |
| 16 | |
Duncan P. N. Exon Smith | ab55bc4 | 2018-07-24 11:32:13 +0000 | [diff] [blame] | 17 | // Check that no bytes are wasted and everything is well-aligned. |
| 18 | namespace { |
| 19 | struct Struct16B { |
| 20 | alignas(16) void *X; |
| 21 | }; |
| 22 | struct Struct32B { |
| 23 | alignas(32) void *X; |
| 24 | }; |
| 25 | } |
| 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"); |
Duncan P. N. Exon Smith | c03b04d | 2018-07-20 00:44:58 +0000 | [diff] [blame] | 37 | static_assert(sizeof(SmallVector<void *, 1>) == |
| 38 | sizeof(unsigned) * 2 + sizeof(void *) * 2, |
Duncan P. N. Exon Smith | ab55bc4 | 2018-07-24 11:32:13 +0000 | [diff] [blame] | 39 | "wasted space in SmallVector size 1"); |
Duncan P. N. Exon Smith | f4c82cf | 2018-06-23 18:39:44 +0000 | [diff] [blame] | 40 | |
Chris Lattner | 4ea86c4 | 2009-12-16 08:44:24 +0000 | [diff] [blame] | 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. |
Duncan P. N. Exon Smith | c03b04d | 2018-07-20 00:44:58 +0000 | [diff] [blame] | 43 | void SmallVectorBase::grow_pod(void *FirstEl, size_t MinCapacity, |
Richard Smith | 24f09cc | 2012-08-22 00:11:07 +0000 | [diff] [blame] | 44 | size_t TSize) { |
Duncan P. N. Exon Smith | c03b04d | 2018-07-20 00:44:58 +0000 | [diff] [blame] | 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"); |
| 48 | |
| 49 | size_t NewCapacity = 2 * capacity() + 1; // Always grow. |
| 50 | NewCapacity = |
| 51 | std::min(std::max(NewCapacity, MinCapacity), size_t(UINT32_MAX)); |
Benjamin Kramer | 4e36e5b | 2010-06-08 11:44:30 +0000 | [diff] [blame] | 52 | |
| 53 | void *NewElts; |
Richard Smith | 24f09cc | 2012-08-22 00:11:07 +0000 | [diff] [blame] | 54 | if (BeginX == FirstEl) { |
Duncan P. N. Exon Smith | c03b04d | 2018-07-20 00:44:58 +0000 | [diff] [blame] | 55 | NewElts = safe_malloc(NewCapacity * TSize); |
Benjamin Kramer | 4e36e5b | 2010-06-08 11:44:30 +0000 | [diff] [blame] | 56 | |
| 57 | // Copy the elements over. No need to run dtors on PODs. |
Duncan P. N. Exon Smith | c03b04d | 2018-07-20 00:44:58 +0000 | [diff] [blame] | 58 | memcpy(NewElts, this->BeginX, size() * TSize); |
Benjamin Kramer | 4e36e5b | 2010-06-08 11:44:30 +0000 | [diff] [blame] | 59 | } else { |
| 60 | // If this wasn't grown from the inline copy, grow the allocated space. |
Duncan P. N. Exon Smith | c03b04d | 2018-07-20 00:44:58 +0000 | [diff] [blame] | 61 | NewElts = safe_realloc(this->BeginX, NewCapacity * TSize); |
Benjamin Kramer | 4e36e5b | 2010-06-08 11:44:30 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Chris Lattner | 4ea86c4 | 2009-12-16 08:44:24 +0000 | [diff] [blame] | 64 | this->BeginX = NewElts; |
Duncan P. N. Exon Smith | c03b04d | 2018-07-20 00:44:58 +0000 | [diff] [blame] | 65 | this->Capacity = NewCapacity; |
Chris Lattner | 4ea86c4 | 2009-12-16 08:44:24 +0000 | [diff] [blame] | 66 | } |