diff --git a/db/blob/db_blob_index_test.cc b/db/blob/db_blob_index_test.cc index 24862f771..ca42466a8 100644 --- a/db/blob/db_blob_index_test.cc +++ b/db/blob/db_blob_index_test.cc @@ -46,7 +46,7 @@ class DBBlobIndexTest : public DBTestBase { ColumnFamilyHandle* cfh() { return dbfull()->DefaultColumnFamily(); } ColumnFamilyData* cfd() { - return reinterpret_cast(cfh())->cfd(); + return static_cast_with_check(cfh())->cfd(); } Status PutBlobIndex(WriteBatch* batch, const Slice& key, diff --git a/db/column_family.cc b/db/column_family.cc index 299c97b79..a5c3422e9 100644 --- a/db/column_family.cc +++ b/db/column_family.cc @@ -34,6 +34,7 @@ #include "table/block_based/block_based_table_factory.h" #include "table/merging_iterator.h" #include "util/autovector.h" +#include "util/cast_util.h" #include "util/compression.h" namespace ROCKSDB_NAMESPACE { @@ -1546,7 +1547,7 @@ ColumnFamilyHandle* ColumnFamilyMemTablesImpl::GetColumnFamilyHandle() { uint32_t GetColumnFamilyID(ColumnFamilyHandle* column_family) { uint32_t column_family_id = 0; if (column_family != nullptr) { - auto cfh = reinterpret_cast(column_family); + auto cfh = static_cast_with_check(column_family); column_family_id = cfh->GetID(); } return column_family_id; diff --git a/db/column_family_test.cc b/db/column_family_test.cc index fa791506f..c4181d9b8 100644 --- a/db/column_family_test.cc +++ b/db/column_family_test.cc @@ -122,7 +122,7 @@ class ColumnFamilyTestBase : public testing::Test { for (int i = 0; i < n; i++) { if (flush_every != 0 && i != 0 && i % flush_every == 0) { - DBImpl* dbi = reinterpret_cast(db_); + DBImpl* dbi = static_cast_with_check(db_); dbi->TEST_FlushMemTable(); } @@ -227,7 +227,7 @@ class ColumnFamilyTestBase : public testing::Test { Open({"default"}); } - DBImpl* dbfull() { return reinterpret_cast(db_); } + DBImpl* dbfull() { return static_cast_with_check(db_); } int GetProperty(int cf, std::string property) { std::string value; @@ -570,7 +570,7 @@ TEST_P(ColumnFamilyTest, DontReuseColumnFamilyID) { Open(); CreateColumnFamilies({"one", "two", "three"}); for (size_t i = 0; i < handles_.size(); ++i) { - auto cfh = reinterpret_cast(handles_[i]); + auto cfh = static_cast_with_check(handles_[i]); ASSERT_EQ(i, cfh->GetID()); } if (iter == 1) { @@ -586,7 +586,7 @@ TEST_P(ColumnFamilyTest, DontReuseColumnFamilyID) { CreateColumnFamilies({"three2"}); // ID 3 that was used for dropped column family "three" should not be // reused - auto cfh3 = reinterpret_cast(handles_[3]); + auto cfh3 = static_cast_with_check(handles_[3]); ASSERT_EQ(4U, cfh3->GetID()); Close(); Destroy(); @@ -3354,7 +3354,7 @@ TEST_P(ColumnFamilyTest, MultipleCFPathsTest) { // Re-open and verify the keys. Reopen({ColumnFamilyOptions(), cf_opt1, cf_opt2}); - DBImpl* dbi = reinterpret_cast(db_); + DBImpl* dbi = static_cast_with_check(db_); for (int cf = 1; cf != 3; ++cf) { ReadOptions read_options; read_options.readahead_size = 0; diff --git a/db/compact_files_test.cc b/db/compact_files_test.cc index 948ada675..048ed6e26 100644 --- a/db/compact_files_test.cc +++ b/db/compact_files_test.cc @@ -16,6 +16,7 @@ #include "rocksdb/env.h" #include "test_util/sync_point.h" #include "test_util/testharness.h" +#include "util/cast_util.h" #include "util/string_util.h" namespace ROCKSDB_NAMESPACE { @@ -148,7 +149,7 @@ TEST_F(CompactFilesTest, ObsoleteFiles) { auto l0_files = collector->GetFlushedFiles(); ASSERT_OK(db->CompactFiles(CompactionOptions(), l0_files, 1)); - reinterpret_cast(db)->TEST_WaitForCompact(); + static_cast_with_check(db)->TEST_WaitForCompact(); // verify all compaction input files are deleted for (auto fname : l0_files) { @@ -183,13 +184,13 @@ TEST_F(CompactFilesTest, NotCutOutputOnLevel0) { for (int i = 0; i < 500; ++i) { db->Put(WriteOptions(), ToString(i), std::string(1000, 'a' + (i % 26))); } - reinterpret_cast(db)->TEST_WaitForFlushMemTable(); + static_cast_with_check(db)->TEST_WaitForFlushMemTable(); auto l0_files_1 = collector->GetFlushedFiles(); collector->ClearFlushedFiles(); for (int i = 0; i < 500; ++i) { db->Put(WriteOptions(), ToString(i), std::string(1000, 'a' + (i % 26))); } - reinterpret_cast(db)->TEST_WaitForFlushMemTable(); + static_cast_with_check(db)->TEST_WaitForFlushMemTable(); auto l0_files_2 = collector->GetFlushedFiles(); ASSERT_OK(db->CompactFiles(CompactionOptions(), l0_files_1, 0)); ASSERT_OK(db->CompactFiles(CompactionOptions(), l0_files_2, 0)); @@ -383,7 +384,7 @@ TEST_F(CompactFilesTest, GetCompactionJobInfo) { for (int i = 0; i < 500; ++i) { db->Put(WriteOptions(), ToString(i), std::string(1000, 'a' + (i % 26))); } - reinterpret_cast(db)->TEST_WaitForFlushMemTable(); + static_cast_with_check(db)->TEST_WaitForFlushMemTable(); auto l0_files_1 = collector->GetFlushedFiles(); CompactionOptions co; co.compression = CompressionType::kLZ4Compression; diff --git a/db/compacted_db_impl.cc b/db/compacted_db_impl.cc index a6daab504..fddccb11f 100644 --- a/db/compacted_db_impl.cc +++ b/db/compacted_db_impl.cc @@ -5,9 +5,11 @@ #ifndef ROCKSDB_LITE #include "db/compacted_db_impl.h" + #include "db/db_impl/db_impl.h" #include "db/version_set.h" #include "table/get_context.h" +#include "util/cast_util.h" namespace ROCKSDB_NAMESPACE { @@ -90,8 +92,8 @@ Status CompactedDBImpl::Init(const Options& options) { ColumnFamilyOptions(options)); Status s = Recover({cf}, true /* read only */, false, true); if (s.ok()) { - cfd_ = reinterpret_cast( - DefaultColumnFamily())->cfd(); + cfd_ = static_cast_with_check(DefaultColumnFamily()) + ->cfd(); cfd_->InstallSuperVersion(&sv_context, &mutex_); } mutex_.Unlock(); diff --git a/db/compaction/compaction_job_stats_test.cc b/db/compaction/compaction_job_stats_test.cc index 51a665797..03becacef 100644 --- a/db/compaction/compaction_job_stats_test.cc +++ b/db/compaction/compaction_job_stats_test.cc @@ -52,6 +52,7 @@ #include "test_util/sync_point.h" #include "test_util/testharness.h" #include "test_util/testutil.h" +#include "util/cast_util.h" #include "util/compression.h" #include "util/hash.h" #include "util/mutexlock.h" @@ -126,9 +127,7 @@ class CompactionJobStatsTest : public testing::Test, static void SetUpTestCase() {} static void TearDownTestCase() {} - DBImpl* dbfull() { - return reinterpret_cast(db_); - } + DBImpl* dbfull() { return static_cast_with_check(db_); } void CreateColumnFamilies(const std::vector& cfs, const Options& options) { @@ -797,7 +796,7 @@ TEST_P(CompactionJobStatsTest, CompactionJobStatsTest) { } ASSERT_OK(Flush(1)); - reinterpret_cast(db_)->TEST_WaitForCompact(); + static_cast_with_check(db_)->TEST_WaitForCompact(); stats_checker->set_verify_next_comp_io_stats(true); std::atomic first_prepare_write(true); @@ -1012,7 +1011,7 @@ TEST_P(CompactionJobStatsTest, UniversalCompactionTest) { &rnd, start_key, start_key + key_base - 1, kKeySize, kValueSize, key_interval, compression_ratio, 1); - reinterpret_cast(db_)->TEST_WaitForCompact(); + static_cast_with_check(db_)->TEST_WaitForCompact(); } ASSERT_EQ(stats_checker->NumberOfUnverifiedStats(), 0U); } diff --git a/db/corruption_test.cc b/db/corruption_test.cc index 7a2498863..4641ec7b1 100644 --- a/db/corruption_test.cc +++ b/db/corruption_test.cc @@ -106,7 +106,7 @@ class CorruptionTest : public testing::Test { WriteBatch batch; for (int i = 0; i < n; i++) { if (flush_every != 0 && i != 0 && i % flush_every == 0) { - DBImpl* dbi = reinterpret_cast(db_); + DBImpl* dbi = static_cast_with_check(db_); dbi->TEST_FlushMemTable(); } //if ((i % 100) == 0) fprintf(stderr, "@ %d of %d\n", i, n); @@ -281,7 +281,7 @@ TEST_F(CorruptionTest, NewFileErrorDuringWrite) { TEST_F(CorruptionTest, TableFile) { Build(100); - DBImpl* dbi = reinterpret_cast(db_); + DBImpl* dbi = static_cast_with_check(db_); dbi->TEST_FlushMemTable(); dbi->TEST_CompactRange(0, nullptr, nullptr); dbi->TEST_CompactRange(1, nullptr, nullptr); @@ -304,7 +304,7 @@ TEST_F(CorruptionTest, VerifyChecksumReadahead) { Reopen(&options); Build(10000); - DBImpl* dbi = reinterpret_cast(db_); + DBImpl* dbi = static_cast_with_check(db_); dbi->TEST_FlushMemTable(); dbi->TEST_CompactRange(0, nullptr, nullptr); dbi->TEST_CompactRange(1, nullptr, nullptr); @@ -351,14 +351,14 @@ TEST_F(CorruptionTest, TableFileIndexData) { Reopen(&options); // build 2 tables, flush at 5000 Build(10000, 5000); - DBImpl* dbi = reinterpret_cast(db_); + DBImpl* dbi = static_cast_with_check(db_); dbi->TEST_FlushMemTable(); // corrupt an index block of an entire file Corrupt(kTableFile, -2000, 500); options.paranoid_checks = false; Reopen(&options); - dbi = reinterpret_cast(db_); + dbi = static_cast_with_check(db_); // one full file may be readable, since only one was corrupted // the other file should be fully non-readable, since index was corrupted Check(0, 5000); @@ -398,7 +398,7 @@ TEST_F(CorruptionTest, SequenceNumberRecovery) { TEST_F(CorruptionTest, CorruptedDescriptor) { ASSERT_OK(db_->Put(WriteOptions(), "foo", "hello")); - DBImpl* dbi = reinterpret_cast(db_); + DBImpl* dbi = static_cast_with_check(db_); dbi->TEST_FlushMemTable(); dbi->TEST_CompactRange(0, nullptr, nullptr); @@ -417,7 +417,7 @@ TEST_F(CorruptionTest, CompactionInputError) { Options options; Reopen(&options); Build(10); - DBImpl* dbi = reinterpret_cast(db_); + DBImpl* dbi = static_cast_with_check(db_); dbi->TEST_FlushMemTable(); dbi->TEST_CompactRange(0, nullptr, nullptr); dbi->TEST_CompactRange(1, nullptr, nullptr); @@ -439,7 +439,7 @@ TEST_F(CorruptionTest, CompactionInputErrorParanoid) { options.write_buffer_size = 131072; options.max_write_buffer_number = 2; Reopen(&options); - DBImpl* dbi = reinterpret_cast(db_); + DBImpl* dbi = static_cast_with_check(db_); // Fill levels >= 1 for (int level = 1; level < dbi->NumberLevels(); level++) { @@ -454,7 +454,7 @@ TEST_F(CorruptionTest, CompactionInputErrorParanoid) { Reopen(&options); - dbi = reinterpret_cast(db_); + dbi = static_cast_with_check(db_); Build(10); dbi->TEST_FlushMemTable(); dbi->TEST_WaitForCompact(); @@ -481,7 +481,7 @@ TEST_F(CorruptionTest, CompactionInputErrorParanoid) { TEST_F(CorruptionTest, UnrelatedKeys) { Build(10); - DBImpl* dbi = reinterpret_cast(db_); + DBImpl* dbi = static_cast_with_check(db_); dbi->TEST_FlushMemTable(); Corrupt(kTableFile, 100, 1); ASSERT_NOK(dbi->VerifyChecksum()); @@ -532,7 +532,7 @@ TEST_F(CorruptionTest, FileSystemStateCorrupted) { Reopen(&options); Build(10); ASSERT_OK(db_->Flush(FlushOptions())); - DBImpl* dbi = reinterpret_cast(db_); + DBImpl* dbi = static_cast_with_check(db_); std::vector metadata; dbi->GetLiveFilesMetaData(&metadata); ASSERT_GT(metadata.size(), size_t(0)); diff --git a/db/cuckoo_table_db_test.cc b/db/cuckoo_table_db_test.cc index 9467840ff..2aaf2c50d 100644 --- a/db/cuckoo_table_db_test.cc +++ b/db/cuckoo_table_db_test.cc @@ -13,6 +13,7 @@ #include "table/meta_blocks.h" #include "test_util/testharness.h" #include "test_util/testutil.h" +#include "util/cast_util.h" #include "util/string_util.h" namespace ROCKSDB_NAMESPACE { @@ -46,9 +47,7 @@ class CuckooTableDBTest : public testing::Test { return options; } - DBImpl* dbfull() { - return reinterpret_cast(db_); - } + DBImpl* dbfull() { return static_cast_with_check(db_); } // The following util methods are copied from plain_table_db_test. void Reopen(Options* options = nullptr) { diff --git a/db/db_basic_test.cc b/db/db_basic_test.cc index e9584b568..3cf52f71b 100644 --- a/db/db_basic_test.cc +++ b/db/db_basic_test.cc @@ -1109,7 +1109,7 @@ TEST_P(DBMultiGetTestWithParam, MultiGetMultiCF) { } int get_sv_count = 0; - ROCKSDB_NAMESPACE::DBImpl* db = reinterpret_cast(db_); + ROCKSDB_NAMESPACE::DBImpl* db = static_cast_with_check(db_); ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack( "DBImpl::MultiGet::AfterRefSV", [&](void* /*arg*/) { if (++get_sv_count == 2) { @@ -1127,7 +1127,7 @@ TEST_P(DBMultiGetTestWithParam, MultiGetMultiCF) { } if (get_sv_count == 11) { for (int i = 0; i < 8; ++i) { - auto* cfd = reinterpret_cast( + auto* cfd = static_cast_with_check( db->GetColumnFamilyHandle(i)) ->cfd(); ASSERT_EQ(cfd->TEST_GetLocalSV()->Get(), SuperVersion::kSVInUse); @@ -1178,9 +1178,10 @@ TEST_P(DBMultiGetTestWithParam, MultiGetMultiCF) { ASSERT_EQ(values[2], std::get<2>(cf_kv_vec[1]) + "_2"); for (int cf = 0; cf < 8; ++cf) { - auto* cfd = reinterpret_cast( - reinterpret_cast(db_)->GetColumnFamilyHandle(cf)) - ->cfd(); + auto* cfd = + static_cast_with_check( + static_cast_with_check(db_)->GetColumnFamilyHandle(cf)) + ->cfd(); ASSERT_NE(cfd->TEST_GetLocalSV()->Get(), SuperVersion::kSVInUse); ASSERT_NE(cfd->TEST_GetLocalSV()->Get(), SuperVersion::kSVObsolete); } @@ -1240,9 +1241,10 @@ TEST_P(DBMultiGetTestWithParam, MultiGetMultiCFMutex) { "cf" + std::to_string(j) + "_val" + std::to_string(retries)); } for (int i = 0; i < 8; ++i) { - auto* cfd = reinterpret_cast( - reinterpret_cast(db_)->GetColumnFamilyHandle(i)) - ->cfd(); + auto* cfd = + static_cast_with_check( + static_cast_with_check(db_)->GetColumnFamilyHandle(i)) + ->cfd(); ASSERT_NE(cfd->TEST_GetLocalSV()->Get(), SuperVersion::kSVInUse); } } @@ -1259,7 +1261,7 @@ TEST_P(DBMultiGetTestWithParam, MultiGetMultiCFSnapshot) { } int get_sv_count = 0; - ROCKSDB_NAMESPACE::DBImpl* db = reinterpret_cast(db_); + ROCKSDB_NAMESPACE::DBImpl* db = static_cast_with_check(db_); ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack( "DBImpl::MultiGet::AfterRefSV", [&](void* /*arg*/) { if (++get_sv_count == 2) { @@ -1271,7 +1273,7 @@ TEST_P(DBMultiGetTestWithParam, MultiGetMultiCFSnapshot) { } if (get_sv_count == 8) { for (int i = 0; i < 8; ++i) { - auto* cfd = reinterpret_cast( + auto* cfd = static_cast_with_check( db->GetColumnFamilyHandle(i)) ->cfd(); ASSERT_TRUE( @@ -1299,9 +1301,10 @@ TEST_P(DBMultiGetTestWithParam, MultiGetMultiCFSnapshot) { ASSERT_EQ(values[j], "cf" + std::to_string(j) + "_val"); } for (int i = 0; i < 8; ++i) { - auto* cfd = reinterpret_cast( - reinterpret_cast(db_)->GetColumnFamilyHandle(i)) - ->cfd(); + auto* cfd = + static_cast_with_check( + static_cast_with_check(db_)->GetColumnFamilyHandle(i)) + ->cfd(); ASSERT_NE(cfd->TEST_GetLocalSV()->Get(), SuperVersion::kSVInUse); } } diff --git a/db/db_flush_test.cc b/db/db_flush_test.cc index 9485d5f22..6255bbd51 100644 --- a/db/db_flush_test.cc +++ b/db/db_flush_test.cc @@ -89,7 +89,7 @@ TEST_F(DBFlushTest, SyncFail) { CreateAndReopenWithCF({"pikachu"}, options); Put("key", "value"); auto* cfd = - reinterpret_cast(db_->DefaultColumnFamily()) + static_cast_with_check(db_->DefaultColumnFamily()) ->cfd(); FlushOptions flush_options; flush_options.wait = false; @@ -379,9 +379,9 @@ TEST_F(DBFlushTest, FireOnFlushCompletedAfterCommittedResult) { DBImpl* db_impl = static_cast_with_check(db); InstrumentedMutex* mutex = db_impl->mutex(); mutex->Lock(); - auto* cfd = - reinterpret_cast(db->DefaultColumnFamily()) - ->cfd(); + auto* cfd = static_cast_with_check( + db->DefaultColumnFamily()) + ->cfd(); ASSERT_LT(seq, cfd->imm()->current()->GetEarliestSequenceNumber()); mutex->Unlock(); } diff --git a/db/db_impl/db_impl.cc b/db/db_impl/db_impl.cc index 42bb73ae9..ac5f2fe99 100644 --- a/db/db_impl/db_impl.cc +++ b/db/db_impl/db_impl.cc @@ -329,7 +329,8 @@ Status DBImpl::ResumeImpl() { // whether there are flush jobs with non-empty data to flush, triggering // appends to MANIFEST. VersionEdit edit; - auto cfh = reinterpret_cast(default_cf_handle_); + auto cfh = + static_cast_with_check(default_cf_handle_); assert(cfh); ColumnFamilyData* cfd = cfh->cfd(); const MutableCFOptions& cf_opts = *cfd->GetLatestMutableCFOptions(); @@ -945,7 +946,8 @@ Status DBImpl::SetOptions( (void)options_map; return Status::NotSupported("Not supported in ROCKSDB LITE"); #else - auto* cfd = reinterpret_cast(column_family)->cfd(); + auto* cfd = + static_cast_with_check(column_family)->cfd(); if (options_map.empty()) { ROCKS_LOG_WARN(immutable_db_options_.info_log, "SetOptions() on column family [%s], empty input", @@ -1352,7 +1354,7 @@ InternalIterator* DBImpl::NewInternalIterator( if (column_family == nullptr) { cfd = default_cf_handle_->cfd(); } else { - auto cfh = reinterpret_cast(column_family); + auto cfh = static_cast_with_check(column_family); cfd = cfh->cfd(); } @@ -1581,8 +1583,8 @@ Status DBImpl::GetImpl(const ReadOptions& read_options, const Slice& key, StopWatch sw(env_, stats_, DB_GET); PERF_TIMER_GUARD(get_snapshot_time); - auto cfh = - reinterpret_cast(get_impl_options.column_family); + auto cfh = static_cast_with_check( + get_impl_options.column_family); auto cfd = cfh->cfd(); if (tracer_) { @@ -1783,7 +1785,7 @@ std::vector DBImpl::MultiGet( std::unordered_map multiget_cf_data( column_family.size()); for (auto cf : column_family) { - auto cfh = reinterpret_cast(cf); + auto cfh = static_cast_with_check(cf); auto cfd = cfh->cfd(); if (multiget_cf_data.find(cfd->GetID()) == multiget_cf_data.end()) { multiget_cf_data.emplace(cfd->GetID(), @@ -2185,10 +2187,11 @@ void DBImpl::PrepareMultiGetKeys( KeyContext* lhs = (*sorted_keys)[index - 1]; KeyContext* rhs = (*sorted_keys)[index]; ColumnFamilyHandleImpl* cfh = - reinterpret_cast(lhs->column_family); + static_cast_with_check(lhs->column_family); uint32_t cfd_id1 = cfh->cfd()->GetID(); const Comparator* comparator = cfh->cfd()->user_comparator(); - cfh = reinterpret_cast(lhs->column_family); + cfh = + static_cast_with_check(lhs->column_family); uint32_t cfd_id2 = cfh->cfd()->GetID(); assert(cfd_id1 <= cfd_id2); @@ -2552,7 +2555,7 @@ Status DBImpl::CreateColumnFamilyImpl(const ColumnFamilyOptions& cf_options, // this is outside the mutex if (s.ok()) { NewThreadStatusCfInfo( - reinterpret_cast(*handle)->cfd()); + static_cast_with_check(*handle)->cfd()); } return s; } @@ -2589,7 +2592,7 @@ Status DBImpl::DropColumnFamilies( } Status DBImpl::DropColumnFamilyImpl(ColumnFamilyHandle* column_family) { - auto cfh = reinterpret_cast(column_family); + auto cfh = static_cast_with_check(column_family); auto cfd = cfh->cfd(); if (cfd->GetID() == 0) { return Status::InvalidArgument("Can't drop default column family"); @@ -2703,7 +2706,7 @@ Iterator* DBImpl::NewIterator(const ReadOptions& read_options, "Iterator requested internal keys which are too old and are not" " guaranteed to be preserved, try larger iter_start_seqnum opt.")); } - auto cfh = reinterpret_cast(column_family); + auto cfh = static_cast_with_check(column_family); ColumnFamilyData* cfd = cfh->cfd(); assert(cfd != nullptr); ReadCallback* read_callback = nullptr; // No read callback provided. @@ -2838,7 +2841,7 @@ Status DBImpl::NewIterators( "Tailing iterator not supported in RocksDB lite"); #else for (auto cfh : column_families) { - auto cfd = reinterpret_cast(cfh)->cfd(); + auto cfd = static_cast_with_check(cfh)->cfd(); SuperVersion* sv = cfd->GetReferencedSuperVersion(this); auto iter = new ForwardIterator(this, read_options, cfd, sv, /* allow_unprepared_value */ true); @@ -2858,7 +2861,8 @@ Status DBImpl::NewIterators( : versions_->LastSequence(); for (size_t i = 0; i < column_families.size(); ++i) { auto* cfd = - reinterpret_cast(column_families[i])->cfd(); + static_cast_with_check(column_families[i]) + ->cfd(); iterators->push_back( NewIteratorImpl(read_options, cfd, snapshot, read_callback)); } @@ -2965,7 +2969,7 @@ void DBImpl::ReleaseSnapshot(const Snapshot* s) { #ifndef ROCKSDB_LITE Status DBImpl::GetPropertiesOfAllTables(ColumnFamilyHandle* column_family, TablePropertiesCollection* props) { - auto cfh = reinterpret_cast(column_family); + auto cfh = static_cast_with_check(column_family); auto cfd = cfh->cfd(); // Increment the ref count @@ -2987,7 +2991,7 @@ Status DBImpl::GetPropertiesOfAllTables(ColumnFamilyHandle* column_family, Status DBImpl::GetPropertiesOfTablesInRange(ColumnFamilyHandle* column_family, const Range* range, std::size_t n, TablePropertiesCollection* props) { - auto cfh = reinterpret_cast(column_family); + auto cfh = static_cast_with_check(column_family); auto cfd = cfh->cfd(); // Increment the ref count @@ -3023,7 +3027,7 @@ FileSystem* DBImpl::GetFileSystem() const { Options DBImpl::GetOptions(ColumnFamilyHandle* column_family) const { InstrumentedMutexLock l(&mutex_); - auto cfh = reinterpret_cast(column_family); + auto cfh = static_cast_with_check(column_family); return Options(BuildDBOptions(immutable_db_options_, mutable_db_options_), cfh->cfd()->GetLatestCFOptions()); } @@ -3037,7 +3041,8 @@ bool DBImpl::GetProperty(ColumnFamilyHandle* column_family, const Slice& property, std::string* value) { const DBPropertyInfo* property_info = GetPropertyInfo(property); value->clear(); - auto cfd = reinterpret_cast(column_family)->cfd(); + auto cfd = + static_cast_with_check(column_family)->cfd(); if (property_info == nullptr) { return false; } else if (property_info->handle_int) { @@ -3071,7 +3076,8 @@ bool DBImpl::GetMapProperty(ColumnFamilyHandle* column_family, std::map* value) { const DBPropertyInfo* property_info = GetPropertyInfo(property); value->clear(); - auto cfd = reinterpret_cast(column_family)->cfd(); + auto cfd = + static_cast_with_check(column_family)->cfd(); if (property_info == nullptr) { return false; } else if (property_info->handle_map) { @@ -3090,7 +3096,8 @@ bool DBImpl::GetIntProperty(ColumnFamilyHandle* column_family, if (property_info == nullptr || property_info->handle_int == nullptr) { return false; } - auto cfd = reinterpret_cast(column_family)->cfd(); + auto cfd = + static_cast_with_check(column_family)->cfd(); return GetIntPropertyInternal(cfd, *property_info, false, value); } @@ -3263,7 +3270,7 @@ void DBImpl::GetApproximateMemTableStats(ColumnFamilyHandle* column_family, uint64_t* const count, uint64_t* const size) { ColumnFamilyHandleImpl* cfh = - reinterpret_cast(column_family); + static_cast_with_check(column_family); ColumnFamilyData* cfd = cfh->cfd(); SuperVersion* sv = GetAndRefSuperVersion(cfd); @@ -3288,7 +3295,7 @@ Status DBImpl::GetApproximateSizes(const SizeApproximationOptions& options, } Version* v; - auto cfh = reinterpret_cast(column_family); + auto cfh = static_cast_with_check(column_family); auto cfd = cfh->cfd(); SuperVersion* sv = GetAndRefSuperVersion(cfd); v = sv->current; @@ -3446,7 +3453,7 @@ Status DBImpl::DeleteFilesInRanges(ColumnFamilyHandle* column_family, const RangePtr* ranges, size_t n, bool include_end) { Status status; - auto cfh = reinterpret_cast(column_family); + auto cfh = static_cast_with_check(column_family); ColumnFamilyData* cfd = cfh->cfd(); VersionEdit edit; std::set deleted_files; @@ -3544,7 +3551,8 @@ Status DBImpl::GetLiveFilesChecksumInfo(FileChecksumList* checksum_list) { void DBImpl::GetColumnFamilyMetaData(ColumnFamilyHandle* column_family, ColumnFamilyMetaData* cf_meta) { assert(column_family); - auto* cfd = reinterpret_cast(column_family)->cfd(); + auto* cfd = + static_cast_with_check(column_family)->cfd(); auto* sv = GetAndRefSuperVersion(cfd); { // Without mutex, Version::GetColumnFamilyMetaData will have data race with @@ -4510,7 +4518,7 @@ Status DBImpl::CreateColumnFamilyWithImport( } // Import sst files from metadata. - auto cfh = reinterpret_cast(*handle); + auto cfh = static_cast_with_check(*handle); auto cfd = cfh->cfd(); ImportColumnFamilyJob import_job(env_, versions_.get(), cfd, immutable_db_options_, file_options_, diff --git a/db/db_impl/db_impl_compaction_flush.cc b/db/db_impl/db_impl_compaction_flush.cc index 1602a40b0..cfb97be8a 100644 --- a/db/db_impl/db_impl_compaction_flush.cc +++ b/db/db_impl/db_impl_compaction_flush.cc @@ -695,7 +695,7 @@ void DBImpl::NotifyOnFlushCompleted( Status DBImpl::CompactRange(const CompactRangeOptions& options, ColumnFamilyHandle* column_family, const Slice* begin, const Slice* end) { - auto cfh = reinterpret_cast(column_family); + auto cfh = static_cast_with_check(column_family); auto cfd = cfh->cfd(); if (options.target_path_id >= cfd->ioptions()->cf_paths.size()) { @@ -885,7 +885,8 @@ Status DBImpl::CompactFiles(const CompactionOptions& compact_options, return Status::InvalidArgument("ColumnFamilyHandle must be non-null."); } - auto cfd = reinterpret_cast(column_family)->cfd(); + auto cfd = + static_cast_with_check(column_family)->cfd(); assert(cfd); Status s; @@ -1359,7 +1360,7 @@ Status DBImpl::ReFitLevel(ColumnFamilyData* cfd, int level, int target_level) { } int DBImpl::NumberLevels(ColumnFamilyHandle* column_family) { - auto cfh = reinterpret_cast(column_family); + auto cfh = static_cast_with_check(column_family); return cfh->cfd()->NumberLevels(); } @@ -1368,7 +1369,7 @@ int DBImpl::MaxMemCompactionLevel(ColumnFamilyHandle* /*column_family*/) { } int DBImpl::Level0StopWriteTrigger(ColumnFamilyHandle* column_family) { - auto cfh = reinterpret_cast(column_family); + auto cfh = static_cast_with_check(column_family); InstrumentedMutexLock l(&mutex_); return cfh->cfd() ->GetSuperVersion() @@ -1377,7 +1378,7 @@ int DBImpl::Level0StopWriteTrigger(ColumnFamilyHandle* column_family) { Status DBImpl::Flush(const FlushOptions& flush_options, ColumnFamilyHandle* column_family) { - auto cfh = reinterpret_cast(column_family); + auto cfh = static_cast_with_check(column_family); ROCKS_LOG_INFO(immutable_db_options_.info_log, "[%s] Manual flush start.", cfh->GetName().c_str()); Status s; diff --git a/db/db_impl/db_impl_debug.cc b/db/db_impl/db_impl_debug.cc index 610b57d39..332040851 100644 --- a/db/db_impl/db_impl_debug.cc +++ b/db/db_impl/db_impl_debug.cc @@ -47,7 +47,7 @@ int64_t DBImpl::TEST_MaxNextLevelOverlappingBytes( if (column_family == nullptr) { cfd = default_cf_handle_->cfd(); } else { - auto cfh = reinterpret_cast(column_family); + auto cfh = static_cast_with_check(column_family); cfd = cfh->cfd(); } InstrumentedMutexLock l(&mutex_); @@ -57,7 +57,7 @@ int64_t DBImpl::TEST_MaxNextLevelOverlappingBytes( void DBImpl::TEST_GetFilesMetaData( ColumnFamilyHandle* column_family, std::vector>* metadata) { - auto cfh = reinterpret_cast(column_family); + auto cfh = static_cast_with_check(column_family); auto cfd = cfh->cfd(); InstrumentedMutexLock l(&mutex_); metadata->resize(NumberLevels()); @@ -88,7 +88,7 @@ Status DBImpl::TEST_CompactRange(int level, const Slice* begin, if (column_family == nullptr) { cfd = default_cf_handle_->cfd(); } else { - auto cfh = reinterpret_cast(column_family); + auto cfh = static_cast_with_check(column_family); cfd = cfh->cfd(); } int output_level = @@ -131,7 +131,7 @@ Status DBImpl::TEST_FlushMemTable(bool wait, bool allow_write_stall, if (cfh == nullptr) { cfd = default_cf_handle_->cfd(); } else { - auto cfhi = reinterpret_cast(cfh); + auto cfhi = static_cast_with_check(cfh); cfd = cfhi->cfd(); } return FlushMemTable(cfd, fo, FlushReason::kTest); @@ -152,7 +152,7 @@ Status DBImpl::TEST_WaitForFlushMemTable(ColumnFamilyHandle* column_family) { if (column_family == nullptr) { cfd = default_cf_handle_->cfd(); } else { - auto cfh = reinterpret_cast(column_family); + auto cfh = static_cast_with_check(column_family); cfd = cfh->cfd(); } return WaitForFlushMemTable(cfd, nullptr, false); @@ -242,7 +242,7 @@ Status DBImpl::TEST_GetLatestMutableCFOptions( ColumnFamilyHandle* column_family, MutableCFOptions* mutable_cf_options) { InstrumentedMutexLock l(&mutex_); - auto cfh = reinterpret_cast(column_family); + auto cfh = static_cast_with_check(column_family); *mutable_cf_options = *cfh->cfd()->GetLatestMutableCFOptions(); return Status::OK(); } diff --git a/db/db_impl/db_impl_experimental.cc b/db/db_impl/db_impl_experimental.cc index f0c17ce95..bd6da6999 100644 --- a/db/db_impl/db_impl_experimental.cc +++ b/db/db_impl/db_impl_experimental.cc @@ -7,22 +7,22 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. See the AUTHORS file for names of contributors. -#include "db/db_impl/db_impl.h" - #include #include #include "db/column_family.h" +#include "db/db_impl/db_impl.h" #include "db/job_context.h" #include "db/version_set.h" #include "rocksdb/status.h" +#include "util/cast_util.h" namespace ROCKSDB_NAMESPACE { #ifndef ROCKSDB_LITE Status DBImpl::SuggestCompactRange(ColumnFamilyHandle* column_family, const Slice* begin, const Slice* end) { - auto cfh = reinterpret_cast(column_family); + auto cfh = static_cast_with_check(column_family); auto cfd = cfh->cfd(); InternalKey start_key, end_key; if (begin != nullptr) { diff --git a/db/db_impl/db_impl_readonly.cc b/db/db_impl/db_impl_readonly.cc index d39ff44d4..5ddde3891 100644 --- a/db/db_impl/db_impl_readonly.cc +++ b/db/db_impl/db_impl_readonly.cc @@ -4,13 +4,14 @@ // (found in the LICENSE.Apache file in the root directory). #include "db/db_impl/db_impl_readonly.h" -#include "db/arena_wrapped_db_iter.h" +#include "db/arena_wrapped_db_iter.h" #include "db/compacted_db_impl.h" #include "db/db_impl/db_impl.h" #include "db/db_iter.h" #include "db/merge_context.h" #include "monitoring/perf_context_imp.h" +#include "util/cast_util.h" namespace ROCKSDB_NAMESPACE { @@ -35,7 +36,7 @@ Status DBImplReadOnly::Get(const ReadOptions& read_options, PERF_TIMER_GUARD(get_snapshot_time); Status s; SequenceNumber snapshot = versions_->LastSequence(); - auto cfh = reinterpret_cast(column_family); + auto cfh = static_cast_with_check(column_family); auto cfd = cfh->cfd(); if (tracer_) { InstrumentedMutexLock lock(&trace_mutex_); @@ -70,7 +71,7 @@ Status DBImplReadOnly::Get(const ReadOptions& read_options, Iterator* DBImplReadOnly::NewIterator(const ReadOptions& read_options, ColumnFamilyHandle* column_family) { - auto cfh = reinterpret_cast(column_family); + auto cfh = static_cast_with_check(column_family); auto cfd = cfh->cfd(); SuperVersion* super_version = cfd->GetSuperVersion()->Ref(); SequenceNumber latest_snapshot = versions_->LastSequence(); @@ -111,7 +112,7 @@ Status DBImplReadOnly::NewIterators( : latest_snapshot; for (auto cfh : column_families) { - auto* cfd = reinterpret_cast(cfh)->cfd(); + auto* cfd = static_cast_with_check(cfh)->cfd(); auto* sv = cfd->GetSuperVersion()->Ref(); auto* db_iter = NewArenaWrappedDbIterator( env_, read_options, *cfd->ioptions(), sv->mutable_cf_options, read_seq, @@ -236,7 +237,7 @@ Status DBImplReadOnly::OpenForReadOnlyWithoutCheck( *dbptr = impl; for (auto* h : *handles) { impl->NewThreadStatusCfInfo( - reinterpret_cast(h)->cfd()); + static_cast_with_check(h)->cfd()); } } else { for (auto h : *handles) { diff --git a/db/db_impl/db_impl_secondary.cc b/db/db_impl/db_impl_secondary.cc index e0c351f90..31dcf75bc 100644 --- a/db/db_impl/db_impl_secondary.cc +++ b/db/db_impl/db_impl_secondary.cc @@ -388,7 +388,7 @@ Iterator* DBImplSecondary::NewIterator(const ReadOptions& read_options, "ReadTier::kPersistedData is not yet supported in iterators.")); } Iterator* result = nullptr; - auto cfh = reinterpret_cast(column_family); + auto cfh = static_cast_with_check(column_family); auto cfd = cfh->cfd(); ReadCallback* read_callback = nullptr; // No read callback provided. if (read_options.tailing) { @@ -642,7 +642,7 @@ Status DB::OpenAsSecondary( *dbptr = impl; for (auto h : *handles) { impl->NewThreadStatusCfInfo( - reinterpret_cast(h)->cfd()); + static_cast_with_check(h)->cfd()); } } else { for (auto h : *handles) { diff --git a/db/db_impl/db_impl_write.cc b/db/db_impl/db_impl_write.cc index b0e21b6d7..b3861c32f 100644 --- a/db/db_impl/db_impl_write.cc +++ b/db/db_impl/db_impl_write.cc @@ -6,14 +6,15 @@ // Copyright (c) 2011 The LevelDB Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. See the AUTHORS file for names of contributors. -#include "db/db_impl/db_impl.h" - #include + +#include "db/db_impl/db_impl.h" #include "db/error_handler.h" #include "db/event_helpers.h" #include "monitoring/perf_context_imp.h" #include "options/options_helper.h" #include "test_util/sync_point.h" +#include "util/cast_util.h" namespace ROCKSDB_NAMESPACE { // Convenience methods @@ -24,7 +25,7 @@ Status DBImpl::Put(const WriteOptions& o, ColumnFamilyHandle* column_family, Status DBImpl::Merge(const WriteOptions& o, ColumnFamilyHandle* column_family, const Slice& key, const Slice& val) { - auto cfh = reinterpret_cast(column_family); + auto cfh = static_cast_with_check(column_family); if (!cfh->cfd()->ioptions()->merge_operator) { return Status::NotSupported("Provide a merge_operator when opening DB"); } else { diff --git a/db/db_iterator_test.cc b/db/db_iterator_test.cc index dcef1f897..6d06fa6ea 100644 --- a/db/db_iterator_test.cc +++ b/db/db_iterator_test.cc @@ -40,7 +40,8 @@ class DBIteratorTest : public DBTestBase, if (column_family == nullptr) { column_family = db_->DefaultColumnFamily(); } - auto* cfd = reinterpret_cast(column_family)->cfd(); + auto* cfd = + static_cast_with_check(column_family)->cfd(); SequenceNumber seq = read_options.snapshot != nullptr ? read_options.snapshot->GetSequenceNumber() : db_->GetLatestSequenceNumber(); @@ -2911,7 +2912,7 @@ TEST_F(DBIteratorWithReadCallbackTest, ReadCallback) { SequenceNumber seq2 = db_->GetLatestSequenceNumber(); auto* cfd = - reinterpret_cast(db_->DefaultColumnFamily()) + static_cast_with_check(db_->DefaultColumnFamily()) ->cfd(); // The iterator are suppose to see data before seq1. Iterator* iter = diff --git a/db/db_range_del_test.cc b/db/db_range_del_test.cc index 83fb27ff3..b6f721b71 100644 --- a/db/db_range_del_test.cc +++ b/db/db_range_del_test.cc @@ -459,7 +459,7 @@ TEST_F(DBRangeDelTest, ValidUniversalSubcompactionBoundaries) { // probably means universal compaction + subcompaction + range deletion are // compatible. ASSERT_OK(dbfull()->RunManualCompaction( - reinterpret_cast(db_->DefaultColumnFamily()) + static_cast_with_check(db_->DefaultColumnFamily()) ->cfd(), 1 /* input_level */, 2 /* output_level */, CompactRangeOptions(), nullptr /* begin */, nullptr /* end */, true /* exclusive */, diff --git a/db/db_test.cc b/db/db_test.cc index b59f92345..4fecae582 100644 --- a/db/db_test.cc +++ b/db/db_test.cc @@ -126,7 +126,7 @@ TEST_F(DBTest, MockEnvTest) { // TEST_FlushMemTable() is not supported in ROCKSDB_LITE #ifndef ROCKSDB_LITE - DBImpl* dbi = reinterpret_cast(db); + DBImpl* dbi = static_cast_with_check(db); ASSERT_OK(dbi->TEST_FlushMemTable()); for (size_t i = 0; i < 3; ++i) { @@ -174,7 +174,7 @@ TEST_F(DBTest, MemEnvTest) { ASSERT_TRUE(!iterator->Valid()); delete iterator; - DBImpl* dbi = reinterpret_cast(db); + DBImpl* dbi = static_cast_with_check(db); ASSERT_OK(dbi->TEST_FlushMemTable()); for (size_t i = 0; i < 3; ++i) { diff --git a/db/db_test2.cc b/db/db_test2.cc index a0fc938b0..e00bf4fb4 100644 --- a/db/db_test2.cc +++ b/db/db_test2.cc @@ -1725,7 +1725,7 @@ TEST_F(DBTest2, DuplicateSnapshot) { Options options; options = CurrentOptions(options); std::vector snapshots; - DBImpl* dbi = reinterpret_cast(db_); + DBImpl* dbi = static_cast_with_check(db_); SequenceNumber oldest_ww_snap, first_ww_snap; Put("k", "v"); // inc seq @@ -4176,7 +4176,7 @@ TEST_F(DBTest2, TestGetColumnFamilyHandleUnlocked) { CreateColumnFamilies({"test1", "test2"}, Options()); ASSERT_EQ(handles_.size(), 2); - DBImpl* dbi = reinterpret_cast(db_); + DBImpl* dbi = static_cast_with_check(db_); port::Thread user_thread1([&]() { auto cfh = dbi->GetColumnFamilyHandleUnlocked(handles_[0]->GetID()); ASSERT_EQ(cfh->GetID(), handles_[0]->GetID()); diff --git a/db/db_test_util.h b/db/db_test_util.h index 7a40a1346..2dbf0fd6b 100644 --- a/db/db_test_util.h +++ b/db/db_test_util.h @@ -10,9 +10,9 @@ #pragma once #include -#include #include +#include #include #include #include @@ -43,12 +43,12 @@ #include "table/plain/plain_table_factory.h" #include "table/scoped_arena_iterator.h" #include "test_util/mock_time_env.h" -#include "util/compression.h" -#include "util/mutexlock.h" - #include "test_util/sync_point.h" #include "test_util/testharness.h" #include "test_util/testutil.h" +#include "util/cast_util.h" +#include "util/compression.h" +#include "util/mutexlock.h" #include "util/string_util.h" #include "utilities/merge_operators.h" @@ -922,7 +922,7 @@ class DBTestBase : public testing::Test { const anon::OptionsOverride& options_override = anon::OptionsOverride()) const; - DBImpl* dbfull() { return reinterpret_cast(db_); } + DBImpl* dbfull() { return static_cast_with_check(db_); } void CreateColumnFamilies(const std::vector& cfs, const Options& options); diff --git a/db/listener_test.cc b/db/listener_test.cc index de5eb58c6..33cf51036 100644 --- a/db/listener_test.cc +++ b/db/listener_test.cc @@ -436,7 +436,7 @@ TEST_F(EventListenerTest, MultiDBMultiListeners) { for (size_t c = 0; c < cf_names.size(); ++c) { for (int d = 0; d < kNumDBs; ++d) { ASSERT_OK(dbs[d]->Flush(FlushOptions(), vec_handles[d][c])); - reinterpret_cast(dbs[d])->TEST_WaitForFlushMemTable(); + static_cast_with_check(dbs[d])->TEST_WaitForFlushMemTable(); } } diff --git a/db/plain_table_db_test.cc b/db/plain_table_db_test.cc index f61a280ad..ea14f9a2a 100644 --- a/db/plain_table_db_test.cc +++ b/db/plain_table_db_test.cc @@ -32,6 +32,7 @@ #include "table/table_builder.h" #include "test_util/testharness.h" #include "test_util/testutil.h" +#include "util/cast_util.h" #include "util/hash.h" #include "util/mutexlock.h" #include "util/string_util.h" @@ -146,9 +147,7 @@ class PlainTableDBTest : public testing::Test, return options; } - DBImpl* dbfull() { - return reinterpret_cast(db_); - } + DBImpl* dbfull() { return static_cast_with_check(db_); } void Reopen(Options* options = nullptr) { ASSERT_OK(TryReopen(options)); diff --git a/db/prefix_test.cc b/db/prefix_test.cc index c61ec2a1e..81387ce76 100644 --- a/db/prefix_test.cc +++ b/db/prefix_test.cc @@ -27,6 +27,7 @@ int main() { #include "rocksdb/slice_transform.h" #include "rocksdb/table.h" #include "test_util/testharness.h" +#include "util/cast_util.h" #include "util/coding.h" #include "util/gflags_compat.h" #include "util/random.h" @@ -807,13 +808,13 @@ TEST_F(PrefixTest, PrefixSeekModePrev2) { PutKey(db.get(), write_options, TestKey(3, 3), "v33"); PutKey(db.get(), write_options, TestKey(3, 4), "v34"); db->Flush(FlushOptions()); - reinterpret_cast(db.get())->TEST_WaitForFlushMemTable(); + static_cast_with_check(db.get())->TEST_WaitForFlushMemTable(); PutKey(db.get(), write_options, TestKey(1, 1), "v11"); PutKey(db.get(), write_options, TestKey(1, 3), "v13"); PutKey(db.get(), write_options, TestKey(2, 1), "v21"); PutKey(db.get(), write_options, TestKey(2, 2), "v22"); db->Flush(FlushOptions()); - reinterpret_cast(db.get())->TEST_WaitForFlushMemTable(); + static_cast_with_check(db.get())->TEST_WaitForFlushMemTable(); std::unique_ptr iter(db->NewIterator(read_options)); SeekIterator(iter.get(), 1, 5); iter->Prev(); @@ -839,13 +840,13 @@ TEST_F(PrefixTest, PrefixSeekModePrev3) { PutKey(db.get(), write_options, TestKey(1, 2), "v12"); PutKey(db.get(), write_options, TestKey(1, 4), "v14"); db->Flush(FlushOptions()); - reinterpret_cast(db.get())->TEST_WaitForFlushMemTable(); + static_cast_with_check(db.get())->TEST_WaitForFlushMemTable(); PutKey(db.get(), write_options, TestKey(1, 1), "v11"); PutKey(db.get(), write_options, TestKey(1, 3), "v13"); PutKey(db.get(), write_options, TestKey(2, 1), "v21"); PutKey(db.get(), write_options, TestKey(2, 2), "v22"); db->Flush(FlushOptions()); - reinterpret_cast(db.get())->TEST_WaitForFlushMemTable(); + static_cast_with_check(db.get())->TEST_WaitForFlushMemTable(); std::unique_ptr iter(db->NewIterator(read_options)); iter->SeekToLast(); ASSERT_EQ(iter->value(), v14); @@ -861,11 +862,11 @@ TEST_F(PrefixTest, PrefixSeekModePrev3) { PutKey(db.get(), write_options, TestKey(3, 3), "v33"); PutKey(db.get(), write_options, TestKey(3, 4), "v34"); db->Flush(FlushOptions()); - reinterpret_cast(db.get())->TEST_WaitForFlushMemTable(); + static_cast_with_check(db.get())->TEST_WaitForFlushMemTable(); PutKey(db.get(), write_options, TestKey(1, 1), "v11"); PutKey(db.get(), write_options, TestKey(1, 3), "v13"); db->Flush(FlushOptions()); - reinterpret_cast(db.get())->TEST_WaitForFlushMemTable(); + static_cast_with_check(db.get())->TEST_WaitForFlushMemTable(); std::unique_ptr iter(db->NewIterator(read_options)); iter->SeekToLast(); ASSERT_EQ(iter->value(), v14); diff --git a/db/write_batch.cc b/db/write_batch.cc index 645bbd70a..76f59d55a 100644 --- a/db/write_batch.cc +++ b/db/write_batch.cc @@ -1641,7 +1641,8 @@ class MemTableInserter : public WriteBatch::Handler { if (cf_handle == nullptr) { cf_handle = db_->DefaultColumnFamily(); } - auto* cfd = reinterpret_cast(cf_handle)->cfd(); + auto* cfd = + static_cast_with_check(cf_handle)->cfd(); if (!cfd->is_delete_range_supported()) { return Status::NotSupported( std::string("DeleteRange not supported for table type ") + diff --git a/db_stress_tool/db_stress_test_base.cc b/db_stress_tool/db_stress_test_base.cc index bdb965d93..1bcd9ffd0 100644 --- a/db_stress_tool/db_stress_test_base.cc +++ b/db_stress_tool/db_stress_test_base.cc @@ -1504,7 +1504,7 @@ void StressTest::TestAcquireSnapshot(ThreadState* thread, Slice key = keystr; ColumnFamilyHandle* column_family = column_families_[rand_column_family]; #ifndef ROCKSDB_LITE - auto db_impl = reinterpret_cast(db_->GetRootDB()); + auto db_impl = static_cast_with_check(db_->GetRootDB()); const bool ww_snapshot = thread->rand.OneIn(10); const Snapshot* snapshot = ww_snapshot ? db_impl->GetSnapshotForWriteConflictBoundary() diff --git a/monitoring/thread_status_updater_debug.cc b/monitoring/thread_status_updater_debug.cc index 5937d91fa..464c23bba 100644 --- a/monitoring/thread_status_updater_debug.cc +++ b/monitoring/thread_status_updater_debug.cc @@ -7,6 +7,7 @@ #include "db/column_family.h" #include "monitoring/thread_status_updater.h" +#include "util/cast_util.h" namespace ROCKSDB_NAMESPACE { @@ -19,7 +20,7 @@ void ThreadStatusUpdater::TEST_VerifyColumnFamilyInfoMap( assert(cf_info_map_.size() == handles.size()); } for (auto* handle : handles) { - auto* cfd = reinterpret_cast(handle)->cfd(); + auto* cfd = static_cast_with_check(handle)->cfd(); auto iter __attribute__((__unused__)) = cf_info_map_.find(cfd); if (check_exist) { assert(iter != cf_info_map_.end()); diff --git a/tools/reduce_levels_test.cc b/tools/reduce_levels_test.cc index 1b1044f95..e301016ab 100644 --- a/tools/reduce_levels_test.cc +++ b/tools/reduce_levels_test.cc @@ -13,6 +13,7 @@ #include "test_util/testharness.h" #include "test_util/testutil.h" #include "tools/ldb_cmd_impl.h" +#include "util/cast_util.h" #include "util/string_util.h" namespace ROCKSDB_NAMESPACE { @@ -47,12 +48,12 @@ public: if (db_ == nullptr) { return Status::InvalidArgument("DB not opened."); } - DBImpl* db_impl = reinterpret_cast(db_); + DBImpl* db_impl = static_cast_with_check(db_); return db_impl->TEST_FlushMemTable(); } void MoveL0FileToLevel(int level) { - DBImpl* db_impl = reinterpret_cast(db_); + DBImpl* db_impl = static_cast_with_check(db_); for (int i = 0; i < level; ++i) { ASSERT_OK(db_impl->TEST_CompactRange(i, nullptr, nullptr)); } diff --git a/util/cast_util.h b/util/cast_util.h index d7d9a9e9c..d84516fba 100644 --- a/util/cast_util.h +++ b/util/cast_util.h @@ -4,11 +4,10 @@ // (found in the LICENSE.Apache file in the root directory). #pragma once - namespace ROCKSDB_NAMESPACE { // The helper function to assert the move from dynamic_cast<> to // static_cast<> is correct. This function is to deal with legacy code. -// It is not recommanded to add new code to issue class casting. The preferred +// It is not recommended to add new code to issue class casting. The preferred // solution is to implement the functionality without a need of casting. template inline DestClass* static_cast_with_check(SrcClass* x) { diff --git a/utilities/backupable/backupable_db_test.cc b/utilities/backupable/backupable_db_test.cc index d61130785..46d7942a7 100644 --- a/utilities/backupable/backupable_db_test.cc +++ b/utilities/backupable/backupable_db_test.cc @@ -9,6 +9,8 @@ #if !defined(ROCKSDB_LITE) && !defined(OS_WIN) +#include "rocksdb/utilities/backupable_db.h" + #include #include #include @@ -22,11 +24,11 @@ #include "rocksdb/rate_limiter.h" #include "rocksdb/transaction_log.h" #include "rocksdb/types.h" -#include "rocksdb/utilities/backupable_db.h" #include "rocksdb/utilities/options_util.h" #include "test_util/sync_point.h" #include "test_util/testharness.h" #include "test_util/testutil.h" +#include "util/cast_util.h" #include "util/mutexlock.h" #include "util/random.h" #include "util/stderr_logger.h" @@ -2009,7 +2011,7 @@ TEST_F(BackupableDBTest, ChangeManifestDuringBackupCreation) { // The last manifest roll would've already been cleaned up by the full scan // that happens when CreateNewBackup invokes EnableFileDeletions. We need to // trigger another roll to verify non-full scan purges stale manifests. - DBImpl* db_impl = reinterpret_cast(db_.get()); + DBImpl* db_impl = static_cast_with_check(db_.get()); std::string prev_manifest_path = DescriptorFileName(dbname_, db_impl->TEST_Current_Manifest_FileNo()); FillDB(db_.get(), 0, 100); diff --git a/utilities/blob_db/blob_db_impl.cc b/utilities/blob_db/blob_db_impl.cc index d1a58e5d2..e80e1f1a4 100644 --- a/utilities/blob_db/blob_db_impl.cc +++ b/utilities/blob_db/blob_db_impl.cc @@ -995,7 +995,8 @@ Status BlobDBImpl::Write(const WriteOptions& options, WriteBatch* updates) { StopWatch write_sw(env_, statistics_, BLOB_DB_WRITE_MICROS); RecordTick(statistics_, BLOB_DB_NUM_WRITE); uint32_t default_cf_id = - reinterpret_cast(DefaultColumnFamily())->GetID(); + static_cast_with_check(DefaultColumnFamily()) + ->GetID(); Status s; BlobInserter blob_inserter(options, this, default_cf_id); { @@ -1050,7 +1051,8 @@ Status BlobDBImpl::PutBlobValue(const WriteOptions& /*options*/, Status s; std::string index_entry; uint32_t column_family_id = - reinterpret_cast(DefaultColumnFamily())->GetID(); + static_cast_with_check(DefaultColumnFamily()) + ->GetID(); if (value.size() < bdb_options_.min_blob_size) { if (expiration == kNoExpiration) { // Put as normal value @@ -2029,7 +2031,8 @@ void BlobDBImpl::CopyBlobFiles( Iterator* BlobDBImpl::NewIterator(const ReadOptions& read_options) { auto* cfd = - reinterpret_cast(DefaultColumnFamily())->cfd(); + static_cast_with_check(DefaultColumnFamily()) + ->cfd(); // Get a snapshot to avoid blob file get deleted between we // fetch and index entry and reading from the file. ManagedSnapshot* own_snapshot = nullptr; diff --git a/utilities/blob_db/blob_db_impl_filesnapshot.cc b/utilities/blob_db/blob_db_impl_filesnapshot.cc index 82bff2ea6..bfd81c51d 100644 --- a/utilities/blob_db/blob_db_impl_filesnapshot.cc +++ b/utilities/blob_db/blob_db_impl_filesnapshot.cc @@ -5,11 +5,11 @@ #ifndef ROCKSDB_LITE -#include "utilities/blob_db/blob_db_impl.h" - #include "file/filename.h" #include "logging/logging.h" +#include "util/cast_util.h" #include "util/mutexlock.h" +#include "utilities/blob_db/blob_db_impl.h" // BlobDBImpl methods to get snapshot of files, e.g. for replication. @@ -98,7 +98,8 @@ void BlobDBImpl::GetLiveFilesMetaData(std::vector* metadata) { // Path should be relative to db_name, but begin with slash. filemetadata.name = BlobFileName("", bdb_options_.blob_dir, file_number); filemetadata.file_number = file_number; - auto cfh = reinterpret_cast(DefaultColumnFamily()); + auto cfh = + static_cast_with_check(DefaultColumnFamily()); filemetadata.column_family_name = cfh->GetName(); metadata->emplace_back(filemetadata); } diff --git a/utilities/cassandra/cassandra_functional_test.cc b/utilities/cassandra/cassandra_functional_test.cc index 7a2449527..5c614b48f 100644 --- a/utilities/cassandra/cassandra_functional_test.cc +++ b/utilities/cassandra/cassandra_functional_test.cc @@ -4,11 +4,13 @@ // (found in the LICENSE.Apache file in the root directory). #include + #include "db/db_impl/db_impl.h" #include "rocksdb/db.h" #include "rocksdb/merge_operator.h" #include "rocksdb/utilities/db_ttl.h" #include "test_util/testharness.h" +#include "util/cast_util.h" #include "util/random.h" #include "utilities/cassandra/cassandra_compaction_filter.h" #include "utilities/cassandra/merge_operator.h" @@ -89,7 +91,7 @@ class CassandraStore { WriteOptions write_option_; ReadOptions get_option_; - DBImpl* dbfull() { return reinterpret_cast(db_.get()); } + DBImpl* dbfull() { return static_cast_with_check(db_.get()); } }; class TestCompactionFilterFactory : public CompactionFilterFactory { diff --git a/utilities/checkpoint/checkpoint_impl.cc b/utilities/checkpoint/checkpoint_impl.cc index 3f8dbf4bd..4fc6ede00 100644 --- a/utilities/checkpoint/checkpoint_impl.cc +++ b/utilities/checkpoint/checkpoint_impl.cc @@ -26,6 +26,7 @@ #include "rocksdb/transaction_log.h" #include "rocksdb/utilities/checkpoint.h" #include "test_util/sync_point.h" +#include "util/cast_util.h" namespace ROCKSDB_NAMESPACE { @@ -367,7 +368,7 @@ Status CheckpointImpl::CreateCustomCheckpoint( Status CheckpointImpl::ExportColumnFamily( ColumnFamilyHandle* handle, const std::string& export_dir, ExportImportFilesMetaData** metadata) { - auto cfh = reinterpret_cast(handle); + auto cfh = static_cast_with_check(handle); const auto cf_name = cfh->GetName(); const auto db_options = db_->GetDBOptions(); diff --git a/utilities/transactions/transaction_test.cc b/utilities/transactions/transaction_test.cc index 1a710f70e..f153b9a14 100644 --- a/utilities/transactions/transaction_test.cc +++ b/utilities/transactions/transaction_test.cc @@ -223,7 +223,7 @@ TEST_P(TransactionTest, ValidateSnapshotTest) { } if (with_flush) { - auto db_impl = reinterpret_cast(db->GetRootDB()); + auto db_impl = static_cast_with_check(db->GetRootDB()); db_impl->TEST_FlushMemTable(true); // Make sure the flushed memtable is not kept in memory int max_memtable_in_history = @@ -882,7 +882,7 @@ TEST_P(TransactionTest, LogMarkLeakTest) { assert(db != nullptr); Random rnd(47); std::vector txns; - DBImpl* db_impl = reinterpret_cast(db->GetRootDB()); + DBImpl* db_impl = static_cast_with_check(db->GetRootDB()); // At the beginning there should be no log containing prepare data ASSERT_EQ(db_impl->TEST_FindMinLogContainingOutstandingPrep(), 0); for (size_t i = 0; i < 100; i++) { @@ -923,7 +923,7 @@ TEST_P(TransactionTest, SimpleTwoPhaseTransactionTest) { string value; Status s; - DBImpl* db_impl = reinterpret_cast(db->GetRootDB()); + DBImpl* db_impl = static_cast_with_check(db->GetRootDB()); Transaction* txn = db->BeginTransaction(write_options, txn_options); s = txn->SetName("xid"); @@ -1160,7 +1160,7 @@ TEST_P(TransactionTest, TwoPhaseEmptyWriteTest) { ASSERT_EQ(value, "bar"); } else { if (test_with_empty_wal) { - DBImpl* db_impl = reinterpret_cast(db->GetRootDB()); + DBImpl* db_impl = static_cast_with_check(db->GetRootDB()); db_impl->TEST_FlushMemTable(true); // After flush the state must be visible s = db->Get(read_options, "foo", &value); @@ -1222,7 +1222,7 @@ TEST_P(TransactionTest, TwoPhaseRollbackTest) { std::string value; Status s; - DBImpl* db_impl = reinterpret_cast(db->GetRootDB()); + DBImpl* db_impl = static_cast_with_check(db->GetRootDB()); Transaction* txn = db->BeginTransaction(write_options, txn_options); s = txn->SetName("xid"); ASSERT_OK(s); @@ -1293,7 +1293,7 @@ TEST_P(TransactionTest, PersistentTwoPhaseTransactionTest) { std::string value; Status s; - DBImpl* db_impl = reinterpret_cast(db->GetRootDB()); + DBImpl* db_impl = static_cast_with_check(db->GetRootDB()); Transaction* txn = db->BeginTransaction(write_options, txn_options); s = txn->SetName("xid"); @@ -1340,7 +1340,7 @@ TEST_P(TransactionTest, PersistentTwoPhaseTransactionTest) { s = ReOpenNoDelete(); ASSERT_OK(s); assert(db != nullptr); - db_impl = reinterpret_cast(db->GetRootDB()); + db_impl = static_cast_with_check(db->GetRootDB()); // find trans in list of prepared transactions std::vector prepared_trans; @@ -1685,7 +1685,7 @@ TEST_P(TransactionTest, TwoPhaseDoubleRecoveryTest) { } TEST_P(TransactionTest, TwoPhaseLogRollingTest) { - DBImpl* db_impl = reinterpret_cast(db->GetRootDB()); + DBImpl* db_impl = static_cast_with_check(db->GetRootDB()); Status s; std::string v; @@ -1837,7 +1837,7 @@ TEST_P(TransactionTest, TwoPhaseLogRollingTest) { } TEST_P(TransactionTest, TwoPhaseLogRollingTest2) { - DBImpl* db_impl = reinterpret_cast(db->GetRootDB()); + DBImpl* db_impl = static_cast_with_check(db->GetRootDB()); Status s; ColumnFamilyHandle *cfa, *cfb; @@ -1852,8 +1852,8 @@ TEST_P(TransactionTest, TwoPhaseLogRollingTest2) { wopts.disableWAL = false; wopts.sync = true; - auto cfh_a = reinterpret_cast(cfa); - auto cfh_b = reinterpret_cast(cfb); + auto cfh_a = static_cast_with_check(cfa); + auto cfh_b = static_cast_with_check(cfb); TransactionOptions topts1; Transaction* txn1 = db->BeginTransaction(wopts, topts1); @@ -1982,7 +1982,7 @@ TEST_P(TransactionTest, TwoPhaseLogRollingTest2) { * hidden behind improperly summed sequence ids */ TEST_P(TransactionTest, TwoPhaseOutOfOrderDelete) { - DBImpl* db_impl = reinterpret_cast(db->GetRootDB()); + DBImpl* db_impl = static_cast_with_check(db->GetRootDB()); WriteOptions wal_on, wal_off; wal_on.sync = true; wal_on.disableWAL = false; @@ -2301,7 +2301,7 @@ TEST_P(TransactionTest, FlushTest2) { TransactionOptions txn_options; string value; - DBImpl* db_impl = reinterpret_cast(db->GetRootDB()); + DBImpl* db_impl = static_cast_with_check(db->GetRootDB()); db->Put(write_options, Slice("foo"), Slice("bar")); db->Put(write_options, Slice("foo2"), Slice("bar2")); @@ -5187,13 +5187,13 @@ TEST_P(TransactionTest, ToggleAutoCompactionTest) { &handles, &db); ASSERT_OK(s); - auto cfh_default = reinterpret_cast(handles[0]); + auto cfh_default = static_cast_with_check(handles[0]); auto opt_default = *cfh_default->cfd()->GetLatestMutableCFOptions(); - auto cfh_a = reinterpret_cast(handles[1]); + auto cfh_a = static_cast_with_check(handles[1]); auto opt_a = *cfh_a->cfd()->GetLatestMutableCFOptions(); - auto cfh_b = reinterpret_cast(handles[2]); + auto cfh_b = static_cast_with_check(handles[2]); auto opt_b = *cfh_b->cfd()->GetLatestMutableCFOptions(); ASSERT_EQ(opt_default.disable_auto_compactions, false); @@ -5441,7 +5441,7 @@ TEST_P(TransactionStressTest, SeqAdvanceTest) { }; const size_t max_n = static_cast(1) << NUM_BRANCHES; for (size_t n = 0; n < max_n; n++) { - DBImpl* db_impl = reinterpret_cast(db->GetRootDB()); + DBImpl* db_impl = static_cast_with_check(db->GetRootDB()); size_t branch = 0; auto seq = db_impl->GetLatestSequenceNumber(); exp_seq = seq; @@ -5457,7 +5457,7 @@ TEST_P(TransactionStressTest, SeqAdvanceTest) { if (!short_test && branch_do(n, &branch)) { ASSERT_OK(db_impl->FlushWAL(true)); ASSERT_OK(ReOpenNoDelete()); - db_impl = reinterpret_cast(db->GetRootDB()); + db_impl = static_cast_with_check(db->GetRootDB()); seq = db_impl->GetLatestSequenceNumber(); ASSERT_EQ(exp_seq, seq); } @@ -5479,7 +5479,7 @@ TEST_P(TransactionStressTest, SeqAdvanceTest) { if (!short_test && branch_do(n, &branch)) { ASSERT_OK(db_impl->FlushWAL(true)); ASSERT_OK(ReOpenNoDelete()); - db_impl = reinterpret_cast(db->GetRootDB()); + db_impl = static_cast_with_check(db->GetRootDB()); seq = db_impl->GetLatestSequenceNumber(); ASSERT_EQ(exp_seq, seq); } @@ -5496,7 +5496,7 @@ TEST_P(TransactionStressTest, SeqAdvanceTest) { if (!short_test && branch_do(n, &branch)) { ASSERT_OK(db_impl->FlushWAL(true)); ASSERT_OK(ReOpenNoDelete()); - db_impl = reinterpret_cast(db->GetRootDB()); + db_impl = static_cast_with_check(db->GetRootDB()); seq = db_impl->GetLatestSequenceNumber(); ASSERT_EQ(exp_seq, seq); } @@ -5514,7 +5514,7 @@ TEST_P(TransactionStressTest, SeqAdvanceTest) { if (!short_test && branch_do(n, &branch)) { ASSERT_OK(db_impl->FlushWAL(true)); ASSERT_OK(ReOpenNoDelete()); - db_impl = reinterpret_cast(db->GetRootDB()); + db_impl = static_cast_with_check(db->GetRootDB()); seq = db_impl->GetLatestSequenceNumber(); ASSERT_EQ(exp_seq, seq); } @@ -5531,7 +5531,7 @@ TEST_P(TransactionStressTest, SeqAdvanceTest) { if (!short_test && branch_do(n, &branch)) { ASSERT_OK(db_impl->FlushWAL(true)); ASSERT_OK(ReOpenNoDelete()); - db_impl = reinterpret_cast(db->GetRootDB()); + db_impl = static_cast_with_check(db->GetRootDB()); seq = db_impl->GetLatestSequenceNumber(); ASSERT_EQ(exp_seq, seq); } @@ -5917,7 +5917,7 @@ TEST_P(TransactionTest, DuplicateKeys) { // This will check the asserts inside recovery code db->FlushWAL(true); // Flush only cf 1 - reinterpret_cast(db->GetRootDB()) + static_cast_with_check(db->GetRootDB()) ->TEST_FlushMemTable(true, false, handles[1]); reinterpret_cast(db)->TEST_Crash(); ASSERT_OK(ReOpenNoDelete(cfds, &handles)); @@ -5955,7 +5955,7 @@ TEST_P(TransactionTest, DuplicateKeys) { // This will check the asserts inside recovery code ASSERT_OK(db->FlushWAL(true)); // Flush only cf 1 - reinterpret_cast(db->GetRootDB()) + static_cast_with_check(db->GetRootDB()) ->TEST_FlushMemTable(true, false, handles[1]); reinterpret_cast(db)->TEST_Crash(); ASSERT_OK(ReOpenNoDelete(cfds, &handles)); @@ -5988,7 +5988,7 @@ TEST_P(TransactionTest, DuplicateKeys) { // This will check the asserts inside recovery code ASSERT_OK(db->FlushWAL(true)); // Flush only cf 1 - reinterpret_cast(db->GetRootDB()) + static_cast_with_check(db->GetRootDB()) ->TEST_FlushMemTable(true, false, handles[1]); reinterpret_cast(db)->TEST_Crash(); ASSERT_OK(ReOpenNoDelete(cfds, &handles)); @@ -6015,7 +6015,7 @@ TEST_P(TransactionTest, DuplicateKeys) { // This will check the asserts inside recovery code ASSERT_OK(db->FlushWAL(true)); // Flush only cf 1 - reinterpret_cast(db->GetRootDB()) + static_cast_with_check(db->GetRootDB()) ->TEST_FlushMemTable(true, false, handles[1]); reinterpret_cast(db)->TEST_Crash(); ASSERT_OK(ReOpenNoDelete(cfds, &handles)); @@ -6042,7 +6042,7 @@ TEST_P(TransactionTest, DuplicateKeys) { // This will check the asserts inside recovery code ASSERT_OK(db->FlushWAL(true)); // Flush only cf 1 - reinterpret_cast(db->GetRootDB()) + static_cast_with_check(db->GetRootDB()) ->TEST_FlushMemTable(true, false, handles[1]); reinterpret_cast(db)->TEST_Crash(); ASSERT_OK(ReOpenNoDelete(cfds, &handles)); @@ -6157,7 +6157,7 @@ TEST_P(TransactionTest, DoubleCrashInRecovery) { ASSERT_OK(db->Put(write_options, "foo4", "bar4")); db->FlushWAL(true); - DBImpl* db_impl = reinterpret_cast(db->GetRootDB()); + DBImpl* db_impl = static_cast_with_check(db->GetRootDB()); uint64_t wal_file_id = db_impl->TEST_LogfileNumber(); std::string fname = LogFileName(dbname, wal_file_id); reinterpret_cast(db)->TEST_Crash(); diff --git a/utilities/transactions/transaction_test.h b/utilities/transactions/transaction_test.h index 2e533d379..4fc0422c7 100644 --- a/utilities/transactions/transaction_test.h +++ b/utilities/transactions/transaction_test.h @@ -437,7 +437,7 @@ class TransactionTestBase : public ::testing::Test { if (txn_db_options.write_policy == WRITE_COMMITTED) { options.unordered_write = false; } - auto db_impl = reinterpret_cast(db->GetRootDB()); + auto db_impl = static_cast_with_check(db->GetRootDB()); // Before upgrade/downgrade the WAL must be emptied if (empty_wal) { db_impl->TEST_FlushMemTable(); @@ -453,7 +453,7 @@ class TransactionTestBase : public ::testing::Test { ASSERT_TRUE(s.IsNotSupported()); return; } - db_impl = reinterpret_cast(db->GetRootDB()); + db_impl = static_cast_with_check(db->GetRootDB()); // Check that WAL is empty VectorLogPtr log_files; db_impl->GetSortedWalFiles(log_files); diff --git a/utilities/transactions/transaction_util.cc b/utilities/transactions/transaction_util.cc index 23532ae42..45b4cb89c 100644 --- a/utilities/transactions/transaction_util.cc +++ b/utilities/transactions/transaction_util.cc @@ -14,6 +14,7 @@ #include "db/db_impl/db_impl.h" #include "rocksdb/status.h" #include "rocksdb/utilities/write_batch_with_index.h" +#include "util/cast_util.h" #include "util/string_util.h" namespace ROCKSDB_NAMESPACE { @@ -24,7 +25,7 @@ Status TransactionUtil::CheckKeyForConflicts( SequenceNumber min_uncommitted) { Status result; - auto cfh = reinterpret_cast(column_family); + auto cfh = static_cast_with_check(column_family); auto cfd = cfh->cfd(); SuperVersion* sv = db_impl->GetAndRefSuperVersion(cfd); diff --git a/utilities/transactions/write_prepared_transaction_test.cc b/utilities/transactions/write_prepared_transaction_test.cc index bf7a3cae6..8adca08e3 100644 --- a/utilities/transactions/write_prepared_transaction_test.cc +++ b/utilities/transactions/write_prepared_transaction_test.cc @@ -1676,7 +1676,7 @@ TEST_P(SeqAdvanceConcurrentTest, SeqAdvanceConcurrent) { if (n % 1000 == 0) { printf("Tested %" ROCKSDB_PRIszt " cases so far\n", n); } - DBImpl* db_impl = reinterpret_cast(db->GetRootDB()); + DBImpl* db_impl = static_cast_with_check(db->GetRootDB()); auto seq = db_impl->TEST_GetLastVisibleSequence(); with_empty_commits = 0; exp_seq = seq; @@ -1768,7 +1768,7 @@ TEST_P(SeqAdvanceConcurrentTest, SeqAdvanceConcurrent) { db_impl->FlushWAL(true); ReOpenNoDelete(); assert(db != nullptr); - db_impl = reinterpret_cast(db->GetRootDB()); + db_impl = static_cast_with_check(db->GetRootDB()); seq = db_impl->TEST_GetLastVisibleSequence(); ASSERT_LE(exp_seq, seq + with_empty_commits); @@ -1781,7 +1781,7 @@ TEST_P(SeqAdvanceConcurrentTest, SeqAdvanceConcurrent) { db_impl->FlushWAL(true); ReOpenNoDelete(); assert(db != nullptr); - db_impl = reinterpret_cast(db->GetRootDB()); + db_impl = static_cast_with_check(db->GetRootDB()); seq = db_impl->GetLatestSequenceNumber(); ASSERT_LE(exp_seq, seq + with_empty_commits); } @@ -1947,7 +1947,7 @@ TEST_P(WritePreparedTransactionTest, IsInSnapshotEmptyMap) { delete txn; } dynamic_cast(db)->TEST_Crash(); - auto db_impl = reinterpret_cast(db->GetRootDB()); + auto db_impl = static_cast_with_check(db->GetRootDB()); db_impl->FlushWAL(true); ReOpenNoDelete(); WritePreparedTxnDB* wp_db = dynamic_cast(db); @@ -2285,7 +2285,7 @@ TEST_P(WritePreparedTransactionTest, Rollback) { if (crash) { delete txn; - auto db_impl = reinterpret_cast(db->GetRootDB()); + auto db_impl = static_cast_with_check(db->GetRootDB()); db_impl->FlushWAL(true); dynamic_cast(db)->TEST_Crash(); ReOpenNoDelete(); @@ -2344,7 +2344,7 @@ TEST_P(WritePreparedTransactionTest, DisableGCDuringRecovery) { } std::reverse(std::begin(versions), std::end(versions)); VerifyInternalKeys(versions); - DBImpl* db_impl = reinterpret_cast(db->GetRootDB()); + DBImpl* db_impl = static_cast_with_check(db->GetRootDB()); db_impl->FlushWAL(true); // Use small buffer to ensure memtable flush during recovery options.write_buffer_size = 1024; @@ -2376,7 +2376,7 @@ TEST_P(WritePreparedTransactionTest, SequenceNumberZero) { TEST_P(WritePreparedTransactionTest, CompactionShouldKeepUncommittedKeys) { options.disable_auto_compactions = true; ReOpen(); - DBImpl* db_impl = reinterpret_cast(db->GetRootDB()); + DBImpl* db_impl = static_cast_with_check(db->GetRootDB()); // Snapshots to avoid keys get evicted. std::vector snapshots; // Keep track of expected sequence number. @@ -2475,7 +2475,7 @@ TEST_P(WritePreparedTransactionTest, CompactionShouldKeepSnapshotVisibleKeys) { ASSERT_OK(txn1->Prepare()); ASSERT_EQ(++expected_seq, db->GetLatestSequenceNumber()); ASSERT_OK(txn1->Commit()); - DBImpl* db_impl = reinterpret_cast(db->GetRootDB()); + DBImpl* db_impl = static_cast_with_check(db->GetRootDB()); ASSERT_EQ(++expected_seq, db_impl->TEST_GetLastVisibleSequence()); delete txn1; // Take a snapshots to avoid keys get evicted before compaction. @@ -2881,7 +2881,7 @@ TEST_P(WritePreparedTransactionTest, ASSERT_EQ(++expected_seq, db->GetLatestSequenceNumber()); SequenceNumber seq1 = expected_seq; ASSERT_OK(db->Put(WriteOptions(), "key2", "value2")); - DBImpl* db_impl = reinterpret_cast(db->GetRootDB()); + DBImpl* db_impl = static_cast_with_check(db->GetRootDB()); expected_seq++; // one for data if (options.two_write_queues) { expected_seq++; // one for commit @@ -3019,7 +3019,7 @@ TEST_P(WritePreparedTransactionTest, NonAtomicCommitOfDelayedPrepared) { UpdateTransactionDBOptions(snapshot_cache_bits, commit_cache_bits); ReOpen(); WritePreparedTxnDB* wp_db = dynamic_cast(db); - DBImpl* db_impl = reinterpret_cast(db->GetRootDB()); + DBImpl* db_impl = static_cast_with_check(db->GetRootDB()); // Fill up the commit cache std::string init_value("value1"); for (int i = 0; i < 10; i++) { diff --git a/utilities/transactions/write_prepared_txn_db.cc b/utilities/transactions/write_prepared_txn_db.cc index 45a192004..e6e42d7e1 100644 --- a/utilities/transactions/write_prepared_txn_db.cc +++ b/utilities/transactions/write_prepared_txn_db.cc @@ -339,7 +339,8 @@ Iterator* WritePreparedTxnDB::NewIterator(const ReadOptions& options, own_snapshot = std::make_shared(db_impl_, snapshot); } assert(snapshot_seq != kMaxSequenceNumber); - auto* cfd = reinterpret_cast(column_family)->cfd(); + auto* cfd = + static_cast_with_check(column_family)->cfd(); auto* state = new IteratorState(this, snapshot_seq, own_snapshot, min_uncommitted); auto* db_iter = @@ -375,7 +376,8 @@ Status WritePreparedTxnDB::NewIterators( iterators->clear(); iterators->reserve(column_families.size()); for (auto* column_family : column_families) { - auto* cfd = reinterpret_cast(column_family)->cfd(); + auto* cfd = + static_cast_with_check(column_family)->cfd(); auto* state = new IteratorState(this, snapshot_seq, own_snapshot, min_uncommitted); auto* db_iter = diff --git a/utilities/transactions/write_unprepared_txn_db.cc b/utilities/transactions/write_unprepared_txn_db.cc index ee3db6a00..dbef7d69e 100644 --- a/utilities/transactions/write_unprepared_txn_db.cc +++ b/utilities/transactions/write_unprepared_txn_db.cc @@ -452,7 +452,8 @@ Iterator* WriteUnpreparedTxnDB::NewIterator(const ReadOptions& options, min_uncommitted = static_cast_with_check(snapshot)->min_uncommitted_; - auto* cfd = reinterpret_cast(column_family)->cfd(); + auto* cfd = + static_cast_with_check(column_family)->cfd(); auto* state = new IteratorState(this, snapshot_seq, own_snapshot, min_uncommitted, txn); auto* db_iter = diff --git a/utilities/write_batch_with_index/write_batch_with_index.cc b/utilities/write_batch_with_index/write_batch_with_index.cc index b64c470af..1e92d5814 100644 --- a/utilities/write_batch_with_index/write_batch_with_index.cc +++ b/utilities/write_batch_with_index/write_batch_with_index.cc @@ -893,7 +893,7 @@ Status WriteBatchWithIndex::GetFromBatchAndDB( if (s.ok() || s.IsNotFound()) { // DB Get Succeeded if (result == WriteBatchWithIndexInternal::Result::kMergeInProgress) { // Merge result from DB with merges in Batch - auto cfh = reinterpret_cast(column_family); + auto cfh = static_cast_with_check(column_family); const MergeOperator* merge_operator = cfh->cfd()->ioptions()->merge_operator; Statistics* statistics = immuable_db_options.statistics.get(); @@ -997,7 +997,7 @@ void WriteBatchWithIndex::MultiGetFromBatchAndDB( &sorted_keys); ColumnFamilyHandleImpl* cfh = - reinterpret_cast(column_family); + static_cast_with_check(column_family); const MergeOperator* merge_operator = cfh->cfd()->ioptions()->merge_operator; for (auto iter = key_context.begin(); iter != key_context.end(); ++iter) { KeyContext& key = *iter; diff --git a/utilities/write_batch_with_index/write_batch_with_index_internal.cc b/utilities/write_batch_with_index/write_batch_with_index_internal.cc index 8c1222f21..0d017ae19 100644 --- a/utilities/write_batch_with_index/write_batch_with_index_internal.cc +++ b/utilities/write_batch_with_index/write_batch_with_index_internal.cc @@ -13,6 +13,7 @@ #include "rocksdb/comparator.h" #include "rocksdb/db.h" #include "rocksdb/utilities/write_batch_with_index.h" +#include "util/cast_util.h" #include "util/coding.h" #include "util/string_util.h" @@ -249,7 +250,8 @@ WriteBatchWithIndexInternal::Result WriteBatchWithIndexInternal::GetFromBatch( const MergeOperator* merge_operator; if (column_family != nullptr) { - auto cfh = reinterpret_cast(column_family); + auto cfh = + static_cast_with_check(column_family); merge_operator = cfh->cfd()->ioptions()->merge_operator; } else { *s = Status::InvalidArgument("Must provide a column_family");