From ce65f5ac3cbf897bb5e3de1a51d80fd00866abaa Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Wed, 22 May 2019 05:06:50 -0700 Subject: Export of internal Abseil changes. -- 1edfe05ddddca43e7650b2d790df7c8498c0e588 by Abseil Team : Adding an assert to catch various misuses of std::optional. PiperOrigin-RevId: 249427865 -- 45463bbb7e59dfbc584b2f024368a63db98bd7a8 by CJ Johnson : Migrates internal member function GetAllocator() to GetAllocPtr() and changes the return type to pointer instead of reference to avoid unnecessary copy in DestroyElements(...) PiperOrigin-RevId: 249319571 -- 507835be22af85676143ee0c43a80a52bc32094c by Abseil Team : Fix -Wstring-conversion in GetEnvVar (Windows implementation). PiperOrigin-RevId: 249201897 GitOrigin-RevId: 1edfe05ddddca43e7650b2d790df7c8498c0e588 Change-Id: I9300131887ee507cf80d399c724cf87341e4f11a --- absl/container/inlined_vector.h | 44 +++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 24 deletions(-) (limited to 'absl/container/inlined_vector.h') 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::value || std::is_nothrow_move_constructible::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::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 reference Construct(pointer p, Args&&... args) { std::allocator_traits::construct( - storage_.GetAllocator(), p, std::forward(args)...); + *storage_.GetAllocPtr(), p, std::forward(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::destroy(storage_.GetAllocator(), + std::allocator_traits::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)...); @@ -1038,7 +1034,7 @@ class InlinedVector { void InitAssign(size_type n) { if (n > static_cast(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(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); -- cgit 1.4.1