OffthreadSnapshot.h (1950B)
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 jit_OffthreadSnapshot_h 8 #define jit_OffthreadSnapshot_h 9 10 #include "gc/Policy.h" 11 #include "gc/Tracer.h" 12 13 // Wrapper for GC things stored in snapshots for offthread compilation. 14 // Asserts the GC pointer is not nursery-allocated. 15 // These pointers must be traced using TraceOffthreadGCPtr. 16 template <typename T> 17 class OffthreadGCPtr { 18 // Note: no pre-barrier is needed because after being initialized to a 19 // non-empty OffthreadGCPtr this is a constant. No post-barrier is needed 20 // because the value is always tenured. 21 T ptr_ = JS::SafelyInitialized<T>::create(); 22 23 public: 24 constexpr OffthreadGCPtr() = default; 25 26 explicit OffthreadGCPtr(const T& ptr) : ptr_(ptr) { 27 MOZ_ASSERT(JS::GCPolicy<T>::isTenured(ptr), 28 "OffthreadSnapshot pointers must be tenured"); 29 } 30 OffthreadGCPtr(const OffthreadGCPtr<T>& other) = default; 31 32 operator T() const { return ptr_; } 33 T operator->() const { return ptr_; } 34 35 void init(T& ptr) { 36 MOZ_ASSERT(JS::GCPolicy<T>::isTenured(ptr), 37 "OffthreadSnapshot pointers must be tenured"); 38 MOZ_ASSERT(ptr_ == JS::SafelyInitialized<T>::create(), 39 "init can only be called on empty OffthreadGCPtr"); 40 ptr_ = ptr; 41 } 42 43 private: 44 void operator=(OffthreadGCPtr<T>& other) = delete; 45 }; 46 47 template <typename T> 48 inline void TraceOffthreadGCPtr(JSTracer* trc, const OffthreadGCPtr<T>& thing, 49 const char* name) { 50 T thingRaw = thing; 51 js::TraceManuallyBarrieredEdge(trc, &thingRaw, name); 52 MOZ_ASSERT(static_cast<T>(thing) == thingRaw, "Unexpected moving GC!"); 53 } 54 55 #endif /* jit_OffthreadSnapshot_h */