Debian Codegraph @

js/src/gc/Allocator-inl.h

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 /*
8 * Inline definitions of the CellAllocator methods.
9 *
10 * This is included from JSContext-inl.h for the definiton of JSContext::newCell
11 * and shouldn't need to be included elsewhere.
12 */
13
14 #ifndef gc_Allocator_inl_h
15 #define gc_Allocator_inl_h
16
17 #include "gc/Allocator.h"
18
19 #include "gc/Cell.h"
20 #include "gc/Zone.h"
21 #include "js/Class.h"
22 #include "js/RootingAPI.h"
23
24 #include "gc/Nursery-inl.h"
25
26 namespace js {
27 namespace gc {
28
29 template <typename T, AllowGC allowGC, typename... Args>
30 T* CellAllocator::NewCell(JSContext* cx, Args&&... args) {
31 static_assert(std::is_base_of_v<gc::Cell, T>);
32
33 // Objects. See the valid parameter list in NewObject, above.
34 if constexpr (std::is_base_of_v<JSObject, T>) {
35 return NewObject<T, allowGC>(cx, std::forward<Args>(args)...);
36 }
37
38 // BigInt
39 else if constexpr (std::is_base_of_v<JS::BigInt, T>) {
40 return NewBigInt<T, allowGC>(cx, std::forward<Args>(args)...);
41 }
42
43 // "Normal" strings (all of which can be nursery allocated). Atoms and
44 // external strings will fall through to the generic code below. All other
45 // strings go through NewString, which will forward the arguments to the
46 // appropriate string class's constructor.
47 else if constexpr (std::is_base_of_v<JSString, T> &&
48 !std::is_base_of_v<JSAtom, T> &&
49 !std::is_base_of_v<JSExternalString, T>) {
50 return NewString<T, allowGC>(cx, std::forward<Args>(args)...);
51 }
52
53 else {
54 // Allocate a new tenured GC thing that's not nursery-allocatable. Use
55 // cx->newCell<T>(...), where the parameters are forwarded to the type's
56 // constructor.
57 return NewTenuredCell<T, allowGC>(cx, std::forward<Args>(args)...);
58 }
59 }
60
61 template <typename T, AllowGC allowGC, typename... Args>
62 /* static */
63 T* CellAllocator::NewString(JSContext* cx, gc::Heap heap, Args&&... args) {
64 static_assert(std::is_base_of_v<JSString, T>);
65 gc::AllocKind kind = gc::MapTypeToAllocKind<T>::kind;
66 void* ptr = AllocNurseryOrTenuredCell<JS::TraceKind::String, allowGC>(
67 cx, kind, sizeof(T), heap, nullptr);
68 if (MOZ_UNLIKELY(!ptr)) {
69 return nullptr;
70 }
71 return new (mozilla::KnownNotNull, ptr) T(std::forward<Args>(args)...);
72 }
73
74 template <typename T, AllowGC allowGC>
75 /* static */
76 T* CellAllocator::NewBigInt(JSContext* cx, Heap heap) {
77 void* ptr = AllocNurseryOrTenuredCell<JS::TraceKind::BigInt, allowGC>(
78 cx, gc::AllocKind::BIGINT, sizeof(T), heap, nullptr);
79 if (MOZ_UNLIKELY(!ptr)) {
80 return nullptr;
81 }
82 return new (mozilla::KnownNotNull, ptr) T();
83 }
84
85 template <typename T, AllowGC allowGC>
86 /* static */
87 T* CellAllocator::NewObject(JSContext* cx, gc::AllocKind kind, gc::Heap heap,
88 const JSClass* clasp, gc::AllocSite* site) {
89 MOZ_ASSERT(IsObjectAllocKind(kind));
90 MOZ_ASSERT_IF(heap != gc::Heap::Tenured && clasp->hasFinalize() &&
91 !clasp->isProxyObject(),
92 CanNurseryAllocateFinalizedClass(clasp));
93 size_t thingSize = JSObject::thingSize(kind);
94 void* cell = AllocNurseryOrTenuredCell<JS::TraceKind::Object, allowGC>(
95 cx, kind, thingSize, heap, site);
96 if (MOZ_UNLIKELY(!cell)) {
97 return nullptr;
98 }
99 return new (mozilla::KnownNotNull, cell) T();
100 }
101
102 template <typename T, AllowGC allowGC, typename... Args>
103 /* static */
104 T* CellAllocator::NewTenuredCell(JSContext* cx, Args&&... args) {
105 gc::AllocKind kind = gc::MapTypeToAllocKind<T>::kind;
106 MOZ_ASSERT(Arena::thingSize(kind) == sizeof(T));
107 void* cell = AllocTenuredCell<allowGC>(cx, kind);
108 if (MOZ_UNLIKELY(!cell)) {
109 return nullptr;
110 }
111 return new (mozilla::KnownNotNull, cell) T(std::forward<Args>(args)...);
112 }
113
114 #if defined(DEBUG) || defined(JS_GC_ZEAL) || defined(JS_OOM_BREAKPOINT)
115
116 // This serves as a single point to perform some unrelated checks that happens
117 // before every allocation. Performs the following:
118 //
119 // - checks we can't GC inside a JS::AutoAssertNoGC region
120 // - runs a zeal GC if needed
121 //
122 // This is a no-op in release builds.
123 //
124 // This is only called on paths where GC is allowed.
125 inline void PreAllocGCChecks(JSContext* cx) {
126 // Crash if we could perform a GC action when it is not safe.
127 if (!cx->suppressGC) {
128 cx->verifyIsSafeToGC();
129 }
130
131 # ifdef JS_GC_ZEAL
132 GCRuntime* gc = &cx->runtime()->gc;
133 if (gc->needZealousGC()) {
134 gc->runDebugGC();
135 }
136 # endif
137 }
138
139 inline bool CheckForSimulatedFailure(JSContext* cx, AllowGC allowGC) {
140 // For testing out of memory conditions.
141 if (js::oom::ShouldFailWithOOM()) {
142 // If we are doing a fallible allocation, percolate up the OOM instead of
143 // reporting it.
144 if (allowGC) {
145 ReportOutOfMemory(cx);
146 }
147 return false;
148 }
149
150 return true;
151 }
152 #else
153
154 inline void PreAllocGCChecks(JSContext* cx) {}
155 inline bool CheckForSimulatedFailure(JSContext* cx, AllowGC allowGC) {
156 return true;
157 }
158
159 #endif // DEBUG || JS_GC_ZEAL || JS_OOM_BREAKPOINT
160
161 template <JS::TraceKind traceKind, AllowGC allowGC>
162 /* static */
163 void* CellAllocator::AllocNurseryOrTenuredCell(JSContext* cx,
164 gc::AllocKind allocKind,
165 size_t thingSize, gc::Heap heap,
166 AllocSite* site) {
167 MOZ_ASSERT(IsNurseryAllocable(allocKind));
168 MOZ_ASSERT(MapAllocToTraceKind(allocKind) == traceKind);
169 MOZ_ASSERT(thingSize == Arena::thingSize(allocKind));
170 MOZ_ASSERT_IF(site && site->initialHeap() == Heap::Tenured,
171 heap == Heap::Tenured);
172 MOZ_ASSERT(!cx->zone()->isAtomsZone());
173 MOZ_ASSERT(CurrentThreadCanAccessRuntime(cx->runtime()));
174
175 if constexpr (allowGC) {
176 PreAllocGCChecks(cx);
177 }
178
179 if (!CheckForSimulatedFailure(cx, allowGC)) {
180 return nullptr;
181 }
182
183 JS::Zone* zone = cx->zone();
184 gc::Heap minHeapToTenure = CheckedHeap(zone->minHeapToTenure(traceKind));
185 if (CheckedHeap(heap) < minHeapToTenure) {
186 if (!site) {
187 site = zone->unknownAllocSite(traceKind);
188 }
189
190 #ifdef JS_GC_ZEAL
191 site = MaybeGenerateMissingAllocSite(cx, traceKind, site);
192 #endif
193
194 void* ptr = cx->nursery().tryAllocateCell(site, thingSize, traceKind);
195 if (MOZ_LIKELY(ptr)) {
196 return ptr;
197 }
198
199 return RetryNurseryAlloc<allowGC>(cx, traceKind, allocKind, thingSize,
200 site);
201 }
202
203 return AllocTenuredCellForNurseryAlloc<allowGC>(cx, allocKind);
204 }
205
206 /* static */
207 MOZ_ALWAYS_INLINE gc::Heap CellAllocator::CheckedHeap(gc::Heap heap) {
208 if (heap > Heap::Tenured) {
209 // This helps the compiler to see that nursery allocation is never
210 // possible if Heap::Tenured is specified.
211 MOZ_MAKE_COMPILER_ASSUME_IS_UNREACHABLE("Bad gc::Heap value");
212 }
213
214 return heap;
215 }
216
217 } // namespace gc
218 } // namespace js
219
220 #endif // gc_Allocator_inl_h