From 302786e211d1f2e6fd260261f642d03a91e5922c Mon Sep 17 00:00:00 2001 From: leveldb Team Date: Wed, 8 Jan 2025 18:54:39 +0000 Subject: [PATCH] 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 --- include/leveldb/slice.h | 3 --- util/env_posix.cc | 9 ++++++--- util/env_windows.cc | 9 ++++++--- util/hash.cc | 2 +- util/no_destructor.h | 10 +++++++--- 5 files changed, 20 insertions(+), 13 deletions(-) diff --git a/include/leveldb/slice.h b/include/leveldb/slice.h index e97223a743..37cb82178d 100644 --- a/include/leveldb/slice.h +++ b/include/leveldb/slice.h @@ -51,9 +51,6 @@ class LEVELDB_EXPORT Slice { // Return true iff the length of the referenced data is zero 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. // REQUIRES: n < size() char operator[](size_t n) const { diff --git a/util/env_posix.cc b/util/env_posix.cc index ffd06c4031..57c19ec70b 100644 --- a/util/env_posix.cc +++ b/util/env_posix.cc @@ -874,7 +874,11 @@ class SingletonEnv { #endif // !defined(NDEBUG) static_assert(sizeof(env_storage_) >= sizeof(EnvType), "env_storage_ will not fit the Env"); - static_assert(alignof(decltype(env_storage_)) >= alignof(EnvType), + static_assert(std::is_standard_layout_v>); + static_assert( + offsetof(SingletonEnv, env_storage_) % alignof(EnvType) == 0, + "env_storage_ does not meet the Env's alignment needs"); + static_assert(alignof(SingletonEnv) % alignof(EnvType) == 0, "env_storage_ does not meet the Env's alignment needs"); new (&env_storage_) EnvType(); } @@ -892,8 +896,7 @@ class SingletonEnv { } private: - typename std::aligned_storage::type - env_storage_; + alignas(EnvType) char env_storage_[sizeof(EnvType)]; #if !defined(NDEBUG) static std::atomic env_initialized_; #endif // !defined(NDEBUG) diff --git a/util/env_windows.cc b/util/env_windows.cc index 1c74b02a7f..e0a19cc5eb 100644 --- a/util/env_windows.cc +++ b/util/env_windows.cc @@ -769,7 +769,11 @@ class SingletonEnv { #endif // !defined(NDEBUG) static_assert(sizeof(env_storage_) >= sizeof(EnvType), "env_storage_ will not fit the Env"); - static_assert(alignof(decltype(env_storage_)) >= alignof(EnvType), + static_assert(std::is_standard_layout_v>); + static_assert( + offsetof(SingletonEnv, env_storage_) % alignof(EnvType) == 0, + "env_storage_ does not meet the Env's alignment needs"); + static_assert(alignof(SingletonEnv) % alignof(EnvType) == 0, "env_storage_ does not meet the Env's alignment needs"); new (&env_storage_) EnvType(); } @@ -787,8 +791,7 @@ class SingletonEnv { } private: - typename std::aligned_storage::type - env_storage_; + alignas(EnvType) char env_storage_[sizeof(EnvType)]; #if !defined(NDEBUG) static std::atomic env_initialized_; #endif // !defined(NDEBUG) diff --git a/util/hash.cc b/util/hash.cc index fa252c72e1..8122fa834d 100644 --- a/util/hash.cc +++ b/util/hash.cc @@ -27,7 +27,7 @@ uint32_t Hash(const char* data, size_t n, uint32_t seed) { uint32_t h = seed ^ (n * m); // Pick up four bytes at a time - while (limit - data >= 4) { + while (data + 4 <= limit) { uint32_t w = DecodeFixed32(data); data += 4; h += w; diff --git a/util/no_destructor.h b/util/no_destructor.h index a0d3b8703d..08ce6a40a9 100644 --- a/util/no_destructor.h +++ b/util/no_destructor.h @@ -5,6 +5,7 @@ #ifndef STORAGE_LEVELDB_UTIL_NO_DESTRUCTOR_H_ #define STORAGE_LEVELDB_UTIL_NO_DESTRUCTOR_H_ +#include #include #include @@ -20,8 +21,12 @@ class NoDestructor { explicit NoDestructor(ConstructorArgTypes&&... constructor_args) { static_assert(sizeof(instance_storage_) >= sizeof(InstanceType), "instance_storage_ is not large enough to hold the instance"); + static_assert(std::is_standard_layout_v>); 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) % alignof(InstanceType) == 0, "instance_storage_ does not meet the instance's alignment requirement"); new (&instance_storage_) InstanceType(std::forward(constructor_args)...); @@ -37,8 +42,7 @@ class NoDestructor { } private: - typename std::aligned_storage::type instance_storage_; + alignas(InstanceType) char instance_storage_[sizeof(InstanceType)]; }; } // namespace leveldb