1 // Copyright 2018 The Abseil Authors.
2 //
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 // https://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 <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"
44 #include "absl/base/config.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"
50 #include "absl/container/internal/compressed_tuple.h"
51 #include "absl/memory/memory.h"
52
53 namespace absl {
54 ABSL_NAMESPACE_BEGIN
55
56 constexpr static auto kFixedArrayUseDefault = static_cast<size_t>(-1);
57
58 // -----------------------------------------------------------------------------
59 // FixedArray
60 // -----------------------------------------------------------------------------
61 //
62 // A `FixedArray` provides a run-time fixed-size array, allocating a small array
63 // inline for efficiency.
64 //
65 // Most users should not specify the `N` template parameter and let `FixedArray`
66 // automatically determine the number of elements to store inline based on
67 // `sizeof(T)`. If `N` is specified, the `FixedArray` implementation will use
68 // inline storage for arrays with a length <= `N`.
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 template <typename T, size_t N = kFixedArrayUseDefault,
76 typename A = std::allocator<T>>
77 class FixedArray {
78 static_assert(!std::is_array<T>::value || std::extent<T>::value > 0,
79 "Arrays with unknown bounds cannot be used with FixedArray.");
80
81 static constexpr size_t kInlineBytesDefault = 256;
82
83 using AllocatorTraits = std::allocator_traits<A>;
84 // std::iterator_traits isn't guaranteed to be SFINAE-friendly until C++17,
85 // but this seems to be mostly pedantic.
86 template <typename Iterator>
87 using EnableIfForwardIterator = absl::enable_if_t<std::is_convertible<
88 typename std::iterator_traits<Iterator>::iterator_category,
89 std::forward_iterator_tag>::value>;
NoexceptCopyable()90 static constexpr bool NoexceptCopyable() {
91 return std::is_nothrow_copy_constructible<StorageElement>::value &&
92 absl::allocator_is_nothrow<allocator_type>::value;
93 }
NoexceptMovable()94 static constexpr bool NoexceptMovable() {
95 return std::is_nothrow_move_constructible<StorageElement>::value &&
96 absl::allocator_is_nothrow<allocator_type>::value;
97 }
DefaultConstructorIsNonTrivial()98 static constexpr bool DefaultConstructorIsNonTrivial() {
99 return !absl::is_trivially_default_constructible<StorageElement>::value;
100 }
101
102 public:
103 using allocator_type = typename AllocatorTraits::allocator_type;
104 using value_type = typename AllocatorTraits::value_type;
105 using pointer = typename AllocatorTraits::pointer;
106 using const_pointer = typename AllocatorTraits::const_pointer;
107 using reference = value_type&;
108 using const_reference = const value_type&;
109 using size_type = typename AllocatorTraits::size_type;
110 using difference_type = typename AllocatorTraits::difference_type;
111 using iterator = pointer;
112 using const_iterator = const_pointer;
113 using reverse_iterator = std::reverse_iterator<iterator>;
114 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
115
116 static constexpr size_type inline_elements =
117 (N == kFixedArrayUseDefault ? kInlineBytesDefault / sizeof(value_type)
118 : static_cast<size_type>(N));
119
120 FixedArray(
121 const FixedArray& other,
noexcept(NoexceptCopyable ())122 const allocator_type& a = allocator_type()) noexcept(NoexceptCopyable())
123 : FixedArray(other.begin(), other.end(), a) {}
124
125 FixedArray(
126 FixedArray&& other,
noexcept(NoexceptMovable ())127 const allocator_type& a = allocator_type()) noexcept(NoexceptMovable())
128 : FixedArray(std::make_move_iterator(other.begin()),
129 std::make_move_iterator(other.end()), a) {}
130
131 // Creates an array object that can store `n` elements.
132 // Note that trivially constructible elements will be uninitialized.
133 explicit FixedArray(size_type n, const allocator_type& a = allocator_type())
storage_(n,a)134 : storage_(n, a) {
135 if (DefaultConstructorIsNonTrivial()) {
136 memory_internal::ConstructRange(storage_.alloc(), storage_.begin(),
137 storage_.end());
138 }
139 }
140
141 // Creates an array initialized with `n` copies of `val`.
142 FixedArray(size_type n, const value_type& val,
143 const allocator_type& a = allocator_type())
storage_(n,a)144 : storage_(n, a) {
145 memory_internal::ConstructRange(storage_.alloc(), storage_.begin(),
146 storage_.end(), val);
147 }
148
149 // Creates an array initialized with the size and contents of `init_list`.
150 FixedArray(std::initializer_list<value_type> init_list,
151 const allocator_type& a = allocator_type())
152 : FixedArray(init_list.begin(), init_list.end(), a) {}
153
154 // Creates an array initialized with the elements from the input
155 // range. The array's size will always be `std::distance(first, last)`.
156 // REQUIRES: Iterator must be a forward_iterator or better.
157 template <typename Iterator, EnableIfForwardIterator<Iterator>* = nullptr>
158 FixedArray(Iterator first, Iterator last,
159 const allocator_type& a = allocator_type())
storage_(std::distance (first,last),a)160 : storage_(std::distance(first, last), a) {
161 memory_internal::CopyRange(storage_.alloc(), storage_.begin(), first, last);
162 }
163
~FixedArray()164 ~FixedArray() noexcept {
165 for (auto* cur = storage_.begin(); cur != storage_.end(); ++cur) {
166 AllocatorTraits::destroy(storage_.alloc(), cur);
167 }
168 }
169
170 // Assignments are deleted because they break the invariant that the size of a
171 // `FixedArray` never changes.
172 void operator=(FixedArray&&) = delete;
173 void operator=(const FixedArray&) = delete;
174
175 // FixedArray::size()
176 //
177 // Returns the length of the fixed array.
size()178 size_type size() const { return storage_.size(); }
179
180 // FixedArray::max_size()
181 //
182 // Returns the largest possible value of `std::distance(begin(), end())` for a
183 // `FixedArray<T>`. This is equivalent to the most possible addressable bytes
184 // over the number of bytes taken by T.
max_size()185 constexpr size_type max_size() const {
186 return (std::numeric_limits<difference_type>::max)() / sizeof(value_type);
187 }
188
189 // FixedArray::empty()
190 //
191 // Returns whether or not the fixed array is empty.
empty()192 bool empty() const { return size() == 0; }
193
194 // FixedArray::memsize()
195 //
196 // Returns the memory size of the fixed array in bytes.
memsize()197 size_t memsize() const { return size() * sizeof(value_type); }
198
199 // FixedArray::data()
200 //
201 // Returns a const T* pointer to elements of the `FixedArray`. This pointer
202 // can be used to access (but not modify) the contained elements.
data()203 const_pointer data() const { return AsValueType(storage_.begin()); }
204
205 // Overload of FixedArray::data() to return a T* pointer to elements of the
206 // fixed array. This pointer can be used to access and modify the contained
207 // elements.
data()208 pointer data() { return AsValueType(storage_.begin()); }
209
210 // FixedArray::operator[]
211 //
212 // Returns a reference the ith element of the fixed array.
213 // REQUIRES: 0 <= i < size()
214 reference operator[](size_type i) {
215 ABSL_HARDENING_ASSERT(i < size());
216 return data()[i];
217 }
218
219 // Overload of FixedArray::operator()[] to return a const reference to the
220 // ith element of the fixed array.
221 // REQUIRES: 0 <= i < size()
222 const_reference operator[](size_type i) const {
223 ABSL_HARDENING_ASSERT(i < size());
224 return data()[i];
225 }
226
227 // FixedArray::at
228 //
229 // Bounds-checked access. Returns a reference to the ith element of the fixed
230 // array, or throws std::out_of_range
at(size_type i)231 reference at(size_type i) {
232 if (ABSL_PREDICT_FALSE(i >= size())) {
233 base_internal::ThrowStdOutOfRange("FixedArray::at failed bounds check");
234 }
235 return data()[i];
236 }
237
238 // Overload of FixedArray::at() to return a const reference to the ith element
239 // of the fixed array.
at(size_type i)240 const_reference at(size_type i) const {
241 if (ABSL_PREDICT_FALSE(i >= size())) {
242 base_internal::ThrowStdOutOfRange("FixedArray::at failed bounds check");
243 }
244 return data()[i];
245 }
246
247 // FixedArray::front()
248 //
249 // Returns a reference to the first element of the fixed array.
front()250 reference front() {
251 ABSL_HARDENING_ASSERT(!empty());
252 return data()[0];
253 }
254
255 // Overload of FixedArray::front() to return a reference to the first element
256 // of a fixed array of const values.
front()257 const_reference front() const {
258 ABSL_HARDENING_ASSERT(!empty());
259 return data()[0];
260 }
261
262 // FixedArray::back()
263 //
264 // Returns a reference to the last element of the fixed array.
back()265 reference back() {
266 ABSL_HARDENING_ASSERT(!empty());
267 return data()[size() - 1];
268 }
269
270 // Overload of FixedArray::back() to return a reference to the last element
271 // of a fixed array of const values.
back()272 const_reference back() const {
273 ABSL_HARDENING_ASSERT(!empty());
274 return data()[size() - 1];
275 }
276
277 // FixedArray::begin()
278 //
279 // Returns an iterator to the beginning of the fixed array.
begin()280 iterator begin() { return data(); }
281
282 // Overload of FixedArray::begin() to return a const iterator to the
283 // beginning of the fixed array.
begin()284 const_iterator begin() const { return data(); }
285
286 // FixedArray::cbegin()
287 //
288 // Returns a const iterator to the beginning of the fixed array.
cbegin()289 const_iterator cbegin() const { return begin(); }
290
291 // FixedArray::end()
292 //
293 // Returns an iterator to the end of the fixed array.
end()294 iterator end() { return data() + size(); }
295
296 // Overload of FixedArray::end() to return a const iterator to the end of the
297 // fixed array.
end()298 const_iterator end() const { return data() + size(); }
299
300 // FixedArray::cend()
301 //
302 // Returns a const iterator to the end of the fixed array.
cend()303 const_iterator cend() const { return end(); }
304
305 // FixedArray::rbegin()
306 //
307 // Returns a reverse iterator from the end of the fixed array.
rbegin()308 reverse_iterator rbegin() { return reverse_iterator(end()); }
309
310 // Overload of FixedArray::rbegin() to return a const reverse iterator from
311 // the end of the fixed array.
rbegin()312 const_reverse_iterator rbegin() const {
313 return const_reverse_iterator(end());
314 }
315
316 // FixedArray::crbegin()
317 //
318 // Returns a const reverse iterator from the end of the fixed array.
crbegin()319 const_reverse_iterator crbegin() const { return rbegin(); }
320
321 // FixedArray::rend()
322 //
323 // Returns a reverse iterator from the beginning of the fixed array.
rend()324 reverse_iterator rend() { return reverse_iterator(begin()); }
325
326 // Overload of FixedArray::rend() for returning a const reverse iterator
327 // from the beginning of the fixed array.
rend()328 const_reverse_iterator rend() const {
329 return const_reverse_iterator(begin());
330 }
331
332 // FixedArray::crend()
333 //
334 // Returns a reverse iterator from the beginning of the fixed array.
crend()335 const_reverse_iterator crend() const { return rend(); }
336
337 // FixedArray::fill()
338 //
339 // Assigns the given `value` to all elements in the fixed array.
fill(const value_type & val)340 void fill(const value_type& val) { std::fill(begin(), end(), val); }
341
342 // Relational operators. Equality operators are elementwise using
343 // `operator==`, while order operators order FixedArrays lexicographically.
344 friend bool operator==(const FixedArray& lhs, const FixedArray& rhs) {
345 return absl::equal(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
346 }
347
348 friend bool operator!=(const FixedArray& lhs, const FixedArray& rhs) {
349 return !(lhs == rhs);
350 }
351
352 friend bool operator<(const FixedArray& lhs, const FixedArray& rhs) {
353 return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(),
354 rhs.end());
355 }
356
357 friend bool operator>(const FixedArray& lhs, const FixedArray& rhs) {
358 return rhs < lhs;
359 }
360
361 friend bool operator<=(const FixedArray& lhs, const FixedArray& rhs) {
362 return !(rhs < lhs);
363 }
364
365 friend bool operator>=(const FixedArray& lhs, const FixedArray& rhs) {
366 return !(lhs < rhs);
367 }
368
369 template <typename H>
AbslHashValue(H h,const FixedArray & v)370 friend H AbslHashValue(H h, const FixedArray& v) {
371 return H::combine(H::combine_contiguous(std::move(h), v.data(), v.size()),
372 v.size());
373 }
374
375 private:
376 // StorageElement
377 //
378 // For FixedArrays with a C-style-array value_type, StorageElement is a POD
379 // wrapper struct called StorageElementWrapper that holds the value_type
380 // instance inside. This is needed for construction and destruction of the
381 // entire array regardless of how many dimensions it has. For all other cases,
382 // StorageElement is just an alias of value_type.
383 //
384 // Maintainer's Note: The simpler solution would be to simply wrap value_type
385 // in a struct whether it's an array or not. That causes some paranoid
386 // diagnostics to misfire, believing that 'data()' returns a pointer to a
387 // single element, rather than the packed array that it really is.
388 // e.g.:
389 //
390 // FixedArray<char> buf(1);
391 // sprintf(buf.data(), "foo");
392 //
393 // error: call to int __builtin___sprintf_chk(etc...)
394 // will always overflow destination buffer [-Werror]
395 //
396 template <typename OuterT, typename InnerT = absl::remove_extent_t<OuterT>,
397 size_t InnerN = std::extent<OuterT>::value>
398 struct StorageElementWrapper {
399 InnerT array[InnerN];
400 };
401
402 using StorageElement =
403 absl::conditional_t<std::is_array<value_type>::value,
404 StorageElementWrapper<value_type>, value_type>;
405
AsValueType(pointer ptr)406 static pointer AsValueType(pointer ptr) { return ptr; }
AsValueType(StorageElementWrapper<value_type> * ptr)407 static pointer AsValueType(StorageElementWrapper<value_type>* ptr) {
408 return std::addressof(ptr->array);
409 }
410
411 static_assert(sizeof(StorageElement) == sizeof(value_type), "");
412 static_assert(alignof(StorageElement) == alignof(value_type), "");
413
414 class NonEmptyInlinedStorage {
415 public:
data()416 StorageElement* data() { return reinterpret_cast<StorageElement*>(buff_); }
417 void AnnotateConstruct(size_type n);
418 void AnnotateDestruct(size_type n);
419
420 #ifdef ABSL_HAVE_ADDRESS_SANITIZER
RedzoneBegin()421 void* RedzoneBegin() { return &redzone_begin_; }
RedzoneEnd()422 void* RedzoneEnd() { return &redzone_end_ + 1; }
423 #endif // ABSL_HAVE_ADDRESS_SANITIZER
424
425 private:
426 ABSL_ADDRESS_SANITIZER_REDZONE(redzone_begin_);
427 alignas(StorageElement) char buff_[sizeof(StorageElement[inline_elements])];
428 ABSL_ADDRESS_SANITIZER_REDZONE(redzone_end_);
429 };
430
431 class EmptyInlinedStorage {
432 public:
data()433 StorageElement* data() { return nullptr; }
AnnotateConstruct(size_type)434 void AnnotateConstruct(size_type) {}
AnnotateDestruct(size_type)435 void AnnotateDestruct(size_type) {}
436 };
437
438 using InlinedStorage =
439 absl::conditional_t<inline_elements == 0, EmptyInlinedStorage,
440 NonEmptyInlinedStorage>;
441
442 // Storage
443 //
444 // An instance of Storage manages the inline and out-of-line memory for
445 // instances of FixedArray. This guarantees that even when construction of
446 // individual elements fails in the FixedArray constructor body, the
447 // destructor for Storage will still be called and out-of-line memory will be
448 // properly deallocated.
449 //
450 class Storage : public InlinedStorage {
451 public:
Storage(size_type n,const allocator_type & a)452 Storage(size_type n, const allocator_type& a)
453 : size_alloc_(n, a), data_(InitializeData()) {}
454
~Storage()455 ~Storage() noexcept {
456 if (UsingInlinedStorage(size())) {
457 InlinedStorage::AnnotateDestruct(size());
458 } else {
459 AllocatorTraits::deallocate(alloc(), AsValueType(begin()), size());
460 }
461 }
462
size()463 size_type size() const { return size_alloc_.template get<0>(); }
begin()464 StorageElement* begin() const { return data_; }
end()465 StorageElement* end() const { return begin() + size(); }
alloc()466 allocator_type& alloc() { return size_alloc_.template get<1>(); }
467
468 private:
UsingInlinedStorage(size_type n)469 static bool UsingInlinedStorage(size_type n) {
470 return n <= inline_elements;
471 }
472
473 #ifdef ABSL_HAVE_ADDRESS_SANITIZER
474 ABSL_ATTRIBUTE_NOINLINE
475 #endif // ABSL_HAVE_ADDRESS_SANITIZER
InitializeData()476 StorageElement* InitializeData() {
477 if (UsingInlinedStorage(size())) {
478 InlinedStorage::AnnotateConstruct(size());
479 return InlinedStorage::data();
480 } else {
481 return reinterpret_cast<StorageElement*>(
482 AllocatorTraits::allocate(alloc(), size()));
483 }
484 }
485
486 // `CompressedTuple` takes advantage of EBCO for stateless `allocator_type`s
487 container_internal::CompressedTuple<size_type, allocator_type> size_alloc_;
488 StorageElement* data_;
489 };
490
491 Storage storage_;
492 };
493
494 #ifdef ABSL_INTERNAL_NEED_REDUNDANT_CONSTEXPR_DECL
495 template <typename T, size_t N, typename A>
496 constexpr size_t FixedArray<T, N, A>::kInlineBytesDefault;
497
498 template <typename T, size_t N, typename A>
499 constexpr typename FixedArray<T, N, A>::size_type
500 FixedArray<T, N, A>::inline_elements;
501 #endif
502
503 template <typename T, size_t N, typename A>
AnnotateConstruct(typename FixedArray<T,N,A>::size_type n)504 void FixedArray<T, N, A>::NonEmptyInlinedStorage::AnnotateConstruct(
505 typename FixedArray<T, N, A>::size_type n) {
506 #ifdef ABSL_HAVE_ADDRESS_SANITIZER
507 if (!n) return;
508 ABSL_ANNOTATE_CONTIGUOUS_CONTAINER(data(), RedzoneEnd(), RedzoneEnd(),
509 data() + n);
510 ABSL_ANNOTATE_CONTIGUOUS_CONTAINER(RedzoneBegin(), data(), data(),
511 RedzoneBegin());
512 #endif // ABSL_HAVE_ADDRESS_SANITIZER
513 static_cast<void>(n); // Mark used when not in asan mode
514 }
515
516 template <typename T, size_t N, typename A>
AnnotateDestruct(typename FixedArray<T,N,A>::size_type n)517 void FixedArray<T, N, A>::NonEmptyInlinedStorage::AnnotateDestruct(
518 typename FixedArray<T, N, A>::size_type n) {
519 #ifdef ABSL_HAVE_ADDRESS_SANITIZER
520 if (!n) return;
521 ABSL_ANNOTATE_CONTIGUOUS_CONTAINER(data(), RedzoneEnd(), data() + n,
522 RedzoneEnd());
523 ABSL_ANNOTATE_CONTIGUOUS_CONTAINER(RedzoneBegin(), data(), RedzoneBegin(),
524 data());
525 #endif // ABSL_HAVE_ADDRESS_SANITIZER
526 static_cast<void>(n); // Mark used when not in asan mode
527 }
528 ABSL_NAMESPACE_END
529 } // namespace absl
530
531 #endif // ABSL_CONTAINER_FIXED_ARRAY_H_
532