Debian Codegraph @

js/src/gc/Nursery-inl.h

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=4 sw=2 et tw=80 ft=cpp:
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7
8 #ifndef gc_Nursery_inl_h
9 #define gc_Nursery_inl_h
10
11 #include "gc/Nursery.h"
12
13 #include "gc/GCRuntime.h"
14 #include "gc/RelocationOverlay.h"
15 #include "js/TracingAPI.h"
16 #include "vm/JSContext.h"
17 #include "vm/NativeObject.h"
18 #include "vm/StringType.h"
19
20 namespace js {
21 namespace gc {
22 struct Cell;
23 } // namespace gc
24 } // namespace js
25
26 inline JSRuntime* js::Nursery::runtime() const { return gc->rt; }
27
28 template <typename T>
29 bool js::Nursery::isInside(const SharedMem<T>& p) const {
30 return isInside(p.unwrap(/*safe - used for value in comparison above*/));
31 }
32
33 inline void js::Nursery::addMallocedBufferBytes(size_t nbytes) {
34 MOZ_ASSERT(nbytes > 0);
35 toSpace.mallocedBufferBytes += nbytes;
36 if (MOZ_UNLIKELY(toSpace.mallocedBufferBytes > capacity() * 8)) {
37 requestMinorGC(JS::GCReason::NURSERY_MALLOC_BUFFERS);
38 }
39 }
40
41 inline bool js::Nursery::addStringBuffer(JSLinearString* s) {
42 MOZ_ASSERT(IsInsideNursery(s));
43 MOZ_ASSERT(isEnabled());
44 MOZ_ASSERT(s->hasStringBuffer());
45
46 auto* buffer = s->stringBuffer();
47 if (!stringBuffers_.emplaceBack(s, buffer)) {
48 return false;
49 }
50
51 // Note: update mallocedBufferBytes only if the buffer has a refcount of 1, to
52 // avoid double counting when the same buffer is used by multiple nursery
53 // strings.
54 if (!buffer->HasMultipleReferences()) {
55 addMallocedBufferBytes(buffer->AllocationSize());
56 }
57 return true;
58 }
59
60 inline bool js::Nursery::addExtensibleStringBuffer(
61 JSLinearString* s, mozilla::StringBuffer* buffer, bool updateMallocBytes) {
62 MOZ_ASSERT(IsInsideNursery(s));
63 MOZ_ASSERT(isEnabled());
64 if (!extensibleStringBuffers_.putNew(s, buffer)) {
65 return false;
66 }
67 MOZ_ASSERT(!buffer->HasMultipleReferences());
68 if (updateMallocBytes) {
69 addMallocedBufferBytes(buffer->AllocationSize());
70 }
71 return true;
72 }
73
74 inline void js::Nursery::removeMallocedBuffer(void* buffer, size_t nbytes) {
75 MOZ_ASSERT(!JS::RuntimeHeapIsMinorCollecting());
76 MOZ_ASSERT(toSpace.mallocedBuffers.has(buffer));
77 MOZ_ASSERT(nbytes > 0);
78 MOZ_ASSERT(toSpace.mallocedBufferBytes >= nbytes);
79 toSpace.mallocedBuffers.remove(buffer);
80 toSpace.mallocedBufferBytes -= nbytes;
81 }
82
83 void js::Nursery::removeMallocedBufferDuringMinorGC(void* buffer) {
84 MOZ_ASSERT(JS::RuntimeHeapIsMinorCollecting());
85 MOZ_ASSERT(fromSpace.mallocedBuffers.has(buffer));
86 fromSpace.mallocedBuffers.remove(buffer);
87 }
88
89 inline void js::Nursery::removeExtensibleStringBuffer(JSLinearString* s,
90 bool updateMallocBytes) {
91 MOZ_ASSERT(gc::IsInsideNursery(s));
92 extensibleStringBuffers_.remove(s);
93
94 if (updateMallocBytes) {
95 size_t nbytes = s->stringBuffer()->AllocationSize();
96 MOZ_ASSERT(toSpace.mallocedBufferBytes >= nbytes);
97 toSpace.mallocedBufferBytes -= nbytes;
98 }
99 }
100
101 inline bool js::Nursery::shouldTenure(gc::Cell* cell) {
102 MOZ_ASSERT(semispaceEnabled());
103 MOZ_ASSERT(inCollectedRegion(cell));
104
105 size_t offset = fromSpace.offsetFromAddress(uintptr_t(cell));
106 MOZ_ASSERT(offset >=
107 fromSpace.offsetFromExclusiveAddress(fromSpace.startPosition_));
108 return offset <= tenureThreshold_;
109 }
110
111 inline bool js::Nursery::inCollectedRegion(const gc::Cell* cell) const {
112 return gc::InCollectedNurseryRegion(cell);
113 }
114
115 inline bool js::Nursery::inCollectedRegion(void* ptr) const {
116 if (!semispaceEnabled()) {
117 return toSpace.isInside(ptr);
118 }
119
120 return fromSpace.isInside(ptr);
121 }
122
123 inline size_t js::Nursery::Space::offsetFromExclusiveAddress(
124 uintptr_t addr) const {
125 if ((addr & gc::ChunkMask) == 0) {
126 // |addr| points one past the end of the previous chunk.
127 return offsetFromAddress(addr - 1) + 1;
128 }
129
130 return offsetFromAddress(addr);
131 }
132
133 inline size_t js::Nursery::Space::offsetFromAddress(uintptr_t addr) const {
134 gc::ChunkBase* chunk =
135 gc::detail::GetCellChunkBase(reinterpret_cast<gc::Cell*>(addr));
136 MOZ_ASSERT(chunk->getKind() == kind);
137 MOZ_ASSERT(findChunkIndex(addr & ~gc::ChunkMask) == chunk->nurseryChunkIndex);
138
139 uint32_t offset = addr & gc::ChunkMask;
140 MOZ_ASSERT(offset >= sizeof(gc::ChunkBase));
141 return (chunk->nurseryChunkIndex << gc::ChunkShift) | offset;
142 }
143
144 MOZ_ALWAYS_INLINE /* static */ bool js::Nursery::getForwardedPointer(
145 js::gc::Cell** ref) {
146 js::gc::Cell* cell = (*ref);
147 MOZ_ASSERT(IsInsideNursery(cell));
148 if (!cell->isForwarded()) {
149 return false;
150 }
151 const gc::RelocationOverlay* overlay = gc::RelocationOverlay::fromCell(cell);
152 *ref = overlay->forwardingAddress();
153 return true;
154 }
155
156 inline void js::Nursery::maybeSetForwardingPointer(JSTracer* trc, void* oldData,
157 void* newData, bool direct) {
158 if (trc->isTenuringTracer()) {
159 setForwardingPointerWhileTenuring(oldData, newData, direct);
160 }
161 }
162
163 inline void js::Nursery::setForwardingPointerWhileTenuring(void* oldData,
164 void* newData,
165 bool direct) {
166 if (isInside(oldData)) {
167 setForwardingPointer(oldData, newData, direct);
168 }
169 }
170
171 inline void js::Nursery::setSlotsForwardingPointer(HeapSlot* oldSlots,
172 HeapSlot* newSlots,
173 uint32_t nslots) {
174 // Slot arrays always have enough space for a forwarding pointer, since the
175 // number of slots is never zero.
176 MOZ_ASSERT(nslots > 0);
177 setDirectForwardingPointer(oldSlots, newSlots);
178 }
179
180 inline void js::Nursery::setElementsForwardingPointer(ObjectElements* oldHeader,
181 ObjectElements* newHeader,
182 uint32_t capacity) {
183 // Only use a direct forwarding pointer if there is enough space for one.
184 setForwardingPointer(oldHeader->elements(), newHeader->elements(),
185 capacity > 0);
186 }
187
188 inline void js::Nursery::setForwardingPointer(void* oldData, void* newData,
189 bool direct) {
190 if (direct) {
191 setDirectForwardingPointer(oldData, newData);
192 return;
193 }
194
195 setIndirectForwardingPointer(oldData, newData);
196 }
197
198 inline void js::Nursery::setDirectForwardingPointer(void* oldData,
199 void* newData) {
200 MOZ_ASSERT(isInside(oldData));
201 MOZ_ASSERT_IF(isInside(newData), !inCollectedRegion(newData));
202
203 new (oldData) BufferRelocationOverlay{newData};
204 }
205
206 inline void* js::Nursery::tryAllocateCell(gc::AllocSite* site, size_t size,
207 JS::TraceKind kind) {
208 // Ensure there's enough space to replace the contents with a
209 // RelocationOverlay.
210 // MOZ_ASSERT(size >= sizeof(RelocationOverlay));
211 MOZ_ASSERT(size % gc::CellAlignBytes == 0);
212 MOZ_ASSERT(size_t(kind) < gc::NurseryTraceKinds);
213 MOZ_ASSERT_IF(kind == JS::TraceKind::String, canAllocateStrings());
214 MOZ_ASSERT_IF(kind == JS::TraceKind::BigInt, canAllocateBigInts());
215
216 void* ptr = tryAllocate(sizeof(gc::NurseryCellHeader) + size);
217 if (MOZ_UNLIKELY(!ptr)) {
218 return nullptr;
219 }
220
221 new (ptr) gc::NurseryCellHeader(site, kind);
222
223 void* cell =
224 reinterpret_cast<void*>(uintptr_t(ptr) + sizeof(gc::NurseryCellHeader));
225 if (!cell) {
226 MOZ_MAKE_COMPILER_ASSUME_IS_UNREACHABLE(
227 "Successful allocation cannot result in nullptr");
228 }
229
230 // Update the allocation site. This code is also inlined in
231 // MacroAssembler::updateAllocSite.
232 uint32_t allocCount = site->incAllocCount();
233 if (allocCount == gc::NormalSiteAttentionThreshold) {
234 pretenuringNursery.insertIntoAllocatedList(site);
235 }
236 MOZ_ASSERT_IF(
237 site->isNormal() && allocCount >= gc::NormalSiteAttentionThreshold,
238 site->isInAllocatedList());
239
240 gc::gcprobes::NurseryAlloc(cell, kind);
241 return cell;
242 }
243
244 inline void* js::Nursery::tryAllocate(size_t size) {
245 MOZ_ASSERT(isEnabled());
246 MOZ_ASSERT_IF(JS::RuntimeHeapIsBusy(), JS::RuntimeHeapIsMinorCollecting());
247 MOZ_ASSERT_IF(currentChunk() == startChunk(), position() >= startPosition());
248 MOZ_ASSERT(size % gc::CellAlignBytes == 0);
249 MOZ_ASSERT(position() % gc::CellAlignBytes == 0);
250
251 if (MOZ_UNLIKELY(currentEnd() < position() + size)) {
252 return nullptr;
253 }
254
255 void* ptr = reinterpret_cast<void*>(position());
256 if (!ptr) {
257 MOZ_MAKE_COMPILER_ASSUME_IS_UNREACHABLE(
258 "Successful allocation cannot result in nullptr");
259 }
260
261 toSpace.position_ = position() + size;
262
263 DebugOnlyPoison(ptr, JS_ALLOCATED_NURSERY_PATTERN, size,
264 MemCheckKind::MakeUndefined);
265
266 return ptr;
267 }
268
269 inline bool js::Nursery::registerTrailer(PointerAndUint7 blockAndListID,
270 size_t nBytes) {
271 MOZ_ASSERT(toSpace.trailersAdded_.length() ==
272 toSpace.trailersRemoved_.length());
273 MOZ_ASSERT(nBytes > 0);
274 if (MOZ_UNLIKELY(!toSpace.trailersAdded_.append(blockAndListID))) {
275 return false;
276 }
277 if (MOZ_UNLIKELY(!toSpace.trailersRemoved_.append(nullptr))) {
278 toSpace.trailersAdded_.popBack();
279 return false;
280 }
281
282 // This is a clone of the logic in ::registerMallocedBuffer. It may be
283 // that some other heuristic is better, once we know more about the
284 // typical behaviour of wasm-GC applications.
285 toSpace.trailerBytes_ += nBytes;
286 if (MOZ_UNLIKELY(toSpace.trailerBytes_ > capacity() * 8)) {
287 requestMinorGC(JS::GCReason::NURSERY_TRAILERS);
288 }
289 return true;
290 }
291
292 inline void js::Nursery::unregisterTrailer(void* block) {
293 // Unlike removeMallocedBuffer this is only called during minor GC.
294 MOZ_ASSERT(fromSpace.trailersRemovedUsed_ <
295 fromSpace.trailersRemoved_.length());
296 fromSpace.trailersRemoved_[fromSpace.trailersRemovedUsed_] = block;
297 fromSpace.trailersRemovedUsed_++;
298 }
299
300 namespace js {
301
302 // The allocation methods below will not run the garbage collector. If the
303 // nursery cannot accomodate the allocation, the malloc heap will be used
304 // instead.
305
306 template <typename T>
307 static inline T* AllocNurseryOrMallocBuffer(Nursery& nursery, gc::Cell* cell,
308 uint32_t count) {
309 size_t nbytes = RoundUp(count * sizeof(T), sizeof(Value));
310 return static_cast<T*>(nursery.allocNurseryOrMallocBuffer(
311 cell->zone(), cell, nbytes, js::MallocArena));
312 }
313
314 template <typename T>
315 static inline T* AllocNurseryOrMallocBuffer(JSContext* cx, gc::Cell* cell,
316 uint32_t count) {
317 T* buffer = AllocNurseryOrMallocBuffer<T>(cx->nursery(), cell, count);
318 if (!buffer) {
319 ReportOutOfMemory(cx);
320 return nullptr;
321 }
322
323 return buffer;
324 }
325
326 template <typename T>
327 static inline T* AllocateCellBuffer(Nursery& nursery, JS::Zone* zone,
328 gc::Cell* cell, uint32_t count) {
329 MOZ_ASSERT(zone == cell->zone());
330
331 size_t nbytes = RoundUp(count * sizeof(T), sizeof(Value));
332 return static_cast<T*>(nursery.allocateBuffer(zone, cell, nbytes));
333 }
334
335 template <typename T>
336 static inline T* AllocateCellBuffer(JSContext* cx, gc::Cell* cell,
337 uint32_t count) {
338 T* buffer = AllocateCellBuffer<T>(cx->nursery(), cx->zone(), cell, count);
339 if (!buffer) {
340 ReportOutOfMemory(cx);
341 return nullptr;
342 }
343
344 return buffer;
345 }
346
347 // If this returns null then the old buffer will be left alone.
348 template <typename T>
349 static inline T* ReallocNurseryOrMallocBuffer(JSContext* cx, gc::Cell* cell,
350 T* oldBuffer, uint32_t oldCount,
351 uint32_t newCount,
352 arena_id_t arenaId) {
353 size_t oldBytes = RoundUp(oldCount * sizeof(T), sizeof(Value));
354 size_t newBytes = RoundUp(newCount * sizeof(T), sizeof(Value));
355
356 T* buffer = static_cast<T*>(cx->nursery().reallocNurseryOrMallocBuffer(
357 cell->zone(), cell, oldBuffer, oldBytes, newBytes, arenaId));
358 if (!buffer) {
359 ReportOutOfMemory(cx);
360 }
361
362 return buffer;
363 }
364
365 // If this returns null then the old buffer will be left alone.
366 template <typename T>
367 static inline T* ReallocateCellBuffer(Nursery& nursery, JS::Zone* zone,
368 gc::Cell* cell, T* oldBuffer,
369 uint32_t oldCount, uint32_t newCount) {
370 MOZ_ASSERT(zone == cell->zone());
371
372 size_t oldBytes = RoundUp(oldCount * sizeof(T), sizeof(Value));
373 size_t newBytes = RoundUp(newCount * sizeof(T), sizeof(Value));
374
375 return static_cast<T*>(
376 nursery.reallocateBuffer(zone, cell, oldBuffer, oldBytes, newBytes));
377 }
378
379 // If this returns null then the old buffer will be left alone.
380 template <typename T>
381 static inline T* ReallocateCellBuffer(JSContext* cx, gc::Cell* cell,
382 T* oldBuffer, uint32_t oldCount,
383 uint32_t newCount) {
384 T* buffer = ReallocateCellBuffer<T>(cx->nursery(), cx->zone(), cell,
385 oldBuffer, oldCount, newCount);
386 if (!buffer) {
387 ReportOutOfMemory(cx);
388 }
389
390 return buffer;
391 }
392
393 } // namespace js
394
395 #endif /* gc_Nursery_inl_h */