Skip to content

Commit 79e0dc1

Browse files
Abseil TeamXiaoyi Zhang
authored andcommitted
Export of internal Abseil changes
-- 990253454819ce26ff1dda9ab4bbc145b61d01e4 by Xiaoyi Zhang <[email protected]>: Import github PR #645 PiperOrigin-RevId: 303119797 -- 5ac845cb7929b7d1eaf59a309afd811db5001175 by Abseil Team <[email protected]>: Fix internal exception spec compatibility error PiperOrigin-RevId: 303104081 -- 3290595dd866eecab3c7044e2e3ca0adb74f1bf5 by Gennadiy Rozental <[email protected]>: Use FlagValue<T> to represent the value of a flag. Place it directly after FlagImpl and use a computed offset refer to it. The offset is computed based on the assumption that the `value_` data member is placed directly after the impl_ data member in Flag<T>. This change will allow us to migrate to `T`-specific storage in the generic case. This change decreases the overhead for int flags by 32 bytes. PiperOrigin-RevId: 303038099 -- f2b37722cd7a6d3a60ef9713f0d2bbff56f3ddbf by Derek Mauro <[email protected]>: Minor correctness fix for an ABSL_HAVE_BUILTIN conditional PiperOrigin-RevId: 302980666 -- 39c079a6141ae1c5728af8bf33a39c8aff9deb9f by Abseil Team <[email protected]>: Use ABSL_HARDENING_ASSERT in b-tree and SwissTable iterators. PiperOrigin-RevId: 302970075 -- 9668a044e080c789df32bcaa1ffb5100831cd9fa by Benjamin Barenblat <[email protected]>: Correct `add_subdirectory` line in CMake googletest support Commit bcefbdc added support for building with CMake against a local googletest checkout, but I missed a line when constructing the diff. Change the `add_subdirectory` line to reference the correct directories. PiperOrigin-RevId: 302947488 -- 0a3c10fabf80a43ca69ab8b1570030e55f2be741 by Andy Soffer <[email protected]>: Remove unused distribution format traits. PiperOrigin-RevId: 302896176 -- 0478f2f6270e5ed64c0e28ec09556ca90b2d46a9 by Samuel Benzaquen <[email protected]>: Fix for CWG:2310. PiperOrigin-RevId: 302734089 -- 3cb978dda5cae5905affdc0914dcc2d27671ed11 by Samuel Benzaquen <[email protected]>: Fix the Allocate/Deallocate functions to use the same underlying allocator type. PiperOrigin-RevId: 302721804 -- ae38d3984fb68b4e3ddc165fa8d5c24d5936be52 by Matthew Brown <[email protected]>: Internal Change PiperOrigin-RevId: 302717314 -- 7357cf7abd03cc60b6e82b5f28a8e34935c3b4dc by Andy Getzendanner <[email protected]>: Fix typo: s/ABSL_HARDENED_ASSERT/ABSL_HARDENING_ASSERT/ PiperOrigin-RevId: 302532164 GitOrigin-RevId: 990253454819ce26ff1dda9ab4bbc145b61d01e4 Change-Id: Ie595a221c16e1e7e1255ad42e029b646c5f3e11d
1 parent 132d791 commit 79e0dc1

29 files changed

+387
-607
lines changed

CMake/Googletest/DownloadGTest.cmake

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,4 @@ set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
3838

3939
# Add googletest directly to our build. This defines the gtest and gtest_main
4040
# targets.
41-
add_subdirectory(${CMAKE_BINARY_DIR}/googletest-src
42-
${CMAKE_BINARY_DIR}/googletest-build
43-
EXCLUDE_FROM_ALL)
41+
add_subdirectory(${absl_gtest_src_dir} ${absl_gtest_build_dir} EXCLUDE_FROM_ALL)

absl/base/macros.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,8 @@ ABSL_NAMESPACE_END
212212
// aborts the program in release mode (when NDEBUG is defined). The
213213
// implementation should abort the program as quickly as possible and ideally it
214214
// should not be possible to ignore the abort request.
215-
#if ABSL_HAVE_BUILTIN(__builtin_trap) || \
215+
#if (ABSL_HAVE_BUILTIN(__builtin_trap) && \
216+
ABSL_HAVE_BUILTIN(__builtin_unreachable)) || \
216217
(defined(__GNUC__) && !defined(__clang__))
217218
#define ABSL_INTERNAL_HARDENING_ABORT() \
218219
do { \
@@ -225,11 +226,11 @@ ABSL_NAMESPACE_END
225226

226227
// ABSL_HARDENING_ASSERT()
227228
//
228-
// `ABSL_HARDENED_ASSERT()` is like `ABSL_ASSERT()`, but used to implement
229+
// `ABSL_HARDENING_ASSERT()` is like `ABSL_ASSERT()`, but used to implement
229230
// runtime assertions that should be enabled in hardened builds even when
230231
// `NDEBUG` is defined.
231232
//
232-
// When `NDEBUG` is not defined, `ABSL_HARDENED_ASSERT()` is identical to
233+
// When `NDEBUG` is not defined, `ABSL_HARDENING_ASSERT()` is identical to
233234
// `ABSL_ASSERT()`.
234235
//
235236
// See `ABSL_OPTION_HARDENED` in `absl/base/options.h` for more information on

absl/container/internal/btree.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -937,8 +937,13 @@ struct btree_iterator {
937937
}
938938

939939
// Accessors for the key/value the iterator is pointing at.
940-
reference operator*() const { return node->value(position); }
941-
pointer operator->() const { return &node->value(position); }
940+
reference operator*() const {
941+
ABSL_HARDENING_ASSERT(node != nullptr);
942+
ABSL_HARDENING_ASSERT(node->start() <= position);
943+
ABSL_HARDENING_ASSERT(node->finish() > position);
944+
return node->value(position);
945+
}
946+
pointer operator->() const { return &operator*(); }
942947

943948
btree_iterator &operator++() {
944949
increment();
@@ -1769,6 +1774,7 @@ void btree_iterator<N, R, P>::increment_slow() {
17691774
position = node->position();
17701775
node = node->parent();
17711776
}
1777+
// TODO(ezb): assert we aren't incrementing end() instead of handling.
17721778
if (position == node->finish()) {
17731779
*this = save;
17741780
}
@@ -1792,6 +1798,7 @@ void btree_iterator<N, R, P>::decrement_slow() {
17921798
position = node->position() - 1;
17931799
node = node->parent();
17941800
}
1801+
// TODO(ezb): assert we aren't decrementing begin() instead of handling.
17951802
if (position < node->start()) {
17961803
*this = save;
17971804
}

absl/container/internal/common.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,15 @@ class node_handle<Policy, PolicyTraits, Alloc,
138138
absl::void_t<typename Policy::mapped_type>>
139139
: public node_handle_base<PolicyTraits, Alloc> {
140140
using Base = node_handle_base<PolicyTraits, Alloc>;
141+
using slot_type = typename PolicyTraits::slot_type;
141142

142143
public:
143144
using key_type = typename Policy::key_type;
144145
using mapped_type = typename Policy::mapped_type;
145146

146147
constexpr node_handle() {}
147148

148-
auto key() const -> decltype(PolicyTraits::key(this->slot())) {
149+
auto key() const -> decltype(PolicyTraits::key(std::declval<slot_type*>())) {
149150
return PolicyTraits::key(this->slot());
150151
}
151152

absl/container/internal/container_memory.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ namespace absl {
3737
ABSL_NAMESPACE_BEGIN
3838
namespace container_internal {
3939

40+
template <size_t Alignment>
41+
struct alignas(Alignment) AlignedType {};
42+
4043
// Allocates at least n bytes aligned to the specified alignment.
4144
// Alignment must be a power of 2. It must be positive.
4245
//
@@ -48,7 +51,7 @@ template <size_t Alignment, class Alloc>
4851
void* Allocate(Alloc* alloc, size_t n) {
4952
static_assert(Alignment > 0, "");
5053
assert(n && "n must be positive");
51-
struct alignas(Alignment) M {};
54+
using M = AlignedType<Alignment>;
5255
using A = typename absl::allocator_traits<Alloc>::template rebind_alloc<M>;
5356
using AT = typename absl::allocator_traits<Alloc>::template rebind_traits<M>;
5457
A mem_alloc(*alloc);
@@ -64,7 +67,7 @@ template <size_t Alignment, class Alloc>
6467
void Deallocate(Alloc* alloc, void* p, size_t n) {
6568
static_assert(Alignment > 0, "");
6669
assert(n && "n must be positive");
67-
struct alignas(Alignment) M {};
70+
using M = AlignedType<Alignment>;
6871
using A = typename absl::allocator_traits<Alloc>::template rebind_alloc<M>;
6972
using AT = typename absl::allocator_traits<Alloc>::template rebind_traits<M>;
7073
A mem_alloc(*alloc);

absl/container/internal/container_memory_test.cc

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
#include <cstdint>
1818
#include <tuple>
19+
#include <typeindex>
20+
#include <typeinfo>
1921
#include <utility>
2022

2123
#include "gmock/gmock.h"
@@ -27,6 +29,9 @@ ABSL_NAMESPACE_BEGIN
2729
namespace container_internal {
2830
namespace {
2931

32+
using ::testing::Gt;
33+
using ::testing::_;
34+
using ::testing::ElementsAre;
3035
using ::testing::Pair;
3136

3237
TEST(Memory, AlignmentLargerThanBase) {
@@ -45,6 +50,39 @@ TEST(Memory, AlignmentSmallerThanBase) {
4550
Deallocate<2>(&alloc, mem, 3);
4651
}
4752

53+
std::map<std::type_index, int>& AllocationMap() {
54+
static auto* map = new std::map<std::type_index, int>;
55+
return *map;
56+
}
57+
58+
template <typename T>
59+
struct TypeCountingAllocator {
60+
TypeCountingAllocator() = default;
61+
template <typename U>
62+
TypeCountingAllocator(const TypeCountingAllocator<U>&) {} // NOLINT
63+
64+
using value_type = T;
65+
66+
T* allocate(size_t n, const void* = nullptr) {
67+
AllocationMap()[typeid(T)] += n;
68+
return std::allocator<T>().allocate(n);
69+
}
70+
void deallocate(T* p, std::size_t n) {
71+
AllocationMap()[typeid(T)] -= n;
72+
return std::allocator<T>().deallocate(p, n);
73+
}
74+
};
75+
76+
TEST(Memory, AllocateDeallocateMatchType) {
77+
TypeCountingAllocator<int> alloc;
78+
void* mem = Allocate<1>(&alloc, 1);
79+
// Verify that it was allocated
80+
EXPECT_THAT(AllocationMap(), ElementsAre(Pair(_, Gt(0))));
81+
Deallocate<1>(&alloc, mem, 1);
82+
// Verify that the deallocation matched.
83+
EXPECT_THAT(AllocationMap(), ElementsAre(Pair(_, 0)));
84+
}
85+
4886
class Fixture : public ::testing::Test {
4987
using Alloc = std::allocator<std::string>;
5088

absl/container/internal/raw_hash_set.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104

105105
#include "absl/base/internal/bits.h"
106106
#include "absl/base/internal/endian.h"
107+
#include "absl/base/macros.h"
107108
#include "absl/base/port.h"
108109
#include "absl/container/internal/common.h"
109110
#include "absl/container/internal/compressed_tuple.h"
@@ -651,9 +652,9 @@ class raw_hash_set {
651652
iterator(ctrl_t* ctrl) : ctrl_(ctrl) {} // for end()
652653
iterator(ctrl_t* ctrl, slot_type* slot) : ctrl_(ctrl), slot_(slot) {}
653654

654-
void assert_is_full() const { assert(IsFull(*ctrl_)); }
655+
void assert_is_full() const { ABSL_HARDENING_ASSERT(IsFull(*ctrl_)); }
655656
void assert_is_valid() const {
656-
assert(!ctrl_ || IsFull(*ctrl_) || *ctrl_ == kSentinel);
657+
ABSL_HARDENING_ASSERT(!ctrl_ || IsFull(*ctrl_) || *ctrl_ == kSentinel);
657658
}
658659

659660
void skip_empty_or_deleted() {

absl/flags/flag_test.cc

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -91,30 +91,30 @@ struct S2 {
9191
};
9292

9393
TEST_F(FlagTest, Traits) {
94-
EXPECT_EQ(flags::FlagValue::Kind<int>(),
94+
EXPECT_EQ(flags::StorageKind<int>(),
9595
flags::FlagValueStorageKind::kOneWordAtomic);
96-
EXPECT_EQ(flags::FlagValue::Kind<bool>(),
96+
EXPECT_EQ(flags::StorageKind<bool>(),
9797
flags::FlagValueStorageKind::kOneWordAtomic);
98-
EXPECT_EQ(flags::FlagValue::Kind<double>(),
98+
EXPECT_EQ(flags::StorageKind<double>(),
9999
flags::FlagValueStorageKind::kOneWordAtomic);
100-
EXPECT_EQ(flags::FlagValue::Kind<int64_t>(),
100+
EXPECT_EQ(flags::StorageKind<int64_t>(),
101101
flags::FlagValueStorageKind::kOneWordAtomic);
102102

103103
#if defined(ABSL_FLAGS_INTERNAL_ATOMIC_DOUBLE_WORD)
104-
EXPECT_EQ(flags::FlagValue::Kind<S1>(),
104+
EXPECT_EQ(flags::StorageKind<S1>(),
105105
flags::FlagValueStorageKind::kTwoWordsAtomic);
106-
EXPECT_EQ(flags::FlagValue::Kind<S2>(),
106+
EXPECT_EQ(flags::StorageKind<S2>(),
107107
flags::FlagValueStorageKind::kTwoWordsAtomic);
108108
#else
109-
EXPECT_EQ(flags::FlagValue::Kind<S1>(),
109+
EXPECT_EQ(flags::StorageKind<S1>(),
110110
flags::FlagValueStorageKind::kHeapAllocated);
111-
EXPECT_EQ(flags::FlagValue::Kind<S2>(),
111+
EXPECT_EQ(flags::StorageKind<S2>(),
112112
flags::FlagValueStorageKind::kHeapAllocated);
113113
#endif
114114

115-
EXPECT_EQ(flags::FlagValue::Kind<std::string>(),
115+
EXPECT_EQ(flags::StorageKind<std::string>(),
116116
flags::FlagValueStorageKind::kHeapAllocated);
117-
EXPECT_EQ(flags::FlagValue::Kind<std::vector<std::string>>(),
117+
EXPECT_EQ(flags::StorageKind<std::vector<std::string>>(),
118118
flags::FlagValueStorageKind::kHeapAllocated);
119119
}
120120

@@ -624,10 +624,10 @@ TEST_F(FlagTest, TestNonDefaultConstructibleType) {
624624
EXPECT_EQ(absl::GetFlag(FLAGS_ndc_flag2).value, 25);
625625
}
626626

627-
// --------------------------------------------------------------------
628-
629627
} // namespace
630628

629+
// --------------------------------------------------------------------
630+
631631
ABSL_RETIRED_FLAG(bool, old_bool_flag, true, "old descr");
632632
ABSL_RETIRED_FLAG(int, old_int_flag, (int)std::sqrt(10), "old descr");
633633
ABSL_RETIRED_FLAG(std::string, old_str_flag, "", absl::StrCat("old ", "descr"));

0 commit comments

Comments
 (0)