68 #ifndef V8_BASE_LAZY_INSTANCE_H_ 69 #define V8_BASE_LAZY_INSTANCE_H_ 71 #include "src/base/macros.h" 72 #include "src/base/once.h" 77 #define LAZY_STATIC_INSTANCE_INITIALIZER { V8_ONCE_INIT, { {} } } 78 #define LAZY_DYNAMIC_INSTANCE_INITIALIZER { V8_ONCE_INIT, 0 } 81 #define LAZY_INSTANCE_INITIALIZER LAZY_STATIC_INSTANCE_INITIALIZER 86 static void Destroy(
T* ) {}
96 struct V8_ALIGNAS(
T, 16) StorageType {
100 STATIC_ASSERT(V8_ALIGNOF(StorageType) >= V8_ALIGNOF(
T));
102 static T* MutableInstance(StorageType* storage) {
103 return reinterpret_cast<T*
>(storage);
106 template <
typename ConstructTrait>
107 static void InitStorageUsingTrait(StorageType* storage) {
108 ConstructTrait::Construct(storage);
113 template <
typename T>
121 template <
typename CreateTrait>
122 static void InitStorageUsingTrait(
StorageType* storage) {
123 *storage = CreateTrait::Create();
128 template <
typename T>
131 static void Construct(
void* allocated_ptr) {
new (allocated_ptr)
T(); }
135 template <
typename T>
144 template <
typename Function,
typename Storage>
145 static void Init(OnceType* once,
Function function, Storage storage) {
146 CallOnce(once,
function, storage);
153 template <
typename Function,
typename Storage>
154 static void Init(OnceType* once,
Function function, Storage storage) {
155 if (*once == ONCE_STATE_UNINITIALIZED) {
157 *once = ONCE_STATE_DONE;
164 template <
typename T,
typename AllocationTrait,
typename CreateTrait,
165 typename InitOnceTrait,
typename DestroyTrait >
168 typedef typename AllocationTrait::StorageType StorageType;
171 static void InitInstance(
void* storage) {
172 AllocationTrait::template InitStorageUsingTrait<CreateTrait>(
173 static_cast<StorageType*
>(storage));
177 InitOnceTrait::Init(&once_, &InitInstance, static_cast<void*>(&storage_));
183 return AllocationTrait::MutableInstance(&storage_);
186 const T& Get()
const {
188 return *AllocationTrait::MutableInstance(&storage_);
191 mutable OnceType once_;
195 mutable StorageType storage_;
199 template <
typename T,
205 CreateTrait, InitOnceTrait, DestroyTrait>
type;
209 template <
typename T,
220 template <
typename T,
226 CreateTrait, InitOnceTrait, DestroyTrait>
type;
232 #endif // V8_BASE_LAZY_INSTANCE_H_