Skip to content

Commit b315753

Browse files
Abseil Teamsuertreus
authored andcommitted
Export of internal Abseil changes
-- 2247e59ee5696e61c7cb24dce479db572980032d by Derek Mauro <[email protected]>: Ensure ThreadIdentity::WaiterState data has the correct alignment PiperOrigin-RevId: 357969866 -- 79bb81a2e6723b186536ad4b4f25cd7ee83f3e72 by Laramie Leavitt <[email protected]>: Rework absl::MockingBitGen internal mock registry. Use a virtual base class with template-specialized derived classes to implement match method rather than taking the address of a function specialization and using casts everywhere. This combines several of the previously type-erased pointers into a single object, eliminates the separate cleanup container previously used, and eliminates some gratuitous static_cast<>s. PiperOrigin-RevId: 357827438 -- a2eb53d002c9b376360a70b2b0f6a18a1de1e16f by Todd Lipcon <[email protected]>: Reduce runtime of sequence_lock_test PiperOrigin-RevId: 357785571 -- dd1175ff13b560f01c4aaa64bc6759a5300e391c by Abseil Team <[email protected]>: Don't assume that compiling for Windows means that MSVC or clang-cl is being used. PiperOrigin-RevId: 357760052 -- 251a9cbede52e729b3a25911b6e225b779c285a8 by Andy Getzendanner <[email protected]>: Internal change. PiperOrigin-RevId: 357702979 GitOrigin-RevId: 2247e59ee5696e61c7cb24dce479db572980032d Change-Id: Icd366d3775d88d623695209b5f328dbd69f01d27
1 parent 143a278 commit b315753

File tree

11 files changed

+105
-69
lines changed

11 files changed

+105
-69
lines changed

absl/BUILD.bazel

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,22 @@ config_setting(
2525
visibility = [":__subpackages__"],
2626
)
2727

28+
config_setting(
29+
name = "msvc_compiler",
30+
flag_values = {
31+
"@bazel_tools//tools/cpp:compiler": "msvc-cl",
32+
},
33+
visibility = [":__subpackages__"],
34+
)
35+
36+
config_setting(
37+
name = "clang-cl_compiler",
38+
flag_values = {
39+
"@bazel_tools//tools/cpp:compiler": "clang-cl",
40+
},
41+
visibility = [":__subpackages__"],
42+
)
43+
2844
config_setting(
2945
name = "osx",
3046
constraint_values = [
@@ -39,15 +55,6 @@ config_setting(
3955
],
4056
)
4157

42-
config_setting(
43-
name = "windows",
44-
constraint_values = [
45-
"@bazel_tools//platforms:x86_64",
46-
"@bazel_tools//platforms:windows",
47-
],
48-
visibility = [":__subpackages__"],
49-
)
50-
5158
config_setting(
5259
name = "ppc",
5360
values = {

absl/base/BUILD.bazel

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ cc_library(
160160
],
161161
copts = ABSL_DEFAULT_COPTS,
162162
linkopts = select({
163-
"//absl:windows": [],
163+
"//absl:msvc_compiler": [],
164+
"//absl:clang-cl_compiler": [],
164165
"//absl:wasm": [],
165166
"//conditions:default": ["-pthread"],
166167
}) + ABSL_DEFAULT_LINKOPTS,
@@ -220,7 +221,10 @@ cc_library(
220221
],
221222
copts = ABSL_DEFAULT_COPTS,
222223
linkopts = select({
223-
"//absl:windows": [
224+
"//absl:msvc_compiler": [
225+
"-DEFAULTLIB:advapi32.lib",
226+
],
227+
"//absl:clang-cl_compiler": [
224228
"-DEFAULTLIB:advapi32.lib",
225229
],
226230
"//absl:wasm": [],

absl/base/config.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,8 @@ static_assert(ABSL_INTERNAL_INLINE_NAMESPACE_STR[0] != 'h' ||
216216
#if ABSL_INTERNAL_HAS_KEYWORD(__builtin_LINE) && \
217217
ABSL_INTERNAL_HAS_KEYWORD(__builtin_FILE)
218218
#define ABSL_HAVE_SOURCE_LOCATION_CURRENT 1
219+
#elif defined(__GNUC__) && __GNUC__ >= 5
220+
#define ABSL_HAVE_SOURCE_LOCATION_CURRENT 1
219221
#endif
220222
#endif
221223

absl/base/internal/thread_identity.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ struct ThreadIdentity {
143143

144144
// Private: Reserved for absl::synchronization_internal::Waiter.
145145
struct WaiterState {
146-
char data[128];
146+
alignas(void*) char data[128];
147147
} waiter_state;
148148

149149
// Used by PerThreadSem::{Get,Set}ThreadBlockedCounter().

absl/copts/configure_copts.bzl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,21 @@ load(
2222
)
2323

2424
ABSL_DEFAULT_COPTS = select({
25-
"//absl:windows": ABSL_MSVC_FLAGS,
25+
"//absl:msvc_compiler": ABSL_MSVC_FLAGS,
26+
"//absl:clang-cl_compiler": ABSL_CLANG_CL_FLAGS,
2627
"//absl:clang_compiler": ABSL_LLVM_FLAGS,
2728
"//conditions:default": ABSL_GCC_FLAGS,
2829
})
2930

3031
ABSL_TEST_COPTS = ABSL_DEFAULT_COPTS + select({
31-
"//absl:windows": ABSL_MSVC_TEST_FLAGS,
32+
"//absl:msvc_compiler": ABSL_MSVC_TEST_FLAGS,
33+
"//absl:clang-cl_compiler": ABSL_CLANG_CL_TEST_FLAGS,
3234
"//absl:clang_compiler": ABSL_LLVM_TEST_FLAGS,
3335
"//conditions:default": ABSL_GCC_TEST_FLAGS,
3436
})
3537

3638
ABSL_DEFAULT_LINKOPTS = select({
37-
"//absl:windows": ABSL_MSVC_LINKOPTS,
39+
"//absl:msvc_compiler": ABSL_MSVC_LINKOPTS,
3840
"//conditions:default": [],
3941
})
4042

absl/debugging/BUILD.bazel

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ cc_library(
6666
],
6767
copts = ABSL_DEFAULT_COPTS,
6868
linkopts = ABSL_DEFAULT_LINKOPTS + select({
69-
"//absl:windows": ["-DEFAULTLIB:dbghelp.lib"],
69+
"//absl:msvc_compiler": ["-DEFAULTLIB:dbghelp.lib"],
70+
"//absl:clang-cl_compiler": ["-DEFAULTLIB:dbghelp.lib"],
7071
"//conditions:default": [],
7172
}),
7273
deps = [
@@ -86,11 +87,13 @@ cc_test(
8687
name = "symbolize_test",
8788
srcs = ["symbolize_test.cc"],
8889
copts = ABSL_TEST_COPTS + select({
89-
"//absl:windows": ["/Z7"],
90+
"//absl:msvc_compiler": ["/Z7"],
91+
"//absl:clang-cl_compiler": ["/Z7"],
9092
"//conditions:default": [],
9193
}),
9294
linkopts = ABSL_DEFAULT_LINKOPTS + select({
93-
"//absl:windows": ["/DEBUG"],
95+
"//absl:msvc_compiler": ["/DEBUG"],
96+
"//absl:clang-cl_compiler": ["/DEBUG"],
9497
"//conditions:default": [],
9598
}),
9699
deps = [
@@ -148,7 +151,8 @@ cc_test(
148151
srcs = ["failure_signal_handler_test.cc"],
149152
copts = ABSL_TEST_COPTS,
150153
linkopts = select({
151-
"//absl:windows": [],
154+
"//absl:msvc_compiler": [],
155+
"//absl:clang-cl_compiler": [],
152156
"//absl:wasm": [],
153157
"//conditions:default": ["-pthread"],
154158
}) + ABSL_DEFAULT_LINKOPTS,

absl/flags/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ cc_test(
488488
],
489489
copts = ABSL_TEST_COPTS,
490490
linkopts = ABSL_DEFAULT_LINKOPTS,
491-
shard_count = 32,
491+
shard_count = 31,
492492
deps = [
493493
":flag_internal",
494494
"//absl/base",

absl/flags/internal/sequence_lock_test.cc

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,25 @@ const int kMaxThreads = absl::base_internal::NumCPUs();
121121
const int kMaxThreads = std::min(absl::base_internal::NumCPUs(), 4);
122122
#endif
123123

124+
// Return all of the interesting buffer sizes worth testing:
125+
// powers of two and adjacent values.
126+
std::vector<int> InterestingBufferSizes() {
127+
std::vector<int> ret;
128+
for (int v : MultiplicativeRange(1, 128, 2)) {
129+
ret.push_back(v);
130+
if (v > 1) {
131+
ret.push_back(v - 1);
132+
}
133+
ret.push_back(v + 1);
134+
}
135+
return ret;
136+
}
137+
124138
INSTANTIATE_TEST_SUITE_P(
125139
TestManyByteSizes, ConcurrentSequenceLockTest,
126140
testing::Combine(
127141
// Buffer size (bytes).
128-
testing::Range(1, 128),
142+
testing::ValuesIn(InterestingBufferSizes()),
129143
// Number of reader threads.
130144
testing::ValuesIn(MultiplicativeRange(1, kMaxThreads, 2))));
131145

absl/random/internal/BUILD.bazel

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ cc_library(
7575
],
7676
copts = ABSL_DEFAULT_COPTS,
7777
linkopts = ABSL_DEFAULT_LINKOPTS + select({
78-
"//absl:windows": ["-DEFAULTLIB:bcrypt.lib"],
78+
"//absl:msvc_compiler": ["-DEFAULTLIB:bcrypt.lib"],
79+
"//absl:clang-cl_compiler": ["-DEFAULTLIB:bcrypt.lib"],
7980
"//conditions:default": [],
8081
}),
8182
deps = [
@@ -98,7 +99,8 @@ cc_library(
9899
],
99100
copts = ABSL_DEFAULT_COPTS,
100101
linkopts = select({
101-
"//absl:windows": [],
102+
"//absl:msvc_compiler": [],
103+
"//absl:clang-cl_compiler": [],
102104
"//absl:wasm": [],
103105
"//conditions:default": ["-pthread"],
104106
}) + ABSL_DEFAULT_LINKOPTS,
@@ -325,7 +327,8 @@ cc_library(
325327
"randen_hwaes.h",
326328
],
327329
copts = ABSL_DEFAULT_COPTS + ABSL_RANDOM_RANDEN_COPTS + select({
328-
"//absl:windows": [],
330+
"//absl:msvc_compiler": [],
331+
"//absl:clang-cl_compiler": [],
329332
"//conditions:default": ["-Wno-pass-failed"],
330333
}),
331334
linkopts = ABSL_DEFAULT_LINKOPTS,

absl/random/mocking_bit_gen.h

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,7 @@ class BitGenRef;
104104
class MockingBitGen {
105105
public:
106106
MockingBitGen() = default;
107-
108-
~MockingBitGen() {
109-
for (const auto& del : deleters_) del();
110-
}
107+
~MockingBitGen() = default;
111108

112109
// URBG interface
113110
using result_type = absl::BitGen::result_type;
@@ -117,14 +114,6 @@ class MockingBitGen {
117114
result_type operator()() { return gen_(); }
118115

119116
private:
120-
using match_impl_fn = void (*)(void* mock_fn, void* t_erased_arg_tuple,
121-
void* t_erased_result);
122-
123-
struct MockData {
124-
void* mock_fn = nullptr;
125-
match_impl_fn match_impl = nullptr;
126-
};
127-
128117
// GetMockFnType returns the testing::MockFunction for a result and tuple.
129118
// This method only exists for type deduction and is otherwise unimplemented.
130119
template <typename ResultT, typename... Args>
@@ -136,17 +125,46 @@ class MockingBitGen {
136125
// NOTE: MockFnCaller is essentially equivalent to the lambda:
137126
// [fn](auto... args) { return fn->Call(std::move(args)...)}
138127
// however that fails to build on some supported platforms.
139-
template <typename ResultT, typename MockFnType, typename Tuple>
128+
template <typename MockFnType, typename ResultT, typename Tuple>
140129
struct MockFnCaller;
130+
141131
// specialization for std::tuple.
142-
template <typename ResultT, typename MockFnType, typename... Args>
143-
struct MockFnCaller<ResultT, MockFnType, std::tuple<Args...>> {
132+
template <typename MockFnType, typename ResultT, typename... Args>
133+
struct MockFnCaller<MockFnType, ResultT, std::tuple<Args...>> {
144134
MockFnType* fn;
145135
inline ResultT operator()(Args... args) {
146136
return fn->Call(std::move(args)...);
147137
}
148138
};
149139

140+
// FunctionHolder owns a particular ::testing::MockFunction associated with
141+
// a mocked type signature, and implement the type-erased Apply call, which
142+
// applies type-erased arguments to the mock.
143+
class FunctionHolder {
144+
public:
145+
virtual ~FunctionHolder() = default;
146+
147+
// Call is a dispatch function which converts the
148+
// generic type-erased parameters into a specific mock invocation call.
149+
virtual void Apply(/*ArgTupleT*/ void* args_tuple,
150+
/*ResultT*/ void* result) = 0;
151+
};
152+
153+
template <typename MockFnType, typename ResultT, typename ArgTupleT>
154+
class FunctionHolderImpl final : public FunctionHolder {
155+
public:
156+
void Apply(void* args_tuple, void* result) override {
157+
// Requires tuple_args to point to a ArgTupleT, which is a
158+
// std::tuple<Args...> used to invoke the mock function. Requires result
159+
// to point to a ResultT, which is the result of the call.
160+
*static_cast<ResultT*>(result) =
161+
absl::apply(MockFnCaller<MockFnType, ResultT, ArgTupleT>{&mock_fn_},
162+
*static_cast<ArgTupleT*>(args_tuple));
163+
}
164+
165+
MockFnType mock_fn_;
166+
};
167+
150168
// MockingBitGen::RegisterMock
151169
//
152170
// RegisterMock<ResultT, ArgTupleT>(FastTypeIdType) is the main extension
@@ -161,33 +179,14 @@ class MockingBitGen {
161179
auto RegisterMock(base_internal::FastTypeIdType type)
162180
-> decltype(GetMockFnType(std::declval<ResultT>(),
163181
std::declval<ArgTupleT>()))& {
164-
using MockFnType = decltype(
165-
GetMockFnType(std::declval<ResultT>(), std::declval<ArgTupleT>()));
182+
using MockFnType = decltype(GetMockFnType(std::declval<ResultT>(),
183+
std::declval<ArgTupleT>()));
184+
using ImplT = FunctionHolderImpl<MockFnType, ResultT, ArgTupleT>;
166185
auto& mock = mocks_[type];
167-
if (!mock.mock_fn) {
168-
auto* mock_fn = new MockFnType;
169-
mock.mock_fn = mock_fn;
170-
mock.match_impl = &MatchImpl<ResultT, ArgTupleT>;
171-
deleters_.emplace_back([mock_fn] { delete mock_fn; });
186+
if (!mock) {
187+
mock = absl::make_unique<ImplT>();
172188
}
173-
return *static_cast<MockFnType*>(mock.mock_fn);
174-
}
175-
176-
// MockingBitGen::MatchImpl<> is a dispatch function which converts the
177-
// generic type-erased parameters into a specific mock invocation call.
178-
// Requires tuple_args to point to a ArgTupleT, which is a std::tuple<Args...>
179-
// used to invoke the mock function.
180-
// Requires result to point to a ResultT, which is the result of the call.
181-
template <typename ResultT, typename ArgTupleT>
182-
static void MatchImpl(/*MockFnType<ResultT, Args...>*/ void* mock_fn,
183-
/*ArgTupleT*/ void* args_tuple,
184-
/*ResultT*/ void* result) {
185-
using MockFnType = decltype(
186-
GetMockFnType(std::declval<ResultT>(), std::declval<ArgTupleT>()));
187-
*static_cast<ResultT*>(result) = absl::apply(
188-
MockFnCaller<ResultT, MockFnType, ArgTupleT>{
189-
static_cast<MockFnType*>(mock_fn)},
190-
*static_cast<ArgTupleT*>(args_tuple));
189+
return static_cast<ImplT*>(mock.get())->mock_fn_;
191190
}
192191

193192
// MockingBitGen::InvokeMock
@@ -206,13 +205,13 @@ class MockingBitGen {
206205
// Trigger a mock, if there exists one that matches `param`.
207206
auto it = mocks_.find(type);
208207
if (it == mocks_.end()) return false;
209-
auto* mock_data = static_cast<MockData*>(&it->second);
210-
mock_data->match_impl(mock_data->mock_fn, args_tuple, result);
208+
it->second->Apply(args_tuple, result);
211209
return true;
212210
}
213211

214-
absl::flat_hash_map<base_internal::FastTypeIdType, MockData> mocks_;
215-
std::vector<std::function<void()>> deleters_;
212+
absl::flat_hash_map<base_internal::FastTypeIdType,
213+
std::unique_ptr<FunctionHolder>>
214+
mocks_;
216215
absl::BitGen gen_;
217216

218217
template <typename>

0 commit comments

Comments
 (0)