1/*
2 * Copyright (C) 2013-2019 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include <type_traits>
29#include <utility>
30#include <wtf/ForbidHeapAllocation.h>
31#include <wtf/RefCounted.h>
32
33// NeverDestroyed is a smart-pointer-like class that ensures that the destructor
34// for the given object is never called, but doesn't use the heap to allocate it.
35// It's useful for static local variables, and can be used like so:
36//
37// MySharedGlobal& mySharedGlobal()
38// {
39// static NeverDestroyed<MySharedGlobal> myGlobal("Hello", 42);
40// return myGlobal;
41// }
42
43namespace WTF {
44
45template<typename T> class NeverDestroyed {
46 WTF_MAKE_NONCOPYABLE(NeverDestroyed);
47 WTF_FORBID_HEAP_ALLOCATION;
48public:
49
50 template<typename... Args> NeverDestroyed(Args&&... args)
51 {
52 MaybeRelax<T>(new (storagePointer()) T(std::forward<Args>(args)...));
53 }
54
55 NeverDestroyed(NeverDestroyed&& other)
56 {
57 MaybeRelax<T>(new (storagePointer()) T(WTFMove(*other.storagePointer())));
58 }
59
60 operator T&() { return *storagePointer(); }
61 T& get() { return *storagePointer(); }
62
63 T* operator->() { return storagePointer(); }
64
65 operator const T&() const { return *storagePointer(); }
66 const T& get() const { return *storagePointer(); }
67
68 const T* operator->() const { return storagePointer(); }
69
70private:
71 using PointerType = typename std::remove_const<T>::type*;
72
73 PointerType storagePointer() const { return const_cast<PointerType>(reinterpret_cast<const T*>(&m_storage)); }
74
75 template<typename PtrType, bool ShouldRelax = std::is_base_of<RefCountedBase, PtrType>::value> struct MaybeRelax {
76 explicit MaybeRelax(PtrType*) { }
77 };
78 template<typename PtrType> struct MaybeRelax<PtrType, true> {
79 explicit MaybeRelax(PtrType* ptr) { ptr->relaxAdoptionRequirement(); }
80 };
81
82 // FIXME: Investigate whether we should allocate a hunk of virtual memory
83 // and hand out chunks of it to NeverDestroyed instead, to reduce fragmentation.
84 typename std::aligned_storage<sizeof(T), std::alignment_of<T>::value>::type m_storage;
85};
86
87template<typename T> NeverDestroyed<T> makeNeverDestroyed(T&&);
88
89// FIXME: It's messy to have to repeat the whole class just to make this "lazy" version.
90// Should revisit clients to see if we really need this, and perhaps use templates to
91// share more of the code with the main NeverDestroyed above.
92template<typename T> class LazyNeverDestroyed {
93 WTF_MAKE_NONCOPYABLE(LazyNeverDestroyed);
94 WTF_FORBID_HEAP_ALLOCATION;
95public:
96 LazyNeverDestroyed() = default;
97
98 template<typename... Args>
99 void construct(Args&&... args)
100 {
101 ASSERT(!m_isConstructed);
102
103#if !ASSERT_DISABLED
104 m_isConstructed = true;
105#endif
106
107 MaybeRelax<T>(new (storagePointer()) T(std::forward<Args>(args)...));
108 }
109
110 operator T&() { return *storagePointer(); }
111 T& get() { return *storagePointer(); }
112
113 T* operator->() { return storagePointer(); }
114
115 operator const T&() const { return *storagePointer(); }
116 const T& get() const { return *storagePointer(); }
117
118 const T* operator->() const { return storagePointer(); }
119
120#if !ASSERT_DISABLED
121 bool isConstructed() const { return m_isConstructed; }
122#endif
123
124private:
125 using PointerType = typename std::remove_const<T>::type*;
126
127 PointerType storagePointer() const
128 {
129 ASSERT(m_isConstructed);
130 return const_cast<PointerType>(reinterpret_cast<const T*>(&m_storage));
131 }
132
133 template<typename PtrType, bool ShouldRelax = std::is_base_of<RefCountedBase, PtrType>::value> struct MaybeRelax {
134 explicit MaybeRelax(PtrType*) { }
135 };
136 template<typename PtrType> struct MaybeRelax<PtrType, true> {
137 explicit MaybeRelax(PtrType* ptr) { ptr->relaxAdoptionRequirement(); }
138 };
139
140#if !ASSERT_DISABLED
141 // LazyNeverDestroyed objects are always static, so this variable is initialized to false.
142 // It must not be initialized dynamically; that would not be thread safe.
143 bool m_isConstructed;
144#endif
145
146 // FIXME: Investigate whether we should allocate a hunk of virtual memory
147 // and hand out chunks of it to NeverDestroyed instead, to reduce fragmentation.
148 typename std::aligned_storage<sizeof(T), std::alignment_of<T>::value>::type m_storage;
149};
150
151template<typename T> inline NeverDestroyed<T> makeNeverDestroyed(T&& argument)
152{
153 return WTFMove(argument);
154}
155
156} // namespace WTF;
157
158using WTF::LazyNeverDestroyed;
159using WTF::NeverDestroyed;
160using WTF::makeNeverDestroyed;
161