mirror of
https://github.com/google/leveldb.git
synced 2025-03-10 09:40:06 +08:00
cl/713346733 changed the type of some variables to pointers, but didn't adjust the placement new statements. From pkasting@: "I suspect your code is wrong and will crash. An array is a pointer, so taking its address also gives a pointer, which is why it compiles; but the value of that pointer is different. You're no longer providing the address of the storage, but rather the address of the array pointer." PiperOrigin-RevId: 717926210
51 lines
1.7 KiB
C++
51 lines
1.7 KiB
C++
// Copyright (c) 2018 The LevelDB Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
|
|
|
#ifndef STORAGE_LEVELDB_UTIL_NO_DESTRUCTOR_H_
|
|
#define STORAGE_LEVELDB_UTIL_NO_DESTRUCTOR_H_
|
|
|
|
#include <cstddef>
|
|
#include <type_traits>
|
|
#include <utility>
|
|
|
|
namespace leveldb {
|
|
|
|
// Wraps an instance whose destructor is never called.
|
|
//
|
|
// This is intended for use with function-level static variables.
|
|
template <typename InstanceType>
|
|
class NoDestructor {
|
|
public:
|
|
template <typename... ConstructorArgTypes>
|
|
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<NoDestructor<InstanceType>>);
|
|
static_assert(
|
|
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");
|
|
new (instance_storage_)
|
|
InstanceType(std::forward<ConstructorArgTypes>(constructor_args)...);
|
|
}
|
|
|
|
~NoDestructor() = default;
|
|
|
|
NoDestructor(const NoDestructor&) = delete;
|
|
NoDestructor& operator=(const NoDestructor&) = delete;
|
|
|
|
InstanceType* get() {
|
|
return reinterpret_cast<InstanceType*>(&instance_storage_);
|
|
}
|
|
|
|
private:
|
|
alignas(InstanceType) char instance_storage_[sizeof(InstanceType)];
|
|
};
|
|
|
|
} // namespace leveldb
|
|
|
|
#endif // STORAGE_LEVELDB_UTIL_NO_DESTRUCTOR_H_
|