| 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 |
#ifndef gc_StoreBuffer_h |
| 8 |
#define gc_StoreBuffer_h |
| 9 |
|
| 10 |
#include "mozilla/Attributes.h" |
| 11 |
#include "mozilla/HashFunctions.h" |
| 12 |
#include "mozilla/ReentrancyGuard.h" |
| 13 |
|
| 14 |
#include <algorithm> |
| 15 |
|
| 16 |
#include "ds/BitArray.h" |
| 17 |
#include "ds/LifoAlloc.h" |
| 18 |
#include "gc/Cell.h" |
| 19 |
#include "gc/Nursery.h" |
| 20 |
#include "gc/TraceKind.h" |
| 21 |
#include "js/AllocPolicy.h" |
| 22 |
#include "js/UniquePtr.h" |
| 23 |
#include "threading/Mutex.h" |
| 24 |
#include "wasm/WasmAnyRef.h" |
| 25 |
|
| 26 |
namespace JS { |
| 27 |
struct GCSizes; |
| 28 |
} |
| 29 |
|
| 30 |
namespace js { |
| 31 |
|
| 32 |
class NativeObject; |
| 33 |
|
| 34 |
#ifdef DEBUG |
| 35 |
extern bool CurrentThreadIsGCMarking(); |
| 36 |
#endif |
| 37 |
|
| 38 |
namespace gc { |
| 39 |
|
| 40 |
class Arena; |
| 41 |
class ArenaCellSet; |
| 42 |
|
| 43 |
#ifdef DEBUG |
| 44 |
extern bool CurrentThreadHasLockedGC(); |
| 45 |
#endif |
| 46 |
|
| 47 |
/* |
| 48 |
* BufferableRef represents an abstract reference for use in the generational |
| 49 |
* GC's remembered set. Entries in the store buffer that cannot be represented |
| 50 |
* with the simple pointer-to-a-pointer scheme must derive from this class and |
| 51 |
* use the generic store buffer interface. |
| 52 |
* |
| 53 |
* A single BufferableRef entry in the generic buffer can represent many entries |
| 54 |
* in the remembered set. For example js::OrderedHashTableRef represents all |
| 55 |
* the incoming edges corresponding to keys in an ordered hash table. |
| 56 |
*/ |
| 57 |
class BufferableRef { |
| 58 |
public: |
| 59 |
virtual void trace(JSTracer* trc) = 0; |
| 60 |
bool maybeInRememberedSet(const Nursery&) const { return true; } |
| 61 |
}; |
| 62 |
|
| 63 |
using EdgeSet = HashSet<void*, PointerHasher<void*>, SystemAllocPolicy>; |
| 64 |
|
| 65 |
/* The size of a single block of store buffer storage space. */ |
| 66 |
static const size_t LifoAllocBlockSize = 8 * 1024; |
| 67 |
|
| 68 |
/* |
| 69 |
* The StoreBuffer observes all writes that occur in the system and performs |
| 70 |
* efficient filtering of them to derive a remembered set for nursery GC. |
| 71 |
*/ |
| 72 |
class StoreBuffer { |
| 73 |
friend class mozilla::ReentrancyGuard; |
| 74 |
|
| 75 |
/* The size at which a block is about to overflow for the generic buffer. */ |
| 76 |
static const size_t GenericBufferLowAvailableThreshold = |
| 77 |
LifoAllocBlockSize / 2; |
| 78 |
|
| 79 |
/* The size at which other store buffers are about to overflow. */ |
| 80 |
static const size_t BufferOverflowThresholdBytes = 128 * 1024; |
| 81 |
|
| 82 |
enum class PutResult { OK, AboutToOverflow }; |
| 83 |
|
| 84 |
/* |
| 85 |
* This buffer holds only a single type of edge. Using this buffer is more |
| 86 |
* efficient than the generic buffer when many writes will be to the same |
| 87 |
* type of edge: e.g. Value or Cell*. |
| 88 |
*/ |
| 89 |
template <typename T> |
| 90 |
struct MonoTypeBuffer { |
| 91 |
/* The canonical set of stores. */ |
| 92 |
using StoreSet = HashSet<T, typename T::Hasher, SystemAllocPolicy>; |
| 93 |
StoreSet stores_; |
| 94 |
|
| 95 |
/* |
| 96 |
* A one element cache in front of the canonical set to speed up |
| 97 |
* temporary instances of HeapPtr. |
| 98 |
*/ |
| 99 |
T last_ = T(); |
| 100 |
|
| 101 |
/* Maximum number of entries before we request a minor GC. */ |
| 102 |
const static size_t MaxEntries = BufferOverflowThresholdBytes / sizeof(T); |
| 103 |
|
| 104 |
MonoTypeBuffer() = default; |
| 105 |
|
| 106 |
MonoTypeBuffer(const MonoTypeBuffer& other) = delete; |
| 107 |
MonoTypeBuffer& operator=(const MonoTypeBuffer& other) = delete; |
| 108 |
|
| 109 |
MonoTypeBuffer(MonoTypeBuffer&& other) |
| 110 |
: stores_(std::move(other.stores_)), last_(std::move(other.last_)) { |
| 111 |
other.clear(); |
| 112 |
} |
| 113 |
MonoTypeBuffer& operator=(MonoTypeBuffer&& other) { |
| 114 |
if (&other != this) { |
| 115 |
this->~MonoTypeBuffer(); |
| 116 |
new (this) MonoTypeBuffer(std::move(other)); |
| 117 |
} |
| 118 |
return *this; |
| 119 |
} |
| 120 |
|
| 121 |
void clear() { |
| 122 |
last_ = T(); |
| 123 |
stores_.clear(); |
| 124 |
} |
| 125 |
|
| 126 |
/* Add one item to the buffer. */ |
| 127 |
PutResult put(const T& t) { |
| 128 |
PutResult r = sinkStore(); |
| 129 |
last_ = t; |
| 130 |
return r; |
| 131 |
} |
| 132 |
|
| 133 |
/* Remove an item from the store buffer. */ |
| 134 |
void unput(const T& v) { |
| 135 |
// Fast, hashless remove of last put. |
| 136 |
if (last_ == v) { |
| 137 |
last_ = T(); |
| 138 |
return; |
| 139 |
} |
| 140 |
stores_.remove(v); |
| 141 |
} |
| 142 |
|
| 143 |
/* Move any buffered stores to the canonical store set. */ |
| 144 |
PutResult sinkStore() { |
| 145 |
if (last_) { |
| 146 |
AutoEnterOOMUnsafeRegion oomUnsafe; |
| 147 |
if (!stores_.put(last_)) { |
| 148 |
oomUnsafe.crash("Failed to allocate for MonoTypeBuffer::put."); |
| 149 |
} |
| 150 |
} |
| 151 |
last_ = T(); |
| 152 |
|
| 153 |
if (stores_.count() > MaxEntries) { |
| 154 |
return PutResult::AboutToOverflow; |
| 155 |
} |
| 156 |
|
| 157 |
return PutResult::OK; |
| 158 |
} |
| 159 |
|
| 160 |
/* Trace the source of all edges in the store buffer. */ |
| 161 |
void trace(TenuringTracer& mover, StoreBuffer* owner); |
| 162 |
|
| 163 |
size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) { |
| 164 |
return stores_.shallowSizeOfExcludingThis(mallocSizeOf); |
| 165 |
} |
| 166 |
|
| 167 |
bool isEmpty() const { return last_ == T() && stores_.empty(); } |
| 168 |
}; |
| 169 |
|
| 170 |
struct WholeCellBuffer { |
| 171 |
UniquePtr<LifoAlloc> storage_; |
| 172 |
ArenaCellSet* sweepHead_ = nullptr; |
| 173 |
const Cell* last_ = nullptr; |
| 174 |
|
| 175 |
WholeCellBuffer() = default; |
| 176 |
|
| 177 |
WholeCellBuffer(const WholeCellBuffer& other) = delete; |
| 178 |
WholeCellBuffer& operator=(const WholeCellBuffer& other) = delete; |
| 179 |
|
| 180 |
WholeCellBuffer(WholeCellBuffer&& other) |
| 181 |
: storage_(std::move(other.storage_)), |
| 182 |
sweepHead_(other.sweepHead_), |
| 183 |
last_(other.last_) { |
| 184 |
other.sweepHead_ = nullptr; |
| 185 |
other.last_ = nullptr; |
| 186 |
} |
| 187 |
WholeCellBuffer& operator=(WholeCellBuffer&& other) { |
| 188 |
if (&other != this) { |
| 189 |
this->~WholeCellBuffer(); |
| 190 |
new (this) WholeCellBuffer(std::move(other)); |
| 191 |
} |
| 192 |
return *this; |
| 193 |
} |
| 194 |
|
| 195 |
[[nodiscard]] bool init(); |
| 196 |
|
| 197 |
void clear(); |
| 198 |
|
| 199 |
bool isAboutToOverflow() const { |
| 200 |
return !storage_->isEmpty() && |
| 201 |
storage_->used() > BufferOverflowThresholdBytes; |
| 202 |
} |
| 203 |
|
| 204 |
void trace(TenuringTracer& mover, StoreBuffer* owner); |
| 205 |
|
| 206 |
inline void put(const Cell* cell); |
| 207 |
inline void putDontCheckLast(const Cell* cell); |
| 208 |
|
| 209 |
size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) { |
| 210 |
return storage_ ? storage_->sizeOfIncludingThis(mallocSizeOf) : 0; |
| 211 |
} |
| 212 |
|
| 213 |
bool isEmpty() const { return !storage_ || storage_->isEmpty(); } |
| 214 |
|
| 215 |
const Cell** lastBufferedPtr() { return &last_; } |
| 216 |
|
| 217 |
private: |
| 218 |
ArenaCellSet* allocateCellSet(Arena* arena); |
| 219 |
}; |
| 220 |
|
| 221 |
struct GenericBuffer { |
| 222 |
UniquePtr<LifoAlloc> storage_; |
| 223 |
|
| 224 |
GenericBuffer() = default; |
| 225 |
|
| 226 |
GenericBuffer(const GenericBuffer& other) = delete; |
| 227 |
GenericBuffer& operator=(const GenericBuffer& other) = delete; |
| 228 |
|
| 229 |
GenericBuffer(GenericBuffer&& other) |
| 230 |
: storage_(std::move(other.storage_)) {} |
| 231 |
GenericBuffer& operator=(GenericBuffer&& other) { |
| 232 |
if (&other != this) { |
| 233 |
this->~GenericBuffer(); |
| 234 |
new (this) GenericBuffer(std::move(other)); |
| 235 |
} |
| 236 |
return *this; |
| 237 |
} |
| 238 |
|
| 239 |
[[nodiscard]] bool init(); |
| 240 |
|
| 241 |
void clear() { |
| 242 |
if (storage_) { |
| 243 |
storage_->used() ? storage_->releaseAll() : storage_->freeAll(); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
bool isAboutToOverflow() const { |
| 248 |
return !storage_->isEmpty() && storage_->availableInCurrentChunk() < |
| 249 |
GenericBufferLowAvailableThreshold; |
| 250 |
} |
| 251 |
|
| 252 |
/* Trace all generic edges. */ |
| 253 |
void trace(JSTracer* trc, StoreBuffer* owner); |
| 254 |
|
| 255 |
template <typename T> |
| 256 |
PutResult put(const T& t) { |
| 257 |
MOZ_ASSERT(storage_); |
| 258 |
|
| 259 |
/* Ensure T is derived from BufferableRef. */ |
| 260 |
(void)static_cast<const BufferableRef*>(&t); |
| 261 |
|
| 262 |
AutoEnterOOMUnsafeRegion oomUnsafe; |
| 263 |
unsigned size = sizeof(T); |
| 264 |
unsigned* sizep = storage_->pod_malloc<unsigned>(); |
| 265 |
if (!sizep) { |
| 266 |
oomUnsafe.crash("Failed to allocate for GenericBuffer::put."); |
| 267 |
} |
| 268 |
*sizep = size; |
| 269 |
|
| 270 |
T* tp = storage_->new_<T>(t); |
| 271 |
if (!tp) { |
| 272 |
oomUnsafe.crash("Failed to allocate for GenericBuffer::put."); |
| 273 |
} |
| 274 |
|
| 275 |
if (isAboutToOverflow()) { |
| 276 |
return PutResult::AboutToOverflow; |
| 277 |
} |
| 278 |
|
| 279 |
return PutResult::OK; |
| 280 |
} |
| 281 |
|
| 282 |
size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) { |
| 283 |
return storage_ ? storage_->sizeOfIncludingThis(mallocSizeOf) : 0; |
| 284 |
} |
| 285 |
|
| 286 |
bool isEmpty() const { return !storage_ || storage_->isEmpty(); } |
| 287 |
}; |
| 288 |
|
| 289 |
template <typename Edge> |
| 290 |
struct PointerEdgeHasher { |
| 291 |
using Lookup = Edge; |
| 292 |
static HashNumber hash(const Lookup& l) { |
| 293 |
return mozilla::HashGeneric(l.edge); |
| 294 |
} |
| 295 |
static bool match(const Edge& k, const Lookup& l) { return k == l; } |
| 296 |
}; |
| 297 |
|
| 298 |
template <typename T> |
| 299 |
struct CellPtrEdge { |
| 300 |
T** edge = nullptr; |
| 301 |
|
| 302 |
CellPtrEdge() = default; |
| 303 |
explicit CellPtrEdge(T** v) : edge(v) {} |
| 304 |
bool operator==(const CellPtrEdge& other) const { |
| 305 |
return edge == other.edge; |
| 306 |
} |
| 307 |
bool operator!=(const CellPtrEdge& other) const { |
| 308 |
return edge != other.edge; |
| 309 |
} |
| 310 |
|
| 311 |
bool maybeInRememberedSet(const Nursery& nursery) const { |
| 312 |
MOZ_ASSERT(IsInsideNursery(*edge)); |
| 313 |
return !nursery.isInside(edge); |
| 314 |
} |
| 315 |
|
| 316 |
void trace(TenuringTracer& mover) const; |
| 317 |
|
| 318 |
explicit operator bool() const { return edge != nullptr; } |
| 319 |
|
| 320 |
using Hasher = PointerEdgeHasher<CellPtrEdge<T>>; |
| 321 |
}; |
| 322 |
|
| 323 |
using ObjectPtrEdge = CellPtrEdge<JSObject>; |
| 324 |
using StringPtrEdge = CellPtrEdge<JSString>; |
| 325 |
using BigIntPtrEdge = CellPtrEdge<JS::BigInt>; |
| 326 |
|
| 327 |
struct ValueEdge { |
| 328 |
JS::Value* edge; |
| 329 |
|
| 330 |
ValueEdge() : edge(nullptr) {} |
| 331 |
explicit ValueEdge(JS::Value* v) : edge(v) {} |
| 332 |
bool operator==(const ValueEdge& other) const { return edge == other.edge; } |
| 333 |
bool operator!=(const ValueEdge& other) const { return edge != other.edge; } |
| 334 |
|
| 335 |
bool isGCThing() const { return edge->isGCThing(); } |
| 336 |
|
| 337 |
Cell* deref() const { |
| 338 |
return isGCThing() ? static_cast<Cell*>(edge->toGCThing()) : nullptr; |
| 339 |
} |
| 340 |
|
| 341 |
bool maybeInRememberedSet(const Nursery& nursery) const { |
| 342 |
MOZ_ASSERT(IsInsideNursery(deref())); |
| 343 |
return !nursery.isInside(edge); |
| 344 |
} |
| 345 |
|
| 346 |
void trace(TenuringTracer& mover) const; |
| 347 |
|
| 348 |
explicit operator bool() const { return edge != nullptr; } |
| 349 |
|
| 350 |
using Hasher = PointerEdgeHasher<ValueEdge>; |
| 351 |
}; |
| 352 |
|
| 353 |
struct SlotsEdge { |
| 354 |
// These definitions must match those in HeapSlot::Kind. |
| 355 |
const static int SlotKind = 0; |
| 356 |
const static int ElementKind = 1; |
| 357 |
|
| 358 |
uintptr_t objectAndKind_; // NativeObject* | Kind |
| 359 |
uint32_t start_; |
| 360 |
uint32_t count_; |
| 361 |
|
| 362 |
SlotsEdge() : objectAndKind_(0), start_(0), count_(0) {} |
| 363 |
SlotsEdge(NativeObject* object, int kind, uint32_t start, uint32_t count) |
| 364 |
: objectAndKind_(uintptr_t(object) | kind), |
| 365 |
start_(start), |
| 366 |
count_(count) { |
| 367 |
MOZ_ASSERT((uintptr_t(object) & 1) == 0); |
| 368 |
MOZ_ASSERT(kind <= 1); |
| 369 |
MOZ_ASSERT(count > 0); |
| 370 |
MOZ_ASSERT(start + count > start); |
| 371 |
} |
| 372 |
|
| 373 |
NativeObject* object() const { |
| 374 |
return reinterpret_cast<NativeObject*>(objectAndKind_ & ~1); |
| 375 |
} |
| 376 |
int kind() const { return (int)(objectAndKind_ & 1); } |
| 377 |
|
| 378 |
bool operator==(const SlotsEdge& other) const { |
| 379 |
return objectAndKind_ == other.objectAndKind_ && start_ == other.start_ && |
| 380 |
count_ == other.count_; |
| 381 |
} |
| 382 |
|
| 383 |
bool operator!=(const SlotsEdge& other) const { return !(*this == other); } |
| 384 |
|
| 385 |
// True if this SlotsEdge range is adjacent to or overlaps with the other |
| 386 |
// SlotsEdge range. The adjacency case will coalesce a series of increasing |
| 387 |
// or decreasing single index writes 0, 1, 2, ..., N into a SlotsEdge range |
| 388 |
// of elements [0, N]. |
| 389 |
bool touches(const SlotsEdge& other) const { |
| 390 |
if (objectAndKind_ != other.objectAndKind_) { |
| 391 |
return false; |
| 392 |
} |
| 393 |
|
| 394 |
if (other.start_ < start_) { |
| 395 |
// If the other range starts before this one, then it touches if its |
| 396 |
// (exclusive) end is at or after this range. |
| 397 |
return other.start_ + other.count_ >= start_; |
| 398 |
} |
| 399 |
|
| 400 |
// Otherwise, the other range touches if it starts before or at |
| 401 |
// the exclusive end of this range. |
| 402 |
return other.start_ <= start_ + count_; |
| 403 |
} |
| 404 |
|
| 405 |
// Destructively make this SlotsEdge range the union of the other |
| 406 |
// SlotsEdge range and this one. A precondition is that the ranges must |
| 407 |
// overlap. |
| 408 |
void merge(const SlotsEdge& other) { |
| 409 |
MOZ_ASSERT(touches(other)); |
| 410 |
uint32_t end = std::max(start_ + count_, other.start_ + other.count_); |
| 411 |
start_ = std::min(start_, other.start_); |
| 412 |
count_ = end - start_; |
| 413 |
} |
| 414 |
|
| 415 |
bool maybeInRememberedSet(const Nursery& n) const { |
| 416 |
return !IsInsideNursery(reinterpret_cast<Cell*>(object())); |
| 417 |
} |
| 418 |
|
| 419 |
void trace(TenuringTracer& mover) const; |
| 420 |
|
| 421 |
explicit operator bool() const { return objectAndKind_ != 0; } |
| 422 |
|
| 423 |
struct Hasher { |
| 424 |
using Lookup = SlotsEdge; |
| 425 |
static HashNumber hash(const Lookup& l) { |
| 426 |
return mozilla::HashGeneric(l.objectAndKind_, l.start_, l.count_); |
| 427 |
} |
| 428 |
static bool match(const SlotsEdge& k, const Lookup& l) { return k == l; } |
| 429 |
}; |
| 430 |
}; |
| 431 |
|
| 432 |
struct WasmAnyRefEdge { |
| 433 |
wasm::AnyRef* edge; |
| 434 |
|
| 435 |
WasmAnyRefEdge() : edge(nullptr) {} |
| 436 |
explicit WasmAnyRefEdge(wasm::AnyRef* v) : edge(v) {} |
| 437 |
bool operator==(const WasmAnyRefEdge& other) const { |
| 438 |
return edge == other.edge; |
| 439 |
} |
| 440 |
bool operator!=(const WasmAnyRefEdge& other) const { |
| 441 |
return edge != other.edge; |
| 442 |
} |
| 443 |
|
| 444 |
bool isGCThing() const { return edge->isGCThing(); } |
| 445 |
|
| 446 |
Cell* deref() const { |
| 447 |
return isGCThing() ? static_cast<Cell*>(edge->toGCThing()) : nullptr; |
| 448 |
} |
| 449 |
|
| 450 |
bool maybeInRememberedSet(const Nursery& nursery) const { |
| 451 |
MOZ_ASSERT(IsInsideNursery(deref())); |
| 452 |
return !nursery.isInside(edge); |
| 453 |
} |
| 454 |
|
| 455 |
void trace(TenuringTracer& mover) const; |
| 456 |
|
| 457 |
explicit operator bool() const { return edge != nullptr; } |
| 458 |
|
| 459 |
using Hasher = PointerEdgeHasher<WasmAnyRefEdge>; |
| 460 |
}; |
| 461 |
|
| 462 |
#ifdef DEBUG |
| 463 |
void checkAccess() const; |
| 464 |
#else |
| 465 |
void checkAccess() const {} |
| 466 |
#endif |
| 467 |
|
| 468 |
template <typename Buffer, typename Edge> |
| 469 |
void unput(Buffer& buffer, const Edge& edge) { |
| 470 |
checkAccess(); |
| 471 |
if (!isEnabled()) { |
| 472 |
return; |
| 473 |
} |
| 474 |
|
| 475 |
mozilla::ReentrancyGuard g(*this); |
| 476 |
|
| 477 |
buffer.unput(edge); |
| 478 |
} |
| 479 |
|
| 480 |
template <typename Buffer, typename Edge> |
| 481 |
void put(Buffer& buffer, const Edge& edge, JS::GCReason overflowReason) { |
| 482 |
checkAccess(); |
| 483 |
if (!isEnabled()) { |
| 484 |
return; |
| 485 |
} |
| 486 |
|
| 487 |
mozilla::ReentrancyGuard g(*this); |
| 488 |
|
| 489 |
if (!edge.maybeInRememberedSet(nursery_)) { |
| 490 |
return; |
| 491 |
} |
| 492 |
|
| 493 |
PutResult r = buffer.put(edge); |
| 494 |
|
| 495 |
if (MOZ_UNLIKELY(r == PutResult::AboutToOverflow)) { |
| 496 |
setAboutToOverflow(overflowReason); |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
MonoTypeBuffer<ValueEdge> bufferVal; |
| 501 |
MonoTypeBuffer<StringPtrEdge> bufStrCell; |
| 502 |
MonoTypeBuffer<BigIntPtrEdge> bufBigIntCell; |
| 503 |
MonoTypeBuffer<ObjectPtrEdge> bufObjCell; |
| 504 |
MonoTypeBuffer<SlotsEdge> bufferSlot; |
| 505 |
MonoTypeBuffer<WasmAnyRefEdge> bufferWasmAnyRef; |
| 506 |
WholeCellBuffer bufferWholeCell; |
| 507 |
GenericBuffer bufferGeneric; |
| 508 |
|
| 509 |
JSRuntime* runtime_; |
| 510 |
Nursery& nursery_; |
| 511 |
|
| 512 |
bool aboutToOverflow_; |
| 513 |
bool enabled_; |
| 514 |
bool mayHavePointersToDeadCells_; |
| 515 |
#ifdef DEBUG |
| 516 |
bool mEntered; /* For ReentrancyGuard. */ |
| 517 |
#endif |
| 518 |
|
| 519 |
public: |
| 520 |
explicit StoreBuffer(JSRuntime* rt); |
| 521 |
|
| 522 |
StoreBuffer(const StoreBuffer& other) = delete; |
| 523 |
StoreBuffer& operator=(const StoreBuffer& other) = delete; |
| 524 |
|
| 525 |
StoreBuffer(StoreBuffer&& other); |
| 526 |
StoreBuffer& operator=(StoreBuffer&& other); |
| 527 |
|
| 528 |
[[nodiscard]] bool enable(); |
| 529 |
|
| 530 |
void disable(); |
| 531 |
bool isEnabled() const { return enabled_; } |
| 532 |
|
| 533 |
bool isEmpty() const; |
| 534 |
void clear(); |
| 535 |
|
| 536 |
const Nursery& nursery() const { return nursery_; } |
| 537 |
|
| 538 |
/* Get the overflowed status. */ |
| 539 |
bool isAboutToOverflow() const { return aboutToOverflow_; } |
| 540 |
|
| 541 |
/* |
| 542 |
* Brain transplants may add whole cell buffer entires for dead cells. We must |
| 543 |
* evict the nursery prior to sweeping arenas if any such entries are present. |
| 544 |
*/ |
| 545 |
bool mayHavePointersToDeadCells() const { |
| 546 |
return mayHavePointersToDeadCells_; |
| 547 |
} |
| 548 |
|
| 549 |
/* Insert a single edge into the buffer/remembered set. */ |
| 550 |
void putValue(JS::Value* vp) { |
| 551 |
put(bufferVal, ValueEdge(vp), JS::GCReason::FULL_VALUE_BUFFER); |
| 552 |
} |
| 553 |
void unputValue(JS::Value* vp) { unput(bufferVal, ValueEdge(vp)); } |
| 554 |
|
| 555 |
void putCell(JSString** strp) { |
| 556 |
put(bufStrCell, StringPtrEdge(strp), |
| 557 |
JS::GCReason::FULL_CELL_PTR_STR_BUFFER); |
| 558 |
} |
| 559 |
void unputCell(JSString** strp) { unput(bufStrCell, StringPtrEdge(strp)); } |
| 560 |
|
| 561 |
void putCell(JS::BigInt** bip) { |
| 562 |
put(bufBigIntCell, BigIntPtrEdge(bip), |
| 563 |
JS::GCReason::FULL_CELL_PTR_BIGINT_BUFFER); |
| 564 |
} |
| 565 |
void unputCell(JS::BigInt** bip) { unput(bufBigIntCell, BigIntPtrEdge(bip)); } |
| 566 |
|
| 567 |
void putCell(JSObject** strp) { |
| 568 |
put(bufObjCell, ObjectPtrEdge(strp), |
| 569 |
JS::GCReason::FULL_CELL_PTR_OBJ_BUFFER); |
| 570 |
} |
| 571 |
void unputCell(JSObject** strp) { unput(bufObjCell, ObjectPtrEdge(strp)); } |
| 572 |
|
| 573 |
void putSlot(NativeObject* obj, int kind, uint32_t start, uint32_t count) { |
| 574 |
SlotsEdge edge(obj, kind, start, count); |
| 575 |
if (bufferSlot.last_.touches(edge)) { |
| 576 |
bufferSlot.last_.merge(edge); |
| 577 |
} else { |
| 578 |
put(bufferSlot, edge, JS::GCReason::FULL_SLOT_BUFFER); |
| 579 |
} |
| 580 |
} |
| 581 |
|
| 582 |
void putWasmAnyRef(wasm::AnyRef* vp) { |
| 583 |
put(bufferWasmAnyRef, WasmAnyRefEdge(vp), |
| 584 |
JS::GCReason::FULL_WASM_ANYREF_BUFFER); |
| 585 |
} |
| 586 |
void unputWasmAnyRef(wasm::AnyRef* vp) { |
| 587 |
unput(bufferWasmAnyRef, WasmAnyRefEdge(vp)); |
| 588 |
} |
| 589 |
|
| 590 |
static inline bool isInWholeCellBuffer(Cell* cell); |
| 591 |
inline void putWholeCell(Cell* cell); |
| 592 |
inline void putWholeCellDontCheckLast(Cell* cell); |
| 593 |
const void* addressOfLastBufferedWholeCell() { |
| 594 |
return bufferWholeCell.lastBufferedPtr(); |
| 595 |
} |
| 596 |
|
| 597 |
/* Insert an entry into the generic buffer. */ |
| 598 |
template <typename T> |
| 599 |
void putGeneric(const T& t) { |
| 600 |
put(bufferGeneric, t, JS::GCReason::FULL_GENERIC_BUFFER); |
| 601 |
} |
| 602 |
|
| 603 |
void setMayHavePointersToDeadCells() { mayHavePointersToDeadCells_ = true; } |
| 604 |
|
| 605 |
/* Methods to trace the source of all edges in the store buffer. */ |
| 606 |
void traceValues(TenuringTracer& mover) { bufferVal.trace(mover, this); } |
| 607 |
void traceCells(TenuringTracer& mover) { |
| 608 |
bufStrCell.trace(mover, this); |
| 609 |
bufBigIntCell.trace(mover, this); |
| 610 |
bufObjCell.trace(mover, this); |
| 611 |
} |
| 612 |
void traceSlots(TenuringTracer& mover) { bufferSlot.trace(mover, this); } |
| 613 |
void traceWasmAnyRefs(TenuringTracer& mover) { |
| 614 |
bufferWasmAnyRef.trace(mover, this); |
| 615 |
} |
| 616 |
void traceWholeCells(TenuringTracer& mover) { |
| 617 |
bufferWholeCell.trace(mover, this); |
| 618 |
} |
| 619 |
void traceGenericEntries(JSTracer* trc) { bufferGeneric.trace(trc, this); } |
| 620 |
|
| 621 |
/* For use by our owned buffers and for testing. */ |
| 622 |
void setAboutToOverflow(JS::GCReason); |
| 623 |
|
| 624 |
void addSizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf, |
| 625 |
JS::GCSizes* sizes); |
| 626 |
|
| 627 |
void checkEmpty() const; |
| 628 |
}; |
| 629 |
|
| 630 |
// A set of cells in an arena used to implement the whole cell store buffer. |
| 631 |
// Also used to store a set of cells that need to be swept. |
| 632 |
class ArenaCellSet { |
| 633 |
friend class StoreBuffer; |
| 634 |
|
| 635 |
using ArenaCellBits = BitArray<MaxArenaCellIndex>; |
| 636 |
|
| 637 |
// The arena this relates to. |
| 638 |
Arena* arena = nullptr; |
| 639 |
|
| 640 |
// Pointer to next set forming a linked list. Used to form the list of cell |
| 641 |
// sets to sweep. |
| 642 |
ArenaCellSet* next = nullptr; |
| 643 |
|
| 644 |
// Bit vector for each possible cell start position. |
| 645 |
ArenaCellBits bits; |
| 646 |
|
| 647 |
#ifdef DEBUG |
| 648 |
// The minor GC number when this was created. This object should not survive |
| 649 |
// past the next minor collection. |
| 650 |
const uint64_t minorGCNumberAtCreation = 0; |
| 651 |
#endif |
| 652 |
|
| 653 |
// Construct the empty sentinel object. |
| 654 |
constexpr ArenaCellSet() = default; |
| 655 |
|
| 656 |
public: |
| 657 |
using WordT = ArenaCellBits::WordT; |
| 658 |
static constexpr size_t BitsPerWord = ArenaCellBits::bitsPerElement; |
| 659 |
static constexpr size_t NumWords = ArenaCellBits::numSlots; |
| 660 |
|
| 661 |
explicit ArenaCellSet(Arena* arena); |
| 662 |
|
| 663 |
bool hasCell(const TenuredCell* cell) const { |
| 664 |
return hasCell(getCellIndex(cell)); |
| 665 |
} |
| 666 |
|
| 667 |
void putCell(const TenuredCell* cell) { putCell(getCellIndex(cell)); } |
| 668 |
|
| 669 |
bool isEmpty() const { return this == &Empty; } |
| 670 |
|
| 671 |
bool hasCell(size_t cellIndex) const; |
| 672 |
|
| 673 |
void putCell(size_t cellIndex); |
| 674 |
|
| 675 |
void check() const; |
| 676 |
|
| 677 |
WordT getWord(size_t wordIndex) const { return bits.getWord(wordIndex); } |
| 678 |
|
| 679 |
void setWord(size_t wordIndex, WordT value) { |
| 680 |
bits.setWord(wordIndex, value); |
| 681 |
} |
| 682 |
|
| 683 |
// Sweep this set, returning whether it also needs to be swept later. |
| 684 |
bool trace(TenuringTracer& mover); |
| 685 |
|
| 686 |
// Sentinel object used for all empty sets. |
| 687 |
// |
| 688 |
// We use a sentinel because it simplifies the JIT code slightly as we can |
| 689 |
// assume all arenas have a cell set. |
| 690 |
static ArenaCellSet Empty; |
| 691 |
|
| 692 |
static size_t getCellIndex(const TenuredCell* cell); |
| 693 |
static void getWordIndexAndMask(size_t cellIndex, size_t* wordp, |
| 694 |
uint32_t* maskp); |
| 695 |
|
| 696 |
// Attempt to trigger a minor GC if free space in the nursery (where these |
| 697 |
// objects are allocated) falls below this threshold. |
| 698 |
static const size_t NurseryFreeThresholdBytes = 64 * 1024; |
| 699 |
|
| 700 |
static size_t offsetOfArena() { return offsetof(ArenaCellSet, arena); } |
| 701 |
static size_t offsetOfBits() { return offsetof(ArenaCellSet, bits); } |
| 702 |
}; |
| 703 |
|
| 704 |
// Post-write barrier implementation for GC cells. |
| 705 |
|
| 706 |
// Implement the post-write barrier for nursery allocateable cell type |T|. Call |
| 707 |
// this from |T::postWriteBarrier|. |
| 708 |
template <typename T> |
| 709 |
MOZ_ALWAYS_INLINE void PostWriteBarrierImpl(void* cellp, T* prev, T* next) { |
| 710 |
MOZ_ASSERT(cellp); |
| 711 |
|
| 712 |
// If the target needs an entry, add it. |
| 713 |
StoreBuffer* buffer; |
| 714 |
if (next && (buffer = next->storeBuffer())) { |
| 715 |
// If we know that the prev has already inserted an entry, we can skip |
| 716 |
// doing the lookup to add the new entry. Note that we cannot safely |
| 717 |
// assert the presence of the entry because it may have been added |
| 718 |
// via a different store buffer. |
| 719 |
if (prev && prev->storeBuffer()) { |
| 720 |
return; |
| 721 |
} |
| 722 |
buffer->putCell(static_cast<T**>(cellp)); |
| 723 |
return; |
| 724 |
} |
| 725 |
|
| 726 |
// Remove the prev entry if the new value does not need it. There will only |
| 727 |
// be a prev entry if the prev value was in the nursery. |
| 728 |
if (prev && (buffer = prev->storeBuffer())) { |
| 729 |
buffer->unputCell(static_cast<T**>(cellp)); |
| 730 |
} |
| 731 |
} |
| 732 |
|
| 733 |
template <typename T> |
| 734 |
MOZ_ALWAYS_INLINE void PostWriteBarrier(T** vp, T* prev, T* next) { |
| 735 |
static_assert(std::is_base_of_v<Cell, T>); |
| 736 |
static_assert(!std::is_same_v<Cell, T> && !std::is_same_v<TenuredCell, T>); |
| 737 |
|
| 738 |
if constexpr (!GCTypeIsTenured<T>()) { |
| 739 |
using BaseT = typename BaseGCType<T>::type; |
| 740 |
PostWriteBarrierImpl<BaseT>(vp, prev, next); |
| 741 |
return; |
| 742 |
} |
| 743 |
|
| 744 |
MOZ_ASSERT_IF(next, !IsInsideNursery(next)); |
| 745 |
} |
| 746 |
|
| 747 |
// Used when we don't have a specific edge to put in the store buffer. |
| 748 |
void PostWriteBarrierCell(Cell* cell, Cell* prev, Cell* next); |
| 749 |
|
| 750 |
} /* namespace gc */ |
| 751 |
} /* namespace js */ |
| 752 |
|
| 753 |
#endif /* gc_StoreBuffer_h */ |