blob: f480047a5db41b444295d01b55d8013be3d51be0 [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//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
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>
34#include <array>
35#include <cassert>
36#include <cstddef>
37#include <initializer_list>
38#include <iterator>
39#include <limits>
40#include <memory>
41#include <new>
42#include <type_traits>
43
44#include "absl/algorithm/algorithm.h"
45#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 {
54
55constexpr static auto kFixedArrayUseDefault = static_cast<size_t>(-1);
56
57// -----------------------------------------------------------------------------
58// FixedArray
59// -----------------------------------------------------------------------------
60//
Abseil Team134496a2018-06-29 14:00:35 -070061// A `FixedArray` provides a run-time fixed-size array, allocating a small array
62// inline for efficiency.
mistergc2e75482017-09-19 16:54:40 -040063//
64// Most users should not specify an `inline_elements` argument and let
Abseil Team134496a2018-06-29 14:00:35 -070065// `FixedArray` automatically determine the number of elements
mistergc2e75482017-09-19 16:54:40 -040066// to store inline based on `sizeof(T)`. If `inline_elements` is specified, the
Abseil Team134496a2018-06-29 14:00:35 -070067// `FixedArray` implementation will use inline storage for arrays with a
mistergc2e75482017-09-19 16:54:40 -040068// length <= `inline_elements`.
69//
70// Note that a `FixedArray` constructed with a `size_type` argument will
71// default-initialize its values by leaving trivially constructible types
72// uninitialized (e.g. int, int[4], double), and others default-constructed.
73// This matches the behavior of c-style arrays and `std::array`, but not
74// `std::vector`.
75//
76// Note that `FixedArray` does not provide a public allocator; if it requires a
77// heap allocation, it will do so with global `::operator new[]()` and
78// `::operator delete[]()`, even if T provides class-scope overrides for these
79// operators.
Abseil Team2125e642018-08-01 04:34:12 -070080template <typename T, size_t N = kFixedArrayUseDefault,
81 typename A = std::allocator<T>>
mistergc2e75482017-09-19 16:54:40 -040082class FixedArray {
Abseil Teamba8d6cf2018-06-28 10:18:50 -070083 static_assert(!std::is_array<T>::value || std::extent<T>::value > 0,
84 "Arrays with unknown bounds cannot be used with FixedArray.");
Abseil Team2125e642018-08-01 04:34:12 -070085
mistergc2e75482017-09-19 16:54:40 -040086 static constexpr size_t kInlineBytesDefault = 256;
87
Abseil Team2125e642018-08-01 04:34:12 -070088 using AllocatorTraits = std::allocator_traits<A>;
mistergc2e75482017-09-19 16:54:40 -040089 // std::iterator_traits isn't guaranteed to be SFINAE-friendly until C++17,
90 // but this seems to be mostly pedantic.
Abseil Team134496a2018-06-29 14:00:35 -070091 template <typename Iterator>
92 using EnableIfForwardIterator = absl::enable_if_t<std::is_convertible<
93 typename std::iterator_traits<Iterator>::iterator_category,
94 std::forward_iterator_tag>::value>;
Abseil Team2125e642018-08-01 04:34:12 -070095 static constexpr bool NoexceptCopyable() {
96 return std::is_nothrow_copy_constructible<StorageElement>::value &&
97 absl::allocator_is_nothrow<allocator_type>::value;
98 }
99 static constexpr bool NoexceptMovable() {
100 return std::is_nothrow_move_constructible<StorageElement>::value &&
101 absl::allocator_is_nothrow<allocator_type>::value;
102 }
103 static constexpr bool DefaultConstructorIsNonTrivial() {
104 return !absl::is_trivially_default_constructible<StorageElement>::value;
105 }
mistergc2e75482017-09-19 16:54:40 -0400106
107 public:
Abseil Team2125e642018-08-01 04:34:12 -0700108 using allocator_type = typename AllocatorTraits::allocator_type;
109 using value_type = typename allocator_type::value_type;
110 using pointer = typename allocator_type::pointer;
111 using const_pointer = typename allocator_type::const_pointer;
112 using reference = typename allocator_type::reference;
113 using const_reference = typename allocator_type::const_reference;
114 using size_type = typename allocator_type::size_type;
115 using difference_type = typename allocator_type::difference_type;
116 using iterator = pointer;
117 using const_iterator = const_pointer;
mistergc2e75482017-09-19 16:54:40 -0400118 using reverse_iterator = std::reverse_iterator<iterator>;
119 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
mistergc2e75482017-09-19 16:54:40 -0400120
121 static constexpr size_type inline_elements =
Abseil Team2125e642018-08-01 04:34:12 -0700122 (N == kFixedArrayUseDefault ? kInlineBytesDefault / sizeof(value_type)
123 : static_cast<size_type>(N));
mistergc2e75482017-09-19 16:54:40 -0400124
Abseil Team2125e642018-08-01 04:34:12 -0700125 FixedArray(
126 const FixedArray& other,
127 const allocator_type& a = allocator_type()) noexcept(NoexceptCopyable())
128 : FixedArray(other.begin(), other.end(), a) {}
Abseil Team87a4c072018-06-25 09:18:19 -0700129
Abseil Team2125e642018-08-01 04:34:12 -0700130 FixedArray(
131 FixedArray&& other,
132 const allocator_type& a = allocator_type()) noexcept(NoexceptMovable())
Abseil Team87a4c072018-06-25 09:18:19 -0700133 : FixedArray(std::make_move_iterator(other.begin()),
Abseil Team2125e642018-08-01 04:34:12 -0700134 std::make_move_iterator(other.end()), a) {}
Abseil Team6365d172018-01-02 08:53:02 -0800135
mistergc2e75482017-09-19 16:54:40 -0400136 // Creates an array object that can store `n` elements.
137 // Note that trivially constructible elements will be uninitialized.
Abseil Team2125e642018-08-01 04:34:12 -0700138 explicit FixedArray(size_type n, const allocator_type& a = allocator_type())
139 : storage_(n, a) {
140 if (DefaultConstructorIsNonTrivial()) {
141 memory_internal::ConstructStorage(storage_.alloc(), storage_.begin(),
142 storage_.end());
143 }
Abseil Team87a4c072018-06-25 09:18:19 -0700144 }
mistergc2e75482017-09-19 16:54:40 -0400145
146 // Creates an array initialized with `n` copies of `val`.
Abseil Team2125e642018-08-01 04:34:12 -0700147 FixedArray(size_type n, const value_type& val,
148 const allocator_type& a = allocator_type())
149 : storage_(n, a) {
150 memory_internal::ConstructStorage(storage_.alloc(), storage_.begin(),
151 storage_.end(), val);
Abseil Team87a4c072018-06-25 09:18:19 -0700152 }
mistergc2e75482017-09-19 16:54:40 -0400153
Abseil Team2125e642018-08-01 04:34:12 -0700154 // Creates an array initialized with the size and contents of `init_list`.
155 FixedArray(std::initializer_list<value_type> init_list,
156 const allocator_type& a = allocator_type())
157 : FixedArray(init_list.begin(), init_list.end(), a) {}
158
mistergc2e75482017-09-19 16:54:40 -0400159 // Creates an array initialized with the elements from the input
160 // range. The array's size will always be `std::distance(first, last)`.
Abseil Team134496a2018-06-29 14:00:35 -0700161 // REQUIRES: Iterator must be a forward_iterator or better.
162 template <typename Iterator, EnableIfForwardIterator<Iterator>* = nullptr>
Abseil Team2125e642018-08-01 04:34:12 -0700163 FixedArray(Iterator first, Iterator last,
164 const allocator_type& a = allocator_type())
165 : storage_(std::distance(first, last), a) {
166 memory_internal::CopyToStorageFromRange(storage_.alloc(), storage_.begin(),
167 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) {
172 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 {
192 return std::numeric_limits<difference_type>::max() / sizeof(value_type);
193 }
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) {
221 assert(i < size());
222 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 {
229 assert(i < size());
230 return data()[i];
231 }
232
233 // FixedArray::at
234 //
235 // Bounds-checked access. Returns a reference to the ith element of the
236 // fiexed array, or throws std::out_of_range
237 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.
256 reference front() { return *begin(); }
257
258 // Overload of FixedArray::front() to return a reference to the first element
259 // of a fixed array of const values.
260 const_reference front() const { return *begin(); }
261
262 // FixedArray::back()
263 //
264 // Returns a reference to the last element of the fixed array.
265 reference back() { return *(end() - 1); }
266
267 // Overload of FixedArray::back() to return a reference to the last element
268 // of a fixed array of const values.
269 const_reference back() const { return *(end() - 1); }
270
271 // FixedArray::begin()
272 //
273 // Returns an iterator to the beginning of the fixed array.
274 iterator begin() { return data(); }
275
276 // Overload of FixedArray::begin() to return a const iterator to the
277 // beginning of the fixed array.
278 const_iterator begin() const { return data(); }
279
280 // FixedArray::cbegin()
281 //
282 // Returns a const iterator to the beginning of the fixed array.
283 const_iterator cbegin() const { return begin(); }
284
285 // FixedArray::end()
286 //
287 // Returns an iterator to the end of the fixed array.
288 iterator end() { return data() + size(); }
289
290 // Overload of FixedArray::end() to return a const iterator to the end of the
291 // fixed array.
292 const_iterator end() const { return data() + size(); }
293
294 // FixedArray::cend()
295 //
296 // Returns a const iterator to the end of the fixed array.
297 const_iterator cend() const { return end(); }
298
299 // FixedArray::rbegin()
300 //
301 // Returns a reverse iterator from the end of the fixed array.
302 reverse_iterator rbegin() { return reverse_iterator(end()); }
303
304 // Overload of FixedArray::rbegin() to return a const reverse iterator from
305 // the end of the fixed array.
306 const_reverse_iterator rbegin() const {
307 return const_reverse_iterator(end());
308 }
309
310 // FixedArray::crbegin()
311 //
312 // Returns a const reverse iterator from the end of the fixed array.
313 const_reverse_iterator crbegin() const { return rbegin(); }
314
315 // FixedArray::rend()
316 //
317 // Returns a reverse iterator from the beginning of the fixed array.
318 reverse_iterator rend() { return reverse_iterator(begin()); }
319
320 // Overload of FixedArray::rend() for returning a const reverse iterator
321 // from the beginning of the fixed array.
322 const_reverse_iterator rend() const {
323 return const_reverse_iterator(begin());
324 }
325
326 // FixedArray::crend()
327 //
328 // Returns a reverse iterator from the beginning of the fixed array.
329 const_reverse_iterator crend() const { return rend(); }
330
331 // FixedArray::fill()
332 //
333 // Assigns the given `value` to all elements in the fixed array.
Abseil Team134496a2018-06-29 14:00:35 -0700334 void fill(const value_type& val) { std::fill(begin(), end(), val); }
mistergc2e75482017-09-19 16:54:40 -0400335
336 // Relational operators. Equality operators are elementwise using
337 // `operator==`, while order operators order FixedArrays lexicographically.
338 friend bool operator==(const FixedArray& lhs, const FixedArray& rhs) {
339 return absl::equal(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
340 }
341
342 friend bool operator!=(const FixedArray& lhs, const FixedArray& rhs) {
343 return !(lhs == rhs);
344 }
345
346 friend bool operator<(const FixedArray& lhs, const FixedArray& rhs) {
347 return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(),
348 rhs.end());
349 }
350
351 friend bool operator>(const FixedArray& lhs, const FixedArray& rhs) {
352 return rhs < lhs;
353 }
354
355 friend bool operator<=(const FixedArray& lhs, const FixedArray& rhs) {
356 return !(rhs < lhs);
357 }
358
359 friend bool operator>=(const FixedArray& lhs, const FixedArray& rhs) {
360 return !(lhs < rhs);
361 }
mistergc2e75482017-09-19 16:54:40 -0400362 private:
Abseil Team134496a2018-06-29 14:00:35 -0700363 // StorageElement
mistergc2e75482017-09-19 16:54:40 -0400364 //
Abseil Team134496a2018-06-29 14:00:35 -0700365 // For FixedArrays with a C-style-array value_type, StorageElement is a POD
366 // wrapper struct called StorageElementWrapper that holds the value_type
367 // instance inside. This is needed for construction and destruction of the
368 // entire array regardless of how many dimensions it has. For all other cases,
369 // StorageElement is just an alias of value_type.
mistergc2e75482017-09-19 16:54:40 -0400370 //
Abseil Team134496a2018-06-29 14:00:35 -0700371 // Maintainer's Note: The simpler solution would be to simply wrap value_type
372 // in a struct whether it's an array or not. That causes some paranoid
373 // diagnostics to misfire, believing that 'data()' returns a pointer to a
374 // single element, rather than the packed array that it really is.
mistergc2e75482017-09-19 16:54:40 -0400375 // e.g.:
376 //
377 // FixedArray<char> buf(1);
378 // sprintf(buf.data(), "foo");
379 //
380 // error: call to int __builtin___sprintf_chk(etc...)
381 // will always overflow destination buffer [-Werror]
382 //
Abseil Teamba8d6cf2018-06-28 10:18:50 -0700383 template <typename OuterT = value_type,
384 typename InnerT = absl::remove_extent_t<OuterT>,
385 size_t InnerN = std::extent<OuterT>::value>
Abseil Team134496a2018-06-29 14:00:35 -0700386 struct StorageElementWrapper {
Abseil Teamba8d6cf2018-06-28 10:18:50 -0700387 InnerT array[InnerN];
mistergc2e75482017-09-19 16:54:40 -0400388 };
389
Abseil Team134496a2018-06-29 14:00:35 -0700390 using StorageElement =
391 absl::conditional_t<std::is_array<value_type>::value,
392 StorageElementWrapper<value_type>, value_type>;
Abseil Team2125e642018-08-01 04:34:12 -0700393 using StorageElementBuffer =
394 absl::aligned_storage_t<sizeof(StorageElement), alignof(StorageElement)>;
Abseil Teamba8d6cf2018-06-28 10:18:50 -0700395
Abseil Team134496a2018-06-29 14:00:35 -0700396 static pointer AsValueType(pointer ptr) { return ptr; }
397 static pointer AsValueType(StorageElementWrapper<value_type>* ptr) {
Abseil Teamba8d6cf2018-06-28 10:18:50 -0700398 return std::addressof(ptr->array);
399 }
mistergc2e75482017-09-19 16:54:40 -0400400
Abseil Team134496a2018-06-29 14:00:35 -0700401 static_assert(sizeof(StorageElement) == sizeof(value_type), "");
402 static_assert(alignof(StorageElement) == alignof(value_type), "");
mistergc2e75482017-09-19 16:54:40 -0400403
Abseil Team134496a2018-06-29 14:00:35 -0700404 struct NonEmptyInlinedStorage {
Abseil Team134496a2018-06-29 14:00:35 -0700405 StorageElement* data() {
406 return reinterpret_cast<StorageElement*>(inlined_storage_.data());
mistergc2e75482017-09-19 16:54:40 -0400407 }
Abseil Team134496a2018-06-29 14:00:35 -0700408
409#ifdef ADDRESS_SANITIZER
410 void* RedzoneBegin() { return &redzone_begin_; }
411 void* RedzoneEnd() { return &redzone_end_ + 1; }
mistergc2e75482017-09-19 16:54:40 -0400412#endif // ADDRESS_SANITIZER
413
Abseil Team2125e642018-08-01 04:34:12 -0700414 void AnnotateConstruct(size_type);
415 void AnnotateDestruct(size_type);
mistergc2e75482017-09-19 16:54:40 -0400416
Abseil Team134496a2018-06-29 14:00:35 -0700417 ADDRESS_SANITIZER_REDZONE(redzone_begin_);
418 std::array<StorageElementBuffer, inline_elements> inlined_storage_;
419 ADDRESS_SANITIZER_REDZONE(redzone_end_);
mistergc2e75482017-09-19 16:54:40 -0400420 };
421
Abseil Team134496a2018-06-29 14:00:35 -0700422 struct EmptyInlinedStorage {
423 StorageElement* data() { return nullptr; }
Abseil Team2125e642018-08-01 04:34:12 -0700424 void AnnotateConstruct(size_type) {}
425 void AnnotateDestruct(size_type) {}
mistergc2e75482017-09-19 16:54:40 -0400426 };
427
Abseil Team134496a2018-06-29 14:00:35 -0700428 using InlinedStorage =
429 absl::conditional_t<inline_elements == 0, EmptyInlinedStorage,
430 NonEmptyInlinedStorage>;
mistergc2e75482017-09-19 16:54:40 -0400431
Abseil Team134496a2018-06-29 14:00:35 -0700432 // Storage
433 //
434 // An instance of Storage manages the inline and out-of-line memory for
435 // instances of FixedArray. This guarantees that even when construction of
436 // individual elements fails in the FixedArray constructor body, the
437 // destructor for Storage will still be called and out-of-line memory will be
438 // properly deallocated.
439 //
440 class Storage : public InlinedStorage {
441 public:
Abseil Team2125e642018-08-01 04:34:12 -0700442 Storage(size_type n, const allocator_type& a)
443 : size_alloc_(n, a), data_(InitializeData()) {}
444
Abseil Team134496a2018-06-29 14:00:35 -0700445 ~Storage() noexcept {
446 if (UsingInlinedStorage(size())) {
Abseil Team2125e642018-08-01 04:34:12 -0700447 InlinedStorage::AnnotateDestruct(size());
Abseil Team134496a2018-06-29 14:00:35 -0700448 } else {
Abseil Team2125e642018-08-01 04:34:12 -0700449 AllocatorTraits::deallocate(*alloc(), AsValueType(begin()), size());
mistergc2e75482017-09-19 16:54:40 -0400450 }
451 }
Abseil Team134496a2018-06-29 14:00:35 -0700452
Abseil Team2125e642018-08-01 04:34:12 -0700453 size_type size() const { return size_alloc_.template get<0>(); }
Abseil Team134496a2018-06-29 14:00:35 -0700454 StorageElement* begin() const { return data_; }
455 StorageElement* end() const { return begin() + size(); }
Abseil Team2125e642018-08-01 04:34:12 -0700456 allocator_type* alloc() {
457 return std::addressof(size_alloc_.template get<1>());
458 }
mistergc2e75482017-09-19 16:54:40 -0400459
460 private:
Abseil Team134496a2018-06-29 14:00:35 -0700461 static bool UsingInlinedStorage(size_type n) {
462 return n <= inline_elements;
463 }
464
Abseil Team2125e642018-08-01 04:34:12 -0700465 StorageElement* InitializeData() {
466 if (UsingInlinedStorage(size())) {
467 InlinedStorage::AnnotateConstruct(size());
Abseil Team134496a2018-06-29 14:00:35 -0700468 return InlinedStorage::data();
469 } else {
Abseil Team2125e642018-08-01 04:34:12 -0700470 return reinterpret_cast<StorageElement*>(
471 AllocatorTraits::allocate(*alloc(), size()));
mistergc2e75482017-09-19 16:54:40 -0400472 }
473 }
474
Abseil Team2125e642018-08-01 04:34:12 -0700475 // `CompressedTuple` takes advantage of EBCO for stateless `allocator_type`s
476 container_internal::CompressedTuple<size_type, allocator_type> size_alloc_;
477 StorageElement* data_;
mistergc2e75482017-09-19 16:54:40 -0400478 };
479
Abseil Team2125e642018-08-01 04:34:12 -0700480 Storage storage_;
mistergc2e75482017-09-19 16:54:40 -0400481};
482
Abseil Team2125e642018-08-01 04:34:12 -0700483template <typename T, size_t N, typename A>
484constexpr size_t FixedArray<T, N, A>::kInlineBytesDefault;
mistergc2e75482017-09-19 16:54:40 -0400485
Abseil Team2125e642018-08-01 04:34:12 -0700486template <typename T, size_t N, typename A>
487constexpr typename FixedArray<T, N, A>::size_type
488 FixedArray<T, N, A>::inline_elements;
mistergc2e75482017-09-19 16:54:40 -0400489
Abseil Team2125e642018-08-01 04:34:12 -0700490template <typename T, size_t N, typename A>
491void FixedArray<T, N, A>::NonEmptyInlinedStorage::AnnotateConstruct(
492 typename FixedArray<T, N, A>::size_type n) {
Abseil Team134496a2018-06-29 14:00:35 -0700493#ifdef ADDRESS_SANITIZER
494 if (!n) return;
495 ANNOTATE_CONTIGUOUS_CONTAINER(data(), RedzoneEnd(), RedzoneEnd(), data() + n);
496 ANNOTATE_CONTIGUOUS_CONTAINER(RedzoneBegin(), data(), data(), RedzoneBegin());
497#endif // ADDRESS_SANITIZER
498 static_cast<void>(n); // Mark used when not in asan mode
499}
500
Abseil Team2125e642018-08-01 04:34:12 -0700501template <typename T, size_t N, typename A>
502void FixedArray<T, N, A>::NonEmptyInlinedStorage::AnnotateDestruct(
503 typename FixedArray<T, N, A>::size_type n) {
Abseil Team134496a2018-06-29 14:00:35 -0700504#ifdef ADDRESS_SANITIZER
505 if (!n) return;
506 ANNOTATE_CONTIGUOUS_CONTAINER(data(), RedzoneEnd(), data() + n, RedzoneEnd());
507 ANNOTATE_CONTIGUOUS_CONTAINER(RedzoneBegin(), data(), RedzoneBegin(), data());
508#endif // ADDRESS_SANITIZER
509 static_cast<void>(n); // Mark used when not in asan mode
510}
mistergc2e75482017-09-19 16:54:40 -0400511} // namespace absl
512#endif // ABSL_CONTAINER_FIXED_ARRAY_H_