Movatterモバイル変換


[0]ホーム

URL:


LLVM 20.0.0git
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#include "llvm/ADT/Twine.h"
15#include "llvm/Support/MemAlloc.h"
16#include <cstdint>
17#ifdef LLVM_ENABLE_EXCEPTIONS
18#include <stdexcept>
19#endif
20using namespacellvm;
21
22// Check that no bytes are wasted and everything is well-aligned.
23namespace{
24// These structures may cause binary compat warnings on AIX. Suppress the
25// warning since we are only using these types for the static assertions below.
26#if defined(_AIX)
27#pragma GCC diagnostic push
28#pragma GCC diagnostic ignored "-Waix-compat"
29#endif
30structStruct16B {
31alignas(16)void *X;
32};
33structStruct32B {
34alignas(32)void *X;
35};
36#if defined(_AIX)
37#pragma GCC diagnostic pop
38#endif
39}
40static_assert(sizeof(SmallVector<void *, 0>) ==
41sizeof(unsigned) * 2 +sizeof(void *),
42"wasted space in SmallVector size 0");
43static_assert(alignof(SmallVector<Struct16B, 0>) >=alignof(Struct16B),
44"wrong alignment for 16-byte aligned T");
45static_assert(alignof(SmallVector<Struct32B, 0>) >=alignof(Struct32B),
46"wrong alignment for 32-byte aligned T");
47static_assert(sizeof(SmallVector<Struct16B, 0>) >=alignof(Struct16B),
48"missing padding for 16-byte aligned T");
49static_assert(sizeof(SmallVector<Struct32B, 0>) >=alignof(Struct32B),
50"missing padding for 32-byte aligned T");
51static_assert(sizeof(SmallVector<void *, 1>) ==
52sizeof(unsigned) * 2 +sizeof(void *) * 2,
53"wasted space in SmallVector size 1");
54
55static_assert(sizeof(SmallVector<char, 0>) ==
56sizeof(void *) * 2 +sizeof(void *),
57"1 byte elements have word-sized type for size and capacity");
58
59/// Report that MinSize doesn't fit into this vector's size type. Throws
60/// std::length_error or calls report_fatal_error.
61[[noreturn]]staticvoidreport_size_overflow(size_t MinSize,size_t MaxSize);
62staticvoidreport_size_overflow(size_t MinSize,size_t MaxSize) {
63 std::string Reason ="SmallVector unable to grow. Requested capacity (" +
64 std::to_string(MinSize) +
65") is larger than maximum value for size type (" +
66 std::to_string(MaxSize) +")";
67#ifdef LLVM_ENABLE_EXCEPTIONS
68throw std::length_error(Reason);
69#else
70report_fatal_error(Twine(Reason));
71#endif
72}
73
74/// Report that this vector is already at maximum capacity. Throws
75/// std::length_error or calls report_fatal_error.
76[[noreturn]]staticvoidreport_at_maximum_capacity(size_t MaxSize);
77staticvoidreport_at_maximum_capacity(size_t MaxSize) {
78 std::string Reason =
79"SmallVector capacity unable to grow. Already at maximum size " +
80 std::to_string(MaxSize);
81#ifdef LLVM_ENABLE_EXCEPTIONS
82throw std::length_error(Reason);
83#else
84report_fatal_error(Twine(Reason));
85#endif
86}
87
88// Note: Moving this function into the header may cause performance regression.
89template <class Size_T>
90staticsize_tgetNewCapacity(size_t MinSize,size_t TSize,size_t OldCapacity) {
91constexprsize_t MaxSize = std::numeric_limits<Size_T>::max();
92
93// Ensure we can fit the new capacity.
94// This is only going to be applicable when the capacity is 32 bit.
95if (MinSize > MaxSize)
96report_size_overflow(MinSize, MaxSize);
97
98// Ensure we can meet the guarantee of space for at least one more element.
99// The above check alone will not catch the case where grow is called with a
100// default MinSize of 0, but the current capacity cannot be increased.
101// This is only going to be applicable when the capacity is 32 bit.
102if (OldCapacity == MaxSize)
103report_at_maximum_capacity(MaxSize);
104
105// In theory 2*capacity can overflow if the capacity is 64 bit, but the
106// original capacity would never be large enough for this to be a problem.
107size_t NewCapacity = 2 * OldCapacity + 1;// Always grow.
108return std::clamp(NewCapacity, MinSize, MaxSize);
109}
110
111/// If vector was first created with capacity 0, getFirstEl() points to the
112/// memory right after, an area unallocated. If a subsequent allocation,
113/// that grows the vector, happens to return the same pointer as getFirstEl(),
114/// get a new allocation, otherwise isSmall() will falsely return that no
115/// allocation was done (true) and the memory will not be freed in the
116/// destructor. If a VSize is given (vector size), also copy that many
117/// elements to the new allocation - used if realloca fails to increase
118/// space, and happens to allocate precisely at BeginX.
119/// This is unlikely to be called often, but resolves a memory leak when the
120/// situation does occur.
121staticvoid *replaceAllocation(void *NewElts,size_t TSize,size_t NewCapacity,
122size_t VSize = 0) {
123void *NewEltsReplace =llvm::safe_malloc(NewCapacity * TSize);
124if (VSize)
125 memcpy(NewEltsReplace, NewElts, VSize * TSize);
126 free(NewElts);
127return NewEltsReplace;
128}
129
130// Note: Moving this function into the header may cause performance regression.
131template <class Size_T>
132void *SmallVectorBase<Size_T>::mallocForGrow(void *FirstEl,size_t MinSize,
133size_t TSize,
134size_t &NewCapacity) {
135 NewCapacity = getNewCapacity<Size_T>(MinSize, TSize, this->capacity());
136// Even if capacity is not 0 now, if the vector was originally created with
137// capacity 0, it's possible for the malloc to return FirstEl.
138void *NewElts =llvm::safe_malloc(NewCapacity * TSize);
139if (NewElts == FirstEl)
140 NewElts =replaceAllocation(NewElts, TSize, NewCapacity);
141return NewElts;
142}
143
144// Note: Moving this function into the header may cause performance regression.
145template <class Size_T>
146voidSmallVectorBase<Size_T>::grow_pod(void *FirstEl,size_t MinSize,
147size_t TSize) {
148size_t NewCapacity = getNewCapacity<Size_T>(MinSize, TSize, this->capacity());
149void *NewElts;
150if (BeginX == FirstEl) {
151 NewElts =llvm::safe_malloc(NewCapacity * TSize);
152if (NewElts == FirstEl)
153 NewElts =replaceAllocation(NewElts, TSize, NewCapacity);
154
155// Copy the elements over. No need to run dtors on PODs.
156 memcpy(NewElts, this->BeginX,size() * TSize);
157 }else {
158// If this wasn't grown from the inline copy, grow the allocated space.
159 NewElts =llvm::safe_realloc(this->BeginX, NewCapacity * TSize);
160if (NewElts == FirstEl)
161 NewElts =replaceAllocation(NewElts, TSize, NewCapacity,size());
162 }
163
164 this->set_allocation_range(NewElts, NewCapacity);
165}
166
167templateclassllvm::SmallVectorBase<uint32_t>;
168
169// Disable the uint64_t instantiation for 32-bit builds.
170// Both uint32_t and uint64_t instantiations are needed for 64-bit builds.
171// This instantiation will never be used in 32-bit builds, and will cause
172// warnings when sizeof(Size_T) > sizeof(size_t).
173#if SIZE_MAX > UINT32_MAX
174templateclassllvm::SmallVectorBase<uint64_t>;
175
176// Assertions to ensure this #if stays in sync with SmallVectorSizeType.
177static_assert(sizeof(SmallVectorSizeType<char>) ==sizeof(uint64_t),
178"Expected SmallVectorBase<uint64_t> variant to be in use.");
179#else
180static_assert(sizeof(SmallVectorSizeType<char>) ==sizeof(uint32_t),
181"Expected SmallVectorBase<uint32_t> variant to be in use.");
182#endif
MemAlloc.h
This file defines counterparts of C library allocation functions defined in the namespace 'std'.
report_size_overflow
static void report_size_overflow(size_t MinSize, size_t MaxSize)
Report that MinSize doesn't fit into this vector's size type.
Definition:SmallVector.cpp:62
getNewCapacity
static size_t getNewCapacity(size_t MinSize, size_t TSize, size_t OldCapacity)
Definition:SmallVector.cpp:90
report_at_maximum_capacity
static void report_at_maximum_capacity(size_t MaxSize)
Report that this vector is already at maximum capacity.
Definition:SmallVector.cpp:77
replaceAllocation
static void * replaceAllocation(void *NewElts, size_t TSize, size_t NewCapacity, size_t VSize=0)
If vector was first created with capacity 0, getFirstEl() points to the memory right after,...
Definition:SmallVector.cpp:121
SmallVector.h
This file defines the SmallVector class.
Twine.h
llvm::SmallVectorBase
This is all the stuff common to all SmallVectors.
Definition:SmallVector.h:52
llvm::SmallVectorBase::grow_pod
void grow_pod(void *FirstEl, size_t MinSize, 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:146
llvm::SmallVectorBase::mallocForGrow
void * mallocForGrow(void *FirstEl, size_t MinSize, size_t TSize, size_t &NewCapacity)
This is a helper for grow() that's out of line to reduce code duplication.
Definition:SmallVector.cpp:132
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition:SmallVector.h:1196
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition:Twine.h:81
uint32_t
uint64_t
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition:AddressRanges.h:18
llvm::size
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition:STLExtras.h:1697
llvm::SmallVectorSizeType
std::conditional_t< sizeof(T)< 4 &&sizeof(void *) >=8, uint64_t, uint32_t > SmallVectorSizeType
Definition:SmallVector.h:107
llvm::report_fatal_error
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition:Error.cpp:167
llvm::safe_malloc
LLVM_ATTRIBUTE_RETURNS_NONNULL void * safe_malloc(size_t Sz)
Definition:MemAlloc.h:25
llvm::safe_realloc
LLVM_ATTRIBUTE_RETURNS_NONNULL void * safe_realloc(void *Ptr, size_t Sz)
Definition:MemAlloc.h:52

Generated on Thu Jul 17 2025 13:50:42 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp