aboutsummaryrefslogtreecommitdiff
path: root/deps/v8/src/base/lazy-instance.h
diff options
context:
space:
mode:
Diffstat (limited to 'deps/v8/src/base/lazy-instance.h')
-rw-r--r--deps/v8/src/base/lazy-instance.h41
1 files changed, 33 insertions, 8 deletions
diff --git a/deps/v8/src/base/lazy-instance.h b/deps/v8/src/base/lazy-instance.h
index e965382b8d..bebb4e1bdc 100644
--- a/deps/v8/src/base/lazy-instance.h
+++ b/deps/v8/src/base/lazy-instance.h
@@ -68,6 +68,8 @@
#ifndef V8_BASE_LAZY_INSTANCE_H_
#define V8_BASE_LAZY_INSTANCE_H_
+#include <type_traits>
+
#include "src/base/macros.h"
#include "src/base/once.h"
@@ -92,12 +94,8 @@ struct LeakyInstanceTrait {
template <typename T>
struct StaticallyAllocatedInstanceTrait {
- // 16-byte alignment fallback to be on the safe side here.
- struct V8_ALIGNAS(T, 16) StorageType {
- char x[sizeof(T)];
- };
-
- STATIC_ASSERT(V8_ALIGNOF(StorageType) >= V8_ALIGNOF(T));
+ using StorageType =
+ typename std::aligned_storage<sizeof(T), alignof(T)>::type;
static T* MutableInstance(StorageType* storage) {
return reinterpret_cast<T*>(storage);
@@ -112,7 +110,7 @@ struct StaticallyAllocatedInstanceTrait {
template <typename T>
struct DynamicallyAllocatedInstanceTrait {
- typedef T* StorageType;
+ using StorageType = T*;
static T* MutableInstance(StorageType* storage) {
return *storage;
@@ -165,7 +163,7 @@ template <typename T, typename AllocationTrait, typename CreateTrait,
typename InitOnceTrait, typename DestroyTrait /* not used yet. */>
struct LazyInstanceImpl {
public:
- typedef typename AllocationTrait::StorageType StorageType;
+ using StorageType = typename AllocationTrait::StorageType;
private:
static void InitInstance(void* storage) {
@@ -226,6 +224,33 @@ struct LazyDynamicInstance {
CreateTrait, InitOnceTrait, DestroyTrait> type;
};
+// LeakyObject<T> wraps an object of type T, which is initialized in the
+// constructor but never destructed. Thus LeakyObject<T> is trivially
+// destructible and can be used in static (lazily initialized) variables.
+template <typename T>
+class LeakyObject {
+ public:
+ template <typename... Args>
+ explicit LeakyObject(Args&&... args) {
+ new (&storage_) T(std::forward<Args>(args)...);
+ }
+
+ T* get() { return reinterpret_cast<T*>(&storage_); }
+
+ private:
+ typename std::aligned_storage<sizeof(T), alignof(T)>::type storage_;
+
+ DISALLOW_COPY_AND_ASSIGN(LeakyObject);
+};
+
+// Define a function which returns a pointer to a lazily initialized and never
+// destructed object of type T.
+#define DEFINE_LAZY_LEAKY_OBJECT_GETTER(T, FunctionName, ...) \
+ T* FunctionName() { \
+ static ::v8::base::LeakyObject<T> object{__VA_ARGS__}; \
+ return object.get(); \
+ }
+
} // namespace base
} // namespace v8