Skip to content

Commit 302786e

Browse files
leveldb Teampwnall
leveldb Team
authored andcommitted
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
1 parent 578eeb7 commit 302786e

File tree

5 files changed

+20
-13
lines changed

5 files changed

+20
-13
lines changed

include/leveldb/slice.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,6 @@ class LEVELDB_EXPORT Slice {
5151
// Return true iff the length of the referenced data is zero
5252
bool empty() const { return size_ == 0; }
5353

54-
const char* begin() const { return data(); }
55-
const char* end() const { return data() + size(); }
56-
5754
// Return the ith byte in the referenced data.
5855
// REQUIRES: n < size()
5956
char operator[](size_t n) const {

util/env_posix.cc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,11 @@ class SingletonEnv {
874874
#endif // !defined(NDEBUG)
875875
static_assert(sizeof(env_storage_) >= sizeof(EnvType),
876876
"env_storage_ will not fit the Env");
877-
static_assert(alignof(decltype(env_storage_)) >= alignof(EnvType),
877+
static_assert(std::is_standard_layout_v<SingletonEnv<EnvType>>);
878+
static_assert(
879+
offsetof(SingletonEnv<EnvType>, env_storage_) % alignof(EnvType) == 0,
880+
"env_storage_ does not meet the Env's alignment needs");
881+
static_assert(alignof(SingletonEnv<EnvType>) % alignof(EnvType) == 0,
878882
"env_storage_ does not meet the Env's alignment needs");
879883
new (&env_storage_) EnvType();
880884
}
@@ -892,8 +896,7 @@ class SingletonEnv {
892896
}
893897

894898
private:
895-
typename std::aligned_storage<sizeof(EnvType), alignof(EnvType)>::type
896-
env_storage_;
899+
alignas(EnvType) char env_storage_[sizeof(EnvType)];
897900
#if !defined(NDEBUG)
898901
static std::atomic<bool> env_initialized_;
899902
#endif // !defined(NDEBUG)

util/env_windows.cc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,11 @@ class SingletonEnv {
769769
#endif // !defined(NDEBUG)
770770
static_assert(sizeof(env_storage_) >= sizeof(EnvType),
771771
"env_storage_ will not fit the Env");
772-
static_assert(alignof(decltype(env_storage_)) >= alignof(EnvType),
772+
static_assert(std::is_standard_layout_v<SingletonEnv<EnvType>>);
773+
static_assert(
774+
offsetof(SingletonEnv<EnvType>, env_storage_) % alignof(EnvType) == 0,
775+
"env_storage_ does not meet the Env's alignment needs");
776+
static_assert(alignof(SingletonEnv<EnvType>) % alignof(EnvType) == 0,
773777
"env_storage_ does not meet the Env's alignment needs");
774778
new (&env_storage_) EnvType();
775779
}
@@ -787,8 +791,7 @@ class SingletonEnv {
787791
}
788792

789793
private:
790-
typename std::aligned_storage<sizeof(EnvType), alignof(EnvType)>::type
791-
env_storage_;
794+
alignas(EnvType) char env_storage_[sizeof(EnvType)];
792795
#if !defined(NDEBUG)
793796
static std::atomic<bool> env_initialized_;
794797
#endif // !defined(NDEBUG)

util/hash.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ uint32_t Hash(const char* data, size_t n, uint32_t seed) {
2727
uint32_t h = seed ^ (n * m);
2828

2929
// Pick up four bytes at a time
30-
while (limit - data >= 4) {
30+
while (data + 4 <= limit) {
3131
uint32_t w = DecodeFixed32(data);
3232
data += 4;
3333
h += w;

util/no_destructor.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#ifndef STORAGE_LEVELDB_UTIL_NO_DESTRUCTOR_H_
66
#define STORAGE_LEVELDB_UTIL_NO_DESTRUCTOR_H_
77

8+
#include <cstddef>
89
#include <type_traits>
910
#include <utility>
1011

@@ -20,8 +21,12 @@ class NoDestructor {
2021
explicit NoDestructor(ConstructorArgTypes&&... constructor_args) {
2122
static_assert(sizeof(instance_storage_) >= sizeof(InstanceType),
2223
"instance_storage_ is not large enough to hold the instance");
24+
static_assert(std::is_standard_layout_v<NoDestructor<InstanceType>>);
2325
static_assert(
24-
alignof(decltype(instance_storage_)) >= alignof(InstanceType),
26+
offsetof(NoDestructor, instance_storage_) % alignof(InstanceType) == 0,
27+
"instance_storage_ does not meet the instance's alignment requirement");
28+
static_assert(
29+
alignof(NoDestructor<InstanceType>) % alignof(InstanceType) == 0,
2530
"instance_storage_ does not meet the instance's alignment requirement");
2631
new (&instance_storage_)
2732
InstanceType(std::forward<ConstructorArgTypes>(constructor_args)...);
@@ -37,8 +42,7 @@ class NoDestructor {
3742
}
3843

3944
private:
40-
typename std::aligned_storage<sizeof(InstanceType),
41-
alignof(InstanceType)>::type instance_storage_;
45+
alignas(InstanceType) char instance_storage_[sizeof(InstanceType)];
4246
};
4347

4448
} // namespace leveldb

0 commit comments

Comments
 (0)