fixed_array.h 19 KB

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