From d15fbae4495e3ab02bb22f7e2ce786755b29047d Mon Sep 17 00:00:00 2001 From: Jay Zhuang Date: Thu, 13 May 2021 14:28:50 -0700 Subject: [PATCH] Refactor Option obj address from char* to void* (#8295) Summary: And replace `reinterpret_cast` with `static_cast` or no cast. Pull Request resolved: https://github.com/facebook/rocksdb/pull/8295 Test Plan: `make check` Reviewed By: mrambacher Differential Revision: D28420303 Pulled By: jay-zhuang fbshipit-source-id: 645be123a0df624dc2bea37cd54a35403fc494fa --- cache/cache.cc | 6 +- db/compaction/compaction_job.cc | 35 ++--- include/rocksdb/utilities/options_type.h | 93 ++++++----- options/cf_options.cc | 34 ++--- options/db_options.cc | 9 +- options/options_helper.cc | 144 +++++++++--------- options/options_test.cc | 19 ++- .../block_based/block_based_table_factory.cc | 31 ++-- 8 files changed, 177 insertions(+), 194 deletions(-) diff --git a/cache/cache.cc b/cache/cache.cc index 78897c416..4eef1c2d6 100644 --- a/cache/cache.cc +++ b/cache/cache.cc @@ -44,9 +44,9 @@ Status Cache::CreateFromString(const ConfigOptions& config_options, } else { #ifndef ROCKSDB_LITE LRUCacheOptions cache_opts; - status = OptionTypeInfo::ParseStruct( - config_options, "", &lru_cache_options_type_info, "", value, - reinterpret_cast(&cache_opts)); + status = OptionTypeInfo::ParseStruct(config_options, "", + &lru_cache_options_type_info, "", + value, &cache_opts); if (status.ok()) { cache = NewLRUCache(cache_opts); } diff --git a/db/compaction/compaction_job.cc b/db/compaction/compaction_job.cc index 97b47a6f5..a55484bf0 100644 --- a/db/compaction/compaction_job.cc +++ b/db/compaction/compaction_job.cc @@ -2102,27 +2102,24 @@ static std::unordered_map cfd_type_info = { {offset_of(&ColumnFamilyDescriptor::options), OptionType::kConfigurable, OptionVerificationType::kNormal, OptionTypeFlags::kNone, [](const ConfigOptions& opts, const std::string& /*name*/, - const std::string& value, char* addr) { - auto cf_options = reinterpret_cast(addr); + const std::string& value, void* addr) { + auto cf_options = static_cast(addr); return GetColumnFamilyOptionsFromString(opts, ColumnFamilyOptions(), value, cf_options); }, [](const ConfigOptions& opts, const std::string& /*name*/, - const char* addr, std::string* value) { - const auto cf_options = - reinterpret_cast(addr); + const void* addr, std::string* value) { + const auto cf_options = static_cast(addr); std::string result; auto status = GetStringFromColumnFamilyOptions(opts, *cf_options, &result); *value = "{" + result + "}"; return status; }, - [](const ConfigOptions& opts, const std::string& name, const char* addr1, - const char* addr2, std::string* mismatch) { - const auto this_one = - reinterpret_cast(addr1); - const auto that_one = - reinterpret_cast(addr2); + [](const ConfigOptions& opts, const std::string& name, const void* addr1, + const void* addr2, std::string* mismatch) { + const auto this_one = static_cast(addr1); + const auto that_one = static_cast(addr2); auto this_conf = CFOptionsAsConfigurable(*this_one); auto that_conf = CFOptionsAsConfigurable(*that_one); std::string mismatch_opt; @@ -2145,22 +2142,22 @@ static std::unordered_map cs_input_type_info = { {offset_of(&CompactionServiceInput::db_options), OptionType::kConfigurable, OptionVerificationType::kNormal, OptionTypeFlags::kNone, [](const ConfigOptions& opts, const std::string& /*name*/, - const std::string& value, char* addr) { - auto options = reinterpret_cast(addr); + const std::string& value, void* addr) { + auto options = static_cast(addr); return GetDBOptionsFromString(opts, DBOptions(), value, options); }, [](const ConfigOptions& opts, const std::string& /*name*/, - const char* addr, std::string* value) { - const auto options = reinterpret_cast(addr); + const void* addr, std::string* value) { + const auto options = static_cast(addr); std::string result; auto status = GetStringFromDBOptions(opts, *options, &result); *value = "{" + result + "}"; return status; }, - [](const ConfigOptions& opts, const std::string& name, const char* addr1, - const char* addr2, std::string* mismatch) { - const auto this_one = reinterpret_cast(addr1); - const auto that_one = reinterpret_cast(addr2); + [](const ConfigOptions& opts, const std::string& name, const void* addr1, + const void* addr2, std::string* mismatch) { + const auto this_one = static_cast(addr1); + const auto that_one = static_cast(addr2); auto this_conf = DBOptionsAsConfigurable(*this_one); auto that_conf = DBOptionsAsConfigurable(*that_one); std::string mismatch_opt; diff --git a/include/rocksdb/utilities/options_type.h b/include/rocksdb/utilities/options_type.h index cdab9df3f..96e0ef8cf 100644 --- a/include/rocksdb/utilities/options_type.h +++ b/include/rocksdb/utilities/options_type.h @@ -177,7 +177,7 @@ bool VectorsAreEqual(const ConfigOptions& config_options, // @param addr Pointer to the object using ParseFunc = std::function; + const std::string& /*value*/, void* /*addr*/)>; // Function for converting an option "addr" into its string representation. // On success, Status::OK is returned and value is the serialized form. @@ -188,7 +188,7 @@ using ParseFunc = std::function; + const void* /*addr*/, std::string* /*value*/)>; // Function for comparing two option values // If they are not equal, updates "mismatch" with the name of the bad option @@ -200,7 +200,7 @@ using SerializeFunc = std::function; + const void* /*addr1*/, const void* /*addr2*/, std::string* mismatch)>; // A struct for storing constant option information such as option name, // option type, and offset. @@ -273,10 +273,10 @@ class OptionTypeInfo { // @return OK if the value is found in the map // @return InvalidArgument if the value is not found in the map [map](const ConfigOptions&, const std::string& name, - const std::string& value, char* addr) { + const std::string& value, void* addr) { if (map == nullptr) { return Status::NotSupported("No enum mapping ", name); - } else if (ParseEnum(*map, value, reinterpret_cast(addr))) { + } else if (ParseEnum(*map, value, static_cast(addr))) { return Status::OK(); } else { return Status::InvalidArgument("No mapping for enum ", name); @@ -287,11 +287,11 @@ class OptionTypeInfo { // value is updated to the corresponding string value in the map. // @return OK if the enum is found in the map // @return InvalidArgument if the enum is not found in the map - [map](const ConfigOptions&, const std::string& name, const char* addr, + [map](const ConfigOptions&, const std::string& name, const void* addr, std::string* value) { if (map == nullptr) { return Status::NotSupported("No enum mapping ", name); - } else if (SerializeEnum(*map, (*reinterpret_cast(addr)), + } else if (SerializeEnum(*map, (*static_cast(addr)), value)) { return Status::OK(); } else { @@ -300,10 +300,10 @@ class OptionTypeInfo { }, // Casts addr1 and addr2 to the enum type and returns true if // they are equal, false otherwise. - [](const ConfigOptions&, const std::string&, const char* addr1, - const char* addr2, std::string*) { - return (*reinterpret_cast(addr1) == - *reinterpret_cast(addr2)); + [](const ConfigOptions&, const std::string&, const void* addr1, + const void* addr2, std::string*) { + return (*static_cast(addr1) == + *static_cast(addr2)); }); } // End OptionTypeInfo::Enum @@ -338,20 +338,20 @@ class OptionTypeInfo { // Parses the struct and updates the fields at addr [struct_name, struct_map](const ConfigOptions& opts, const std::string& name, - const std::string& value, char* addr) { + const std::string& value, void* addr) { return ParseStruct(opts, struct_name, struct_map, name, value, addr); }, // Serializes the struct options into value [struct_name, struct_map](const ConfigOptions& opts, - const std::string& name, const char* addr, + const std::string& name, const void* addr, std::string* value) { return SerializeStruct(opts, struct_name, struct_map, name, addr, value); }, // Compares the struct fields of addr1 and addr2 for equality [struct_name, struct_map](const ConfigOptions& opts, - const std::string& name, const char* addr1, - const char* addr2, std::string* mismatch) { + const std::string& name, const void* addr1, + const void* addr2, std::string* mismatch) { return StructsAreEqual(opts, struct_name, struct_map, name, addr1, addr2, mismatch); }); @@ -364,14 +364,14 @@ class OptionTypeInfo { return OptionTypeInfo( offset, OptionType::kStruct, verification, flags, parse_func, [struct_name, struct_map](const ConfigOptions& opts, - const std::string& name, const char* addr, + const std::string& name, const void* addr, std::string* value) { return SerializeStruct(opts, struct_name, struct_map, name, addr, value); }, [struct_name, struct_map](const ConfigOptions& opts, - const std::string& name, const char* addr1, - const char* addr2, std::string* mismatch) { + const std::string& name, const void* addr1, + const void* addr2, std::string* mismatch) { return StructsAreEqual(opts, struct_name, struct_map, name, addr1, addr2, mismatch); }); @@ -387,23 +387,23 @@ class OptionTypeInfo { _offset, OptionType::kVector, _verification, _flags, [elem_info, separator](const ConfigOptions& opts, const std::string& name, - const std::string& value, char* addr) { - auto result = reinterpret_cast*>(addr); + const std::string& value, void* addr) { + auto result = static_cast*>(addr); return ParseVector(opts, elem_info, separator, name, value, result); }, [elem_info, separator](const ConfigOptions& opts, - const std::string& name, const char* addr, + const std::string& name, const void* addr, std::string* value) { - const auto& vec = *(reinterpret_cast*>(addr)); + const auto& vec = *(static_cast*>(addr)); return SerializeVector(opts, elem_info, separator, name, vec, value); }, [elem_info](const ConfigOptions& opts, const std::string& name, - const char* addr1, const char* addr2, + const void* addr1, const void* addr2, std::string* mismatch) { - const auto& vec1 = *(reinterpret_cast*>(addr1)); - const auto& vec2 = *(reinterpret_cast*>(addr2)); + const auto& vec1 = *(static_cast*>(addr1)); + const auto& vec2 = *(static_cast*>(addr2)); return VectorsAreEqual(opts, elem_info, name, vec1, vec2, mismatch); }); @@ -435,8 +435,8 @@ class OptionTypeInfo { offset, OptionType::kCustomizable, ovt, flags | OptionTypeFlags::kShared, [](const ConfigOptions& opts, const std::string&, - const std::string& value, char* addr) { - auto* shared = reinterpret_cast*>(addr); + const std::string& value, void* addr) { + auto* shared = static_cast*>(addr); return T::CreateFromString(opts, value, shared); }, serialize_func, equals_func); @@ -468,8 +468,8 @@ class OptionTypeInfo { offset, OptionType::kCustomizable, ovt, flags | OptionTypeFlags::kUnique, [](const ConfigOptions& opts, const std::string&, - const std::string& value, char* addr) { - auto* unique = reinterpret_cast*>(addr); + const std::string& value, void* addr) { + auto* unique = static_cast*>(addr); return T::CreateFromString(opts, value, unique); }, serialize_func, equals_func); @@ -499,8 +499,8 @@ class OptionTypeInfo { offset, OptionType::kCustomizable, ovt, flags | OptionTypeFlags::kRawPointer, [](const ConfigOptions& opts, const std::string&, - const std::string& value, char* addr) { - auto** pointer = reinterpret_cast(addr); + const std::string& value, void* addr) { + auto** pointer = static_cast(addr); return T::CreateFromString(opts, value, pointer); }, serialize_func, equals_func); @@ -596,20 +596,20 @@ class OptionTypeInfo { if (base_addr == nullptr) { return nullptr; } - const auto opt_addr = reinterpret_cast(base_addr) + offset_; + const void* opt_addr = static_cast(base_addr) + offset_; if (IsUniquePtr()) { const std::unique_ptr* ptr = - reinterpret_cast*>(opt_addr); + static_cast*>(opt_addr); return ptr->get(); } else if (IsSharedPtr()) { const std::shared_ptr* ptr = - reinterpret_cast*>(opt_addr); + static_cast*>(opt_addr); return ptr->get(); } else if (IsRawPtr()) { - const T* const* ptr = reinterpret_cast(opt_addr); + const T* const* ptr = static_cast(opt_addr); return *ptr; } else { - return reinterpret_cast(opt_addr); + return static_cast(opt_addr); } } @@ -620,18 +620,18 @@ class OptionTypeInfo { if (base_addr == nullptr) { return nullptr; } - auto opt_addr = reinterpret_cast(base_addr) + offset_; + void* opt_addr = static_cast(base_addr) + offset_; if (IsUniquePtr()) { - std::unique_ptr* ptr = reinterpret_cast*>(opt_addr); + std::unique_ptr* ptr = static_cast*>(opt_addr); return ptr->get(); } else if (IsSharedPtr()) { - std::shared_ptr* ptr = reinterpret_cast*>(opt_addr); + std::shared_ptr* ptr = static_cast*>(opt_addr); return ptr->get(); } else if (IsRawPtr()) { - T** ptr = reinterpret_cast(opt_addr); + T** ptr = static_cast(opt_addr); return *ptr; } else { - return reinterpret_cast(opt_addr); + return static_cast(opt_addr); } } @@ -704,7 +704,7 @@ class OptionTypeInfo { static Status ParseStruct( const ConfigOptions& config_options, const std::string& struct_name, const std::unordered_map* map, - const std::string& opt_name, const std::string& value, char* opt_addr); + const std::string& opt_name, const std::string& value, void* opt_addr); // Serializes the values from opt_addr using the rules in type_map. // Returns the serialized form in result. @@ -721,7 +721,7 @@ class OptionTypeInfo { static Status SerializeStruct( const ConfigOptions& config_options, const std::string& struct_name, const std::unordered_map* map, - const std::string& opt_name, const char* opt_addr, std::string* value); + const std::string& opt_name, const void* opt_addr, std::string* value); // Compares the values in this_addr and that_addr using the rules in type_map. // If the values are equal, returns true @@ -740,8 +740,8 @@ class OptionTypeInfo { static bool StructsAreEqual( const ConfigOptions& config_options, const std::string& struct_name, const std::unordered_map* map, - const std::string& opt_name, const char* this_offset, - const char* that_offset, std::string* mismatch); + const std::string& opt_name, const void* this_offset, + const void* that_offset, std::string* mismatch); // Finds the entry for the opt_name in the opt_map, returning // nullptr if not found. @@ -831,8 +831,7 @@ Status ParseVector(const ConfigOptions& config_options, status = OptionTypeInfo::NextToken(value, separator, start, &end, &token); if (status.ok()) { T elem; - status = - elem_info.Parse(copy, name, token, reinterpret_cast(&elem)); + status = elem_info.Parse(copy, name, token, &elem); if (status.ok()) { result->emplace_back(elem); } else if (config_options.ignore_unsupported_options && diff --git a/options/cf_options.cc b/options/cf_options.cc index 13e2d5520..005a90c85 100644 --- a/options/cf_options.cc +++ b/options/cf_options.cc @@ -364,7 +364,7 @@ static std::unordered_map offsetof(struct MutableCFOptions, compaction_options_fifo), OptionVerificationType::kNormal, OptionTypeFlags::kMutable, [](const ConfigOptions& opts, const std::string& name, - const std::string& value, char* addr) { + const std::string& value, void* addr) { // This is to handle backward compatibility, where // compaction_options_fifo could be assigned a single scalar // value, say, like "23", which would be assigned to @@ -372,7 +372,7 @@ static std::unordered_map if (name == "compaction_options_fifo" && value.find("=") == std::string::npos) { // Old format. Parse just a single uint64_t value. - auto options = reinterpret_cast(addr); + auto options = static_cast(addr); options->max_table_files_size = ParseUint64(value); return Status::OK(); } else { @@ -433,13 +433,12 @@ static std::unordered_map OptionVerificationType::kNormal, (OptionTypeFlags::kMutable | OptionTypeFlags::kCompareNever), [](const ConfigOptions& opts, const std::string& name, - const std::string& value, char* addr) { + const std::string& value, void* addr) { // This is to handle backward compatibility, where // compression_options was a ":" separated list. if (name == kOptNameCompOpts && value.find("=") == std::string::npos) { - auto* compression = - reinterpret_cast(addr); + auto* compression = static_cast(addr); return ParseCompressionOptions(value, name, *compression); } else { return OptionTypeInfo::ParseStruct( @@ -454,13 +453,12 @@ static std::unordered_map OptionVerificationType::kNormal, (OptionTypeFlags::kMutable | OptionTypeFlags::kCompareNever), [](const ConfigOptions& opts, const std::string& name, - const std::string& value, char* addr) { + const std::string& value, void* addr) { // This is to handle backward compatibility, where // compression_options was a ":" separated list. if (name == kOptNameBMCompOpts && value.find("=") == std::string::npos) { - auto* compression = - reinterpret_cast(addr); + auto* compression = static_cast(addr); return ParseCompressionOptions(value, name, *compression); } else { return OptionTypeInfo::ParseStruct( @@ -542,8 +540,8 @@ static std::unordered_map OptionTypeFlags::kCompareLoose, // Parses the string and sets the corresponding comparator [](const ConfigOptions& opts, const std::string& /*name*/, - const std::string& value, char* addr) { - auto old_comparator = reinterpret_cast(addr); + const std::string& value, void* addr) { + auto old_comparator = static_cast(addr); const Comparator* new_comparator = *old_comparator; Status status = opts.registry->NewStaticObject(value, &new_comparator); @@ -568,12 +566,12 @@ static std::unordered_map OptionTypeFlags::kNone, // Parses the value string and updates the memtable_factory [](const ConfigOptions& /*opts*/, const std::string& /*name*/, - const std::string& value, char* addr) { + const std::string& value, void* addr) { std::unique_ptr new_mem_factory; Status s = GetMemTableRepFactoryFromString(value, &new_mem_factory); if (s.ok()) { auto memtable_factory = - reinterpret_cast*>(addr); + static_cast*>(addr); memtable_factory->reset(new_mem_factory.release()); } return s; @@ -590,10 +588,10 @@ static std::unordered_map OptionTypeFlags::kShared | OptionTypeFlags::kCompareLoose, // Parses the input value and creates a BlockBasedTableFactory [](const ConfigOptions& opts, const std::string& name, - const std::string& value, char* addr) { + const std::string& value, void* addr) { BlockBasedTableOptions* old_opts = nullptr; auto table_factory = - reinterpret_cast*>(addr); + static_cast*>(addr); if (table_factory->get() != nullptr) { old_opts = table_factory->get()->GetOptions(); @@ -622,10 +620,10 @@ static std::unordered_map OptionTypeFlags::kShared | OptionTypeFlags::kCompareLoose, // Parses the input value and creates a PlainTableFactory [](const ConfigOptions& opts, const std::string& name, - const std::string& value, char* addr) { + const std::string& value, void* addr) { PlainTableOptions* old_opts = nullptr; auto table_factory = - reinterpret_cast*>(addr); + static_cast*>(addr); if (table_factory->get() != nullptr) { old_opts = table_factory->get()->GetOptions(); } @@ -662,8 +660,8 @@ static std::unordered_map OptionTypeFlags::kCompareLoose, // Parses the input value as a MergeOperator, updating the value [](const ConfigOptions& opts, const std::string& /*name*/, - const std::string& value, char* addr) { - auto mop = reinterpret_cast*>(addr); + const std::string& value, void* addr) { + auto mop = static_cast*>(addr); Status status = opts.registry->NewSharedObject(value, mop); // Only support static comparator for now. diff --git a/options/db_options.cc b/options/db_options.cc index c37e391d5..ede1d80e6 100644 --- a/options/db_options.cc +++ b/options/db_options.cc @@ -403,9 +403,8 @@ static std::unordered_map (OptionTypeFlags::kDontSerialize | OptionTypeFlags::kCompareNever), // Parse the input value as a RateLimiter [](const ConfigOptions& /*opts*/, const std::string& /*name*/, - const std::string& value, char* addr) { - auto limiter = - reinterpret_cast*>(addr); + const std::string& value, void* addr) { + auto limiter = static_cast*>(addr); limiter->reset(NewGenericRateLimiter( static_cast(ParseUint64(value)))); return Status::OK(); @@ -416,8 +415,8 @@ static std::unordered_map (OptionTypeFlags::kDontSerialize | OptionTypeFlags::kCompareNever), // Parse the input value as an Env [](const ConfigOptions& /*opts*/, const std::string& /*name*/, - const std::string& value, char* addr) { - auto old_env = reinterpret_cast(addr); // Get the old value + const std::string& value, void* addr) { + auto old_env = static_cast(addr); // Get the old value Env* new_env = *old_env; // Set new to old Status s = Env::LoadEnv(value, &new_env); // Update new value if (s.ok()) { // It worked diff --git a/options/options_helper.cc b/options/options_helper.cc index 670e6e9c4..0b5b07b7a 100644 --- a/options/options_helper.cc +++ b/options/options_helper.cc @@ -417,69 +417,66 @@ bool ParseSliceTransform( return false; } -static bool ParseOptionHelper(char* opt_address, const OptionType& opt_type, +static bool ParseOptionHelper(void* opt_address, const OptionType& opt_type, const std::string& value) { switch (opt_type) { case OptionType::kBoolean: - *reinterpret_cast(opt_address) = ParseBoolean("", value); + *static_cast(opt_address) = ParseBoolean("", value); break; case OptionType::kInt: - *reinterpret_cast(opt_address) = ParseInt(value); + *static_cast(opt_address) = ParseInt(value); break; case OptionType::kInt32T: - *reinterpret_cast(opt_address) = ParseInt32(value); + *static_cast(opt_address) = ParseInt32(value); break; case OptionType::kInt64T: - PutUnaligned(reinterpret_cast(opt_address), ParseInt64(value)); + PutUnaligned(static_cast(opt_address), ParseInt64(value)); break; case OptionType::kUInt: - *reinterpret_cast(opt_address) = ParseUint32(value); + *static_cast(opt_address) = ParseUint32(value); break; case OptionType::kUInt32T: - *reinterpret_cast(opt_address) = ParseUint32(value); + *static_cast(opt_address) = ParseUint32(value); break; case OptionType::kUInt64T: - PutUnaligned(reinterpret_cast(opt_address), ParseUint64(value)); + PutUnaligned(static_cast(opt_address), ParseUint64(value)); break; case OptionType::kSizeT: - PutUnaligned(reinterpret_cast(opt_address), ParseSizeT(value)); + PutUnaligned(static_cast(opt_address), ParseSizeT(value)); break; case OptionType::kString: - *reinterpret_cast(opt_address) = value; + *static_cast(opt_address) = value; break; case OptionType::kDouble: - *reinterpret_cast(opt_address) = ParseDouble(value); + *static_cast(opt_address) = ParseDouble(value); break; case OptionType::kCompactionStyle: return ParseEnum( compaction_style_string_map, value, - reinterpret_cast(opt_address)); + static_cast(opt_address)); case OptionType::kCompactionPri: - return ParseEnum( - compaction_pri_string_map, value, - reinterpret_cast(opt_address)); + return ParseEnum(compaction_pri_string_map, value, + static_cast(opt_address)); case OptionType::kCompressionType: return ParseEnum( compression_type_string_map, value, - reinterpret_cast(opt_address)); + static_cast(opt_address)); case OptionType::kSliceTransform: return ParseSliceTransform( - value, reinterpret_cast*>( - opt_address)); + value, + static_cast*>(opt_address)); case OptionType::kChecksumType: - return ParseEnum( - checksum_type_string_map, value, - reinterpret_cast(opt_address)); + return ParseEnum(checksum_type_string_map, value, + static_cast(opt_address)); case OptionType::kEncodingType: - return ParseEnum( - encoding_type_string_map, value, - reinterpret_cast(opt_address)); + return ParseEnum(encoding_type_string_map, value, + static_cast(opt_address)); case OptionType::kCompactionStopStyle: return ParseEnum( compaction_stop_style_string_map, value, - reinterpret_cast(opt_address)); + static_cast(opt_address)); case OptionType::kEncodedString: { - std::string* output_addr = reinterpret_cast(opt_address); + std::string* output_addr = static_cast(opt_address); (Slice(value)).DecodeHex(output_addr); break; } @@ -489,70 +486,69 @@ static bool ParseOptionHelper(char* opt_address, const OptionType& opt_type, return true; } -bool SerializeSingleOptionHelper(const char* opt_address, +bool SerializeSingleOptionHelper(const void* opt_address, const OptionType opt_type, std::string* value) { - assert(value); switch (opt_type) { case OptionType::kBoolean: - *value = *(reinterpret_cast(opt_address)) ? "true" : "false"; + *value = *(static_cast(opt_address)) ? "true" : "false"; break; case OptionType::kInt: - *value = ToString(*(reinterpret_cast(opt_address))); + *value = ToString(*(static_cast(opt_address))); break; case OptionType::kInt32T: - *value = ToString(*(reinterpret_cast(opt_address))); + *value = ToString(*(static_cast(opt_address))); break; case OptionType::kInt64T: { int64_t v; - GetUnaligned(reinterpret_cast(opt_address), &v); + GetUnaligned(static_cast(opt_address), &v); *value = ToString(v); } break; case OptionType::kUInt: - *value = ToString(*(reinterpret_cast(opt_address))); + *value = ToString(*(static_cast(opt_address))); break; case OptionType::kUInt32T: - *value = ToString(*(reinterpret_cast(opt_address))); + *value = ToString(*(static_cast(opt_address))); break; case OptionType::kUInt64T: { uint64_t v; - GetUnaligned(reinterpret_cast(opt_address), &v); + GetUnaligned(static_cast(opt_address), &v); *value = ToString(v); } break; case OptionType::kSizeT: { size_t v; - GetUnaligned(reinterpret_cast(opt_address), &v); + GetUnaligned(static_cast(opt_address), &v); *value = ToString(v); } break; case OptionType::kDouble: - *value = ToString(*(reinterpret_cast(opt_address))); + *value = ToString(*(static_cast(opt_address))); break; case OptionType::kString: - *value = EscapeOptionString( - *(reinterpret_cast(opt_address))); + *value = + EscapeOptionString(*(static_cast(opt_address))); break; case OptionType::kCompactionStyle: return SerializeEnum( compaction_style_string_map, - *(reinterpret_cast(opt_address)), value); + *(static_cast(opt_address)), value); case OptionType::kCompactionPri: return SerializeEnum( compaction_pri_string_map, - *(reinterpret_cast(opt_address)), value); + *(static_cast(opt_address)), value); case OptionType::kCompressionType: return SerializeEnum( compression_type_string_map, - *(reinterpret_cast(opt_address)), value); + *(static_cast(opt_address)), value); case OptionType::kSliceTransform: { const auto* slice_transform_ptr = - reinterpret_cast*>( + static_cast*>( opt_address); *value = slice_transform_ptr->get() ? slice_transform_ptr->get()->Name() : kNullptrString; @@ -560,7 +556,7 @@ bool SerializeSingleOptionHelper(const char* opt_address, } case OptionType::kComparator: { // it's a const pointer of const Comparator* - const auto* ptr = reinterpret_cast(opt_address); + const auto* ptr = static_cast(opt_address); // Since the user-specified comparator will be wrapped by // InternalKeyComparator, we should persist the user-specified one // instead of InternalKeyComparator. @@ -578,43 +574,42 @@ bool SerializeSingleOptionHelper(const char* opt_address, case OptionType::kCompactionFilter: { // it's a const pointer of const CompactionFilter* const auto* ptr = - reinterpret_cast(opt_address); + static_cast(opt_address); *value = *ptr ? (*ptr)->Name() : kNullptrString; break; } case OptionType::kCompactionFilterFactory: { const auto* ptr = - reinterpret_cast*>( + static_cast*>( opt_address); *value = ptr->get() ? ptr->get()->Name() : kNullptrString; break; } case OptionType::kMemTableRepFactory: { const auto* ptr = - reinterpret_cast*>( - opt_address); + static_cast*>(opt_address); *value = ptr->get() ? ptr->get()->Name() : kNullptrString; break; } case OptionType::kMergeOperator: { const auto* ptr = - reinterpret_cast*>(opt_address); + static_cast*>(opt_address); *value = ptr->get() ? ptr->get()->Name() : kNullptrString; break; } case OptionType::kFilterPolicy: { const auto* ptr = - reinterpret_cast*>(opt_address); + static_cast*>(opt_address); *value = ptr->get() ? ptr->get()->Name() : kNullptrString; break; } case OptionType::kChecksumType: return SerializeEnum( checksum_type_string_map, - *reinterpret_cast(opt_address), value); + *static_cast(opt_address), value); case OptionType::kFlushBlockPolicyFactory: { const auto* ptr = - reinterpret_cast*>( + static_cast*>( opt_address); *value = ptr->get() ? ptr->get()->Name() : kNullptrString; break; @@ -622,13 +617,13 @@ bool SerializeSingleOptionHelper(const char* opt_address, case OptionType::kEncodingType: return SerializeEnum( encoding_type_string_map, - *reinterpret_cast(opt_address), value); + *static_cast(opt_address), value); case OptionType::kCompactionStopStyle: return SerializeEnum( compaction_stop_style_string_map, - *reinterpret_cast(opt_address), value); + *static_cast(opt_address), value); case OptionType::kEncodedString: { - const auto* ptr = reinterpret_cast(opt_address); + const auto* ptr = static_cast(opt_address); *value = (Slice(*ptr)).ToString(true); break; } @@ -980,7 +975,7 @@ Status OptionTypeInfo::Parse(const ConfigOptions& config_options, return Status::OK(); } try { - char* opt_addr = reinterpret_cast(opt_ptr) + offset_; + void* opt_addr = static_cast(opt_ptr) + offset_; const std::string& opt_value = config_options.input_strings_escaped ? UnescapeOptionString(value) : value; @@ -1061,7 +1056,7 @@ Status OptionTypeInfo::ParseType( Status OptionTypeInfo::ParseStruct( const ConfigOptions& config_options, const std::string& struct_name, const std::unordered_map* struct_map, - const std::string& opt_name, const std::string& opt_value, char* opt_addr) { + const std::string& opt_name, const std::string& opt_value, void* opt_addr) { assert(struct_map); Status status; if (opt_name == struct_name || EndsWith(opt_name, "." + struct_name)) { @@ -1103,7 +1098,7 @@ Status OptionTypeInfo::Serialize(const ConfigOptions& config_options, std::string* opt_value) const { // If the option is no longer used in rocksdb and marked as deprecated, // we skip it in the serialization. - const char* opt_addr = reinterpret_cast(opt_ptr) + offset_; + const void* opt_addr = static_cast(opt_ptr) + offset_; if (opt_addr == nullptr || IsDeprecated()) { return Status::OK(); } else if (IsEnabled(OptionTypeFlags::kDontSerialize)) { @@ -1162,7 +1157,7 @@ Status OptionTypeInfo::SerializeType( Status OptionTypeInfo::SerializeStruct( const ConfigOptions& config_options, const std::string& struct_name, const std::unordered_map* struct_map, - const std::string& opt_name, const char* opt_addr, std::string* value) { + const std::string& opt_name, const void* opt_addr, std::string* value) { assert(struct_map); Status status; if (EndsWith(opt_name, struct_name)) { @@ -1204,17 +1199,16 @@ Status OptionTypeInfo::SerializeStruct( } template -bool IsOptionEqual(const char* offset1, const char* offset2) { - return (*reinterpret_cast(offset1) == - *reinterpret_cast(offset2)); +bool IsOptionEqual(const void* offset1, const void* offset2) { + return (*static_cast(offset1) == *static_cast(offset2)); } static bool AreEqualDoubles(const double a, const double b) { return (fabs(a - b) < 0.00001); } -static bool AreOptionsEqual(OptionType type, const char* this_offset, - const char* that_offset) { +static bool AreOptionsEqual(OptionType type, const void* this_offset, + const void* that_offset) { switch (type) { case OptionType::kBoolean: return IsOptionEqual(this_offset, that_offset); @@ -1226,29 +1220,29 @@ static bool AreOptionsEqual(OptionType type, const char* this_offset, return IsOptionEqual(this_offset, that_offset); case OptionType::kInt64T: { int64_t v1, v2; - GetUnaligned(reinterpret_cast(this_offset), &v1); - GetUnaligned(reinterpret_cast(that_offset), &v2); + GetUnaligned(static_cast(this_offset), &v1); + GetUnaligned(static_cast(that_offset), &v2); return (v1 == v2); } case OptionType::kUInt32T: return IsOptionEqual(this_offset, that_offset); case OptionType::kUInt64T: { uint64_t v1, v2; - GetUnaligned(reinterpret_cast(this_offset), &v1); - GetUnaligned(reinterpret_cast(that_offset), &v2); + GetUnaligned(static_cast(this_offset), &v1); + GetUnaligned(static_cast(that_offset), &v2); return (v1 == v2); } case OptionType::kSizeT: { size_t v1, v2; - GetUnaligned(reinterpret_cast(this_offset), &v1); - GetUnaligned(reinterpret_cast(that_offset), &v2); + GetUnaligned(static_cast(this_offset), &v1); + GetUnaligned(static_cast(that_offset), &v2); return (v1 == v2); } case OptionType::kString: return IsOptionEqual(this_offset, that_offset); case OptionType::kDouble: - return AreEqualDoubles(*reinterpret_cast(this_offset), - *reinterpret_cast(that_offset)); + return AreEqualDoubles(*static_cast(this_offset), + *static_cast(that_offset)); case OptionType::kCompactionStyle: return IsOptionEqual(this_offset, that_offset); case OptionType::kCompactionStopStyle: @@ -1277,8 +1271,8 @@ bool OptionTypeInfo::AreEqual(const ConfigOptions& config_options, if (!config_options.IsCheckEnabled(level)) { return true; // If the sanity level is not being checked, skip it } - const auto this_addr = reinterpret_cast(this_ptr) + offset_; - const auto that_addr = reinterpret_cast(that_ptr) + offset_; + const void* this_addr = static_cast(this_ptr) + offset_; + const void* that_addr = static_cast(that_ptr) + offset_; if (this_addr == nullptr || that_addr == nullptr) { if (this_addr == that_addr) { return true; @@ -1335,7 +1329,7 @@ bool OptionTypeInfo::TypesAreEqual( bool OptionTypeInfo::StructsAreEqual( const ConfigOptions& config_options, const std::string& struct_name, const std::unordered_map* struct_map, - const std::string& opt_name, const char* this_addr, const char* that_addr, + const std::string& opt_name, const void* this_addr, const void* that_addr, std::string* mismatch) { assert(struct_map); bool matches = true; diff --git a/options/options_test.cc b/options/options_test.cc index d341af59e..aa152257c 100644 --- a/options/options_test.cc +++ b/options/options_test.cc @@ -3707,8 +3707,8 @@ TEST_F(OptionTypeInfoTest, TestInvalidArgs) { OptionVerificationType::kNormal, OptionTypeFlags::kNone, [](const ConfigOptions&, const std::string&, - const std::string& value, char* addr) { - auto ptr = reinterpret_cast(addr); + const std::string& value, void* addr) { + auto ptr = static_cast(addr); *ptr = ParseInt(value); return Status::OK(); }); @@ -3721,8 +3721,8 @@ TEST_F(OptionTypeInfoTest, TestParseFunc) { 0, OptionType::kUnknown, OptionVerificationType::kNormal, OptionTypeFlags::kNone, [](const ConfigOptions& /*opts*/, const std::string& name, - const std::string& value, char* addr) { - auto ptr = reinterpret_cast(addr); + const std::string& value, void* addr) { + auto ptr = static_cast(addr); if (name == "Oops") { return Status::InvalidArgument(value); } else { @@ -3742,7 +3742,7 @@ TEST_F(OptionTypeInfoTest, TestSerializeFunc) { 0, OptionType::kString, OptionVerificationType::kNormal, OptionTypeFlags::kNone, nullptr, [](const ConfigOptions& /*opts*/, const std::string& name, - const char* /*addr*/, std::string* value) { + const void* /*addr*/, std::string* value) { if (name == "Oops") { return Status::InvalidArgument(name); } else { @@ -3764,9 +3764,9 @@ TEST_F(OptionTypeInfoTest, TestEqualsFunc) { 0, OptionType::kInt, OptionVerificationType::kNormal, OptionTypeFlags::kNone, nullptr, nullptr, [](const ConfigOptions& /*opts*/, const std::string& name, - const char* addr1, const char* addr2, std::string* mismatch) { - auto i1 = *(reinterpret_cast(addr1)); - auto i2 = *(reinterpret_cast(addr2)); + const void* addr1, const void* addr2, std::string* mismatch) { + auto i1 = *(static_cast(addr1)); + auto i2 = *(static_cast(addr2)); if (name == "LT") { return i1 < i2; } else if (name == "GT") { @@ -3820,8 +3820,7 @@ TEST_F(OptionTypeInfoTest, TestOptionFlags) { // An alias can change the value via parse, but does nothing on serialize on // match std::string result; - ASSERT_OK(opt_alias.Parse(config_options, "Alias", "Alias", - reinterpret_cast(&base))); + ASSERT_OK(opt_alias.Parse(config_options, "Alias", "Alias", &base)); ASSERT_OK(opt_alias.Serialize(config_options, "Alias", &base, &result)); ASSERT_TRUE( opt_alias.AreEqual(config_options, "Alias", &base, &comp, &result)); diff --git a/table/block_based/block_based_table_factory.cc b/table/block_based/block_based_table_factory.cc index 33cf0b141..2fa1e40ed 100644 --- a/table/block_based/block_based_table_factory.cc +++ b/table/block_based/block_based_table_factory.cc @@ -306,17 +306,16 @@ static std::unordered_map OptionTypeFlags::kNone, // Parses the Filter policy [](const ConfigOptions& opts, const std::string&, - const std::string& value, char* addr) { + const std::string& value, void* addr) { auto* policy = - reinterpret_cast*>(addr); + static_cast*>(addr); return FilterPolicy::CreateFromString(opts, value, policy); }, // Converts the FilterPolicy to its string representation - [](const ConfigOptions&, const std::string&, const char* addr, + [](const ConfigOptions&, const std::string&, const void* addr, std::string* value) { const auto* policy = - reinterpret_cast*>( - addr); + static_cast*>(addr); if (policy->get()) { *value = (*policy)->Name(); } else { @@ -325,15 +324,13 @@ static std::unordered_map return Status::OK(); }, // Compares two FilterPolicy objects for equality - [](const ConfigOptions&, const std::string&, const char* addr1, - const char* addr2, std::string*) { + [](const ConfigOptions&, const std::string&, const void* addr1, + const void* addr2, std::string*) { const auto* policy1 = - reinterpret_cast*>( - addr1) + static_cast*>(addr1) ->get(); const auto* policy2 = - reinterpret_cast*>(addr2) - ->get(); + static_cast*>(addr2)->get(); if (policy1 == policy2) { return true; } else if (policy1 != nullptr && policy2 != nullptr) { @@ -362,7 +359,7 @@ static std::unordered_map OptionType::kUInt32T, OptionVerificationType::kNormal, OptionTypeFlags::kNone, [](const ConfigOptions& /*opts*/, const std::string& /*name*/, - const std::string& value, char* addr) { + const std::string& value, void* addr) { // A workaround to fix a bug in 6.10, 6.11, 6.12, 6.13 // and 6.14. The bug will write out 8 bytes to OPTIONS file from the // starting address of BlockBasedTableOptions.read_amp_bytes_per_bit @@ -373,7 +370,7 @@ static std::unordered_map // generated by affected releases before the fix, we need to // manually parse read_amp_bytes_per_bit with this special hack. uint64_t read_amp_bytes_per_bit = ParseUint64(value); - *(reinterpret_cast(addr)) = + *(static_cast(addr)) = static_cast(read_amp_bytes_per_bit); return Status::OK(); }}}, @@ -401,8 +398,8 @@ static std::unordered_map (OptionTypeFlags::kCompareNever | OptionTypeFlags::kDontSerialize), // Parses the input vsalue as a Cache [](const ConfigOptions& opts, const std::string&, - const std::string& value, char* addr) { - auto* cache = reinterpret_cast*>(addr); + const std::string& value, void* addr) { + auto* cache = static_cast*>(addr); return Cache::CreateFromString(opts, value, cache); }}}, {"block_cache_compressed", @@ -411,8 +408,8 @@ static std::unordered_map (OptionTypeFlags::kCompareNever | OptionTypeFlags::kDontSerialize), // Parses the input vsalue as a Cache [](const ConfigOptions& opts, const std::string&, - const std::string& value, char* addr) { - auto* cache = reinterpret_cast*>(addr); + const std::string& value, void* addr) { + auto* cache = static_cast*>(addr); return Cache::CreateFromString(opts, value, cache); }}}, {"max_auto_readahead_size",