fixed_array.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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 CERES_PUBLIC_INTERNAL_FIXED_ARRAY_H_
  30. #define CERES_PUBLIC_INTERNAL_FIXED_ARRAY_H_
  31. #include <Eigen/Core> // For Eigen::aligned_allocator
  32. #include <algorithm>
  33. #include <array>
  34. #include <cstddef>
  35. #include <memory>
  36. #include <tuple>
  37. #include <type_traits>
  38. #include "ceres/internal/memory.h"
  39. #include "glog/logging.h"
  40. namespace ceres::internal {
  41. constexpr static auto kFixedArrayUseDefault = static_cast<size_t>(-1);
  42. // The default fixed array allocator.
  43. //
  44. // As one can not easily detect if a struct contains or inherits from a fixed
  45. // size Eigen type, to be safe the Eigen::aligned_allocator is used by default.
  46. // But trivial types can never contain Eigen types, so std::allocator is used to
  47. // safe some heap memory.
  48. template <typename T>
  49. using FixedArrayDefaultAllocator =
  50. typename std::conditional<std::is_trivial<T>::value,
  51. std::allocator<T>,
  52. Eigen::aligned_allocator<T>>::type;
  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. //
  72. // Note that `FixedArray` does not provide a public allocator; if it requires a
  73. // heap allocation, it will do so with global `::operator new[]()` and
  74. // `::operator delete[]()`, even if T provides class-scope overrides for these
  75. // operators.
  76. template <typename T,
  77. size_t N = kFixedArrayUseDefault,
  78. typename A = FixedArrayDefaultAllocator<T>>
  79. class FixedArray {
  80. static_assert(!std::is_array<T>::value || std::extent<T>::value > 0,
  81. "Arrays with unknown bounds cannot be used with FixedArray.");
  82. static constexpr size_t kInlineBytesDefault = 256;
  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 = typename std::enable_if<std::is_convertible<
  88. typename std::iterator_traits<Iterator>::iterator_category,
  89. std::forward_iterator_tag>::value>::type;
  90. static constexpr bool DefaultConstructorIsNonTrivial() {
  91. return !std::is_trivially_default_constructible<StorageElement>::value;
  92. }
  93. public:
  94. using allocator_type = typename AllocatorTraits::allocator_type;
  95. using value_type = typename AllocatorTraits::value_type;
  96. using pointer = typename AllocatorTraits::pointer;
  97. using const_pointer = typename AllocatorTraits::const_pointer;
  98. using reference = value_type&;
  99. using const_reference = const value_type&;
  100. using size_type = typename AllocatorTraits::size_type;
  101. using difference_type = typename AllocatorTraits::difference_type;
  102. using iterator = pointer;
  103. using const_iterator = const_pointer;
  104. using reverse_iterator = std::reverse_iterator<iterator>;
  105. using const_reverse_iterator = std::reverse_iterator<const_iterator>;
  106. static constexpr size_type inline_elements =
  107. (N == kFixedArrayUseDefault ? kInlineBytesDefault / sizeof(value_type)
  108. : static_cast<size_type>(N));
  109. FixedArray(const FixedArray& other,
  110. const allocator_type& a = allocator_type())
  111. : FixedArray(other.begin(), other.end(), a) {}
  112. FixedArray(FixedArray&& other, const allocator_type& a = allocator_type())
  113. : FixedArray(std::make_move_iterator(other.begin()),
  114. std::make_move_iterator(other.end()),
  115. a) {}
  116. // Creates an array object that can store `n` elements.
  117. // Note that trivially constructible elements will be uninitialized.
  118. explicit FixedArray(size_type n, const allocator_type& a = allocator_type())
  119. : storage_(n, a) {
  120. if (DefaultConstructorIsNonTrivial()) {
  121. ConstructRange(storage_.alloc(), storage_.begin(), storage_.end());
  122. }
  123. }
  124. // Creates an array initialized with `n` copies of `val`.
  125. FixedArray(size_type n,
  126. const value_type& val,
  127. const allocator_type& a = allocator_type())
  128. : storage_(n, a) {
  129. ConstructRange(storage_.alloc(), storage_.begin(), storage_.end(), val);
  130. }
  131. // Creates an array initialized with the size and contents of `init_list`.
  132. FixedArray(std::initializer_list<value_type> init_list,
  133. const allocator_type& a = allocator_type())
  134. : FixedArray(init_list.begin(), init_list.end(), a) {}
  135. // Creates an array initialized with the elements from the input
  136. // range. The array's size will always be `std::distance(first, last)`.
  137. // REQUIRES: Iterator must be a forward_iterator or better.
  138. template <typename Iterator, EnableIfForwardIterator<Iterator>* = nullptr>
  139. FixedArray(Iterator first,
  140. Iterator last,
  141. const allocator_type& a = allocator_type())
  142. : storage_(std::distance(first, last), a) {
  143. CopyRange(storage_.alloc(), storage_.begin(), first, last);
  144. }
  145. ~FixedArray() noexcept {
  146. for (auto* cur = storage_.begin(); cur != storage_.end(); ++cur) {
  147. AllocatorTraits::destroy(storage_.alloc(), cur);
  148. }
  149. }
  150. // Assignments are deleted because they break the invariant that the size of a
  151. // `FixedArray` never changes.
  152. void operator=(FixedArray&&) = delete;
  153. void operator=(const FixedArray&) = delete;
  154. // FixedArray::size()
  155. //
  156. // Returns the length of the fixed array.
  157. size_type size() const { return storage_.size(); }
  158. // FixedArray::max_size()
  159. //
  160. // Returns the largest possible value of `std::distance(begin(), end())` for a
  161. // `FixedArray<T>`. This is equivalent to the most possible addressable bytes
  162. // over the number of bytes taken by T.
  163. constexpr size_type max_size() const {
  164. return (std::numeric_limits<difference_type>::max)() / sizeof(value_type);
  165. }
  166. // FixedArray::empty()
  167. //
  168. // Returns whether or not the fixed array is empty.
  169. bool empty() const { return size() == 0; }
  170. // FixedArray::memsize()
  171. //
  172. // Returns the memory size of the fixed array in bytes.
  173. size_t memsize() const { return size() * sizeof(value_type); }
  174. // FixedArray::data()
  175. //
  176. // Returns a const T* pointer to elements of the `FixedArray`. This pointer
  177. // can be used to access (but not modify) the contained elements.
  178. const_pointer data() const { return AsValueType(storage_.begin()); }
  179. // Overload of FixedArray::data() to return a T* pointer to elements of the
  180. // fixed array. This pointer can be used to access and modify the contained
  181. // elements.
  182. pointer data() { return AsValueType(storage_.begin()); }
  183. // FixedArray::operator[]
  184. //
  185. // Returns a reference the ith element of the fixed array.
  186. // REQUIRES: 0 <= i < size()
  187. reference operator[](size_type i) {
  188. DCHECK_LT(i, size());
  189. return data()[i];
  190. }
  191. // Overload of FixedArray::operator()[] to return a const reference to the
  192. // ith element of the fixed array.
  193. // REQUIRES: 0 <= i < size()
  194. const_reference operator[](size_type i) const {
  195. DCHECK_LT(i, size());
  196. return data()[i];
  197. }
  198. // FixedArray::front()
  199. //
  200. // Returns a reference to the first element of the fixed array.
  201. reference front() { return *begin(); }
  202. // Overload of FixedArray::front() to return a reference to the first element
  203. // of a fixed array of const values.
  204. const_reference front() const { return *begin(); }
  205. // FixedArray::back()
  206. //
  207. // Returns a reference to the last element of the fixed array.
  208. reference back() { return *(end() - 1); }
  209. // Overload of FixedArray::back() to return a reference to the last element
  210. // of a fixed array of const values.
  211. const_reference back() const { return *(end() - 1); }
  212. // FixedArray::begin()
  213. //
  214. // Returns an iterator to the beginning of the fixed array.
  215. iterator begin() { return data(); }
  216. // Overload of FixedArray::begin() to return a const iterator to the
  217. // beginning of the fixed array.
  218. const_iterator begin() const { return data(); }
  219. // FixedArray::cbegin()
  220. //
  221. // Returns a const iterator to the beginning of the fixed array.
  222. const_iterator cbegin() const { return begin(); }
  223. // FixedArray::end()
  224. //
  225. // Returns an iterator to the end of the fixed array.
  226. iterator end() { return data() + size(); }
  227. // Overload of FixedArray::end() to return a const iterator to the end of the
  228. // fixed array.
  229. const_iterator end() const { return data() + size(); }
  230. // FixedArray::cend()
  231. //
  232. // Returns a const iterator to the end of the fixed array.
  233. const_iterator cend() const { return end(); }
  234. // FixedArray::rbegin()
  235. //
  236. // Returns a reverse iterator from the end of the fixed array.
  237. reverse_iterator rbegin() { return reverse_iterator(end()); }
  238. // Overload of FixedArray::rbegin() to return a const reverse iterator from
  239. // the end of the fixed array.
  240. const_reverse_iterator rbegin() const {
  241. return const_reverse_iterator(end());
  242. }
  243. // FixedArray::crbegin()
  244. //
  245. // Returns a const reverse iterator from the end of the fixed array.
  246. const_reverse_iterator crbegin() const { return rbegin(); }
  247. // FixedArray::rend()
  248. //
  249. // Returns a reverse iterator from the beginning of the fixed array.
  250. reverse_iterator rend() { return reverse_iterator(begin()); }
  251. // Overload of FixedArray::rend() for returning a const reverse iterator
  252. // from the beginning of the fixed array.
  253. const_reverse_iterator rend() const {
  254. return const_reverse_iterator(begin());
  255. }
  256. // FixedArray::crend()
  257. //
  258. // Returns a reverse iterator from the beginning of the fixed array.
  259. const_reverse_iterator crend() const { return rend(); }
  260. // FixedArray::fill()
  261. //
  262. // Assigns the given `value` to all elements in the fixed array.
  263. void fill(const value_type& val) { std::fill(begin(), end(), val); }
  264. // Relational operators. Equality operators are elementwise using
  265. // `operator==`, while order operators order FixedArrays lexicographically.
  266. friend bool operator==(const FixedArray& lhs, const FixedArray& rhs) {
  267. return std::equal(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
  268. }
  269. friend bool operator!=(const FixedArray& lhs, const FixedArray& rhs) {
  270. return !(lhs == rhs);
  271. }
  272. friend bool operator<(const FixedArray& lhs, const FixedArray& rhs) {
  273. return std::lexicographical_compare(
  274. lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
  275. }
  276. friend bool operator>(const FixedArray& lhs, const FixedArray& rhs) {
  277. return rhs < lhs;
  278. }
  279. friend bool operator<=(const FixedArray& lhs, const FixedArray& rhs) {
  280. return !(rhs < lhs);
  281. }
  282. friend bool operator>=(const FixedArray& lhs, const FixedArray& rhs) {
  283. return !(lhs < rhs);
  284. }
  285. private:
  286. // StorageElement
  287. //
  288. // For FixedArrays with a C-style-array value_type, StorageElement is a POD
  289. // wrapper struct called StorageElementWrapper that holds the value_type
  290. // instance inside. This is needed for construction and destruction of the
  291. // entire array regardless of how many dimensions it has. For all other cases,
  292. // StorageElement is just an alias of value_type.
  293. //
  294. // Maintainer's Note: The simpler solution would be to simply wrap value_type
  295. // in a struct whether it's an array or not. That causes some paranoid
  296. // diagnostics to misfire, believing that 'data()' returns a pointer to a
  297. // single element, rather than the packed array that it really is.
  298. // e.g.:
  299. //
  300. // FixedArray<char> buf(1);
  301. // sprintf(buf.data(), "foo");
  302. //
  303. // error: call to int __builtin___sprintf_chk(etc...)
  304. // will always overflow destination buffer [-Werror]
  305. //
  306. template <typename OuterT,
  307. typename InnerT = typename std::remove_extent<OuterT>::type,
  308. size_t InnerN = std::extent<OuterT>::value>
  309. struct StorageElementWrapper {
  310. InnerT array[InnerN];
  311. };
  312. using StorageElement =
  313. typename std::conditional<std::is_array<value_type>::value,
  314. StorageElementWrapper<value_type>,
  315. value_type>::type;
  316. static pointer AsValueType(pointer ptr) { return ptr; }
  317. static pointer AsValueType(StorageElementWrapper<value_type>* ptr) {
  318. return std::addressof(ptr->array);
  319. }
  320. static_assert(sizeof(StorageElement) == sizeof(value_type));
  321. static_assert(alignof(StorageElement) == alignof(value_type));
  322. class NonEmptyInlinedStorage {
  323. public:
  324. StorageElement* data() { return reinterpret_cast<StorageElement*>(buff_); }
  325. void AnnotateConstruct(size_type) {}
  326. void AnnotateDestruct(size_type) {}
  327. // #ifdef ADDRESS_SANITIZER
  328. // void* RedzoneBegin() { return &redzone_begin_; }
  329. // void* RedzoneEnd() { return &redzone_end_ + 1; }
  330. // #endif // ADDRESS_SANITIZER
  331. private:
  332. // ADDRESS_SANITIZER_REDZONE(redzone_begin_);
  333. alignas(StorageElement) char buff_[sizeof(StorageElement[inline_elements])];
  334. // ADDRESS_SANITIZER_REDZONE(redzone_end_);
  335. };
  336. class EmptyInlinedStorage {
  337. public:
  338. StorageElement* data() { return nullptr; }
  339. void AnnotateConstruct(size_type) {}
  340. void AnnotateDestruct(size_type) {}
  341. };
  342. using InlinedStorage =
  343. typename std::conditional<inline_elements == 0,
  344. EmptyInlinedStorage,
  345. NonEmptyInlinedStorage>::type;
  346. // Storage
  347. //
  348. // An instance of Storage manages the inline and out-of-line memory for
  349. // instances of FixedArray. This guarantees that even when construction of
  350. // individual elements fails in the FixedArray constructor body, the
  351. // destructor for Storage will still be called and out-of-line memory will be
  352. // properly deallocated.
  353. //
  354. class Storage : public InlinedStorage {
  355. public:
  356. Storage(size_type n, const allocator_type& a)
  357. : size_alloc_(n, a), data_(InitializeData()) {}
  358. ~Storage() noexcept {
  359. if (UsingInlinedStorage(size())) {
  360. InlinedStorage::AnnotateDestruct(size());
  361. } else {
  362. AllocatorTraits::deallocate(alloc(), AsValueType(begin()), size());
  363. }
  364. }
  365. size_type size() const { return std::get<0>(size_alloc_); }
  366. StorageElement* begin() const { return data_; }
  367. StorageElement* end() const { return begin() + size(); }
  368. allocator_type& alloc() { return std::get<1>(size_alloc_); }
  369. private:
  370. static bool UsingInlinedStorage(size_type n) {
  371. return n <= inline_elements;
  372. }
  373. StorageElement* InitializeData() {
  374. if (UsingInlinedStorage(size())) {
  375. InlinedStorage::AnnotateConstruct(size());
  376. return InlinedStorage::data();
  377. } else {
  378. return reinterpret_cast<StorageElement*>(
  379. AllocatorTraits::allocate(alloc(), size()));
  380. }
  381. }
  382. // Using std::tuple and not absl::CompressedTuple, as it has a lot of
  383. // dependencies to other absl headers.
  384. std::tuple<size_type, allocator_type> size_alloc_;
  385. StorageElement* data_;
  386. };
  387. Storage storage_;
  388. };
  389. template <typename T, size_t N, typename A>
  390. constexpr size_t FixedArray<T, N, A>::kInlineBytesDefault;
  391. template <typename T, size_t N, typename A>
  392. constexpr typename FixedArray<T, N, A>::size_type
  393. FixedArray<T, N, A>::inline_elements;
  394. } // namespace ceres::internal
  395. #endif // CERES_PUBLIC_INTERNAL_FIXED_ARRAY_H_