blob: fcb3e545b243a1724ced7248ce7548e2c87327d7 [file] [log] [blame]
Abseil Team134496a2018-06-29 14:00:35 -07001// Copyright 2018 The Abseil Authors.
mistergc2e75482017-09-19 16:54:40 -04002//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
nik727338b70432019-03-08 10:27:53 -05007// https://www.apache.org/licenses/LICENSE-2.0
mistergc2e75482017-09-19 16:54:40 -04008//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// -----------------------------------------------------------------------------
16// File: fixed_array.h
17// -----------------------------------------------------------------------------
18//
19// A `FixedArray<T>` represents a non-resizable array of `T` where the length of
20// the array can be determined at run-time. It is a good replacement for
21// non-standard and deprecated uses of `alloca()` and variable length arrays
22// within the GCC extension. (See
23// https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html).
24//
25// `FixedArray` allocates small arrays inline, keeping performance fast by
26// avoiding heap operations. It also helps reduce the chances of
27// accidentally overflowing your stack if large input is passed to
28// your function.
29
30#ifndef ABSL_CONTAINER_FIXED_ARRAY_H_
31#define ABSL_CONTAINER_FIXED_ARRAY_H_
32
33#include <algorithm>
mistergc2e75482017-09-19 16:54:40 -040034#include <cassert>
35#include <cstddef>
36#include <initializer_list>
37#include <iterator>
38#include <limits>
39#include <memory>
40#include <new>
41#include <type_traits>
42
43#include "absl/algorithm/algorithm.h"
Abseil Team184cf252020-07-30 13:58:50 -070044#include "absl/base/config.h"
mistergc2e75482017-09-19 16:54:40 -040045#include "absl/base/dynamic_annotations.h"
46#include "absl/base/internal/throw_delegate.h"
47#include "absl/base/macros.h"
48#include "absl/base/optimization.h"
49#include "absl/base/port.h"
Abseil Team2125e642018-08-01 04:34:12 -070050#include "absl/container/internal/compressed_tuple.h"
Abseil Team6365d172018-01-02 08:53:02 -080051#include "absl/memory/memory.h"
mistergc2e75482017-09-19 16:54:40 -040052
53namespace absl {
Abseil Team12bc53e2019-12-12 10:36:03 -080054ABSL_NAMESPACE_BEGIN
mistergc2e75482017-09-19 16:54:40 -040055
56constexpr static auto kFixedArrayUseDefault = static_cast<size_t>(-1);
57
58// -----------------------------------------------------------------------------
59// FixedArray
60// -----------------------------------------------------------------------------
61//
Abseil Team134496a2018-06-29 14:00:35 -070062// A `FixedArray` provides a run-time fixed-size array, allocating a small array
63// inline for efficiency.
mistergc2e75482017-09-19 16:54:40 -040064//
65// Most users should not specify an `inline_elements` argument and let
Abseil Team134496a2018-06-29 14:00:35 -070066// `FixedArray` automatically determine the number of elements
mistergc2e75482017-09-19 16:54:40 -040067// to store inline based on `sizeof(T)`. If `inline_elements` is specified, the
Abseil Team134496a2018-06-29 14:00:35 -070068// `FixedArray` implementation will use inline storage for arrays with a
mistergc2e75482017-09-19 16:54:40 -040069// length <= `inline_elements`.
70//
71// Note that a `FixedArray` constructed with a `size_type` argument will
72// default-initialize its values by leaving trivially constructible types
73// uninitialized (e.g. int, int[4], double), and others default-constructed.
74// This matches the behavior of c-style arrays and `std::array`, but not
75// `std::vector`.
76//
77// Note that `FixedArray` does not provide a public allocator; if it requires a
78// heap allocation, it will do so with global `::operator new[]()` and
79// `::operator delete[]()`, even if T provides class-scope overrides for these
80// operators.
Abseil Team2125e642018-08-01 04:34:12 -070081template <typename T, size_t N = kFixedArrayUseDefault,
82 typename A = std::allocator<T>>
mistergc2e75482017-09-19 16:54:40 -040083class FixedArray {
Abseil Teamba8d6cf2018-06-28 10:18:50 -070084 static_assert(!std::is_array<T>::value || std::extent<T>::value > 0,
85 "Arrays with unknown bounds cannot be used with FixedArray.");
Abseil Team2125e642018-08-01 04:34:12 -070086
mistergc2e75482017-09-19 16:54:40 -040087 static constexpr size_t kInlineBytesDefault = 256;
88
Abseil Team2125e642018-08-01 04:34:12 -070089 using AllocatorTraits = std::allocator_traits<A>;
mistergc2e75482017-09-19 16:54:40 -040090 // std::iterator_traits isn't guaranteed to be SFINAE-friendly until C++17,
91 // but this seems to be mostly pedantic.
Abseil Team134496a2018-06-29 14:00:35 -070092 template <typename Iterator>
93 using EnableIfForwardIterator = absl::enable_if_t<std::is_convertible<
94 typename std::iterator_traits<Iterator>::iterator_category,
95 std::forward_iterator_tag>::value>;
Abseil Team2125e642018-08-01 04:34:12 -070096 static constexpr bool NoexceptCopyable() {
97 return std::is_nothrow_copy_constructible<StorageElement>::value &&
98 absl::allocator_is_nothrow<allocator_type>::value;
99 }
100 static constexpr bool NoexceptMovable() {
101 return std::is_nothrow_move_constructible<StorageElement>::value &&
102 absl::allocator_is_nothrow<allocator_type>::value;
103 }
104 static constexpr bool DefaultConstructorIsNonTrivial() {
105 return !absl::is_trivially_default_constructible<StorageElement>::value;
106 }
mistergc2e75482017-09-19 16:54:40 -0400107
108 public:
Abseil Team2125e642018-08-01 04:34:12 -0700109 using allocator_type = typename AllocatorTraits::allocator_type;
Abseil Teamdb5773a2020-04-15 15:13:54 -0700110 using value_type = typename AllocatorTraits::value_type;
111 using pointer = typename AllocatorTraits::pointer;
112 using const_pointer = typename AllocatorTraits::const_pointer;
113 using reference = value_type&;
114 using const_reference = const value_type&;
115 using size_type = typename AllocatorTraits::size_type;
116 using difference_type = typename AllocatorTraits::difference_type;
Abseil Team2125e642018-08-01 04:34:12 -0700117 using iterator = pointer;
118 using const_iterator = const_pointer;
mistergc2e75482017-09-19 16:54:40 -0400119 using reverse_iterator = std::reverse_iterator<iterator>;
120 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
mistergc2e75482017-09-19 16:54:40 -0400121
122 static constexpr size_type inline_elements =
Abseil Team2125e642018-08-01 04:34:12 -0700123 (N == kFixedArrayUseDefault ? kInlineBytesDefault / sizeof(value_type)
124 : static_cast<size_type>(N));
mistergc2e75482017-09-19 16:54:40 -0400125
Abseil Team2125e642018-08-01 04:34:12 -0700126 FixedArray(
127 const FixedArray& other,
128 const allocator_type& a = allocator_type()) noexcept(NoexceptCopyable())
129 : FixedArray(other.begin(), other.end(), a) {}
Abseil Team87a4c072018-06-25 09:18:19 -0700130
Abseil Team2125e642018-08-01 04:34:12 -0700131 FixedArray(
132 FixedArray&& other,
133 const allocator_type& a = allocator_type()) noexcept(NoexceptMovable())
Abseil Team87a4c072018-06-25 09:18:19 -0700134 : FixedArray(std::make_move_iterator(other.begin()),
Abseil Team2125e642018-08-01 04:34:12 -0700135 std::make_move_iterator(other.end()), a) {}
Abseil Team6365d172018-01-02 08:53:02 -0800136
mistergc2e75482017-09-19 16:54:40 -0400137 // Creates an array object that can store `n` elements.
138 // Note that trivially constructible elements will be uninitialized.
Abseil Team2125e642018-08-01 04:34:12 -0700139 explicit FixedArray(size_type n, const allocator_type& a = allocator_type())
140 : storage_(n, a) {
141 if (DefaultConstructorIsNonTrivial()) {
Abseil Teamfefc8362018-08-21 08:05:11 -0700142 memory_internal::ConstructRange(storage_.alloc(), storage_.begin(),
143 storage_.end());
Abseil Team2125e642018-08-01 04:34:12 -0700144 }
Abseil Team87a4c072018-06-25 09:18:19 -0700145 }
mistergc2e75482017-09-19 16:54:40 -0400146
147 // Creates an array initialized with `n` copies of `val`.
Abseil Team2125e642018-08-01 04:34:12 -0700148 FixedArray(size_type n, const value_type& val,
149 const allocator_type& a = allocator_type())
150 : storage_(n, a) {
Abseil Teamfefc8362018-08-21 08:05:11 -0700151 memory_internal::ConstructRange(storage_.alloc(), storage_.begin(),
152 storage_.end(), val);
Abseil Team87a4c072018-06-25 09:18:19 -0700153 }
mistergc2e75482017-09-19 16:54:40 -0400154
Abseil Team2125e642018-08-01 04:34:12 -0700155 // Creates an array initialized with the size and contents of `init_list`.
156 FixedArray(std::initializer_list<value_type> init_list,
157 const allocator_type& a = allocator_type())
158 : FixedArray(init_list.begin(), init_list.end(), a) {}
159
mistergc2e75482017-09-19 16:54:40 -0400160 // Creates an array initialized with the elements from the input
161 // range. The array's size will always be `std::distance(first, last)`.
Abseil Team134496a2018-06-29 14:00:35 -0700162 // REQUIRES: Iterator must be a forward_iterator or better.
163 template <typename Iterator, EnableIfForwardIterator<Iterator>* = nullptr>
Abseil Team2125e642018-08-01 04:34:12 -0700164 FixedArray(Iterator first, Iterator last,
165 const allocator_type& a = allocator_type())
166 : storage_(std::distance(first, last), a) {
Abseil Teamfefc8362018-08-21 08:05:11 -0700167 memory_internal::CopyRange(storage_.alloc(), storage_.begin(), first, last);
Abseil Team87a4c072018-06-25 09:18:19 -0700168 }
mistergc2e75482017-09-19 16:54:40 -0400169
Abseil Team87a4c072018-06-25 09:18:19 -0700170 ~FixedArray() noexcept {
Abseil Team2125e642018-08-01 04:34:12 -0700171 for (auto* cur = storage_.begin(); cur != storage_.end(); ++cur) {
Abseil Teamfefc8362018-08-21 08:05:11 -0700172 AllocatorTraits::destroy(storage_.alloc(), cur);
Abseil Team87a4c072018-06-25 09:18:19 -0700173 }
174 }
mistergc2e75482017-09-19 16:54:40 -0400175
Abseil Team6365d172018-01-02 08:53:02 -0800176 // Assignments are deleted because they break the invariant that the size of a
177 // `FixedArray` never changes.
178 void operator=(FixedArray&&) = delete;
mistergc2e75482017-09-19 16:54:40 -0400179 void operator=(const FixedArray&) = delete;
180
181 // FixedArray::size()
182 //
183 // Returns the length of the fixed array.
Abseil Team134496a2018-06-29 14:00:35 -0700184 size_type size() const { return storage_.size(); }
mistergc2e75482017-09-19 16:54:40 -0400185
186 // FixedArray::max_size()
187 //
188 // Returns the largest possible value of `std::distance(begin(), end())` for a
189 // `FixedArray<T>`. This is equivalent to the most possible addressable bytes
190 // over the number of bytes taken by T.
191 constexpr size_type max_size() const {
Abseil Teama4c3fff2018-10-29 15:53:34 -0700192 return (std::numeric_limits<difference_type>::max)() / sizeof(value_type);
mistergc2e75482017-09-19 16:54:40 -0400193 }
194
195 // FixedArray::empty()
196 //
197 // Returns whether or not the fixed array is empty.
198 bool empty() const { return size() == 0; }
199
200 // FixedArray::memsize()
201 //
202 // Returns the memory size of the fixed array in bytes.
203 size_t memsize() const { return size() * sizeof(value_type); }
204
205 // FixedArray::data()
206 //
207 // Returns a const T* pointer to elements of the `FixedArray`. This pointer
208 // can be used to access (but not modify) the contained elements.
Abseil Team134496a2018-06-29 14:00:35 -0700209 const_pointer data() const { return AsValueType(storage_.begin()); }
mistergc2e75482017-09-19 16:54:40 -0400210
211 // Overload of FixedArray::data() to return a T* pointer to elements of the
212 // fixed array. This pointer can be used to access and modify the contained
213 // elements.
Abseil Team134496a2018-06-29 14:00:35 -0700214 pointer data() { return AsValueType(storage_.begin()); }
Abseil Team787891a2018-01-22 13:10:49 -0800215
mistergc2e75482017-09-19 16:54:40 -0400216 // FixedArray::operator[]
217 //
218 // Returns a reference the ith element of the fixed array.
219 // REQUIRES: 0 <= i < size()
220 reference operator[](size_type i) {
Abseil Team518f1752020-03-23 13:16:18 -0700221 ABSL_HARDENING_ASSERT(i < size());
mistergc2e75482017-09-19 16:54:40 -0400222 return data()[i];
223 }
224
225 // Overload of FixedArray::operator()[] to return a const reference to the
226 // ith element of the fixed array.
227 // REQUIRES: 0 <= i < size()
228 const_reference operator[](size_type i) const {
Abseil Team518f1752020-03-23 13:16:18 -0700229 ABSL_HARDENING_ASSERT(i < size());
mistergc2e75482017-09-19 16:54:40 -0400230 return data()[i];
231 }
232
233 // FixedArray::at
234 //
Abseil Teamb8e890f2020-11-02 16:38:50 -0800235 // Bounds-checked access. Returns a reference to the ith element of the fixed
236 // array, or throws std::out_of_range
mistergc2e75482017-09-19 16:54:40 -0400237 reference at(size_type i) {
238 if (ABSL_PREDICT_FALSE(i >= size())) {
239 base_internal::ThrowStdOutOfRange("FixedArray::at failed bounds check");
240 }
241 return data()[i];
242 }
243
244 // Overload of FixedArray::at() to return a const reference to the ith element
245 // of the fixed array.
246 const_reference at(size_type i) const {
Abseil Team5b535402018-04-18 05:56:39 -0700247 if (ABSL_PREDICT_FALSE(i >= size())) {
mistergc2e75482017-09-19 16:54:40 -0400248 base_internal::ThrowStdOutOfRange("FixedArray::at failed bounds check");
249 }
250 return data()[i];
251 }
252
253 // FixedArray::front()
254 //
255 // Returns a reference to the first element of the fixed array.
Abseil Team518f1752020-03-23 13:16:18 -0700256 reference front() {
257 ABSL_HARDENING_ASSERT(!empty());
258 return data()[0];
259 }
mistergc2e75482017-09-19 16:54:40 -0400260
261 // Overload of FixedArray::front() to return a reference to the first element
262 // of a fixed array of const values.
Abseil Team518f1752020-03-23 13:16:18 -0700263 const_reference front() const {
264 ABSL_HARDENING_ASSERT(!empty());
265 return data()[0];
266 }
mistergc2e75482017-09-19 16:54:40 -0400267
268 // FixedArray::back()
269 //
270 // Returns a reference to the last element of the fixed array.
Abseil Team518f1752020-03-23 13:16:18 -0700271 reference back() {
272 ABSL_HARDENING_ASSERT(!empty());
273 return data()[size() - 1];
274 }
mistergc2e75482017-09-19 16:54:40 -0400275
276 // Overload of FixedArray::back() to return a reference to the last element
277 // of a fixed array of const values.
Abseil Team518f1752020-03-23 13:16:18 -0700278 const_reference back() const {
279 ABSL_HARDENING_ASSERT(!empty());
280 return data()[size() - 1];
281 }
mistergc2e75482017-09-19 16:54:40 -0400282
283 // FixedArray::begin()
284 //
285 // Returns an iterator to the beginning of the fixed array.
286 iterator begin() { return data(); }
287
288 // Overload of FixedArray::begin() to return a const iterator to the
289 // beginning of the fixed array.
290 const_iterator begin() const { return data(); }
291
292 // FixedArray::cbegin()
293 //
294 // Returns a const iterator to the beginning of the fixed array.
295 const_iterator cbegin() const { return begin(); }
296
297 // FixedArray::end()
298 //
299 // Returns an iterator to the end of the fixed array.
300 iterator end() { return data() + size(); }
301
302 // Overload of FixedArray::end() to return a const iterator to the end of the
303 // fixed array.
304 const_iterator end() const { return data() + size(); }
305
306 // FixedArray::cend()
307 //
308 // Returns a const iterator to the end of the fixed array.
309 const_iterator cend() const { return end(); }
310
311 // FixedArray::rbegin()
312 //
313 // Returns a reverse iterator from the end of the fixed array.
314 reverse_iterator rbegin() { return reverse_iterator(end()); }
315
316 // Overload of FixedArray::rbegin() to return a const reverse iterator from
317 // the end of the fixed array.
318 const_reverse_iterator rbegin() const {
319 return const_reverse_iterator(end());
320 }
321
322 // FixedArray::crbegin()
323 //
324 // Returns a const reverse iterator from the end of the fixed array.
325 const_reverse_iterator crbegin() const { return rbegin(); }
326
327 // FixedArray::rend()
328 //
329 // Returns a reverse iterator from the beginning of the fixed array.
330 reverse_iterator rend() { return reverse_iterator(begin()); }
331
332 // Overload of FixedArray::rend() for returning a const reverse iterator
333 // from the beginning of the fixed array.
334 const_reverse_iterator rend() const {
335 return const_reverse_iterator(begin());
336 }
337
338 // FixedArray::crend()
339 //
340 // Returns a reverse iterator from the beginning of the fixed array.
341 const_reverse_iterator crend() const { return rend(); }
342
343 // FixedArray::fill()
344 //
345 // Assigns the given `value` to all elements in the fixed array.
Abseil Team134496a2018-06-29 14:00:35 -0700346 void fill(const value_type& val) { std::fill(begin(), end(), val); }
mistergc2e75482017-09-19 16:54:40 -0400347
348 // Relational operators. Equality operators are elementwise using
349 // `operator==`, while order operators order FixedArrays lexicographically.
350 friend bool operator==(const FixedArray& lhs, const FixedArray& rhs) {
351 return absl::equal(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
352 }
353
354 friend bool operator!=(const FixedArray& lhs, const FixedArray& rhs) {
355 return !(lhs == rhs);
356 }
357
358 friend bool operator<(const FixedArray& lhs, const FixedArray& rhs) {
359 return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(),
360 rhs.end());
361 }
362
363 friend bool operator>(const FixedArray& lhs, const FixedArray& rhs) {
364 return rhs < lhs;
365 }
366
367 friend bool operator<=(const FixedArray& lhs, const FixedArray& rhs) {
368 return !(rhs < lhs);
369 }
370
371 friend bool operator>=(const FixedArray& lhs, const FixedArray& rhs) {
372 return !(lhs < rhs);
373 }
Abseil Teamf21d1872018-10-02 12:09:18 -0700374
375 template <typename H>
376 friend H AbslHashValue(H h, const FixedArray& v) {
377 return H::combine(H::combine_contiguous(std::move(h), v.data(), v.size()),
378 v.size());
379 }
380
mistergc2e75482017-09-19 16:54:40 -0400381 private:
Abseil Team134496a2018-06-29 14:00:35 -0700382 // StorageElement
mistergc2e75482017-09-19 16:54:40 -0400383 //
Abseil Team134496a2018-06-29 14:00:35 -0700384 // For FixedArrays with a C-style-array value_type, StorageElement is a POD
385 // wrapper struct called StorageElementWrapper that holds the value_type
386 // instance inside. This is needed for construction and destruction of the
387 // entire array regardless of how many dimensions it has. For all other cases,
388 // StorageElement is just an alias of value_type.
mistergc2e75482017-09-19 16:54:40 -0400389 //
Abseil Team134496a2018-06-29 14:00:35 -0700390 // Maintainer's Note: The simpler solution would be to simply wrap value_type
391 // in a struct whether it's an array or not. That causes some paranoid
392 // diagnostics to misfire, believing that 'data()' returns a pointer to a
393 // single element, rather than the packed array that it really is.
mistergc2e75482017-09-19 16:54:40 -0400394 // e.g.:
395 //
396 // FixedArray<char> buf(1);
397 // sprintf(buf.data(), "foo");
398 //
399 // error: call to int __builtin___sprintf_chk(etc...)
400 // will always overflow destination buffer [-Werror]
401 //
Abseil Teamf0afae02019-08-20 11:39:40 -0700402 template <typename OuterT, typename InnerT = absl::remove_extent_t<OuterT>,
Abseil Teamba8d6cf2018-06-28 10:18:50 -0700403 size_t InnerN = std::extent<OuterT>::value>
Abseil Team134496a2018-06-29 14:00:35 -0700404 struct StorageElementWrapper {
Abseil Teamba8d6cf2018-06-28 10:18:50 -0700405 InnerT array[InnerN];
mistergc2e75482017-09-19 16:54:40 -0400406 };
407
Abseil Team134496a2018-06-29 14:00:35 -0700408 using StorageElement =
409 absl::conditional_t<std::is_array<value_type>::value,
410 StorageElementWrapper<value_type>, value_type>;
Abseil Teamba8d6cf2018-06-28 10:18:50 -0700411
Abseil Team134496a2018-06-29 14:00:35 -0700412 static pointer AsValueType(pointer ptr) { return ptr; }
413 static pointer AsValueType(StorageElementWrapper<value_type>* ptr) {
Abseil Teamba8d6cf2018-06-28 10:18:50 -0700414 return std::addressof(ptr->array);
415 }
mistergc2e75482017-09-19 16:54:40 -0400416
Abseil Team134496a2018-06-29 14:00:35 -0700417 static_assert(sizeof(StorageElement) == sizeof(value_type), "");
418 static_assert(alignof(StorageElement) == alignof(value_type), "");
mistergc2e75482017-09-19 16:54:40 -0400419
Abseil Teamf0afae02019-08-20 11:39:40 -0700420 class NonEmptyInlinedStorage {
421 public:
422 StorageElement* data() { return reinterpret_cast<StorageElement*>(buff_); }
423 void AnnotateConstruct(size_type n);
424 void AnnotateDestruct(size_type n);
Abseil Team134496a2018-06-29 14:00:35 -0700425
Abseil Team184cf252020-07-30 13:58:50 -0700426#ifdef ABSL_HAVE_ADDRESS_SANITIZER
Abseil Team134496a2018-06-29 14:00:35 -0700427 void* RedzoneBegin() { return &redzone_begin_; }
428 void* RedzoneEnd() { return &redzone_end_ + 1; }
Abseil Team184cf252020-07-30 13:58:50 -0700429#endif // ABSL_HAVE_ADDRESS_SANITIZER
mistergc2e75482017-09-19 16:54:40 -0400430
Abseil Teamf0afae02019-08-20 11:39:40 -0700431 private:
Abseil Teamb86fff12020-06-24 12:46:11 -0700432 ABSL_ADDRESS_SANITIZER_REDZONE(redzone_begin_);
Abseil Teamf0afae02019-08-20 11:39:40 -0700433 alignas(StorageElement) char buff_[sizeof(StorageElement[inline_elements])];
Abseil Teamb86fff12020-06-24 12:46:11 -0700434 ABSL_ADDRESS_SANITIZER_REDZONE(redzone_end_);
mistergc2e75482017-09-19 16:54:40 -0400435 };
436
Abseil Teamf0afae02019-08-20 11:39:40 -0700437 class EmptyInlinedStorage {
438 public:
Abseil Team134496a2018-06-29 14:00:35 -0700439 StorageElement* data() { return nullptr; }
Abseil Team2125e642018-08-01 04:34:12 -0700440 void AnnotateConstruct(size_type) {}
441 void AnnotateDestruct(size_type) {}
mistergc2e75482017-09-19 16:54:40 -0400442 };
443
Abseil Team134496a2018-06-29 14:00:35 -0700444 using InlinedStorage =
445 absl::conditional_t<inline_elements == 0, EmptyInlinedStorage,
446 NonEmptyInlinedStorage>;
mistergc2e75482017-09-19 16:54:40 -0400447
Abseil Team134496a2018-06-29 14:00:35 -0700448 // Storage
449 //
450 // An instance of Storage manages the inline and out-of-line memory for
451 // instances of FixedArray. This guarantees that even when construction of
452 // individual elements fails in the FixedArray constructor body, the
453 // destructor for Storage will still be called and out-of-line memory will be
454 // properly deallocated.
455 //
456 class Storage : public InlinedStorage {
457 public:
Abseil Team2125e642018-08-01 04:34:12 -0700458 Storage(size_type n, const allocator_type& a)
459 : size_alloc_(n, a), data_(InitializeData()) {}
460
Abseil Team134496a2018-06-29 14:00:35 -0700461 ~Storage() noexcept {
462 if (UsingInlinedStorage(size())) {
Abseil Team2125e642018-08-01 04:34:12 -0700463 InlinedStorage::AnnotateDestruct(size());
Abseil Team134496a2018-06-29 14:00:35 -0700464 } else {
Abseil Teamfefc8362018-08-21 08:05:11 -0700465 AllocatorTraits::deallocate(alloc(), AsValueType(begin()), size());
mistergc2e75482017-09-19 16:54:40 -0400466 }
467 }
Abseil Team134496a2018-06-29 14:00:35 -0700468
Abseil Team2125e642018-08-01 04:34:12 -0700469 size_type size() const { return size_alloc_.template get<0>(); }
Abseil Team134496a2018-06-29 14:00:35 -0700470 StorageElement* begin() const { return data_; }
471 StorageElement* end() const { return begin() + size(); }
Abseil Teamf0afae02019-08-20 11:39:40 -0700472 allocator_type& alloc() { return size_alloc_.template get<1>(); }
mistergc2e75482017-09-19 16:54:40 -0400473
474 private:
Abseil Team134496a2018-06-29 14:00:35 -0700475 static bool UsingInlinedStorage(size_type n) {
476 return n <= inline_elements;
477 }
478
Abseil Team2125e642018-08-01 04:34:12 -0700479 StorageElement* InitializeData() {
480 if (UsingInlinedStorage(size())) {
481 InlinedStorage::AnnotateConstruct(size());
Abseil Team134496a2018-06-29 14:00:35 -0700482 return InlinedStorage::data();
483 } else {
Abseil Team2125e642018-08-01 04:34:12 -0700484 return reinterpret_cast<StorageElement*>(
Abseil Teamfefc8362018-08-21 08:05:11 -0700485 AllocatorTraits::allocate(alloc(), size()));
mistergc2e75482017-09-19 16:54:40 -0400486 }
487 }
488
Abseil Team2125e642018-08-01 04:34:12 -0700489 // `CompressedTuple` takes advantage of EBCO for stateless `allocator_type`s
490 container_internal::CompressedTuple<size_type, allocator_type> size_alloc_;
491 StorageElement* data_;
mistergc2e75482017-09-19 16:54:40 -0400492 };
493
Abseil Team2125e642018-08-01 04:34:12 -0700494 Storage storage_;
mistergc2e75482017-09-19 16:54:40 -0400495};
496
Abseil Team2125e642018-08-01 04:34:12 -0700497template <typename T, size_t N, typename A>
498constexpr size_t FixedArray<T, N, A>::kInlineBytesDefault;
mistergc2e75482017-09-19 16:54:40 -0400499
Abseil Team2125e642018-08-01 04:34:12 -0700500template <typename T, size_t N, typename A>
501constexpr typename FixedArray<T, N, A>::size_type
502 FixedArray<T, N, A>::inline_elements;
mistergc2e75482017-09-19 16:54:40 -0400503
Abseil Team2125e642018-08-01 04:34:12 -0700504template <typename T, size_t N, typename A>
505void FixedArray<T, N, A>::NonEmptyInlinedStorage::AnnotateConstruct(
506 typename FixedArray<T, N, A>::size_type n) {
Abseil Team184cf252020-07-30 13:58:50 -0700507#ifdef ABSL_HAVE_ADDRESS_SANITIZER
Abseil Team134496a2018-06-29 14:00:35 -0700508 if (!n) return;
Abseil Teamb86fff12020-06-24 12:46:11 -0700509 ABSL_ANNOTATE_CONTIGUOUS_CONTAINER(data(), RedzoneEnd(), RedzoneEnd(),
510 data() + n);
511 ABSL_ANNOTATE_CONTIGUOUS_CONTAINER(RedzoneBegin(), data(), data(),
512 RedzoneBegin());
Abseil Team184cf252020-07-30 13:58:50 -0700513#endif // ABSL_HAVE_ADDRESS_SANITIZER
Abseil Team134496a2018-06-29 14:00:35 -0700514 static_cast<void>(n); // Mark used when not in asan mode
515}
516
Abseil Team2125e642018-08-01 04:34:12 -0700517template <typename T, size_t N, typename A>
518void FixedArray<T, N, A>::NonEmptyInlinedStorage::AnnotateDestruct(
519 typename FixedArray<T, N, A>::size_type n) {
Abseil Team184cf252020-07-30 13:58:50 -0700520#ifdef ABSL_HAVE_ADDRESS_SANITIZER
Abseil Team134496a2018-06-29 14:00:35 -0700521 if (!n) return;
Abseil Teamb86fff12020-06-24 12:46:11 -0700522 ABSL_ANNOTATE_CONTIGUOUS_CONTAINER(data(), RedzoneEnd(), data() + n,
523 RedzoneEnd());
524 ABSL_ANNOTATE_CONTIGUOUS_CONTAINER(RedzoneBegin(), data(), RedzoneBegin(),
525 data());
Abseil Team184cf252020-07-30 13:58:50 -0700526#endif // ABSL_HAVE_ADDRESS_SANITIZER
Abseil Team134496a2018-06-29 14:00:35 -0700527 static_cast<void>(n); // Mark used when not in asan mode
528}
Abseil Team12bc53e2019-12-12 10:36:03 -0800529ABSL_NAMESPACE_END
mistergc2e75482017-09-19 16:54:40 -0400530} // namespace absl
Abseil Teambf294702019-03-19 11:14:01 -0700531
mistergc2e75482017-09-19 16:54:40 -0400532#endif // ABSL_CONTAINER_FIXED_ARRAY_H_