From b11ff347b499c972f57f13cacc6209053dc19f61 Mon Sep 17 00:00:00 2001 From: mrambacher Date: Tue, 17 May 2022 09:39:22 -0700 Subject: [PATCH] Use STATIC_AVOID_DESTRUCTION for static objects with non-trivial destructors (#9958) Summary: Changed the static objects that had non-trivial destructors to use the STATIC_AVOID_DESTRUCTION construct. Pull Request resolved: https://github.com/facebook/rocksdb/pull/9958 Reviewed By: pdillinger Differential Revision: D36442982 Pulled By: mrambacher fbshipit-source-id: 029d47b1374d30d198bfede369a4c0ae7a4eb519 --- db/snapshot_checker.h | 3 +-- env/env_posix.cc | 7 ++++--- env/fs_posix.cc | 8 ++++---- port/win/env_win.cc | 9 +++++---- table/persistent_cache_options.h | 3 --- util/comparator.cc | 8 +++++--- utilities/transactions/snapshot_checker.cc | 6 +++++- 7 files changed, 24 insertions(+), 20 deletions(-) diff --git a/db/snapshot_checker.h b/db/snapshot_checker.h index 1d2c2c316..0bfb1aa07 100644 --- a/db/snapshot_checker.h +++ b/db/snapshot_checker.h @@ -33,10 +33,9 @@ class DisableGCSnapshotChecker : public SnapshotChecker { // By returning kNotInSnapshot, we prevent all the values from being GCed return SnapshotCheckerResult::kNotInSnapshot; } - static DisableGCSnapshotChecker* Instance() { return &instance_; } + static DisableGCSnapshotChecker* Instance(); protected: - static DisableGCSnapshotChecker instance_; explicit DisableGCSnapshotChecker() {} }; diff --git a/env/env_posix.cc b/env/env_posix.cc index f7f71c1e7..66848d11f 100644 --- a/env/env_posix.cc +++ b/env/env_posix.cc @@ -488,6 +488,7 @@ Env* Env::Default() { CompressionContextCache::InitSingleton(); INIT_SYNC_POINT_SINGLETONS(); // ~PosixEnv must be called on exit + //**TODO: Can we make this a STATIC_AVOID_DESTRUCTION? static PosixEnv default_env; return &default_env; } @@ -496,9 +497,9 @@ Env* Env::Default() { // Default Posix SystemClock // const std::shared_ptr& SystemClock::Default() { - static std::shared_ptr default_clock = - std::make_shared(); - return default_clock; + STATIC_AVOID_DESTRUCTION(std::shared_ptr, instance) + (std::make_shared()); + return instance; } } // namespace ROCKSDB_NAMESPACE diff --git a/env/fs_posix.cc b/env/fs_posix.cc index 51d4393f7..545dfade1 100644 --- a/env/fs_posix.cc +++ b/env/fs_posix.cc @@ -51,6 +51,7 @@ #include "logging/posix_logger.h" #include "monitoring/iostats_context_imp.h" #include "monitoring/thread_status_updater.h" +#include "port/lang.h" #include "port/port.h" #include "rocksdb/options.h" #include "rocksdb/slice.h" @@ -1210,10 +1211,9 @@ PosixFileSystem::PosixFileSystem() // Default Posix FileSystem // std::shared_ptr FileSystem::Default() { - static PosixFileSystem default_fs; - static std::shared_ptr default_fs_ptr( - &default_fs, [](PosixFileSystem*) {}); - return default_fs_ptr; + STATIC_AVOID_DESTRUCTION(std::shared_ptr, instance) + (std::make_shared()); + return instance; } #ifndef ROCKSDB_LITE diff --git a/port/win/env_win.cc b/port/win/env_win.cc index 7ee58a85b..c5038bbfc 100644 --- a/port/win/env_win.cc +++ b/port/win/env_win.cc @@ -28,6 +28,7 @@ #include "monitoring/iostats_context_imp.h" #include "monitoring/thread_status_updater.h" #include "monitoring/thread_status_util.h" +#include "port/lang.h" #include "port/port.h" #include "port/port_dirent.h" #include "port/win/io_win.h" @@ -192,8 +193,8 @@ WinFileSystem::WinFileSystem(const std::shared_ptr& clock) } const std::shared_ptr& WinFileSystem::Default() { - static std::shared_ptr fs = - std::make_shared(WinClock::Default()); + STATIC_AVOID_DESTRUCTION(std::shared_ptr, fs) + (std::make_shared(WinClock::Default())); return fs; } @@ -1410,8 +1411,8 @@ std::shared_ptr FileSystem::Default() { } const std::shared_ptr& SystemClock::Default() { - static std::shared_ptr clock = - std::make_shared(); + STATIC_AVOID_DESTRUCTION(std::shared_ptr, clock) + (std::make_shared()); return clock; } } // namespace ROCKSDB_NAMESPACE diff --git a/table/persistent_cache_options.h b/table/persistent_cache_options.h index bd1daaac6..b543ab3a3 100644 --- a/table/persistent_cache_options.h +++ b/table/persistent_cache_options.h @@ -24,9 +24,6 @@ struct PersistentCacheOptions { : persistent_cache(_persistent_cache), base_cache_key(_base_cache_key), statistics(_statistics) {} - - virtual ~PersistentCacheOptions() {} - std::shared_ptr persistent_cache; OffsetableCacheKey base_cache_key; Statistics* statistics = nullptr; diff --git a/util/comparator.cc b/util/comparator.cc index 9fbc111b4..d04031e39 100644 --- a/util/comparator.cc +++ b/util/comparator.cc @@ -17,6 +17,7 @@ #include #include "db/dbformat.h" +#include "port/lang.h" #include "port/port.h" #include "rocksdb/convenience.h" #include "rocksdb/slice.h" @@ -290,17 +291,18 @@ class ComparatorWithU64TsImpl : public Comparator { }// namespace const Comparator* BytewiseComparator() { - static BytewiseComparatorImpl bytewise; + STATIC_AVOID_DESTRUCTION(BytewiseComparatorImpl, bytewise); return &bytewise; } const Comparator* ReverseBytewiseComparator() { - static ReverseBytewiseComparatorImpl rbytewise; + STATIC_AVOID_DESTRUCTION(ReverseBytewiseComparatorImpl, rbytewise); return &rbytewise; } const Comparator* BytewiseComparatorWithU64Ts() { - static ComparatorWithU64TsImpl comp_with_u64_ts; + STATIC_AVOID_DESTRUCTION(ComparatorWithU64TsImpl, + comp_with_u64_ts); return &comp_with_u64_ts; } diff --git a/utilities/transactions/snapshot_checker.cc b/utilities/transactions/snapshot_checker.cc index 9c43bef43..76d16681a 100644 --- a/utilities/transactions/snapshot_checker.cc +++ b/utilities/transactions/snapshot_checker.cc @@ -9,6 +9,7 @@ #include #endif // ROCKSDB_LITE +#include "port/lang.h" #include "utilities/transactions/write_prepared_txn_db.h" namespace ROCKSDB_NAMESPACE { @@ -44,6 +45,9 @@ SnapshotCheckerResult WritePreparedSnapshotChecker::CheckInSnapshot( } #endif // ROCKSDB_LITE -DisableGCSnapshotChecker DisableGCSnapshotChecker::instance_; +DisableGCSnapshotChecker* DisableGCSnapshotChecker::Instance() { + STATIC_AVOID_DESTRUCTION(DisableGCSnapshotChecker, instance); + return &instance; +} } // namespace ROCKSDB_NAMESPACE