Export of internal Abseil changes.

--
1edfe05ddddca43e7650b2d790df7c8498c0e588 by Abseil Team <absl-team@google.com>:

Adding an assert to catch various misuses of std::optional.

PiperOrigin-RevId: 249427865

--
45463bbb7e59dfbc584b2f024368a63db98bd7a8 by CJ Johnson <johnsoncj@google.com>:

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 <absl-team@google.com>:

Fix -Wstring-conversion in GetEnvVar (Windows implementation).

PiperOrigin-RevId: 249201897
GitOrigin-RevId: 1edfe05ddddca43e7650b2d790df7c8498c0e588
Change-Id: I9300131887ee507cf80d399c724cf87341e4f11a
This commit is contained in:
Abseil Team 2019-05-22 05:06:50 -07:00 committed by Derek Mauro
parent a18fc7461e
commit ce65f5ac3c
4 changed files with 31 additions and 31 deletions

View file

@ -155,7 +155,7 @@ class InlinedVector {
// Creates a copy of an `other` inlined vector using `other`'s allocator. // Creates a copy of an `other` inlined vector using `other`'s allocator.
InlinedVector(const InlinedVector& other) 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. // Creates a copy of an `other` inlined vector using a specified allocator.
InlinedVector(const InlinedVector& other, const allocator_type& alloc) InlinedVector(const InlinedVector& other, const allocator_type& alloc)
@ -189,7 +189,7 @@ class InlinedVector {
InlinedVector(InlinedVector&& other) noexcept( InlinedVector(InlinedVector&& other) noexcept(
absl::allocator_is_nothrow<allocator_type>::value || absl::allocator_is_nothrow<allocator_type>::value ||
std::is_nothrow_move_constructible<value_type>::value) std::is_nothrow_move_constructible<value_type>::value)
: storage_(other.storage_.GetAllocator()) { : storage_(*other.storage_.GetAllocPtr()) {
if (other.storage_.GetIsAllocated()) { if (other.storage_.GetIsAllocated()) {
// We can just steal the underlying buffer from the source. // We can just steal the underlying buffer from the source.
// That leaves the source empty, so we clear its size. // That leaves the source empty, so we clear its size.
@ -224,7 +224,7 @@ class InlinedVector {
absl::allocator_is_nothrow<allocator_type>::value) absl::allocator_is_nothrow<allocator_type>::value)
: storage_(alloc) { : storage_(alloc) {
if (other.storage_.GetIsAllocated()) { if (other.storage_.GetIsAllocated()) {
if (alloc == other.storage_.GetAllocator()) { if (*storage_.GetAllocPtr() == *other.storage_.GetAllocPtr()) {
// We can just steal the allocation from the source. // We can just steal the allocation from the source.
storage_.SetAllocatedSize(other.size()); storage_.SetAllocatedSize(other.size());
storage_.SetAllocatedData(other.storage_.GetAllocatedData()); storage_.SetAllocatedData(other.storage_.GetAllocatedData());
@ -437,7 +437,7 @@ class InlinedVector {
// `InlinedVector::get_allocator()` // `InlinedVector::get_allocator()`
// //
// Returns a copy of the allocator of the inlined vector. // 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 // InlinedVector Member Mutators
@ -794,19 +794,15 @@ class InlinedVector {
// deallocates the heap allocation if the inlined vector was allocated. // deallocates the heap allocation if the inlined vector was allocated.
void clear() noexcept { void clear() noexcept {
const bool is_allocated = storage_.GetIsAllocated(); const bool is_allocated = storage_.GetIsAllocated();
pointer the_data = pointer the_data =
is_allocated ? storage_.GetAllocatedData() : storage_.GetInlinedData(); is_allocated ? storage_.GetAllocatedData() : storage_.GetInlinedData();
inlined_vector_internal::DestroyElements(storage_.GetAllocPtr(), the_data,
inlined_vector_internal::DestroyElements(storage_.GetAllocator(), the_data,
storage_.GetSize()); storage_.GetSize());
storage_.SetInlinedSize(0);
if (is_allocated) { if (is_allocated) {
AllocatorTraits::deallocate(storage_.GetAllocator(), the_data, AllocatorTraits::deallocate(*storage_.GetAllocPtr(), the_data,
storage_.GetAllocatedCapacity()); storage_.GetAllocatedCapacity());
} }
storage_.SetInlinedSize(/* size = */ 0);
} }
// `InlinedVector::reserve()` // `InlinedVector::reserve()`
@ -854,7 +850,7 @@ class InlinedVector {
// Reallocate storage and move elements. // Reallocate storage and move elements.
// We can't simply use the same approach as above, because `assign()` would // We can't simply use the same approach as above, because `assign()` would
// call into `reserve()` internally and reserve larger capacity than we need // 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()), UninitializedCopy(std::make_move_iterator(storage_.GetAllocatedData()),
std::make_move_iterator(storage_.GetAllocatedData() + s), std::make_move_iterator(storage_.GetAllocatedData() + s),
new_data); new_data);
@ -880,7 +876,7 @@ class InlinedVector {
Destroy(storage_.GetAllocatedData(), Destroy(storage_.GetAllocatedData(),
storage_.GetAllocatedData() + size()); storage_.GetAllocatedData() + size());
assert(begin() == storage_.GetAllocatedData()); assert(begin() == storage_.GetAllocatedData());
AllocatorTraits::deallocate(storage_.GetAllocator(), AllocatorTraits::deallocate(*storage_.GetAllocPtr(),
storage_.GetAllocatedData(), storage_.GetAllocatedData(),
storage_.GetAllocatedCapacity()); storage_.GetAllocatedCapacity());
} else { } else {
@ -895,7 +891,7 @@ class InlinedVector {
template <typename... Args> template <typename... Args>
reference Construct(pointer p, Args&&... args) { reference Construct(pointer p, Args&&... args) {
std::allocator_traits<allocator_type>::construct( std::allocator_traits<allocator_type>::construct(
storage_.GetAllocator(), p, std::forward<Args>(args)...); *storage_.GetAllocPtr(), p, std::forward<Args>(args)...);
return *p; return *p;
} }
@ -912,7 +908,7 @@ class InlinedVector {
// Destroy [`from`, `to`) in place. // Destroy [`from`, `to`) in place.
void Destroy(pointer from, pointer to) { void Destroy(pointer from, pointer to) {
for (pointer cur = from; cur != to; ++cur) { for (pointer cur = from; cur != to; ++cur) {
std::allocator_traits<allocator_type>::destroy(storage_.GetAllocator(), std::allocator_traits<allocator_type>::destroy(*storage_.GetAllocPtr(),
cur); cur);
} }
#if !defined(NDEBUG) #if !defined(NDEBUG)
@ -943,7 +939,7 @@ class InlinedVector {
} }
pointer new_data = pointer new_data =
AllocatorTraits::allocate(storage_.GetAllocator(), new_capacity); AllocatorTraits::allocate(*storage_.GetAllocPtr(), new_capacity);
UninitializedCopy(std::make_move_iterator(data()), UninitializedCopy(std::make_move_iterator(data()),
std::make_move_iterator(data() + s), new_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 // Move everyone into the new allocation, leaving a gap of `n` for the
// requested shift. // requested shift.
pointer new_data = pointer new_data =
AllocatorTraits::allocate(storage_.GetAllocator(), new_capacity); AllocatorTraits::allocate(*storage_.GetAllocPtr(), new_capacity);
size_type index = position - begin(); size_type index = position - begin();
UninitializedCopy(std::make_move_iterator(data()), UninitializedCopy(std::make_move_iterator(data()),
std::make_move_iterator(data() + index), new_data); std::make_move_iterator(data() + index), new_data);
@ -1024,7 +1020,7 @@ class InlinedVector {
size_type new_capacity = 2 * capacity(); size_type new_capacity = 2 * capacity();
pointer new_data = pointer new_data =
AllocatorTraits::allocate(storage_.GetAllocator(), new_capacity); AllocatorTraits::allocate(*storage_.GetAllocPtr(), new_capacity);
reference new_element = reference new_element =
Construct(new_data + s, std::forward<Args>(args)...); Construct(new_data + s, std::forward<Args>(args)...);
@ -1038,7 +1034,7 @@ class InlinedVector {
void InitAssign(size_type n) { void InitAssign(size_type n) {
if (n > static_cast<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_.SetAllocatedData(new_data);
storage_.SetAllocatedCapacity(n); storage_.SetAllocatedCapacity(n);
UninitializedFill(storage_.GetAllocatedData(), UninitializedFill(storage_.GetAllocatedData(),
@ -1053,7 +1049,7 @@ class InlinedVector {
void InitAssign(size_type n, const_reference v) { void InitAssign(size_type n, const_reference v) {
if (n > static_cast<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_.SetAllocatedData(new_data);
storage_.SetAllocatedCapacity(n); storage_.SetAllocatedCapacity(n);
UninitializedFill(storage_.GetAllocatedData(), UninitializedFill(storage_.GetAllocatedData(),
@ -1126,7 +1122,7 @@ class InlinedVector {
// Both out of line, so just swap the tag, allocation, and allocator. // Both out of line, so just swap the tag, allocation, and allocator.
storage_.SwapSizeAndIsAllocated(std::addressof(other.storage_)); storage_.SwapSizeAndIsAllocated(std::addressof(other.storage_));
storage_.SwapAllocatedSizeAndCapacity(std::addressof(other.storage_)); storage_.SwapAllocatedSizeAndCapacity(std::addressof(other.storage_));
swap(storage_.GetAllocator(), other.storage_.GetAllocator()); swap(*storage_.GetAllocPtr(), *other.storage_.GetAllocPtr());
return; return;
} }
@ -1156,7 +1152,7 @@ class InlinedVector {
a->storage_.GetInlinedData() + a_size); a->storage_.GetInlinedData() + a_size);
storage_.SwapSizeAndIsAllocated(std::addressof(other.storage_)); storage_.SwapSizeAndIsAllocated(std::addressof(other.storage_));
swap(storage_.GetAllocator(), other.storage_.GetAllocator()); swap(*storage_.GetAllocPtr(), *other.storage_.GetAllocPtr());
assert(b->size() == a_size); assert(b->size() == a_size);
assert(a->size() == b_size); assert(a->size() == b_size);
@ -1198,8 +1194,8 @@ class InlinedVector {
a->storage_.SetAllocatedData(b_data); a->storage_.SetAllocatedData(b_data);
a->storage_.SetAllocatedCapacity(b_capacity); a->storage_.SetAllocatedCapacity(b_capacity);
if (a->storage_.GetAllocator() != b->storage_.GetAllocator()) { if (*a->storage_.GetAllocPtr() != *b->storage_.GetAllocPtr()) {
swap(a->storage_.GetAllocator(), b->storage_.GetAllocator()); swap(*a->storage_.GetAllocPtr(), *b->storage_.GetAllocPtr());
} }
assert(b->size() == a_size); assert(b->size() == a_size);

View file

@ -33,7 +33,7 @@ using IsAtLeastForwardIterator = std::is_convertible<
std::forward_iterator_tag>; std::forward_iterator_tag>;
template <typename AllocatorType, typename ValueType, typename SizeType> 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) { SizeType destroy_size) {
using AllocatorTraits = std::allocator_traits<AllocatorType>; 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 // NOTE: We assume destructors do not throw and thus make no attempt to roll
// back. // back.
for (SizeType i = 0; i < destroy_size; ++i) { for (SizeType i = 0; i < destroy_size; ++i) {
AllocatorTraits::destroy(alloc, destroy_first + i); AllocatorTraits::destroy(*alloc_ptr, destroy_first + i);
} }
#ifndef NDEBUG #ifndef NDEBUG
@ -104,10 +104,12 @@ class Storage {
return data_.allocated.allocated_capacity; 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 { const allocator_type* GetAllocPtr() const {
return metadata_.template get<0>(); return std::addressof(metadata_.template get<0>());
} }
void SetAllocatedSize(size_type size) { void SetAllocatedSize(size_type size) {

View file

@ -190,7 +190,7 @@ bool GetEnvVar(const char* var_name, std::string* var_value) {
char buf[1024]; char buf[1024];
auto get_res = GetEnvironmentVariableA(var_name, buf, sizeof(buf)); auto get_res = GetEnvironmentVariableA(var_name, buf, sizeof(buf));
if (get_res >= sizeof(buf)) { if (get_res >= sizeof(buf)) {
return ""; return false;
} }
if (get_res == 0) { if (get_res == 0) {

View file

@ -421,7 +421,9 @@ class optional : private optional_internal::optional_data<T>,
// //
// Accesses the underlying `T` value of an `optional`. If the `optional` is // Accesses the underlying `T` value of an `optional`. If the `optional` is
// empty, behavior is undefined. // empty, behavior is undefined.
constexpr const T& operator*() const & { return reference(); } constexpr const T& operator*() const& {
return ABSL_ASSERT(this->engaged_), reference();
}
T& operator*() & { T& operator*() & {
assert(this->engaged_); assert(this->engaged_);
return reference(); return reference();