Export of internal Abseil changes

--
0e867881e4b9f388a13d6fa8ed715192460130ab by Abseil Team <absl-team@google.com>:

Minor wording change to header comment for Mutex::AwaitWithDeadline(). No functional changes.

PiperOrigin-RevId: 306729491

--
fc64361fb831003fa5e6fbb84a9a89338fd2838c by Derek Mauro <dmauro@google.com>:

Uses C++20 compatible allocator traits in Abseil types

This merges both instances of CountingAllocator in the Abseil codebase.
Makes the presubmits test C++20 mode.

Fixes #651

PiperOrigin-RevId: 306728102

--
d759e5681b9dd6b7339fc019ed58fb5fdececdc3 by Derek Mauro <dmauro@google.com>:

Makes btree's iterator comparisons C++20 compatible

See https://stackoverflow.com/questions/60386792/c20-comparison-warning-about-ambiguous-reversed-operator

PiperOrigin-RevId: 306702048

--
e9da5f409bc5ddb1bad308f9d8c41213c67a1d1e by Derek Mauro <dmauro@google.com>:

Switch a few uses of at() that should have been data() in the implementation of InlinedVector.
Use ABSL_HARDENING_ASSERT in resize().

PiperOrigin-RevId: 306670992
GitOrigin-RevId: 0e867881e4b9f388a13d6fa8ed715192460130ab
Change-Id: If431f3e5d77097e9901654773552dcc01dface87
This commit is contained in:
Abseil Team 2020-04-15 15:13:54 -07:00 committed by Mark Barolak
parent 71079e42cb
commit db5773a721
13 changed files with 90 additions and 108 deletions

View file

@ -73,6 +73,7 @@ cc_test(
copts = ABSL_TEST_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS,
deps = [
":counting_allocator",
":fixed_array",
"//absl/base:config",
"//absl/base:exception_testing",

View file

@ -147,6 +147,7 @@ absl_cc_test(
${ABSL_TEST_COPTS}
DEPS
absl::fixed_array
absl::counting_allocator
absl::config
absl::exception_testing
absl::hash_testing

View file

@ -106,13 +106,13 @@ class FixedArray {
public:
using allocator_type = typename AllocatorTraits::allocator_type;
using value_type = typename allocator_type::value_type;
using pointer = typename allocator_type::pointer;
using const_pointer = typename allocator_type::const_pointer;
using reference = typename allocator_type::reference;
using const_reference = typename allocator_type::const_reference;
using size_type = typename allocator_type::size_type;
using difference_type = typename allocator_type::difference_type;
using value_type = typename AllocatorTraits::value_type;
using pointer = typename AllocatorTraits::pointer;
using const_pointer = typename AllocatorTraits::const_pointer;
using reference = value_type&;
using const_reference = const value_type&;
using size_type = typename AllocatorTraits::size_type;
using difference_type = typename AllocatorTraits::difference_type;
using iterator = pointer;
using const_iterator = const_pointer;
using reverse_iterator = std::reverse_iterator<iterator>;

View file

@ -29,6 +29,7 @@
#include "gtest/gtest.h"
#include "absl/base/internal/exception_testing.h"
#include "absl/base/options.h"
#include "absl/container/internal/counting_allocator.h"
#include "absl/hash/hash_testing.h"
#include "absl/memory/memory.h"
@ -638,70 +639,9 @@ TEST(FixedArrayTest, DefaultCtorDoesNotValueInit) {
}
#endif // __GNUC__
// This is a stateful allocator, but the state lives outside of the
// allocator (in whatever test is using the allocator). This is odd
// but helps in tests where the allocator is propagated into nested
// containers - that chain of allocators uses the same state and is
// thus easier to query for aggregate allocation information.
template <typename T>
class CountingAllocator : public std::allocator<T> {
public:
using Alloc = std::allocator<T>;
using pointer = typename Alloc::pointer;
using size_type = typename Alloc::size_type;
CountingAllocator() : bytes_used_(nullptr), instance_count_(nullptr) {}
explicit CountingAllocator(int64_t* b)
: bytes_used_(b), instance_count_(nullptr) {}
CountingAllocator(int64_t* b, int64_t* a)
: bytes_used_(b), instance_count_(a) {}
template <typename U>
explicit CountingAllocator(const CountingAllocator<U>& x)
: Alloc(x),
bytes_used_(x.bytes_used_),
instance_count_(x.instance_count_) {}
pointer allocate(size_type n, const void* const hint = nullptr) {
assert(bytes_used_ != nullptr);
*bytes_used_ += n * sizeof(T);
return Alloc::allocate(n, hint);
}
void deallocate(pointer p, size_type n) {
Alloc::deallocate(p, n);
assert(bytes_used_ != nullptr);
*bytes_used_ -= n * sizeof(T);
}
template <typename... Args>
void construct(pointer p, Args&&... args) {
Alloc::construct(p, absl::forward<Args>(args)...);
if (instance_count_) {
*instance_count_ += 1;
}
}
void destroy(pointer p) {
Alloc::destroy(p);
if (instance_count_) {
*instance_count_ -= 1;
}
}
template <typename U>
class rebind {
public:
using other = CountingAllocator<U>;
};
int64_t* bytes_used_;
int64_t* instance_count_;
};
TEST(AllocatorSupportTest, CountInlineAllocations) {
constexpr size_t inlined_size = 4;
using Alloc = CountingAllocator<int>;
using Alloc = absl::container_internal::CountingAllocator<int>;
using AllocFxdArr = absl::FixedArray<int, inlined_size, Alloc>;
int64_t allocated = 0;
@ -722,7 +662,7 @@ TEST(AllocatorSupportTest, CountInlineAllocations) {
TEST(AllocatorSupportTest, CountOutoflineAllocations) {
constexpr size_t inlined_size = 4;
using Alloc = CountingAllocator<int>;
using Alloc = absl::container_internal::CountingAllocator<int>;
using AllocFxdArr = absl::FixedArray<int, inlined_size, Alloc>;
int64_t allocated = 0;
@ -743,7 +683,7 @@ TEST(AllocatorSupportTest, CountOutoflineAllocations) {
TEST(AllocatorSupportTest, CountCopyInlineAllocations) {
constexpr size_t inlined_size = 4;
using Alloc = CountingAllocator<int>;
using Alloc = absl::container_internal::CountingAllocator<int>;
using AllocFxdArr = absl::FixedArray<int, inlined_size, Alloc>;
int64_t allocated1 = 0;
@ -771,7 +711,7 @@ TEST(AllocatorSupportTest, CountCopyInlineAllocations) {
TEST(AllocatorSupportTest, CountCopyOutoflineAllocations) {
constexpr size_t inlined_size = 4;
using Alloc = CountingAllocator<int>;
using Alloc = absl::container_internal::CountingAllocator<int>;
using AllocFxdArr = absl::FixedArray<int, inlined_size, Alloc>;
int64_t allocated1 = 0;
@ -803,7 +743,7 @@ TEST(AllocatorSupportTest, SizeValAllocConstructor) {
using testing::SizeIs;
constexpr size_t inlined_size = 4;
using Alloc = CountingAllocator<int>;
using Alloc = absl::container_internal::CountingAllocator<int>;
using AllocFxdArr = absl::FixedArray<int, inlined_size, Alloc>;
{

View file

@ -351,14 +351,14 @@ class InlinedVector {
// Returns a `reference` to the first element of the inlined vector.
reference front() {
ABSL_HARDENING_ASSERT(!empty());
return at(0);
return data()[0];
}
// Overload of `InlinedVector::front()` that returns a `const_reference` to
// the first element of the inlined vector.
const_reference front() const {
ABSL_HARDENING_ASSERT(!empty());
return at(0);
return data()[0];
}
// `InlinedVector::back()`
@ -366,14 +366,14 @@ class InlinedVector {
// Returns a `reference` to the last element of the inlined vector.
reference back() {
ABSL_HARDENING_ASSERT(!empty());
return at(size() - 1);
return data()[size() - 1];
}
// Overload of `InlinedVector::back()` that returns a `const_reference` to the
// last element of the inlined vector.
const_reference back() const {
ABSL_HARDENING_ASSERT(!empty());
return at(size() - 1);
return data()[size() - 1];
}
// `InlinedVector::begin()`
@ -524,7 +524,7 @@ class InlinedVector {
void assign(InputIterator first, InputIterator last) {
size_type i = 0;
for (; i < size() && first != last; ++i, static_cast<void>(++first)) {
at(i) = *first;
data()[i] = *first;
}
erase(data() + i, data() + size());
@ -537,7 +537,10 @@ class InlinedVector {
//
// NOTE: if `n` is smaller than `size()`, extra elements are destroyed. If `n`
// is larger than `size()`, new elements are value-initialized.
void resize(size_type n) { storage_.Resize(DefaultValueAdapter(), n); }
void resize(size_type n) {
ABSL_HARDENING_ASSERT(n <= max_size());
storage_.Resize(DefaultValueAdapter(), n);
}
// Overload of `InlinedVector::resize(...)` that resizes the inlined vector to
// contain `n` elements.
@ -545,6 +548,7 @@ class InlinedVector {
// NOTE: if `n` is smaller than `size()`, extra elements are destroyed. If `n`
// is larger than `size()`, new elements are copied-constructed from `v`.
void resize(size_type n, const_reference v) {
ABSL_HARDENING_ASSERT(n <= max_size());
storage_.Resize(CopyValueAdapter(v), n);
}

View file

@ -929,9 +929,15 @@ struct btree_iterator {
void decrement_slow();
public:
bool operator==(const iterator &other) const {
return node == other.node && position == other.position;
}
bool operator==(const const_iterator &other) const {
return node == other.node && position == other.position;
}
bool operator!=(const iterator &other) const {
return node != other.node || position != other.position;
}
bool operator!=(const const_iterator &other) const {
return node != other.node || position != other.position;
}

View file

@ -15,7 +15,6 @@
#ifndef ABSL_CONTAINER_INTERNAL_COUNTING_ALLOCATOR_H_
#define ABSL_CONTAINER_INTERNAL_COUNTING_ALLOCATOR_H_
#include <cassert>
#include <cstdint>
#include <memory>
@ -31,33 +30,63 @@ namespace container_internal {
// containers - that chain of allocators uses the same state and is
// thus easier to query for aggregate allocation information.
template <typename T>
class CountingAllocator : public std::allocator<T> {
class CountingAllocator {
public:
using Alloc = std::allocator<T>;
using pointer = typename Alloc::pointer;
using size_type = typename Alloc::size_type;
using Allocator = std::allocator<T>;
using AllocatorTraits = std::allocator_traits<Allocator>;
using value_type = typename AllocatorTraits::value_type;
using pointer = typename AllocatorTraits::pointer;
using const_pointer = typename AllocatorTraits::const_pointer;
using size_type = typename AllocatorTraits::size_type;
using difference_type = typename AllocatorTraits::difference_type;
CountingAllocator() : bytes_used_(nullptr) {}
explicit CountingAllocator(int64_t* b) : bytes_used_(b) {}
CountingAllocator() = default;
explicit CountingAllocator(int64_t* bytes_used) : bytes_used_(bytes_used) {}
CountingAllocator(int64_t* bytes_used, int64_t* instance_count)
: bytes_used_(bytes_used), instance_count_(instance_count) {}
template <typename U>
CountingAllocator(const CountingAllocator<U>& x)
: Alloc(x), bytes_used_(x.bytes_used_) {}
: bytes_used_(x.bytes_used_), instance_count_(x.instance_count_) {}
pointer allocate(size_type n,
std::allocator<void>::const_pointer hint = nullptr) {
assert(bytes_used_ != nullptr);
*bytes_used_ += n * sizeof(T);
return Alloc::allocate(n, hint);
pointer allocate(
size_type n,
typename AllocatorTraits::const_void_pointer hint = nullptr) {
Allocator allocator;
pointer ptr = AllocatorTraits::allocate(allocator, n, hint);
if (bytes_used_ != nullptr) {
*bytes_used_ += n * sizeof(T);
}
return ptr;
}
void deallocate(pointer p, size_type n) {
Alloc::deallocate(p, n);
assert(bytes_used_ != nullptr);
*bytes_used_ -= n * sizeof(T);
Allocator allocator;
AllocatorTraits::deallocate(allocator, p, n);
if (bytes_used_ != nullptr) {
*bytes_used_ -= n * sizeof(T);
}
}
template<typename U>
template <typename U, typename... Args>
void construct(U* p, Args&&... args) {
Allocator allocator;
AllocatorTraits::construct(allocator, p, std::forward<Args>(args)...);
if (instance_count_ != nullptr) {
*instance_count_ += 1;
}
}
template <typename U>
void destroy(U* p) {
Allocator allocator;
AllocatorTraits::destroy(allocator, p);
if (instance_count_ != nullptr) {
*instance_count_ -= 1;
}
}
template <typename U>
class rebind {
public:
using other = CountingAllocator<U>;
@ -65,7 +94,8 @@ class CountingAllocator : public std::allocator<T> {
friend bool operator==(const CountingAllocator& a,
const CountingAllocator& b) {
return a.bytes_used_ == b.bytes_used_;
return a.bytes_used_ == b.bytes_used_ &&
a.instance_count_ == b.instance_count_;
}
friend bool operator!=(const CountingAllocator& a,
@ -73,7 +103,8 @@ class CountingAllocator : public std::allocator<T> {
return !(a == b);
}
int64_t* bytes_used_;
int64_t* bytes_used_ = nullptr;
int64_t* instance_count_ = nullptr;
};
} // namespace container_internal

View file

@ -331,17 +331,16 @@ class ABSL_LOCKABLE Mutex {
// Mutex::AwaitWithTimeout()
// Mutex::AwaitWithDeadline()
//
// If `cond` is initially true, do nothing, or act as though `cond` is
// initially false.
//
// If `cond` is initially false, unlock this `Mutex` and block until
// simultaneously:
// Unlocks this `Mutex` and blocks until simultaneously:
// - either `cond` is true or the {timeout has expired, deadline has passed}
// and
// - this `Mutex` can be reacquired,
// then reacquire this `Mutex` in the same mode in which it was previously
// held, returning `true` iff `cond` is `true` on return.
//
// If the condition is initially `true`, the implementation *may* skip the
// release/re-acquire step and return immediately.
//
// Deadlines in the past are equivalent to an immediate deadline.
// Negative timeouts are equivalent to a zero timeout.
//

View file

@ -25,7 +25,7 @@ if [[ -z ${ABSEIL_ROOT:-} ]]; then
fi
if [[ -z ${STD:-} ]]; then
STD="c++11 c++14 c++17"
STD="c++11 c++14 c++17 c++20"
fi
if [[ -z ${COMPILATION_MODE:-} ]]; then

View file

@ -25,7 +25,7 @@ if [[ -z ${ABSEIL_ROOT:-} ]]; then
fi
if [[ -z ${STD:-} ]]; then
STD="c++11 c++14 c++17"
STD="c++11 c++14 c++17 c++20"
fi
if [[ -z ${COMPILATION_MODE:-} ]]; then

View file

@ -25,7 +25,7 @@ if [[ -z ${ABSEIL_ROOT:-} ]]; then
fi
if [[ -z ${STD:-} ]]; then
STD="c++11 c++14 c++17"
STD="c++11 c++14 c++17 c++20"
fi
if [[ -z ${COMPILATION_MODE:-} ]]; then

View file

@ -25,7 +25,7 @@ if [[ -z ${ABSEIL_ROOT:-} ]]; then
fi
if [[ -z ${STD:-} ]]; then
STD="c++11 c++14 c++17"
STD="c++11 c++14 c++17 c++20"
fi
if [[ -z ${COMPILATION_MODE:-} ]]; then

View file

@ -25,7 +25,7 @@ if [[ -z ${ABSEIL_ROOT:-} ]]; then
fi
if [[ -z ${STD:-} ]]; then
STD="c++11 c++14 c++17"
STD="c++11 c++14 c++17 c++2a"
fi
if [[ -z ${COMPILATION_MODE:-} ]]; then