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
This commit is contained in:
Jay Zhuang 2021-05-13 14:28:50 -07:00 committed by Facebook GitHub Bot
parent d76c46e6a0
commit d15fbae449
8 changed files with 177 additions and 194 deletions

6
cache/cache.cc vendored
View File

@ -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<char*>(&cache_opts));
status = OptionTypeInfo::ParseStruct(config_options, "",
&lru_cache_options_type_info, "",
value, &cache_opts);
if (status.ok()) {
cache = NewLRUCache(cache_opts);
}

View File

@ -2102,27 +2102,24 @@ static std::unordered_map<std::string, OptionTypeInfo> 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<ColumnFamilyOptions*>(addr);
const std::string& value, void* addr) {
auto cf_options = static_cast<ColumnFamilyOptions*>(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<const ColumnFamilyOptions*>(addr);
const void* addr, std::string* value) {
const auto cf_options = static_cast<const ColumnFamilyOptions*>(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<const ColumnFamilyOptions*>(addr1);
const auto that_one =
reinterpret_cast<const ColumnFamilyOptions*>(addr2);
[](const ConfigOptions& opts, const std::string& name, const void* addr1,
const void* addr2, std::string* mismatch) {
const auto this_one = static_cast<const ColumnFamilyOptions*>(addr1);
const auto that_one = static_cast<const ColumnFamilyOptions*>(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<std::string, OptionTypeInfo> 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<DBOptions*>(addr);
const std::string& value, void* addr) {
auto options = static_cast<DBOptions*>(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<const DBOptions*>(addr);
const void* addr, std::string* value) {
const auto options = static_cast<const DBOptions*>(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<const DBOptions*>(addr1);
const auto that_one = reinterpret_cast<const DBOptions*>(addr2);
[](const ConfigOptions& opts, const std::string& name, const void* addr1,
const void* addr2, std::string* mismatch) {
const auto this_one = static_cast<const DBOptions*>(addr1);
const auto that_one = static_cast<const DBOptions*>(addr2);
auto this_conf = DBOptionsAsConfigurable(*this_one);
auto that_conf = DBOptionsAsConfigurable(*that_one);
std::string mismatch_opt;

View File

@ -177,7 +177,7 @@ bool VectorsAreEqual(const ConfigOptions& config_options,
// @param addr Pointer to the object
using ParseFunc = std::function<Status(
const ConfigOptions& /*opts*/, const std::string& /*name*/,
const std::string& /*value*/, char* /*addr*/)>;
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<Status(
// @param value The result of the serialization.
using SerializeFunc = std::function<Status(
const ConfigOptions& /*opts*/, const std::string& /*name*/,
const char* /*addr*/, std::string* /*value*/)>;
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<Status(
// first differs
using EqualsFunc = std::function<bool(
const ConfigOptions& /*opts*/, const std::string& /*name*/,
const char* /*addr1*/, const char* /*addr2*/, std::string* mismatch)>;
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<T>(*map, value, reinterpret_cast<T*>(addr))) {
} else if (ParseEnum<T>(*map, value, static_cast<T*>(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<T>(*map, (*reinterpret_cast<const T*>(addr)),
} else if (SerializeEnum<T>(*map, (*static_cast<const T*>(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<const T*>(addr1) ==
*reinterpret_cast<const T*>(addr2));
[](const ConfigOptions&, const std::string&, const void* addr1,
const void* addr2, std::string*) {
return (*static_cast<const T*>(addr1) ==
*static_cast<const T*>(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<std::vector<T>*>(addr);
const std::string& value, void* addr) {
auto result = static_cast<std::vector<T>*>(addr);
return ParseVector<T>(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<const std::vector<T>*>(addr));
const auto& vec = *(static_cast<const std::vector<T>*>(addr));
return SerializeVector<T>(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<const std::vector<T>*>(addr1));
const auto& vec2 = *(reinterpret_cast<const std::vector<T>*>(addr2));
const auto& vec1 = *(static_cast<const std::vector<T>*>(addr1));
const auto& vec2 = *(static_cast<const std::vector<T>*>(addr2));
return VectorsAreEqual<T>(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<std::shared_ptr<T>*>(addr);
const std::string& value, void* addr) {
auto* shared = static_cast<std::shared_ptr<T>*>(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<std::unique_ptr<T>*>(addr);
const std::string& value, void* addr) {
auto* unique = static_cast<std::unique_ptr<T>*>(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<T**>(addr);
const std::string& value, void* addr) {
auto** pointer = static_cast<T**>(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<const char*>(base_addr) + offset_;
const void* opt_addr = static_cast<const char*>(base_addr) + offset_;
if (IsUniquePtr()) {
const std::unique_ptr<T>* ptr =
reinterpret_cast<const std::unique_ptr<T>*>(opt_addr);
static_cast<const std::unique_ptr<T>*>(opt_addr);
return ptr->get();
} else if (IsSharedPtr()) {
const std::shared_ptr<T>* ptr =
reinterpret_cast<const std::shared_ptr<T>*>(opt_addr);
static_cast<const std::shared_ptr<T>*>(opt_addr);
return ptr->get();
} else if (IsRawPtr()) {
const T* const* ptr = reinterpret_cast<const T* const*>(opt_addr);
const T* const* ptr = static_cast<const T* const*>(opt_addr);
return *ptr;
} else {
return reinterpret_cast<const T*>(opt_addr);
return static_cast<const T*>(opt_addr);
}
}
@ -620,18 +620,18 @@ class OptionTypeInfo {
if (base_addr == nullptr) {
return nullptr;
}
auto opt_addr = reinterpret_cast<char*>(base_addr) + offset_;
void* opt_addr = static_cast<char*>(base_addr) + offset_;
if (IsUniquePtr()) {
std::unique_ptr<T>* ptr = reinterpret_cast<std::unique_ptr<T>*>(opt_addr);
std::unique_ptr<T>* ptr = static_cast<std::unique_ptr<T>*>(opt_addr);
return ptr->get();
} else if (IsSharedPtr()) {
std::shared_ptr<T>* ptr = reinterpret_cast<std::shared_ptr<T>*>(opt_addr);
std::shared_ptr<T>* ptr = static_cast<std::shared_ptr<T>*>(opt_addr);
return ptr->get();
} else if (IsRawPtr()) {
T** ptr = reinterpret_cast<T**>(opt_addr);
T** ptr = static_cast<T**>(opt_addr);
return *ptr;
} else {
return reinterpret_cast<T*>(opt_addr);
return static_cast<T*>(opt_addr);
}
}
@ -704,7 +704,7 @@ class OptionTypeInfo {
static Status ParseStruct(
const ConfigOptions& config_options, const std::string& struct_name,
const std::unordered_map<std::string, OptionTypeInfo>* 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<std::string, OptionTypeInfo>* 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<std::string, OptionTypeInfo>* 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<char*>(&elem));
status = elem_info.Parse(copy, name, token, &elem);
if (status.ok()) {
result->emplace_back(elem);
} else if (config_options.ignore_unsupported_options &&

View File

@ -364,7 +364,7 @@ static std::unordered_map<std::string, OptionTypeInfo>
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<std::string, OptionTypeInfo>
if (name == "compaction_options_fifo" &&
value.find("=") == std::string::npos) {
// Old format. Parse just a single uint64_t value.
auto options = reinterpret_cast<CompactionOptionsFIFO*>(addr);
auto options = static_cast<CompactionOptionsFIFO*>(addr);
options->max_table_files_size = ParseUint64(value);
return Status::OK();
} else {
@ -433,13 +433,12 @@ static std::unordered_map<std::string, OptionTypeInfo>
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<CompressionOptions*>(addr);
auto* compression = static_cast<CompressionOptions*>(addr);
return ParseCompressionOptions(value, name, *compression);
} else {
return OptionTypeInfo::ParseStruct(
@ -454,13 +453,12 @@ static std::unordered_map<std::string, OptionTypeInfo>
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<CompressionOptions*>(addr);
auto* compression = static_cast<CompressionOptions*>(addr);
return ParseCompressionOptions(value, name, *compression);
} else {
return OptionTypeInfo::ParseStruct(
@ -542,8 +540,8 @@ static std::unordered_map<std::string, OptionTypeInfo>
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<const Comparator**>(addr);
const std::string& value, void* addr) {
auto old_comparator = static_cast<const Comparator**>(addr);
const Comparator* new_comparator = *old_comparator;
Status status =
opts.registry->NewStaticObject(value, &new_comparator);
@ -568,12 +566,12 @@ static std::unordered_map<std::string, OptionTypeInfo>
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<MemTableRepFactory> new_mem_factory;
Status s = GetMemTableRepFactoryFromString(value, &new_mem_factory);
if (s.ok()) {
auto memtable_factory =
reinterpret_cast<std::shared_ptr<MemTableRepFactory>*>(addr);
static_cast<std::shared_ptr<MemTableRepFactory>*>(addr);
memtable_factory->reset(new_mem_factory.release());
}
return s;
@ -590,10 +588,10 @@ static std::unordered_map<std::string, OptionTypeInfo>
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<std::shared_ptr<TableFactory>*>(addr);
static_cast<std::shared_ptr<TableFactory>*>(addr);
if (table_factory->get() != nullptr) {
old_opts =
table_factory->get()->GetOptions<BlockBasedTableOptions>();
@ -622,10 +620,10 @@ static std::unordered_map<std::string, OptionTypeInfo>
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<std::shared_ptr<TableFactory>*>(addr);
static_cast<std::shared_ptr<TableFactory>*>(addr);
if (table_factory->get() != nullptr) {
old_opts = table_factory->get()->GetOptions<PlainTableOptions>();
}
@ -662,8 +660,8 @@ static std::unordered_map<std::string, OptionTypeInfo>
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<std::shared_ptr<MergeOperator>*>(addr);
const std::string& value, void* addr) {
auto mop = static_cast<std::shared_ptr<MergeOperator>*>(addr);
Status status =
opts.registry->NewSharedObject<MergeOperator>(value, mop);
// Only support static comparator for now.

View File

@ -403,9 +403,8 @@ static std::unordered_map<std::string, OptionTypeInfo>
(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<std::shared_ptr<RateLimiter>*>(addr);
const std::string& value, void* addr) {
auto limiter = static_cast<std::shared_ptr<RateLimiter>*>(addr);
limiter->reset(NewGenericRateLimiter(
static_cast<int64_t>(ParseUint64(value))));
return Status::OK();
@ -416,8 +415,8 @@ static std::unordered_map<std::string, OptionTypeInfo>
(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<Env**>(addr); // Get the old value
const std::string& value, void* addr) {
auto old_env = static_cast<Env**>(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

View File

@ -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<bool*>(opt_address) = ParseBoolean("", value);
*static_cast<bool*>(opt_address) = ParseBoolean("", value);
break;
case OptionType::kInt:
*reinterpret_cast<int*>(opt_address) = ParseInt(value);
*static_cast<int*>(opt_address) = ParseInt(value);
break;
case OptionType::kInt32T:
*reinterpret_cast<int32_t*>(opt_address) = ParseInt32(value);
*static_cast<int32_t*>(opt_address) = ParseInt32(value);
break;
case OptionType::kInt64T:
PutUnaligned(reinterpret_cast<int64_t*>(opt_address), ParseInt64(value));
PutUnaligned(static_cast<int64_t*>(opt_address), ParseInt64(value));
break;
case OptionType::kUInt:
*reinterpret_cast<unsigned int*>(opt_address) = ParseUint32(value);
*static_cast<unsigned int*>(opt_address) = ParseUint32(value);
break;
case OptionType::kUInt32T:
*reinterpret_cast<uint32_t*>(opt_address) = ParseUint32(value);
*static_cast<uint32_t*>(opt_address) = ParseUint32(value);
break;
case OptionType::kUInt64T:
PutUnaligned(reinterpret_cast<uint64_t*>(opt_address), ParseUint64(value));
PutUnaligned(static_cast<uint64_t*>(opt_address), ParseUint64(value));
break;
case OptionType::kSizeT:
PutUnaligned(reinterpret_cast<size_t*>(opt_address), ParseSizeT(value));
PutUnaligned(static_cast<size_t*>(opt_address), ParseSizeT(value));
break;
case OptionType::kString:
*reinterpret_cast<std::string*>(opt_address) = value;
*static_cast<std::string*>(opt_address) = value;
break;
case OptionType::kDouble:
*reinterpret_cast<double*>(opt_address) = ParseDouble(value);
*static_cast<double*>(opt_address) = ParseDouble(value);
break;
case OptionType::kCompactionStyle:
return ParseEnum<CompactionStyle>(
compaction_style_string_map, value,
reinterpret_cast<CompactionStyle*>(opt_address));
static_cast<CompactionStyle*>(opt_address));
case OptionType::kCompactionPri:
return ParseEnum<CompactionPri>(
compaction_pri_string_map, value,
reinterpret_cast<CompactionPri*>(opt_address));
return ParseEnum<CompactionPri>(compaction_pri_string_map, value,
static_cast<CompactionPri*>(opt_address));
case OptionType::kCompressionType:
return ParseEnum<CompressionType>(
compression_type_string_map, value,
reinterpret_cast<CompressionType*>(opt_address));
static_cast<CompressionType*>(opt_address));
case OptionType::kSliceTransform:
return ParseSliceTransform(
value, reinterpret_cast<std::shared_ptr<const SliceTransform>*>(
opt_address));
value,
static_cast<std::shared_ptr<const SliceTransform>*>(opt_address));
case OptionType::kChecksumType:
return ParseEnum<ChecksumType>(
checksum_type_string_map, value,
reinterpret_cast<ChecksumType*>(opt_address));
return ParseEnum<ChecksumType>(checksum_type_string_map, value,
static_cast<ChecksumType*>(opt_address));
case OptionType::kEncodingType:
return ParseEnum<EncodingType>(
encoding_type_string_map, value,
reinterpret_cast<EncodingType*>(opt_address));
return ParseEnum<EncodingType>(encoding_type_string_map, value,
static_cast<EncodingType*>(opt_address));
case OptionType::kCompactionStopStyle:
return ParseEnum<CompactionStopStyle>(
compaction_stop_style_string_map, value,
reinterpret_cast<CompactionStopStyle*>(opt_address));
static_cast<CompactionStopStyle*>(opt_address));
case OptionType::kEncodedString: {
std::string* output_addr = reinterpret_cast<std::string*>(opt_address);
std::string* output_addr = static_cast<std::string*>(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<const bool*>(opt_address)) ? "true" : "false";
*value = *(static_cast<const bool*>(opt_address)) ? "true" : "false";
break;
case OptionType::kInt:
*value = ToString(*(reinterpret_cast<const int*>(opt_address)));
*value = ToString(*(static_cast<const int*>(opt_address)));
break;
case OptionType::kInt32T:
*value = ToString(*(reinterpret_cast<const int32_t*>(opt_address)));
*value = ToString(*(static_cast<const int32_t*>(opt_address)));
break;
case OptionType::kInt64T:
{
int64_t v;
GetUnaligned(reinterpret_cast<const int64_t*>(opt_address), &v);
GetUnaligned(static_cast<const int64_t*>(opt_address), &v);
*value = ToString(v);
}
break;
case OptionType::kUInt:
*value = ToString(*(reinterpret_cast<const unsigned int*>(opt_address)));
*value = ToString(*(static_cast<const unsigned int*>(opt_address)));
break;
case OptionType::kUInt32T:
*value = ToString(*(reinterpret_cast<const uint32_t*>(opt_address)));
*value = ToString(*(static_cast<const uint32_t*>(opt_address)));
break;
case OptionType::kUInt64T:
{
uint64_t v;
GetUnaligned(reinterpret_cast<const uint64_t*>(opt_address), &v);
GetUnaligned(static_cast<const uint64_t*>(opt_address), &v);
*value = ToString(v);
}
break;
case OptionType::kSizeT:
{
size_t v;
GetUnaligned(reinterpret_cast<const size_t*>(opt_address), &v);
GetUnaligned(static_cast<const size_t*>(opt_address), &v);
*value = ToString(v);
}
break;
case OptionType::kDouble:
*value = ToString(*(reinterpret_cast<const double*>(opt_address)));
*value = ToString(*(static_cast<const double*>(opt_address)));
break;
case OptionType::kString:
*value = EscapeOptionString(
*(reinterpret_cast<const std::string*>(opt_address)));
*value =
EscapeOptionString(*(static_cast<const std::string*>(opt_address)));
break;
case OptionType::kCompactionStyle:
return SerializeEnum<CompactionStyle>(
compaction_style_string_map,
*(reinterpret_cast<const CompactionStyle*>(opt_address)), value);
*(static_cast<const CompactionStyle*>(opt_address)), value);
case OptionType::kCompactionPri:
return SerializeEnum<CompactionPri>(
compaction_pri_string_map,
*(reinterpret_cast<const CompactionPri*>(opt_address)), value);
*(static_cast<const CompactionPri*>(opt_address)), value);
case OptionType::kCompressionType:
return SerializeEnum<CompressionType>(
compression_type_string_map,
*(reinterpret_cast<const CompressionType*>(opt_address)), value);
*(static_cast<const CompressionType*>(opt_address)), value);
case OptionType::kSliceTransform: {
const auto* slice_transform_ptr =
reinterpret_cast<const std::shared_ptr<const SliceTransform>*>(
static_cast<const std::shared_ptr<const SliceTransform>*>(
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<const Comparator* const*>(opt_address);
const auto* ptr = static_cast<const Comparator* const*>(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<const CompactionFilter* const*>(opt_address);
static_cast<const CompactionFilter* const*>(opt_address);
*value = *ptr ? (*ptr)->Name() : kNullptrString;
break;
}
case OptionType::kCompactionFilterFactory: {
const auto* ptr =
reinterpret_cast<const std::shared_ptr<CompactionFilterFactory>*>(
static_cast<const std::shared_ptr<CompactionFilterFactory>*>(
opt_address);
*value = ptr->get() ? ptr->get()->Name() : kNullptrString;
break;
}
case OptionType::kMemTableRepFactory: {
const auto* ptr =
reinterpret_cast<const std::shared_ptr<MemTableRepFactory>*>(
opt_address);
static_cast<const std::shared_ptr<MemTableRepFactory>*>(opt_address);
*value = ptr->get() ? ptr->get()->Name() : kNullptrString;
break;
}
case OptionType::kMergeOperator: {
const auto* ptr =
reinterpret_cast<const std::shared_ptr<MergeOperator>*>(opt_address);
static_cast<const std::shared_ptr<MergeOperator>*>(opt_address);
*value = ptr->get() ? ptr->get()->Name() : kNullptrString;
break;
}
case OptionType::kFilterPolicy: {
const auto* ptr =
reinterpret_cast<const std::shared_ptr<FilterPolicy>*>(opt_address);
static_cast<const std::shared_ptr<FilterPolicy>*>(opt_address);
*value = ptr->get() ? ptr->get()->Name() : kNullptrString;
break;
}
case OptionType::kChecksumType:
return SerializeEnum<ChecksumType>(
checksum_type_string_map,
*reinterpret_cast<const ChecksumType*>(opt_address), value);
*static_cast<const ChecksumType*>(opt_address), value);
case OptionType::kFlushBlockPolicyFactory: {
const auto* ptr =
reinterpret_cast<const std::shared_ptr<FlushBlockPolicyFactory>*>(
static_cast<const std::shared_ptr<FlushBlockPolicyFactory>*>(
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<EncodingType>(
encoding_type_string_map,
*reinterpret_cast<const EncodingType*>(opt_address), value);
*static_cast<const EncodingType*>(opt_address), value);
case OptionType::kCompactionStopStyle:
return SerializeEnum<CompactionStopStyle>(
compaction_stop_style_string_map,
*reinterpret_cast<const CompactionStopStyle*>(opt_address), value);
*static_cast<const CompactionStopStyle*>(opt_address), value);
case OptionType::kEncodedString: {
const auto* ptr = reinterpret_cast<const std::string*>(opt_address);
const auto* ptr = static_cast<const std::string*>(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<char*>(opt_ptr) + offset_;
void* opt_addr = static_cast<char*>(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<std::string, OptionTypeInfo>* 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<const char*>(opt_ptr) + offset_;
const void* opt_addr = static_cast<const char*>(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<std::string, OptionTypeInfo>* 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 <typename T>
bool IsOptionEqual(const char* offset1, const char* offset2) {
return (*reinterpret_cast<const T*>(offset1) ==
*reinterpret_cast<const T*>(offset2));
bool IsOptionEqual(const void* offset1, const void* offset2) {
return (*static_cast<const T*>(offset1) == *static_cast<const T*>(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<bool>(this_offset, that_offset);
@ -1226,29 +1220,29 @@ static bool AreOptionsEqual(OptionType type, const char* this_offset,
return IsOptionEqual<int32_t>(this_offset, that_offset);
case OptionType::kInt64T: {
int64_t v1, v2;
GetUnaligned(reinterpret_cast<const int64_t*>(this_offset), &v1);
GetUnaligned(reinterpret_cast<const int64_t*>(that_offset), &v2);
GetUnaligned(static_cast<const int64_t*>(this_offset), &v1);
GetUnaligned(static_cast<const int64_t*>(that_offset), &v2);
return (v1 == v2);
}
case OptionType::kUInt32T:
return IsOptionEqual<uint32_t>(this_offset, that_offset);
case OptionType::kUInt64T: {
uint64_t v1, v2;
GetUnaligned(reinterpret_cast<const uint64_t*>(this_offset), &v1);
GetUnaligned(reinterpret_cast<const uint64_t*>(that_offset), &v2);
GetUnaligned(static_cast<const uint64_t*>(this_offset), &v1);
GetUnaligned(static_cast<const uint64_t*>(that_offset), &v2);
return (v1 == v2);
}
case OptionType::kSizeT: {
size_t v1, v2;
GetUnaligned(reinterpret_cast<const size_t*>(this_offset), &v1);
GetUnaligned(reinterpret_cast<const size_t*>(that_offset), &v2);
GetUnaligned(static_cast<const size_t*>(this_offset), &v1);
GetUnaligned(static_cast<const size_t*>(that_offset), &v2);
return (v1 == v2);
}
case OptionType::kString:
return IsOptionEqual<std::string>(this_offset, that_offset);
case OptionType::kDouble:
return AreEqualDoubles(*reinterpret_cast<const double*>(this_offset),
*reinterpret_cast<const double*>(that_offset));
return AreEqualDoubles(*static_cast<const double*>(this_offset),
*static_cast<const double*>(that_offset));
case OptionType::kCompactionStyle:
return IsOptionEqual<CompactionStyle>(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<const char*>(this_ptr) + offset_;
const auto that_addr = reinterpret_cast<const char*>(that_ptr) + offset_;
const void* this_addr = static_cast<const char*>(this_ptr) + offset_;
const void* that_addr = static_cast<const char*>(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<std::string, OptionTypeInfo>* 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;

View File

@ -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<int*>(addr);
const std::string& value, void* addr) {
auto ptr = static_cast<int*>(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<std::string*>(addr);
const std::string& value, void* addr) {
auto ptr = static_cast<std::string*>(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<const int*>(addr1));
auto i2 = *(reinterpret_cast<const int*>(addr2));
const void* addr1, const void* addr2, std::string* mismatch) {
auto i1 = *(static_cast<const int*>(addr1));
auto i2 = *(static_cast<const int*>(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<char*>(&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));

View File

@ -306,17 +306,16 @@ static std::unordered_map<std::string, OptionTypeInfo>
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<std::shared_ptr<const FilterPolicy>*>(addr);
static_cast<std::shared_ptr<const FilterPolicy>*>(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<const std::shared_ptr<const FilterPolicy>*>(
addr);
static_cast<const std::shared_ptr<const FilterPolicy>*>(addr);
if (policy->get()) {
*value = (*policy)->Name();
} else {
@ -325,15 +324,13 @@ static std::unordered_map<std::string, OptionTypeInfo>
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<const std::shared_ptr<const FilterPolicy>*>(
addr1)
static_cast<const std::shared_ptr<const FilterPolicy>*>(addr1)
->get();
const auto* policy2 =
reinterpret_cast<const std::shared_ptr<FilterPolicy>*>(addr2)
->get();
static_cast<const std::shared_ptr<FilterPolicy>*>(addr2)->get();
if (policy1 == policy2) {
return true;
} else if (policy1 != nullptr && policy2 != nullptr) {
@ -362,7 +359,7 @@ static std::unordered_map<std::string, OptionTypeInfo>
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<std::string, OptionTypeInfo>
// 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<uint32_t*>(addr)) =
*(static_cast<uint32_t*>(addr)) =
static_cast<uint32_t>(read_amp_bytes_per_bit);
return Status::OK();
}}},
@ -401,8 +398,8 @@ static std::unordered_map<std::string, OptionTypeInfo>
(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<std::shared_ptr<Cache>*>(addr);
const std::string& value, void* addr) {
auto* cache = static_cast<std::shared_ptr<Cache>*>(addr);
return Cache::CreateFromString(opts, value, cache);
}}},
{"block_cache_compressed",
@ -411,8 +408,8 @@ static std::unordered_map<std::string, OptionTypeInfo>
(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<std::shared_ptr<Cache>*>(addr);
const std::string& value, void* addr) {
auto* cache = static_cast<std::shared_ptr<Cache>*>(addr);
return Cache::CreateFromString(opts, value, cache);
}}},
{"max_auto_readahead_size",