ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
SmallVector.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 //
8 // Adapted for CloudViewer.
9 // Commit 75e164f61d391979b4829bf2746a5d74b94e95f2 2022-01-21
10 // Documentation:
11 // https://llvm.org/docs/ProgrammersManual.html#llvm-adt-smallvector-h
12 //
13 //===- llvm/ADT/SmallVector.cpp - 'Normally small' vectors ----------------===//
14 //
15 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
16 // See https://llvm.org/LICENSE.txt for license information.
17 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
18 //
19 //===----------------------------------------------------------------------===//
20 //
21 // This file implements the SmallVector class.
22 //
23 //===----------------------------------------------------------------------===//
24 
26 
27 #define LLVM_ENABLE_EXCEPTIONS
28 
29 #ifdef LLVM_ENABLE_EXCEPTIONS
30 #include <stdexcept>
31 #endif
32 #include <string>
33 namespace cloudViewer {
34 namespace core {
35 
36 // Check that no bytes are wasted and everything is well-aligned.
37 namespace {
38 // These structures may cause binary compat warnings on AIX. Suppress the
39 // warning since we are only using these types for the static assertions below.
40 #if defined(_AIX)
41 #pragma GCC diagnostic push
42 #pragma GCC diagnostic ignored "-Waix-compat"
43 #endif
44 struct Struct16B {
45  alignas(16) void *X;
46 };
47 struct Struct32B {
48  alignas(32) void *X;
49 };
50 #if defined(_AIX)
51 #pragma GCC diagnostic pop
52 #endif
53 } // namespace
54 static_assert(sizeof(SmallVector<void *, 0>) ==
55  sizeof(unsigned) * 2 + sizeof(void *),
56  "wasted space in SmallVector size 0");
57 static_assert(alignof(SmallVector<Struct16B, 0>) >= alignof(Struct16B),
58  "wrong alignment for 16-byte aligned T");
59 static_assert(alignof(SmallVector<Struct32B, 0>) >= alignof(Struct32B),
60  "wrong alignment for 32-byte aligned T");
61 static_assert(sizeof(SmallVector<Struct16B, 0>) >= alignof(Struct16B),
62  "missing padding for 16-byte aligned T");
63 static_assert(sizeof(SmallVector<Struct32B, 0>) >= alignof(Struct32B),
64  "missing padding for 32-byte aligned T");
65 static_assert(sizeof(SmallVector<void *, 1>) ==
66  sizeof(unsigned) * 2 + sizeof(void *) * 2,
67  "wasted space in SmallVector size 1");
68 
69 static_assert(sizeof(SmallVector<char, 0>) ==
70  sizeof(void *) * 2 + sizeof(void *),
71  "1 byte elements have word-sized type for size and capacity");
72 
75 [[noreturn]] static void report_size_overflow(size_t MinSize, size_t MaxSize);
76 static void report_size_overflow(size_t MinSize, size_t MaxSize) {
77  std::string Reason = "SmallVector unable to grow. Requested capacity (" +
78  std::to_string(MinSize) +
79  ") is larger than maximum value for size type (" +
80  std::to_string(MaxSize) + ")";
81 #ifdef LLVM_ENABLE_EXCEPTIONS
82  throw std::length_error(Reason);
83 #else
84  report_fatal_error(Twine(Reason));
85 #endif
86 }
87 
90 [[noreturn]] static void report_at_maximum_capacity(size_t MaxSize);
91 static void report_at_maximum_capacity(size_t MaxSize) {
92  std::string Reason =
93  "SmallVector capacity unable to grow. Already at maximum size " +
94  std::to_string(MaxSize);
95 #ifdef LLVM_ENABLE_EXCEPTIONS
96  throw std::length_error(Reason);
97 #else
98  report_fatal_error(Twine(Reason));
99 #endif
100 }
101 
102 // Note: Moving this function into the header may cause performance regression.
103 template <class Size_T>
104 static size_t getNewCapacity(size_t MinSize, size_t TSize, size_t OldCapacity) {
105  constexpr size_t MaxSize = std::numeric_limits<Size_T>::max();
106 
107  // Ensure we can fit the new capacity.
108  // This is only going to be applicable when the capacity is 32 bit.
109  if (MinSize > MaxSize) report_size_overflow(MinSize, MaxSize);
110 
111  // Ensure we can meet the guarantee of space for at least one more element.
112  // The above check alone will not catch the case where grow is called with a
113  // default MinSize of 0, but the current capacity cannot be increased.
114  // This is only going to be applicable when the capacity is 32 bit.
115  if (OldCapacity == MaxSize) report_at_maximum_capacity(MaxSize);
116 
117  // In theory 2*capacity can overflow if the capacity is 64 bit, but the
118  // original capacity would never be large enough for this to be a problem.
119  size_t NewCapacity = 2 * OldCapacity + 1; // Always grow.
120  return std::min(std::max(NewCapacity, MinSize), MaxSize);
121 }
122 
123 // Note: Moving this function into the header may cause performance regression.
124 template <class Size_T>
126  size_t TSize,
127  size_t &NewCapacity) {
128  NewCapacity = getNewCapacity<Size_T>(MinSize, TSize, this->capacity());
129  return cloudViewer::core::safe_malloc(NewCapacity * TSize);
130 }
131 
132 // Note: Moving this function into the header may cause performance regression.
133 template <class Size_T>
135  size_t MinSize,
136  size_t TSize) {
137  size_t NewCapacity =
138  getNewCapacity<Size_T>(MinSize, TSize, this->capacity());
139  void *NewElts;
140  if (BeginX == FirstEl) {
141  NewElts = safe_malloc(NewCapacity * TSize);
142 
143  // Copy the elements over. No need to run dtors on PODs.
144  memcpy(NewElts, this->BeginX, size() * TSize);
145  } else {
146  // If this wasn't grown from the inline copy, grow the allocated space.
147  NewElts = safe_realloc(this->BeginX, NewCapacity * TSize);
148  }
149 
150  this->BeginX = NewElts;
151  this->Capacity = NewCapacity;
152 }
153 
154 template class SmallVectorBase<uint32_t>;
155 
156 // Disable the uint64_t instantiation for 32-bit builds.
157 // Both uint32_t and uint64_t instantiations are needed for 64-bit builds.
158 // This instantiation will never be used in 32-bit builds, and will cause
159 // warnings when sizeof(Size_T) > sizeof(size_t).
160 #if SIZE_MAX > UINT32_MAX
161 template class SmallVectorBase<uint64_t>;
162 
163 // Assertions to ensure this #if stays in sync with SmallVectorSizeType.
164 static_assert(sizeof(SmallVectorSizeType<char>) == sizeof(uint64_t),
165  "Expected SmallVectorBase<uint64_t> variant to be in use.");
166 #else
167 static_assert(sizeof(SmallVectorSizeType<char>) == sizeof(uint32_t),
168  "Expected SmallVectorBase<uint32_t> variant to be in use.");
169 #endif
170 } // namespace core
171 } // namespace cloudViewer
int size
void * X
Definition: SmallVector.cpp:45
void * mallocForGrow(size_t MinSize, size_t TSize, size_t &NewCapacity)
void grow_pod(void *FirstEl, size_t MinSize, size_t TSize)
int min(int a, int b)
Definition: cutil_math.h:53
int max(int a, int b)
Definition: cutil_math.h:48
void * safe_malloc(size_t Sz)
Definition: SmallVector.h:58
typename std::conditional< sizeof(T)< 4 &&sizeof(void *) >=8, uint64_t, uint32_t >::type SmallVectorSizeType
Definition: SmallVector.h:139
static void report_at_maximum_capacity(size_t MaxSize)
Definition: SmallVector.cpp:91
static void report_size_overflow(size_t MinSize, size_t MaxSize)
Definition: SmallVector.cpp:76
static size_t getNewCapacity(size_t MinSize, size_t TSize, size_t OldCapacity)
void * safe_realloc(void *Ptr, size_t Sz)
Definition: SmallVector.h:70
Generic file read and write utility for python interface.
std::string to_string(const T &n)
Definition: Common.h:20