You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

fixed_array.h 23 kB

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