summaryrefslogtreecommitdiff
path: root/absl/container/internal
diff options
context:
space:
mode:
authorGravatar Abseil Team <absl-team@google.com>2023-12-26 21:22:51 -0800
committerGravatar Copybara-Service <copybara-worker@google.com>2023-12-26 21:23:47 -0800
commitf9228ec834edef9b623d4824dd006890c203adc3 (patch)
treec41a1f543553fb5c8416005bfdb55048b2902d10 /absl/container/internal
parentbd47468324e0db12aac9a57128d40fc23c233fb4 (diff)
Migrate static objects to NoDestructor in tests, testing libraries and benchmarks.
PiperOrigin-RevId: 593918110 Change-Id: Ide100c69b10e28011af17c7f82bb10eea072cad4
Diffstat (limited to 'absl/container/internal')
-rw-r--r--absl/container/internal/container_memory_test.cc3
-rw-r--r--absl/container/internal/hash_generator_testing.cc12
-rw-r--r--absl/container/internal/raw_hash_set_probe_benchmark.cc16
3 files changed, 18 insertions, 13 deletions
diff --git a/absl/container/internal/container_memory_test.cc b/absl/container/internal/container_memory_test.cc
index c1e57834..90d64bf5 100644
--- a/absl/container/internal/container_memory_test.cc
+++ b/absl/container/internal/container_memory_test.cc
@@ -25,6 +25,7 @@
#include "gmock/gmock.h"
#include "gtest/gtest.h"
+#include "absl/base/no_destructor.h"
#include "absl/container/internal/test_instance_tracker.h"
#include "absl/meta/type_traits.h"
#include "absl/strings/string_view.h"
@@ -58,7 +59,7 @@ TEST(Memory, AlignmentSmallerThanBase) {
}
std::map<std::type_index, int>& AllocationMap() {
- static auto* map = new std::map<std::type_index, int>;
+ static absl::NoDestructor<std::map<std::type_index, int>> map;
return *map;
}
diff --git a/absl/container/internal/hash_generator_testing.cc b/absl/container/internal/hash_generator_testing.cc
index 59cc5aac..e89dfdb5 100644
--- a/absl/container/internal/hash_generator_testing.cc
+++ b/absl/container/internal/hash_generator_testing.cc
@@ -16,6 +16,8 @@
#include <deque>
+#include "absl/base/no_destructor.h"
+
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
@@ -41,11 +43,11 @@ class RandomDeviceSeedSeq {
} // namespace
std::mt19937_64* GetSharedRng() {
- static auto* rng = [] {
+ static absl::NoDestructor<std::mt19937_64> rng([] {
RandomDeviceSeedSeq seed_seq;
- return new std::mt19937_64(seed_seq);
- }();
- return rng;
+ return std::mt19937_64(seed_seq);
+ }());
+ return rng.get();
}
std::string Generator<std::string>::operator()() const {
@@ -59,7 +61,7 @@ std::string Generator<std::string>::operator()() const {
}
absl::string_view Generator<absl::string_view>::operator()() const {
- static auto* arena = new std::deque<std::string>();
+ static absl::NoDestructor<std::deque<std::string>> arena;
// NOLINTNEXTLINE(runtime/int)
std::uniform_int_distribution<short> chars(0x20, 0x7E);
arena->emplace_back();
diff --git a/absl/container/internal/raw_hash_set_probe_benchmark.cc b/absl/container/internal/raw_hash_set_probe_benchmark.cc
index 7169a2e2..5d4184b2 100644
--- a/absl/container/internal/raw_hash_set_probe_benchmark.cc
+++ b/absl/container/internal/raw_hash_set_probe_benchmark.cc
@@ -19,6 +19,7 @@
#include <regex> // NOLINT
#include <vector>
+#include "absl/base/no_destructor.h"
#include "absl/container/flat_hash_map.h"
#include "absl/container/internal/hash_function_defaults.h"
#include "absl/container/internal/hashtable_debug.h"
@@ -29,6 +30,7 @@
#include "absl/strings/str_format.h"
#include "absl/strings/string_view.h"
#include "absl/strings/strip.h"
+#include "absl/types/optional.h"
namespace {
@@ -71,7 +73,7 @@ struct Policy {
};
absl::BitGen& GlobalBitGen() {
- static auto* value = new absl::BitGen;
+ static absl::NoDestructor<absl::BitGen> value;
return *value;
}
@@ -112,7 +114,7 @@ class RandomizedAllocator {
static constexpr size_t kRandomPool = 20;
static std::vector<T*>& GetPointers(size_t n) {
- static auto* m = new absl::flat_hash_map<size_t, std::vector<T*>>();
+ static absl::NoDestructor<absl::flat_hash_map<size_t, std::vector<T*>>> m;
return (*m)[n];
}
};
@@ -460,12 +462,12 @@ constexpr int kNameWidth = 15;
constexpr int kDistWidth = 16;
bool CanRunBenchmark(absl::string_view name) {
- static std::regex* const filter = []() -> std::regex* {
+ static const absl::NoDestructor<absl::optional<std::regex>> filter([] {
return benchmarks.empty() || benchmarks == "all"
- ? nullptr
- : new std::regex(std::string(benchmarks));
- }();
- return filter == nullptr || std::regex_search(std::string(name), *filter);
+ ? absl::nullopt
+ : absl::make_optional(std::regex(std::string(benchmarks)));
+ }());
+ return !filter->has_value() || std::regex_search(std::string(name), **filter);
}
struct Result {