diff options
Diffstat (limited to 'absl/container')
-rw-r--r-- | absl/container/inlined_vector.h | 44 | ||||
-rw-r--r-- | absl/container/internal/inlined_vector.h | 12 |
2 files changed, 27 insertions, 29 deletions
diff --git a/absl/container/inlined_vector.h b/absl/container/inlined_vector.h index 6c2d08571785..564c9535129e 100644 --- a/absl/container/inlined_vector.h +++ b/absl/container/inlined_vector.h @@ -155,7 +155,7 @@ class InlinedVector { // Creates a copy of an `other` inlined vector using `other`'s allocator. InlinedVector(const InlinedVector& other) - : InlinedVector(other, other.storage_.GetAllocator()) {} + : InlinedVector(other, *other.storage_.GetAllocPtr()) {} // Creates a copy of an `other` inlined vector using a specified allocator. InlinedVector(const InlinedVector& other, const allocator_type& alloc) @@ -189,7 +189,7 @@ class InlinedVector { InlinedVector(InlinedVector&& other) noexcept( absl::allocator_is_nothrow<allocator_type>::value || std::is_nothrow_move_constructible<value_type>::value) - : storage_(other.storage_.GetAllocator()) { + : storage_(*other.storage_.GetAllocPtr()) { if (other.storage_.GetIsAllocated()) { // We can just steal the underlying buffer from the source. // That leaves the source empty, so we clear its size. @@ -224,7 +224,7 @@ class InlinedVector { absl::allocator_is_nothrow<allocator_type>::value) : storage_(alloc) { if (other.storage_.GetIsAllocated()) { - if (alloc == other.storage_.GetAllocator()) { + if (*storage_.GetAllocPtr() == *other.storage_.GetAllocPtr()) { // We can just steal the allocation from the source. storage_.SetAllocatedSize(other.size()); storage_.SetAllocatedData(other.storage_.GetAllocatedData()); @@ -437,7 +437,7 @@ class InlinedVector { // `InlinedVector::get_allocator()` // // Returns a copy of the allocator of the inlined vector. - allocator_type get_allocator() const { return storage_.GetAllocator(); } + allocator_type get_allocator() const { return *storage_.GetAllocPtr(); } // --------------------------------------------------------------------------- // InlinedVector Member Mutators @@ -794,19 +794,15 @@ class InlinedVector { // deallocates the heap allocation if the inlined vector was allocated. void clear() noexcept { const bool is_allocated = storage_.GetIsAllocated(); - pointer the_data = is_allocated ? storage_.GetAllocatedData() : storage_.GetInlinedData(); - - inlined_vector_internal::DestroyElements(storage_.GetAllocator(), the_data, + inlined_vector_internal::DestroyElements(storage_.GetAllocPtr(), the_data, storage_.GetSize()); - + storage_.SetInlinedSize(0); if (is_allocated) { - AllocatorTraits::deallocate(storage_.GetAllocator(), the_data, + AllocatorTraits::deallocate(*storage_.GetAllocPtr(), the_data, storage_.GetAllocatedCapacity()); } - - storage_.SetInlinedSize(/* size = */ 0); } // `InlinedVector::reserve()` @@ -854,7 +850,7 @@ class InlinedVector { // Reallocate storage and move elements. // We can't simply use the same approach as above, because `assign()` would // call into `reserve()` internally and reserve larger capacity than we need - pointer new_data = AllocatorTraits::allocate(storage_.GetAllocator(), s); + pointer new_data = AllocatorTraits::allocate(*storage_.GetAllocPtr(), s); UninitializedCopy(std::make_move_iterator(storage_.GetAllocatedData()), std::make_move_iterator(storage_.GetAllocatedData() + s), new_data); @@ -880,7 +876,7 @@ class InlinedVector { Destroy(storage_.GetAllocatedData(), storage_.GetAllocatedData() + size()); assert(begin() == storage_.GetAllocatedData()); - AllocatorTraits::deallocate(storage_.GetAllocator(), + AllocatorTraits::deallocate(*storage_.GetAllocPtr(), storage_.GetAllocatedData(), storage_.GetAllocatedCapacity()); } else { @@ -895,7 +891,7 @@ class InlinedVector { template <typename... Args> reference Construct(pointer p, Args&&... args) { std::allocator_traits<allocator_type>::construct( - storage_.GetAllocator(), p, std::forward<Args>(args)...); + *storage_.GetAllocPtr(), p, std::forward<Args>(args)...); return *p; } @@ -912,7 +908,7 @@ class InlinedVector { // Destroy [`from`, `to`) in place. void Destroy(pointer from, pointer to) { for (pointer cur = from; cur != to; ++cur) { - std::allocator_traits<allocator_type>::destroy(storage_.GetAllocator(), + std::allocator_traits<allocator_type>::destroy(*storage_.GetAllocPtr(), cur); } #if !defined(NDEBUG) @@ -943,7 +939,7 @@ class InlinedVector { } pointer new_data = - AllocatorTraits::allocate(storage_.GetAllocator(), new_capacity); + AllocatorTraits::allocate(*storage_.GetAllocPtr(), new_capacity); UninitializedCopy(std::make_move_iterator(data()), std::make_move_iterator(data() + s), new_data); @@ -975,7 +971,7 @@ class InlinedVector { // Move everyone into the new allocation, leaving a gap of `n` for the // requested shift. pointer new_data = - AllocatorTraits::allocate(storage_.GetAllocator(), new_capacity); + AllocatorTraits::allocate(*storage_.GetAllocPtr(), new_capacity); size_type index = position - begin(); UninitializedCopy(std::make_move_iterator(data()), std::make_move_iterator(data() + index), new_data); @@ -1024,7 +1020,7 @@ class InlinedVector { size_type new_capacity = 2 * capacity(); pointer new_data = - AllocatorTraits::allocate(storage_.GetAllocator(), new_capacity); + AllocatorTraits::allocate(*storage_.GetAllocPtr(), new_capacity); reference new_element = Construct(new_data + s, std::forward<Args>(args)...); @@ -1038,7 +1034,7 @@ class InlinedVector { void InitAssign(size_type n) { if (n > static_cast<size_type>(N)) { - pointer new_data = AllocatorTraits::allocate(storage_.GetAllocator(), n); + pointer new_data = AllocatorTraits::allocate(*storage_.GetAllocPtr(), n); storage_.SetAllocatedData(new_data); storage_.SetAllocatedCapacity(n); UninitializedFill(storage_.GetAllocatedData(), @@ -1053,7 +1049,7 @@ class InlinedVector { void InitAssign(size_type n, const_reference v) { if (n > static_cast<size_type>(N)) { - pointer new_data = AllocatorTraits::allocate(storage_.GetAllocator(), n); + pointer new_data = AllocatorTraits::allocate(*storage_.GetAllocPtr(), n); storage_.SetAllocatedData(new_data); storage_.SetAllocatedCapacity(n); UninitializedFill(storage_.GetAllocatedData(), @@ -1126,7 +1122,7 @@ class InlinedVector { // Both out of line, so just swap the tag, allocation, and allocator. storage_.SwapSizeAndIsAllocated(std::addressof(other.storage_)); storage_.SwapAllocatedSizeAndCapacity(std::addressof(other.storage_)); - swap(storage_.GetAllocator(), other.storage_.GetAllocator()); + swap(*storage_.GetAllocPtr(), *other.storage_.GetAllocPtr()); return; } @@ -1156,7 +1152,7 @@ class InlinedVector { a->storage_.GetInlinedData() + a_size); storage_.SwapSizeAndIsAllocated(std::addressof(other.storage_)); - swap(storage_.GetAllocator(), other.storage_.GetAllocator()); + swap(*storage_.GetAllocPtr(), *other.storage_.GetAllocPtr()); assert(b->size() == a_size); assert(a->size() == b_size); @@ -1198,8 +1194,8 @@ class InlinedVector { a->storage_.SetAllocatedData(b_data); a->storage_.SetAllocatedCapacity(b_capacity); - if (a->storage_.GetAllocator() != b->storage_.GetAllocator()) { - swap(a->storage_.GetAllocator(), b->storage_.GetAllocator()); + if (*a->storage_.GetAllocPtr() != *b->storage_.GetAllocPtr()) { + swap(*a->storage_.GetAllocPtr(), *b->storage_.GetAllocPtr()); } assert(b->size() == a_size); diff --git a/absl/container/internal/inlined_vector.h b/absl/container/internal/inlined_vector.h index 4589ce0837a4..a2b3d24d98e9 100644 --- a/absl/container/internal/inlined_vector.h +++ b/absl/container/internal/inlined_vector.h @@ -33,7 +33,7 @@ using IsAtLeastForwardIterator = std::is_convertible< std::forward_iterator_tag>; template <typename AllocatorType, typename ValueType, typename SizeType> -void DestroyElements(AllocatorType alloc, ValueType* destroy_first, +void DestroyElements(AllocatorType* alloc_ptr, ValueType* destroy_first, SizeType destroy_size) { using AllocatorTraits = std::allocator_traits<AllocatorType>; @@ -45,7 +45,7 @@ void DestroyElements(AllocatorType alloc, ValueType* destroy_first, // NOTE: We assume destructors do not throw and thus make no attempt to roll // back. for (SizeType i = 0; i < destroy_size; ++i) { - AllocatorTraits::destroy(alloc, destroy_first + i); + AllocatorTraits::destroy(*alloc_ptr, destroy_first + i); } #ifndef NDEBUG @@ -104,10 +104,12 @@ class Storage { return data_.allocated.allocated_capacity; } - allocator_type& GetAllocator() { return metadata_.template get<0>(); } + allocator_type* GetAllocPtr() { + return std::addressof(metadata_.template get<0>()); + } - const allocator_type& GetAllocator() const { - return metadata_.template get<0>(); + const allocator_type* GetAllocPtr() const { + return std::addressof(metadata_.template get<0>()); } void SetAllocatedSize(size_type size) { |