Fix C++23 compilation errors in leveldb

Remove usages of std::aligned_storage, which is deprecated.
More details about the replacement in https://crbug.com/388068052.

PiperOrigin-RevId: 713346733
This commit is contained in:
leveldb Team 2025-01-08 18:54:39 +00:00 committed by Victor Costan
parent 578eeb702e
commit 302786e211
5 changed files with 20 additions and 13 deletions

View File

@ -51,9 +51,6 @@ class LEVELDB_EXPORT Slice {
// Return true iff the length of the referenced data is zero // Return true iff the length of the referenced data is zero
bool empty() const { return size_ == 0; } bool empty() const { return size_ == 0; }
const char* begin() const { return data(); }
const char* end() const { return data() + size(); }
// Return the ith byte in the referenced data. // Return the ith byte in the referenced data.
// REQUIRES: n < size() // REQUIRES: n < size()
char operator[](size_t n) const { char operator[](size_t n) const {

View File

@ -874,7 +874,11 @@ class SingletonEnv {
#endif // !defined(NDEBUG) #endif // !defined(NDEBUG)
static_assert(sizeof(env_storage_) >= sizeof(EnvType), static_assert(sizeof(env_storage_) >= sizeof(EnvType),
"env_storage_ will not fit the Env"); "env_storage_ will not fit the Env");
static_assert(alignof(decltype(env_storage_)) >= alignof(EnvType), static_assert(std::is_standard_layout_v<SingletonEnv<EnvType>>);
static_assert(
offsetof(SingletonEnv<EnvType>, env_storage_) % alignof(EnvType) == 0,
"env_storage_ does not meet the Env's alignment needs");
static_assert(alignof(SingletonEnv<EnvType>) % alignof(EnvType) == 0,
"env_storage_ does not meet the Env's alignment needs"); "env_storage_ does not meet the Env's alignment needs");
new (&env_storage_) EnvType(); new (&env_storage_) EnvType();
} }
@ -892,8 +896,7 @@ class SingletonEnv {
} }
private: private:
typename std::aligned_storage<sizeof(EnvType), alignof(EnvType)>::type alignas(EnvType) char env_storage_[sizeof(EnvType)];
env_storage_;
#if !defined(NDEBUG) #if !defined(NDEBUG)
static std::atomic<bool> env_initialized_; static std::atomic<bool> env_initialized_;
#endif // !defined(NDEBUG) #endif // !defined(NDEBUG)

View File

@ -769,7 +769,11 @@ class SingletonEnv {
#endif // !defined(NDEBUG) #endif // !defined(NDEBUG)
static_assert(sizeof(env_storage_) >= sizeof(EnvType), static_assert(sizeof(env_storage_) >= sizeof(EnvType),
"env_storage_ will not fit the Env"); "env_storage_ will not fit the Env");
static_assert(alignof(decltype(env_storage_)) >= alignof(EnvType), static_assert(std::is_standard_layout_v<SingletonEnv<EnvType>>);
static_assert(
offsetof(SingletonEnv<EnvType>, env_storage_) % alignof(EnvType) == 0,
"env_storage_ does not meet the Env's alignment needs");
static_assert(alignof(SingletonEnv<EnvType>) % alignof(EnvType) == 0,
"env_storage_ does not meet the Env's alignment needs"); "env_storage_ does not meet the Env's alignment needs");
new (&env_storage_) EnvType(); new (&env_storage_) EnvType();
} }
@ -787,8 +791,7 @@ class SingletonEnv {
} }
private: private:
typename std::aligned_storage<sizeof(EnvType), alignof(EnvType)>::type alignas(EnvType) char env_storage_[sizeof(EnvType)];
env_storage_;
#if !defined(NDEBUG) #if !defined(NDEBUG)
static std::atomic<bool> env_initialized_; static std::atomic<bool> env_initialized_;
#endif // !defined(NDEBUG) #endif // !defined(NDEBUG)

View File

@ -27,7 +27,7 @@ uint32_t Hash(const char* data, size_t n, uint32_t seed) {
uint32_t h = seed ^ (n * m); uint32_t h = seed ^ (n * m);
// Pick up four bytes at a time // Pick up four bytes at a time
while (limit - data >= 4) { while (data + 4 <= limit) {
uint32_t w = DecodeFixed32(data); uint32_t w = DecodeFixed32(data);
data += 4; data += 4;
h += w; h += w;

View File

@ -5,6 +5,7 @@
#ifndef STORAGE_LEVELDB_UTIL_NO_DESTRUCTOR_H_ #ifndef STORAGE_LEVELDB_UTIL_NO_DESTRUCTOR_H_
#define STORAGE_LEVELDB_UTIL_NO_DESTRUCTOR_H_ #define STORAGE_LEVELDB_UTIL_NO_DESTRUCTOR_H_
#include <cstddef>
#include <type_traits> #include <type_traits>
#include <utility> #include <utility>
@ -20,8 +21,12 @@ class NoDestructor {
explicit NoDestructor(ConstructorArgTypes&&... constructor_args) { explicit NoDestructor(ConstructorArgTypes&&... constructor_args) {
static_assert(sizeof(instance_storage_) >= sizeof(InstanceType), static_assert(sizeof(instance_storage_) >= sizeof(InstanceType),
"instance_storage_ is not large enough to hold the instance"); "instance_storage_ is not large enough to hold the instance");
static_assert(std::is_standard_layout_v<NoDestructor<InstanceType>>);
static_assert( static_assert(
alignof(decltype(instance_storage_)) >= alignof(InstanceType), offsetof(NoDestructor, instance_storage_) % alignof(InstanceType) == 0,
"instance_storage_ does not meet the instance's alignment requirement");
static_assert(
alignof(NoDestructor<InstanceType>) % alignof(InstanceType) == 0,
"instance_storage_ does not meet the instance's alignment requirement"); "instance_storage_ does not meet the instance's alignment requirement");
new (&instance_storage_) new (&instance_storage_)
InstanceType(std::forward<ConstructorArgTypes>(constructor_args)...); InstanceType(std::forward<ConstructorArgTypes>(constructor_args)...);
@ -37,8 +42,7 @@ class NoDestructor {
} }
private: private:
typename std::aligned_storage<sizeof(InstanceType), alignas(InstanceType) char instance_storage_[sizeof(InstanceType)];
alignof(InstanceType)>::type instance_storage_;
}; };
} // namespace leveldb } // namespace leveldb