inlined_vector.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  1. // Copyright 2019 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. #ifndef ABSL_CONTAINER_INTERNAL_INLINED_VECTOR_INTERNAL_H_
  15. #define ABSL_CONTAINER_INTERNAL_INLINED_VECTOR_INTERNAL_H_
  16. #include <algorithm>
  17. #include <cstddef>
  18. #include <cstring>
  19. #include <iterator>
  20. #include <limits>
  21. #include <memory>
  22. #include <new>
  23. #include <type_traits>
  24. #include <utility>
  25. #include "absl/base/attributes.h"
  26. #include "absl/base/macros.h"
  27. #include "absl/container/internal/compressed_tuple.h"
  28. #include "absl/memory/memory.h"
  29. #include "absl/meta/type_traits.h"
  30. #include "absl/types/span.h"
  31. namespace absl {
  32. ABSL_NAMESPACE_BEGIN
  33. namespace inlined_vector_internal {
  34. // GCC does not deal very well with the below code
  35. #if !defined(__clang__) && defined(__GNUC__)
  36. #pragma GCC diagnostic push
  37. #pragma GCC diagnostic ignored "-Warray-bounds"
  38. #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
  39. #endif
  40. template <typename A>
  41. using AllocatorTraits = std::allocator_traits<A>;
  42. template <typename A>
  43. using ValueType = typename AllocatorTraits<A>::value_type;
  44. template <typename A>
  45. using SizeType = typename AllocatorTraits<A>::size_type;
  46. template <typename A>
  47. using Pointer = typename AllocatorTraits<A>::pointer;
  48. template <typename A>
  49. using ConstPointer = typename AllocatorTraits<A>::const_pointer;
  50. template <typename A>
  51. using SizeType = typename AllocatorTraits<A>::size_type;
  52. template <typename A>
  53. using DifferenceType = typename AllocatorTraits<A>::difference_type;
  54. template <typename A>
  55. using Reference = ValueType<A>&;
  56. template <typename A>
  57. using ConstReference = const ValueType<A>&;
  58. template <typename A>
  59. using Iterator = Pointer<A>;
  60. template <typename A>
  61. using ConstIterator = ConstPointer<A>;
  62. template <typename A>
  63. using ReverseIterator = typename std::reverse_iterator<Iterator<A>>;
  64. template <typename A>
  65. using ConstReverseIterator = typename std::reverse_iterator<ConstIterator<A>>;
  66. template <typename A>
  67. using MoveIterator = typename std::move_iterator<Iterator<A>>;
  68. template <typename Iterator>
  69. using IsAtLeastForwardIterator = std::is_convertible<
  70. typename std::iterator_traits<Iterator>::iterator_category,
  71. std::forward_iterator_tag>;
  72. template <typename A>
  73. using IsMemcpyOk =
  74. absl::conjunction<std::is_same<A, std::allocator<ValueType<A>>>,
  75. absl::is_trivially_copy_constructible<ValueType<A>>,
  76. absl::is_trivially_copy_assignable<ValueType<A>>,
  77. absl::is_trivially_destructible<ValueType<A>>>;
  78. template <typename T>
  79. struct TypeIdentity {
  80. using type = T;
  81. };
  82. // Used for function arguments in template functions to prevent ADL by forcing
  83. // callers to explicitly specify the template parameter.
  84. template <typename T>
  85. using NoTypeDeduction = typename TypeIdentity<T>::type;
  86. template <typename A>
  87. void DestroyElements(NoTypeDeduction<A>& allocator, Pointer<A> destroy_first,
  88. SizeType<A> destroy_size) {
  89. if (destroy_first != nullptr) {
  90. for (SizeType<A> i = destroy_size; i != 0;) {
  91. --i;
  92. AllocatorTraits<A>::destroy(allocator, destroy_first + i);
  93. }
  94. }
  95. }
  96. template <typename A>
  97. struct Allocation {
  98. Pointer<A> data;
  99. SizeType<A> capacity;
  100. };
  101. template <typename A,
  102. bool IsOverAligned =
  103. (alignof(ValueType<A>) > ABSL_INTERNAL_DEFAULT_NEW_ALIGNMENT)>
  104. struct MallocAdapter {
  105. static Allocation<A> Allocate(A& allocator, SizeType<A> requested_capacity) {
  106. return {AllocatorTraits<A>::allocate(allocator, requested_capacity),
  107. requested_capacity};
  108. }
  109. static void Deallocate(A& allocator, Pointer<A> pointer,
  110. SizeType<A> capacity) {
  111. AllocatorTraits<A>::deallocate(allocator, pointer, capacity);
  112. }
  113. };
  114. template <typename A, typename ValueAdapter>
  115. void ConstructElements(NoTypeDeduction<A>& allocator,
  116. Pointer<A> construct_first, ValueAdapter& values,
  117. SizeType<A> construct_size) {
  118. for (SizeType<A> i = 0; i < construct_size; ++i) {
  119. ABSL_INTERNAL_TRY { values.ConstructNext(allocator, construct_first + i); }
  120. ABSL_INTERNAL_CATCH_ANY {
  121. DestroyElements<A>(allocator, construct_first, i);
  122. ABSL_INTERNAL_RETHROW;
  123. }
  124. }
  125. }
  126. template <typename A, typename ValueAdapter>
  127. void AssignElements(Pointer<A> assign_first, ValueAdapter& values,
  128. SizeType<A> assign_size) {
  129. for (SizeType<A> i = 0; i < assign_size; ++i) {
  130. values.AssignNext(assign_first + i);
  131. }
  132. }
  133. template <typename A>
  134. struct StorageView {
  135. Pointer<A> data;
  136. SizeType<A> size;
  137. SizeType<A> capacity;
  138. };
  139. template <typename A, typename Iterator>
  140. class IteratorValueAdapter {
  141. public:
  142. explicit IteratorValueAdapter(const Iterator& it) : it_(it) {}
  143. void ConstructNext(A& allocator, Pointer<A> construct_at) {
  144. AllocatorTraits<A>::construct(allocator, construct_at, *it_);
  145. ++it_;
  146. }
  147. void AssignNext(Pointer<A> assign_at) {
  148. *assign_at = *it_;
  149. ++it_;
  150. }
  151. private:
  152. Iterator it_;
  153. };
  154. template <typename A>
  155. class CopyValueAdapter {
  156. public:
  157. explicit CopyValueAdapter(ConstPointer<A> p) : ptr_(p) {}
  158. void ConstructNext(A& allocator, Pointer<A> construct_at) {
  159. AllocatorTraits<A>::construct(allocator, construct_at, *ptr_);
  160. }
  161. void AssignNext(Pointer<A> assign_at) { *assign_at = *ptr_; }
  162. private:
  163. ConstPointer<A> ptr_;
  164. };
  165. template <typename A>
  166. class DefaultValueAdapter {
  167. public:
  168. explicit DefaultValueAdapter() {}
  169. void ConstructNext(A& allocator, Pointer<A> construct_at) {
  170. AllocatorTraits<A>::construct(allocator, construct_at);
  171. }
  172. void AssignNext(Pointer<A> assign_at) { *assign_at = ValueType<A>(); }
  173. };
  174. template <typename A>
  175. class AllocationTransaction {
  176. public:
  177. explicit AllocationTransaction(A& allocator)
  178. : allocator_data_(allocator, nullptr), capacity_(0) {}
  179. ~AllocationTransaction() {
  180. if (DidAllocate()) {
  181. MallocAdapter<A>::Deallocate(GetAllocator(), GetData(), GetCapacity());
  182. }
  183. }
  184. AllocationTransaction(const AllocationTransaction&) = delete;
  185. void operator=(const AllocationTransaction&) = delete;
  186. A& GetAllocator() { return allocator_data_.template get<0>(); }
  187. Pointer<A>& GetData() { return allocator_data_.template get<1>(); }
  188. SizeType<A>& GetCapacity() { return capacity_; }
  189. bool DidAllocate() { return GetData() != nullptr; }
  190. Pointer<A> Allocate(SizeType<A> requested_capacity) {
  191. Allocation<A> result =
  192. MallocAdapter<A>::Allocate(GetAllocator(), requested_capacity);
  193. GetData() = result.data;
  194. GetCapacity() = result.capacity;
  195. return result.data;
  196. }
  197. ABSL_MUST_USE_RESULT Allocation<A> Release() && {
  198. Allocation<A> result = {GetData(), GetCapacity()};
  199. Reset();
  200. return result;
  201. }
  202. private:
  203. void Reset() {
  204. GetData() = nullptr;
  205. GetCapacity() = 0;
  206. }
  207. container_internal::CompressedTuple<A, Pointer<A>> allocator_data_;
  208. SizeType<A> capacity_;
  209. };
  210. template <typename A>
  211. class ConstructionTransaction {
  212. public:
  213. explicit ConstructionTransaction(A& allocator)
  214. : allocator_data_(allocator, nullptr), size_(0) {}
  215. ~ConstructionTransaction() {
  216. if (DidConstruct()) {
  217. DestroyElements<A>(GetAllocator(), GetData(), GetSize());
  218. }
  219. }
  220. ConstructionTransaction(const ConstructionTransaction&) = delete;
  221. void operator=(const ConstructionTransaction&) = delete;
  222. A& GetAllocator() { return allocator_data_.template get<0>(); }
  223. Pointer<A>& GetData() { return allocator_data_.template get<1>(); }
  224. SizeType<A>& GetSize() { return size_; }
  225. bool DidConstruct() { return GetData() != nullptr; }
  226. template <typename ValueAdapter>
  227. void Construct(Pointer<A> data, ValueAdapter& values, SizeType<A> size) {
  228. ConstructElements<A>(GetAllocator(), data, values, size);
  229. GetData() = data;
  230. GetSize() = size;
  231. }
  232. void Commit() && {
  233. GetData() = nullptr;
  234. GetSize() = 0;
  235. }
  236. private:
  237. container_internal::CompressedTuple<A, Pointer<A>> allocator_data_;
  238. SizeType<A> size_;
  239. };
  240. template <typename T, size_t N, typename A>
  241. class Storage {
  242. public:
  243. static SizeType<A> NextCapacity(SizeType<A> current_capacity) {
  244. return current_capacity * 2;
  245. }
  246. static SizeType<A> ComputeCapacity(SizeType<A> current_capacity,
  247. SizeType<A> requested_capacity) {
  248. return (std::max)(NextCapacity(current_capacity), requested_capacity);
  249. }
  250. // ---------------------------------------------------------------------------
  251. // Storage Constructors and Destructor
  252. // ---------------------------------------------------------------------------
  253. Storage() : metadata_(A(), /* size and is_allocated */ 0) {}
  254. explicit Storage(const A& allocator)
  255. : metadata_(allocator, /* size and is_allocated */ 0) {}
  256. ~Storage() {
  257. if (GetSizeAndIsAllocated() == 0) {
  258. // Empty and not allocated; nothing to do.
  259. } else if (IsMemcpyOk<A>::value) {
  260. // No destructors need to be run; just deallocate if necessary.
  261. DeallocateIfAllocated();
  262. } else {
  263. DestroyContents();
  264. }
  265. }
  266. // ---------------------------------------------------------------------------
  267. // Storage Member Accessors
  268. // ---------------------------------------------------------------------------
  269. SizeType<A>& GetSizeAndIsAllocated() { return metadata_.template get<1>(); }
  270. const SizeType<A>& GetSizeAndIsAllocated() const {
  271. return metadata_.template get<1>();
  272. }
  273. SizeType<A> GetSize() const { return GetSizeAndIsAllocated() >> 1; }
  274. bool GetIsAllocated() const { return GetSizeAndIsAllocated() & 1; }
  275. Pointer<A> GetAllocatedData() { return data_.allocated.allocated_data; }
  276. ConstPointer<A> GetAllocatedData() const {
  277. return data_.allocated.allocated_data;
  278. }
  279. Pointer<A> GetInlinedData() {
  280. return reinterpret_cast<Pointer<A>>(
  281. std::addressof(data_.inlined.inlined_data[0]));
  282. }
  283. ConstPointer<A> GetInlinedData() const {
  284. return reinterpret_cast<ConstPointer<A>>(
  285. std::addressof(data_.inlined.inlined_data[0]));
  286. }
  287. SizeType<A> GetAllocatedCapacity() const {
  288. return data_.allocated.allocated_capacity;
  289. }
  290. SizeType<A> GetInlinedCapacity() const { return static_cast<SizeType<A>>(N); }
  291. StorageView<A> MakeStorageView() {
  292. return GetIsAllocated() ? StorageView<A>{GetAllocatedData(), GetSize(),
  293. GetAllocatedCapacity()}
  294. : StorageView<A>{GetInlinedData(), GetSize(),
  295. GetInlinedCapacity()};
  296. }
  297. A& GetAllocator() { return metadata_.template get<0>(); }
  298. const A& GetAllocator() const { return metadata_.template get<0>(); }
  299. // ---------------------------------------------------------------------------
  300. // Storage Member Mutators
  301. // ---------------------------------------------------------------------------
  302. ABSL_ATTRIBUTE_NOINLINE void InitFrom(const Storage& other);
  303. template <typename ValueAdapter>
  304. void Initialize(ValueAdapter values, SizeType<A> new_size);
  305. template <typename ValueAdapter>
  306. void Assign(ValueAdapter values, SizeType<A> new_size);
  307. template <typename ValueAdapter>
  308. void Resize(ValueAdapter values, SizeType<A> new_size);
  309. template <typename ValueAdapter>
  310. Iterator<A> Insert(ConstIterator<A> pos, ValueAdapter values,
  311. SizeType<A> insert_count);
  312. template <typename... Args>
  313. Reference<A> EmplaceBack(Args&&... args);
  314. Iterator<A> Erase(ConstIterator<A> from, ConstIterator<A> to);
  315. void Reserve(SizeType<A> requested_capacity);
  316. void ShrinkToFit();
  317. void Swap(Storage* other_storage_ptr);
  318. void SetIsAllocated() {
  319. GetSizeAndIsAllocated() |= static_cast<SizeType<A>>(1);
  320. }
  321. void UnsetIsAllocated() {
  322. GetSizeAndIsAllocated() &= ((std::numeric_limits<SizeType<A>>::max)() - 1);
  323. }
  324. void SetSize(SizeType<A> size) {
  325. GetSizeAndIsAllocated() =
  326. (size << 1) | static_cast<SizeType<A>>(GetIsAllocated());
  327. }
  328. void SetAllocatedSize(SizeType<A> size) {
  329. GetSizeAndIsAllocated() = (size << 1) | static_cast<SizeType<A>>(1);
  330. }
  331. void SetInlinedSize(SizeType<A> size) {
  332. GetSizeAndIsAllocated() = size << static_cast<SizeType<A>>(1);
  333. }
  334. void AddSize(SizeType<A> count) {
  335. GetSizeAndIsAllocated() += count << static_cast<SizeType<A>>(1);
  336. }
  337. void SubtractSize(SizeType<A> count) {
  338. assert(count <= GetSize());
  339. GetSizeAndIsAllocated() -= count << static_cast<SizeType<A>>(1);
  340. }
  341. void SetAllocation(Allocation<A> allocation) {
  342. data_.allocated.allocated_data = allocation.data;
  343. data_.allocated.allocated_capacity = allocation.capacity;
  344. }
  345. void MemcpyFrom(const Storage& other_storage) {
  346. assert(IsMemcpyOk<A>::value || other_storage.GetIsAllocated());
  347. GetSizeAndIsAllocated() = other_storage.GetSizeAndIsAllocated();
  348. data_ = other_storage.data_;
  349. }
  350. void DeallocateIfAllocated() {
  351. if (GetIsAllocated()) {
  352. MallocAdapter<A>::Deallocate(GetAllocator(), GetAllocatedData(),
  353. GetAllocatedCapacity());
  354. }
  355. }
  356. private:
  357. ABSL_ATTRIBUTE_NOINLINE void DestroyContents();
  358. using Metadata = container_internal::CompressedTuple<A, SizeType<A>>;
  359. struct Allocated {
  360. Pointer<A> allocated_data;
  361. SizeType<A> allocated_capacity;
  362. };
  363. struct Inlined {
  364. alignas(ValueType<A>) char inlined_data[sizeof(ValueType<A>[N])];
  365. };
  366. union Data {
  367. Allocated allocated;
  368. Inlined inlined;
  369. };
  370. template <typename... Args>
  371. ABSL_ATTRIBUTE_NOINLINE Reference<A> EmplaceBackSlow(Args&&... args);
  372. Metadata metadata_;
  373. Data data_;
  374. };
  375. template <typename T, size_t N, typename A>
  376. void Storage<T, N, A>::DestroyContents() {
  377. Pointer<A> data = GetIsAllocated() ? GetAllocatedData() : GetInlinedData();
  378. DestroyElements<A>(GetAllocator(), data, GetSize());
  379. DeallocateIfAllocated();
  380. }
  381. template <typename T, size_t N, typename A>
  382. void Storage<T, N, A>::InitFrom(const Storage& other) {
  383. const SizeType<A> n = other.GetSize();
  384. assert(n > 0); // Empty sources handled handled in caller.
  385. ConstPointer<A> src;
  386. Pointer<A> dst;
  387. if (!other.GetIsAllocated()) {
  388. dst = GetInlinedData();
  389. src = other.GetInlinedData();
  390. } else {
  391. // Because this is only called from the `InlinedVector` constructors, it's
  392. // safe to take on the allocation with size `0`. If `ConstructElements(...)`
  393. // throws, deallocation will be automatically handled by `~Storage()`.
  394. SizeType<A> requested_capacity = ComputeCapacity(GetInlinedCapacity(), n);
  395. Allocation<A> allocation =
  396. MallocAdapter<A>::Allocate(GetAllocator(), requested_capacity);
  397. SetAllocation(allocation);
  398. dst = allocation.data;
  399. src = other.GetAllocatedData();
  400. }
  401. if (IsMemcpyOk<A>::value) {
  402. std::memcpy(reinterpret_cast<char*>(dst),
  403. reinterpret_cast<const char*>(src), n * sizeof(ValueType<A>));
  404. } else {
  405. auto values = IteratorValueAdapter<A, ConstPointer<A>>(src);
  406. ConstructElements<A>(GetAllocator(), dst, values, n);
  407. }
  408. GetSizeAndIsAllocated() = other.GetSizeAndIsAllocated();
  409. }
  410. template <typename T, size_t N, typename A>
  411. template <typename ValueAdapter>
  412. auto Storage<T, N, A>::Initialize(ValueAdapter values, SizeType<A> new_size)
  413. -> void {
  414. // Only callable from constructors!
  415. assert(!GetIsAllocated());
  416. assert(GetSize() == 0);
  417. Pointer<A> construct_data;
  418. if (new_size > GetInlinedCapacity()) {
  419. // Because this is only called from the `InlinedVector` constructors, it's
  420. // safe to take on the allocation with size `0`. If `ConstructElements(...)`
  421. // throws, deallocation will be automatically handled by `~Storage()`.
  422. SizeType<A> requested_capacity =
  423. ComputeCapacity(GetInlinedCapacity(), new_size);
  424. Allocation<A> allocation =
  425. MallocAdapter<A>::Allocate(GetAllocator(), requested_capacity);
  426. construct_data = allocation.data;
  427. SetAllocation(allocation);
  428. SetIsAllocated();
  429. } else {
  430. construct_data = GetInlinedData();
  431. }
  432. ConstructElements<A>(GetAllocator(), construct_data, values, new_size);
  433. // Since the initial size was guaranteed to be `0` and the allocated bit is
  434. // already correct for either case, *adding* `new_size` gives us the correct
  435. // result faster than setting it directly.
  436. AddSize(new_size);
  437. }
  438. template <typename T, size_t N, typename A>
  439. template <typename ValueAdapter>
  440. auto Storage<T, N, A>::Assign(ValueAdapter values, SizeType<A> new_size)
  441. -> void {
  442. StorageView<A> storage_view = MakeStorageView();
  443. AllocationTransaction<A> allocation_tx(GetAllocator());
  444. absl::Span<ValueType<A>> assign_loop;
  445. absl::Span<ValueType<A>> construct_loop;
  446. absl::Span<ValueType<A>> destroy_loop;
  447. if (new_size > storage_view.capacity) {
  448. SizeType<A> requested_capacity =
  449. ComputeCapacity(storage_view.capacity, new_size);
  450. construct_loop = {allocation_tx.Allocate(requested_capacity), new_size};
  451. destroy_loop = {storage_view.data, storage_view.size};
  452. } else if (new_size > storage_view.size) {
  453. assign_loop = {storage_view.data, storage_view.size};
  454. construct_loop = {storage_view.data + storage_view.size,
  455. new_size - storage_view.size};
  456. } else {
  457. assign_loop = {storage_view.data, new_size};
  458. destroy_loop = {storage_view.data + new_size, storage_view.size - new_size};
  459. }
  460. AssignElements<A>(assign_loop.data(), values, assign_loop.size());
  461. ConstructElements<A>(GetAllocator(), construct_loop.data(), values,
  462. construct_loop.size());
  463. DestroyElements<A>(GetAllocator(), destroy_loop.data(), destroy_loop.size());
  464. if (allocation_tx.DidAllocate()) {
  465. DeallocateIfAllocated();
  466. SetAllocation(std::move(allocation_tx).Release());
  467. SetIsAllocated();
  468. }
  469. SetSize(new_size);
  470. }
  471. template <typename T, size_t N, typename A>
  472. template <typename ValueAdapter>
  473. auto Storage<T, N, A>::Resize(ValueAdapter values, SizeType<A> new_size)
  474. -> void {
  475. StorageView<A> storage_view = MakeStorageView();
  476. Pointer<A> const base = storage_view.data;
  477. const SizeType<A> size = storage_view.size;
  478. A& alloc = GetAllocator();
  479. if (new_size <= size) {
  480. // Destroy extra old elements.
  481. DestroyElements<A>(alloc, base + new_size, size - new_size);
  482. } else if (new_size <= storage_view.capacity) {
  483. // Construct new elements in place.
  484. ConstructElements<A>(alloc, base + size, values, new_size - size);
  485. } else {
  486. // Steps:
  487. // a. Allocate new backing store.
  488. // b. Construct new elements in new backing store.
  489. // c. Move existing elements from old backing store to now.
  490. // d. Destroy all elements in old backing store.
  491. // Use transactional wrappers for the first two steps so we can roll
  492. // back if necessary due to exceptions.
  493. AllocationTransaction<A> allocation_tx(alloc);
  494. SizeType<A> requested_capacity =
  495. ComputeCapacity(storage_view.capacity, new_size);
  496. Pointer<A> new_data = allocation_tx.Allocate(requested_capacity);
  497. ConstructionTransaction<A> construction_tx(alloc);
  498. construction_tx.Construct(new_data + size, values, new_size - size);
  499. IteratorValueAdapter<A, MoveIterator<A>> move_values(
  500. (MoveIterator<A>(base)));
  501. ConstructElements<A>(alloc, new_data, move_values, size);
  502. DestroyElements<A>(alloc, base, size);
  503. std::move(construction_tx).Commit();
  504. DeallocateIfAllocated();
  505. SetAllocation(std::move(allocation_tx).Release());
  506. SetIsAllocated();
  507. }
  508. SetSize(new_size);
  509. }
  510. template <typename T, size_t N, typename A>
  511. template <typename ValueAdapter>
  512. auto Storage<T, N, A>::Insert(ConstIterator<A> pos, ValueAdapter values,
  513. SizeType<A> insert_count) -> Iterator<A> {
  514. StorageView<A> storage_view = MakeStorageView();
  515. SizeType<A> insert_index =
  516. std::distance(ConstIterator<A>(storage_view.data), pos);
  517. SizeType<A> insert_end_index = insert_index + insert_count;
  518. SizeType<A> new_size = storage_view.size + insert_count;
  519. if (new_size > storage_view.capacity) {
  520. AllocationTransaction<A> allocation_tx(GetAllocator());
  521. ConstructionTransaction<A> construction_tx(GetAllocator());
  522. ConstructionTransaction<A> move_construction_tx(GetAllocator());
  523. IteratorValueAdapter<A, MoveIterator<A>> move_values(
  524. MoveIterator<A>(storage_view.data));
  525. SizeType<A> requested_capacity =
  526. ComputeCapacity(storage_view.capacity, new_size);
  527. Pointer<A> new_data = allocation_tx.Allocate(requested_capacity);
  528. construction_tx.Construct(new_data + insert_index, values, insert_count);
  529. move_construction_tx.Construct(new_data, move_values, insert_index);
  530. ConstructElements<A>(GetAllocator(), new_data + insert_end_index,
  531. move_values, storage_view.size - insert_index);
  532. DestroyElements<A>(GetAllocator(), storage_view.data, storage_view.size);
  533. std::move(construction_tx).Commit();
  534. std::move(move_construction_tx).Commit();
  535. DeallocateIfAllocated();
  536. SetAllocation(std::move(allocation_tx).Release());
  537. SetAllocatedSize(new_size);
  538. return Iterator<A>(new_data + insert_index);
  539. } else {
  540. SizeType<A> move_construction_destination_index =
  541. (std::max)(insert_end_index, storage_view.size);
  542. ConstructionTransaction<A> move_construction_tx(GetAllocator());
  543. IteratorValueAdapter<A, MoveIterator<A>> move_construction_values(
  544. MoveIterator<A>(storage_view.data +
  545. (move_construction_destination_index - insert_count)));
  546. absl::Span<ValueType<A>> move_construction = {
  547. storage_view.data + move_construction_destination_index,
  548. new_size - move_construction_destination_index};
  549. Pointer<A> move_assignment_values = storage_view.data + insert_index;
  550. absl::Span<ValueType<A>> move_assignment = {
  551. storage_view.data + insert_end_index,
  552. move_construction_destination_index - insert_end_index};
  553. absl::Span<ValueType<A>> insert_assignment = {move_assignment_values,
  554. move_construction.size()};
  555. absl::Span<ValueType<A>> insert_construction = {
  556. insert_assignment.data() + insert_assignment.size(),
  557. insert_count - insert_assignment.size()};
  558. move_construction_tx.Construct(move_construction.data(),
  559. move_construction_values,
  560. move_construction.size());
  561. for (Pointer<A>
  562. destination = move_assignment.data() + move_assignment.size(),
  563. last_destination = move_assignment.data(),
  564. source = move_assignment_values + move_assignment.size();
  565. ;) {
  566. --destination;
  567. --source;
  568. if (destination < last_destination) break;
  569. *destination = std::move(*source);
  570. }
  571. AssignElements<A>(insert_assignment.data(), values,
  572. insert_assignment.size());
  573. ConstructElements<A>(GetAllocator(), insert_construction.data(), values,
  574. insert_construction.size());
  575. std::move(move_construction_tx).Commit();
  576. AddSize(insert_count);
  577. return Iterator<A>(storage_view.data + insert_index);
  578. }
  579. }
  580. template <typename T, size_t N, typename A>
  581. template <typename... Args>
  582. auto Storage<T, N, A>::EmplaceBack(Args&&... args) -> Reference<A> {
  583. StorageView<A> storage_view = MakeStorageView();
  584. const SizeType<A> n = storage_view.size;
  585. if (ABSL_PREDICT_TRUE(n != storage_view.capacity)) {
  586. // Fast path; new element fits.
  587. Pointer<A> last_ptr = storage_view.data + n;
  588. AllocatorTraits<A>::construct(GetAllocator(), last_ptr,
  589. std::forward<Args>(args)...);
  590. AddSize(1);
  591. return *last_ptr;
  592. }
  593. // TODO(b/173712035): Annotate with musttail attribute to prevent regression.
  594. return EmplaceBackSlow(std::forward<Args>(args)...);
  595. }
  596. template <typename T, size_t N, typename A>
  597. template <typename... Args>
  598. auto Storage<T, N, A>::EmplaceBackSlow(Args&&... args) -> Reference<A> {
  599. StorageView<A> storage_view = MakeStorageView();
  600. AllocationTransaction<A> allocation_tx(GetAllocator());
  601. IteratorValueAdapter<A, MoveIterator<A>> move_values(
  602. MoveIterator<A>(storage_view.data));
  603. SizeType<A> requested_capacity = NextCapacity(storage_view.capacity);
  604. Pointer<A> construct_data = allocation_tx.Allocate(requested_capacity);
  605. Pointer<A> last_ptr = construct_data + storage_view.size;
  606. // Construct new element.
  607. AllocatorTraits<A>::construct(GetAllocator(), last_ptr,
  608. std::forward<Args>(args)...);
  609. // Move elements from old backing store to new backing store.
  610. ABSL_INTERNAL_TRY {
  611. ConstructElements<A>(GetAllocator(), allocation_tx.GetData(), move_values,
  612. storage_view.size);
  613. }
  614. ABSL_INTERNAL_CATCH_ANY {
  615. AllocatorTraits<A>::destroy(GetAllocator(), last_ptr);
  616. ABSL_INTERNAL_RETHROW;
  617. }
  618. // Destroy elements in old backing store.
  619. DestroyElements<A>(GetAllocator(), storage_view.data, storage_view.size);
  620. DeallocateIfAllocated();
  621. SetAllocation(std::move(allocation_tx).Release());
  622. SetIsAllocated();
  623. AddSize(1);
  624. return *last_ptr;
  625. }
  626. template <typename T, size_t N, typename A>
  627. auto Storage<T, N, A>::Erase(ConstIterator<A> from, ConstIterator<A> to)
  628. -> Iterator<A> {
  629. StorageView<A> storage_view = MakeStorageView();
  630. SizeType<A> erase_size = std::distance(from, to);
  631. SizeType<A> erase_index =
  632. std::distance(ConstIterator<A>(storage_view.data), from);
  633. SizeType<A> erase_end_index = erase_index + erase_size;
  634. IteratorValueAdapter<A, MoveIterator<A>> move_values(
  635. MoveIterator<A>(storage_view.data + erase_end_index));
  636. AssignElements<A>(storage_view.data + erase_index, move_values,
  637. storage_view.size - erase_end_index);
  638. DestroyElements<A>(GetAllocator(),
  639. storage_view.data + (storage_view.size - erase_size),
  640. erase_size);
  641. SubtractSize(erase_size);
  642. return Iterator<A>(storage_view.data + erase_index);
  643. }
  644. template <typename T, size_t N, typename A>
  645. auto Storage<T, N, A>::Reserve(SizeType<A> requested_capacity) -> void {
  646. StorageView<A> storage_view = MakeStorageView();
  647. if (ABSL_PREDICT_FALSE(requested_capacity <= storage_view.capacity)) return;
  648. AllocationTransaction<A> allocation_tx(GetAllocator());
  649. IteratorValueAdapter<A, MoveIterator<A>> move_values(
  650. MoveIterator<A>(storage_view.data));
  651. SizeType<A> new_requested_capacity =
  652. ComputeCapacity(storage_view.capacity, requested_capacity);
  653. Pointer<A> new_data = allocation_tx.Allocate(new_requested_capacity);
  654. ConstructElements<A>(GetAllocator(), new_data, move_values,
  655. storage_view.size);
  656. DestroyElements<A>(GetAllocator(), storage_view.data, storage_view.size);
  657. DeallocateIfAllocated();
  658. SetAllocation(std::move(allocation_tx).Release());
  659. SetIsAllocated();
  660. }
  661. template <typename T, size_t N, typename A>
  662. auto Storage<T, N, A>::ShrinkToFit() -> void {
  663. // May only be called on allocated instances!
  664. assert(GetIsAllocated());
  665. StorageView<A> storage_view{GetAllocatedData(), GetSize(),
  666. GetAllocatedCapacity()};
  667. if (ABSL_PREDICT_FALSE(storage_view.size == storage_view.capacity)) return;
  668. AllocationTransaction<A> allocation_tx(GetAllocator());
  669. IteratorValueAdapter<A, MoveIterator<A>> move_values(
  670. MoveIterator<A>(storage_view.data));
  671. Pointer<A> construct_data;
  672. if (storage_view.size > GetInlinedCapacity()) {
  673. SizeType<A> requested_capacity = storage_view.size;
  674. construct_data = allocation_tx.Allocate(requested_capacity);
  675. if (allocation_tx.GetCapacity() >= storage_view.capacity) {
  676. // Already using the smallest available heap allocation.
  677. return;
  678. }
  679. } else {
  680. construct_data = GetInlinedData();
  681. }
  682. ABSL_INTERNAL_TRY {
  683. ConstructElements<A>(GetAllocator(), construct_data, move_values,
  684. storage_view.size);
  685. }
  686. ABSL_INTERNAL_CATCH_ANY {
  687. SetAllocation({storage_view.data, storage_view.capacity});
  688. ABSL_INTERNAL_RETHROW;
  689. }
  690. DestroyElements<A>(GetAllocator(), storage_view.data, storage_view.size);
  691. MallocAdapter<A>::Deallocate(GetAllocator(), storage_view.data,
  692. storage_view.capacity);
  693. if (allocation_tx.DidAllocate()) {
  694. SetAllocation(std::move(allocation_tx).Release());
  695. } else {
  696. UnsetIsAllocated();
  697. }
  698. }
  699. template <typename T, size_t N, typename A>
  700. auto Storage<T, N, A>::Swap(Storage* other_storage_ptr) -> void {
  701. using std::swap;
  702. assert(this != other_storage_ptr);
  703. if (GetIsAllocated() && other_storage_ptr->GetIsAllocated()) {
  704. swap(data_.allocated, other_storage_ptr->data_.allocated);
  705. } else if (!GetIsAllocated() && !other_storage_ptr->GetIsAllocated()) {
  706. Storage* small_ptr = this;
  707. Storage* large_ptr = other_storage_ptr;
  708. if (small_ptr->GetSize() > large_ptr->GetSize()) swap(small_ptr, large_ptr);
  709. for (SizeType<A> i = 0; i < small_ptr->GetSize(); ++i) {
  710. swap(small_ptr->GetInlinedData()[i], large_ptr->GetInlinedData()[i]);
  711. }
  712. IteratorValueAdapter<A, MoveIterator<A>> move_values(
  713. MoveIterator<A>(large_ptr->GetInlinedData() + small_ptr->GetSize()));
  714. ConstructElements<A>(large_ptr->GetAllocator(),
  715. small_ptr->GetInlinedData() + small_ptr->GetSize(),
  716. move_values,
  717. large_ptr->GetSize() - small_ptr->GetSize());
  718. DestroyElements<A>(large_ptr->GetAllocator(),
  719. large_ptr->GetInlinedData() + small_ptr->GetSize(),
  720. large_ptr->GetSize() - small_ptr->GetSize());
  721. } else {
  722. Storage* allocated_ptr = this;
  723. Storage* inlined_ptr = other_storage_ptr;
  724. if (!allocated_ptr->GetIsAllocated()) swap(allocated_ptr, inlined_ptr);
  725. StorageView<A> allocated_storage_view{
  726. allocated_ptr->GetAllocatedData(), allocated_ptr->GetSize(),
  727. allocated_ptr->GetAllocatedCapacity()};
  728. IteratorValueAdapter<A, MoveIterator<A>> move_values(
  729. MoveIterator<A>(inlined_ptr->GetInlinedData()));
  730. ABSL_INTERNAL_TRY {
  731. ConstructElements<A>(inlined_ptr->GetAllocator(),
  732. allocated_ptr->GetInlinedData(), move_values,
  733. inlined_ptr->GetSize());
  734. }
  735. ABSL_INTERNAL_CATCH_ANY {
  736. allocated_ptr->SetAllocation(
  737. {allocated_storage_view.data, allocated_storage_view.capacity});
  738. ABSL_INTERNAL_RETHROW;
  739. }
  740. DestroyElements<A>(inlined_ptr->GetAllocator(),
  741. inlined_ptr->GetInlinedData(), inlined_ptr->GetSize());
  742. inlined_ptr->SetAllocation(
  743. {allocated_storage_view.data, allocated_storage_view.capacity});
  744. }
  745. swap(GetSizeAndIsAllocated(), other_storage_ptr->GetSizeAndIsAllocated());
  746. swap(GetAllocator(), other_storage_ptr->GetAllocator());
  747. }
  748. // End ignore "array-bounds" and "maybe-uninitialized"
  749. #if !defined(__clang__) && defined(__GNUC__)
  750. #pragma GCC diagnostic pop
  751. #endif
  752. } // namespace inlined_vector_internal
  753. ABSL_NAMESPACE_END
  754. } // namespace absl
  755. #endif // ABSL_CONTAINER_INTERNAL_INLINED_VECTOR_INTERNAL_H_