Skip to content

Commit b16aeb6

Browse files
Abseil Teamshaindelschwartz
Abseil Team
authored andcommitted
Export of internal Abseil changes.
-- 5f1cf6547231f1b1daad6d1b785df6b0b999b3c9 by Samuel Benzaquen <sbenza@google.com>: Fix uninitialized member in the `iterator` class by using a union of the two possible states of the iterator. This silences a Wuninitialized warning in gcc>=7. PiperOrigin-RevId: 228175148 -- 98b4e3204c0ec3cfd4cb037e24d443ea4b63fc84 by CJ Johnson <johnsoncj@google.com>: Factors out the implementation of InlinedVector::swap(...) into a private member function PiperOrigin-RevId: 228173383 -- f1432ad3a8b05285c6d55bc4754cfae765485b7f by Abseil Team <absl-team@google.com>: Import of CCTZ from GitHub. PiperOrigin-RevId: 227891984 -- 03fc00c7a4efc6000e6d9125cb2e252bffda76fe by Andy Getzendanner <durandal@google.com>: Add a missing linebreak to a comment and markdownify two unordered lists. PiperOrigin-RevId: 227861389 -- 0d66c9afba4fc9aa52e61d9fb410e165018a7b48 by Abseil Team <absl-team@google.com>: Add an API to register a new source for the cycle clock. PiperOrigin-RevId: 227779218 -- 14d3f9b70c8818b8541e5fb2f6ca4c59d479de31 by Andy Getzendanner <durandal@google.com>: Correct a typo in a stripping marker. PiperOrigin-RevId: 227750014 -- 59df88740f4e315beb57a8772f8bcf7879440c74 by Matt Kulukundis <kfm@google.com>: Switch thread local handling to be more cross platform PiperOrigin-RevId: 227695133 -- 75deed5bfcb5c42534e933f104aa7d94e11e348d by Abseil Team <absl-team@google.com>: Rollback workaround toolchain bug for incorrect handling of thread_local in inline functions PiperOrigin-RevId: 227689133 -- 54994bf0afec026e6e0e7a199df0bbb4b7d9a4aa by Derek Mauro <dmauro@google.com>: Add -pthread to linkopts where it actually belongs, on the library that uses it. Fixes abseil#240. PiperOrigin-RevId: 227612492 -- 893875f3536b7e0a1bad993aa6b2e083abb3b25a by Derek Mauro <dmauro@google.com>: Internal change PiperOrigin-RevId: 227582833 -- 506c9b8e9002ca3389c7040473b68d4cbf94bdcc by Matt Kulukundis <kfm@google.com>: Workaround toolchain bug for incorrect handling of thread_local in inline functions PiperOrigin-RevId: 227561449 -- 29ee90d96dfe3114cf93f9bb92ea0cc9e768a407 by Derek Mauro <dmauro@google.com>: Internal change PiperOrigin-RevId: 227054634 GitOrigin-RevId: 5f1cf6547231f1b1daad6d1b785df6b0b999b3c9 Change-Id: Ibc90566d92ee6e0ad7e150f513ec7f5d22ec0a94
1 parent 7ffbe09 commit b16aeb6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+253
-161
lines changed
File renamed without changes.

absl/base/BUILD.bazel

+8-8
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ cc_library(
8989
"internal/low_level_alloc.h",
9090
],
9191
copts = ABSL_DEFAULT_COPTS,
92+
linkopts = select({
93+
"//absl:windows": [],
94+
"//conditions:default": ["-pthread"],
95+
}),
9296
visibility = [
9397
"//absl:__subpackages__",
9498
],
@@ -142,6 +146,10 @@ cc_library(
142146
"log_severity.h",
143147
],
144148
copts = ABSL_DEFAULT_COPTS,
149+
linkopts = select({
150+
"//absl:windows": [],
151+
"//conditions:default": ["-pthread"],
152+
}),
145153
deps = [
146154
":base_internal",
147155
":config",
@@ -423,10 +431,6 @@ cc_test(
423431
size = "small",
424432
srcs = ["internal/low_level_alloc_test.cc"],
425433
copts = ABSL_TEST_COPTS,
426-
linkopts = select({
427-
"//absl:windows": [],
428-
"//conditions:default": ["-pthread"],
429-
}),
430434
tags = ["no_test_ios_x86_64"],
431435
deps = [":malloc_internal"],
432436
)
@@ -436,10 +440,6 @@ cc_test(
436440
size = "small",
437441
srcs = ["internal/thread_identity_test.cc"],
438442
copts = ABSL_TEST_COPTS,
439-
linkopts = select({
440-
"//absl:windows": [],
441-
"//conditions:default": ["-pthread"],
442-
}),
443443
tags = [
444444
"no_test_wasm",
445445
],

absl/base/internal/cycleclock.cc

+11-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include "absl/base/internal/cycleclock.h"
2424

25+
#include <atomic>
2526
#include <chrono> // NOLINT(build/c++11)
2627

2728
#include "absl/base/internal/unscaledcycleclock.h"
@@ -52,17 +53,26 @@ static constexpr int32_t kShift = 2;
5253
#endif
5354

5455
static constexpr double kFrequencyScale = 1.0 / (1 << kShift);
56+
static std::atomic<CycleClockSourceFunc> cycle_clock_source;
5557

5658
} // namespace
5759

5860
int64_t CycleClock::Now() {
59-
return base_internal::UnscaledCycleClock::Now() >> kShift;
61+
auto fn = cycle_clock_source.load(std::memory_order_relaxed);
62+
if (fn == nullptr) {
63+
return base_internal::UnscaledCycleClock::Now() >> kShift;
64+
}
65+
return fn() >> kShift;
6066
}
6167

6268
double CycleClock::Frequency() {
6369
return kFrequencyScale * base_internal::UnscaledCycleClock::Frequency();
6470
}
6571

72+
void CycleClockSource::Register(CycleClockSourceFunc source) {
73+
cycle_clock_source.store(source, std::memory_order_relaxed);
74+
}
75+
6676
#else
6777

6878
int64_t CycleClock::Now() {

absl/base/internal/cycleclock.h

+14
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,20 @@ class CycleClock {
7171
CycleClock& operator=(const CycleClock&) = delete;
7272
};
7373

74+
using CycleClockSourceFunc = int64_t (*)();
75+
76+
class CycleClockSource {
77+
private:
78+
// CycleClockSource::Register()
79+
//
80+
// Register a function that provides an alternate source for the unscaled CPU
81+
// cycle count value. The source function must be async signal safe, must not
82+
// call CycleClock::Now(), and must have a frequency that matches that of the
83+
// unscaled clock used by CycleClock. A nullptr value resets CycleClock to use
84+
// the default source.
85+
static void Register(CycleClockSourceFunc source);
86+
};
87+
7488
} // namespace base_internal
7589
} // namespace absl
7690

absl/base/internal/per_thread_tls.h

+8-4
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,17 @@
1616
#define ABSL_BASE_INTERNAL_PER_THREAD_TLS_H_
1717

1818
// This header defines two macros:
19+
//
1920
// If the platform supports thread-local storage:
20-
// ABSL_PER_THREAD_TLS_KEYWORD is the C keyword needed to declare a
21-
// thread-local variable ABSL_PER_THREAD_TLS is 1
21+
//
22+
// * ABSL_PER_THREAD_TLS_KEYWORD is the C keyword needed to declare a
23+
// thread-local variable
24+
// * ABSL_PER_THREAD_TLS is 1
2225
//
2326
// Otherwise:
24-
// ABSL_PER_THREAD_TLS_KEYWORD is empty
25-
// ABSL_PER_THREAD_TLS is 0
27+
//
28+
// * ABSL_PER_THREAD_TLS_KEYWORD is empty
29+
// * ABSL_PER_THREAD_TLS is 0
2630
//
2731
// Microsoft C supports thread-local storage.
2832
// GCC supports it if the appropriate version of glibc is available,

absl/container/BUILD.bazel

+11
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,21 @@ cc_library(
123123
],
124124
)
125125

126+
cc_library(
127+
name = "counting_allocator",
128+
testonly = 1,
129+
hdrs = ["internal/counting_allocator.h"],
130+
copts = ABSL_DEFAULT_COPTS,
131+
visibility = ["//visibility:private"],
132+
)
133+
126134
cc_test(
127135
name = "inlined_vector_test",
128136
srcs = ["inlined_vector_test.cc"],
129137
copts = ABSL_TEST_COPTS + ABSL_EXCEPTIONS_FLAG,
130138
linkopts = ABSL_EXCEPTIONS_FLAG_LINKOPTS,
131139
deps = [
140+
":counting_allocator",
132141
":inlined_vector",
133142
":test_instance_tracker",
134143
"//absl/base",
@@ -146,6 +155,7 @@ cc_test(
146155
srcs = ["inlined_vector_test.cc"],
147156
copts = ABSL_TEST_COPTS,
148157
deps = [
158+
":counting_allocator",
149159
":inlined_vector",
150160
":test_instance_tracker",
151161
"//absl/base",
@@ -443,6 +453,7 @@ cc_library(
443453
copts = ABSL_DEFAULT_COPTS,
444454
deps = [
445455
":have_sse",
456+
"//absl/base",
446457
"//absl/base:core_headers",
447458
"//absl/debugging:stacktrace",
448459
"//absl/memory",

absl/container/CMakeLists.txt

+11
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,15 @@ absl_cc_library(
122122
PUBLIC
123123
)
124124

125+
absl_cc_library(
126+
NAME
127+
counting_allocator
128+
HDRS
129+
"internal/counting_allocator.h"
130+
COPTS
131+
${ABSL_DEFAULT_COPTS}
132+
)
133+
125134
absl_cc_test(
126135
NAME
127136
inlined_vector_test
@@ -132,6 +141,7 @@ absl_cc_test(
132141
LINKOPTS
133142
${ABSL_EXCEPTIONS_FLAG_LINKOPTS}
134143
DEPS
144+
absl::counting_allocator
135145
absl::inlined_vector
136146
absl::test_instance_tracker
137147
absl::base
@@ -437,6 +447,7 @@ absl_cc_library(
437447
COPTS
438448
${ABSL_DEFAULT_COPTS}
439449
DEPS
450+
absl::base
440451
absl::have_sse
441452
absl::synchronization
442453
)

absl/container/inlined_vector.h

+78-73
Original file line numberDiff line numberDiff line change
@@ -795,79 +795,7 @@ class InlinedVector {
795795
void swap(InlinedVector& other) {
796796
if (ABSL_PREDICT_FALSE(this == &other)) return;
797797

798-
using std::swap; // Augment ADL with `std::swap`.
799-
if (allocated() && other.allocated()) {
800-
// Both out of line, so just swap the tag, allocation, and allocator.
801-
swap(tag(), other.tag());
802-
swap(allocation(), other.allocation());
803-
swap(allocator(), other.allocator());
804-
return;
805-
}
806-
if (!allocated() && !other.allocated()) {
807-
// Both inlined: swap up to smaller size, then move remaining elements.
808-
InlinedVector* a = this;
809-
InlinedVector* b = &other;
810-
if (size() < other.size()) {
811-
swap(a, b);
812-
}
813-
814-
const size_type a_size = a->size();
815-
const size_type b_size = b->size();
816-
assert(a_size >= b_size);
817-
// `a` is larger. Swap the elements up to the smaller array size.
818-
std::swap_ranges(a->inlined_space(), a->inlined_space() + b_size,
819-
b->inlined_space());
820-
821-
// Move the remaining elements:
822-
// [`b_size`, `a_size`) from `a` -> [`b_size`, `a_size`) from `b`
823-
b->UninitializedCopy(a->inlined_space() + b_size,
824-
a->inlined_space() + a_size,
825-
b->inlined_space() + b_size);
826-
a->Destroy(a->inlined_space() + b_size, a->inlined_space() + a_size);
827-
828-
swap(a->tag(), b->tag());
829-
swap(a->allocator(), b->allocator());
830-
assert(b->size() == a_size);
831-
assert(a->size() == b_size);
832-
return;
833-
}
834-
835-
// One is out of line, one is inline.
836-
// We first move the elements from the inlined vector into the
837-
// inlined space in the other vector. We then put the other vector's
838-
// pointer/capacity into the originally inlined vector and swap
839-
// the tags.
840-
InlinedVector* a = this;
841-
InlinedVector* b = &other;
842-
if (a->allocated()) {
843-
swap(a, b);
844-
}
845-
assert(!a->allocated());
846-
assert(b->allocated());
847-
const size_type a_size = a->size();
848-
const size_type b_size = b->size();
849-
// In an optimized build, `b_size` would be unused.
850-
static_cast<void>(b_size);
851-
852-
// Made Local copies of `size()`, don't need `tag()` accurate anymore
853-
swap(a->tag(), b->tag());
854-
855-
// Copy `b_allocation` out before `b`'s union gets clobbered by
856-
// `inline_space`
857-
Allocation b_allocation = b->allocation();
858-
859-
b->UninitializedCopy(a->inlined_space(), a->inlined_space() + a_size,
860-
b->inlined_space());
861-
a->Destroy(a->inlined_space(), a->inlined_space() + a_size);
862-
863-
a->allocation() = b_allocation;
864-
865-
if (a->allocator() != b->allocator()) {
866-
swap(a->allocator(), b->allocator());
867-
}
868-
869-
assert(b->size() == a_size);
870-
assert(a->size() == b_size);
798+
SwapImpl(other);
871799
}
872800

873801
private:
@@ -1238,6 +1166,83 @@ class InlinedVector {
12381166
return begin() + index;
12391167
}
12401168

1169+
void SwapImpl(InlinedVector& other) {
1170+
using std::swap; // Augment ADL with `std::swap`.
1171+
1172+
if (allocated() && other.allocated()) {
1173+
// Both out of line, so just swap the tag, allocation, and allocator.
1174+
swap(tag(), other.tag());
1175+
swap(allocation(), other.allocation());
1176+
swap(allocator(), other.allocator());
1177+
return;
1178+
}
1179+
if (!allocated() && !other.allocated()) {
1180+
// Both inlined: swap up to smaller size, then move remaining elements.
1181+
InlinedVector* a = this;
1182+
InlinedVector* b = &other;
1183+
if (size() < other.size()) {
1184+
swap(a, b);
1185+
}
1186+
1187+
const size_type a_size = a->size();
1188+
const size_type b_size = b->size();
1189+
assert(a_size >= b_size);
1190+
// `a` is larger. Swap the elements up to the smaller array size.
1191+
std::swap_ranges(a->inlined_space(), a->inlined_space() + b_size,
1192+
b->inlined_space());
1193+
1194+
// Move the remaining elements:
1195+
// [`b_size`, `a_size`) from `a` -> [`b_size`, `a_size`) from `b`
1196+
b->UninitializedCopy(a->inlined_space() + b_size,
1197+
a->inlined_space() + a_size,
1198+
b->inlined_space() + b_size);
1199+
a->Destroy(a->inlined_space() + b_size, a->inlined_space() + a_size);
1200+
1201+
swap(a->tag(), b->tag());
1202+
swap(a->allocator(), b->allocator());
1203+
assert(b->size() == a_size);
1204+
assert(a->size() == b_size);
1205+
return;
1206+
}
1207+
1208+
// One is out of line, one is inline.
1209+
// We first move the elements from the inlined vector into the
1210+
// inlined space in the other vector. We then put the other vector's
1211+
// pointer/capacity into the originally inlined vector and swap
1212+
// the tags.
1213+
InlinedVector* a = this;
1214+
InlinedVector* b = &other;
1215+
if (a->allocated()) {
1216+
swap(a, b);
1217+
}
1218+
assert(!a->allocated());
1219+
assert(b->allocated());
1220+
const size_type a_size = a->size();
1221+
const size_type b_size = b->size();
1222+
// In an optimized build, `b_size` would be unused.
1223+
static_cast<void>(b_size);
1224+
1225+
// Made Local copies of `size()`, don't need `tag()` accurate anymore
1226+
swap(a->tag(), b->tag());
1227+
1228+
// Copy `b_allocation` out before `b`'s union gets clobbered by
1229+
// `inline_space`
1230+
Allocation b_allocation = b->allocation();
1231+
1232+
b->UninitializedCopy(a->inlined_space(), a->inlined_space() + a_size,
1233+
b->inlined_space());
1234+
a->Destroy(a->inlined_space(), a->inlined_space() + a_size);
1235+
1236+
a->allocation() = b_allocation;
1237+
1238+
if (a->allocator() != b->allocator()) {
1239+
swap(a->allocator(), b->allocator());
1240+
}
1241+
1242+
assert(b->size() == a_size);
1243+
assert(a->size() == b_size);
1244+
}
1245+
12411246
// Stores either the inlined or allocated representation
12421247
union Rep {
12431248
using ValueTypeBuffer =

0 commit comments

Comments
 (0)