| 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 |
* Marking and sweeping APIs for use by implementations of different GC cell |
| 9 |
* kinds. |
| 10 |
*/ |
| 11 |
|
| 12 |
#ifndef gc_Marking_h |
| 13 |
#define gc_Marking_h |
| 14 |
|
| 15 |
#include "gc/Barrier.h" |
| 16 |
#include "gc/Tracer.h" |
| 17 |
#include "js/TypeDecls.h" |
| 18 |
|
| 19 |
class JSTracer; |
| 20 |
struct JSClass; |
| 21 |
|
| 22 |
namespace js { |
| 23 |
class GCMarker; |
| 24 |
class Shape; |
| 25 |
class WeakMapBase; |
| 26 |
|
| 27 |
namespace gc { |
| 28 |
|
| 29 |
struct Cell; |
| 30 |
|
| 31 |
/*** Liveness ***/ |
| 32 |
|
| 33 |
// The IsMarkedInternal and IsAboutToBeFinalizedInternal function templates are |
| 34 |
// used to implement the IsMarked and IsAboutToBeFinalized set of functions. |
| 35 |
// These internal functions are instantiated for the base GC types and should |
| 36 |
// not be called directly. |
| 37 |
// |
| 38 |
// Note that there are two function templates declared for each, not one |
| 39 |
// template and a specialization. This is necessary so that pointer arguments |
| 40 |
// (e.g. JSObject**) and tagged value arguments (e.g. JS::Value*) are routed to |
| 41 |
// separate implementations. |
| 42 |
|
| 43 |
template <typename T> |
| 44 |
bool IsMarkedInternal(JSRuntime* rt, T* thing); |
| 45 |
|
| 46 |
template <typename T> |
| 47 |
bool IsAboutToBeFinalizedInternal(T* thing); |
| 48 |
template <typename T> |
| 49 |
bool IsAboutToBeFinalizedInternal(const T& thing); |
| 50 |
|
| 51 |
// Report whether a GC thing has been marked with any color. Things which are in |
| 52 |
// zones that are not currently being collected or are owned by another runtime |
| 53 |
// are always reported as being marked. |
| 54 |
template <typename T> |
| 55 |
inline bool IsMarked(JSRuntime* rt, const BarrieredBase<T>& thing) { |
| 56 |
return IsMarkedInternal(rt, *ConvertToBase(thing.unbarrieredAddress())); |
| 57 |
} |
| 58 |
template <typename T> |
| 59 |
inline bool IsMarkedUnbarriered(JSRuntime* rt, T thing) { |
| 60 |
return IsMarkedInternal(rt, *ConvertToBase(&thing)); |
| 61 |
} |
| 62 |
|
| 63 |
// Report whether a GC thing is dead and will be finalized in the current sweep |
| 64 |
// group. This is mainly used in read barriers for incremental sweeping. |
| 65 |
// |
| 66 |
// This no longer updates pointers moved by the GC (tracing should be used for |
| 67 |
// this instead). |
| 68 |
template <typename T> |
| 69 |
inline bool IsAboutToBeFinalized(const BarrieredBase<T>& thing) { |
| 70 |
return IsAboutToBeFinalizedInternal( |
| 71 |
*ConvertToBase(thing.unbarrieredAddress())); |
| 72 |
} |
| 73 |
template <typename T> |
| 74 |
inline bool IsAboutToBeFinalizedUnbarriered(T* thing) { |
| 75 |
return IsAboutToBeFinalizedInternal(*ConvertToBase(&thing)); |
| 76 |
} |
| 77 |
template <typename T> |
| 78 |
inline bool IsAboutToBeFinalizedUnbarriered(const T& thing) { |
| 79 |
return IsAboutToBeFinalizedInternal(thing); |
| 80 |
} |
| 81 |
|
| 82 |
inline bool IsAboutToBeFinalizedDuringMinorSweep(Cell* cell); |
| 83 |
|
| 84 |
inline Cell* ToMarkable(const Value& v) { |
| 85 |
if (v.isGCThing()) { |
| 86 |
return (Cell*)v.toGCThing(); |
| 87 |
} |
| 88 |
return nullptr; |
| 89 |
} |
| 90 |
|
| 91 |
inline Cell* ToMarkable(Cell* cell) { return cell; } |
| 92 |
|
| 93 |
bool UnmarkGrayGCThingUnchecked(GCMarker* marker, JS::GCCellPtr thing); |
| 94 |
|
| 95 |
} /* namespace gc */ |
| 96 |
|
| 97 |
// The return value indicates if anything was unmarked. |
| 98 |
bool UnmarkGrayShapeRecursively(Shape* shape); |
| 99 |
|
| 100 |
namespace gc { |
| 101 |
|
| 102 |
// Functions for checking and updating GC thing pointers that might have been |
| 103 |
// moved by compacting GC. Overloads are also provided that work with Values. |
| 104 |
// |
| 105 |
// IsForwarded - check whether a pointer refers to an GC thing that has been |
| 106 |
// moved. |
| 107 |
// |
| 108 |
// Forwarded - return a pointer to the new location of a GC thing given a |
| 109 |
// pointer to old location. |
| 110 |
// |
| 111 |
// MaybeForwarded - used before dereferencing a pointer that may refer to a |
| 112 |
// moved GC thing without updating it. For JSObjects this will |
| 113 |
// also update the object's shape pointer if it has been moved |
| 114 |
// to allow slots to be accessed. |
| 115 |
|
| 116 |
template <typename T> |
| 117 |
inline bool IsForwarded(const T* t); |
| 118 |
|
| 119 |
template <typename T> |
| 120 |
inline T* Forwarded(const T* t); |
| 121 |
|
| 122 |
inline Value Forwarded(const JS::Value& value); |
| 123 |
|
| 124 |
template <typename T> |
| 125 |
inline T MaybeForwarded(T t); |
| 126 |
|
| 127 |
// Helper functions for use in situations where the object's group might be |
| 128 |
// forwarded, for example while marking. |
| 129 |
|
| 130 |
inline const JSClass* MaybeForwardedObjectClass(const JSObject* obj); |
| 131 |
|
| 132 |
template <typename T> |
| 133 |
inline bool MaybeForwardedObjectIs(const JSObject* obj); |
| 134 |
|
| 135 |
template <typename T> |
| 136 |
inline T& MaybeForwardedObjectAs(JSObject* obj); |
| 137 |
|
| 138 |
#ifdef JSGC_HASH_TABLE_CHECKS |
| 139 |
|
| 140 |
template <typename T> |
| 141 |
inline bool IsGCThingValidAfterMovingGC(T* t); |
| 142 |
|
| 143 |
template <typename T> |
| 144 |
inline void CheckGCThingAfterMovingGC(T* t); |
| 145 |
|
| 146 |
template <typename T> |
| 147 |
inline void CheckGCThingAfterMovingGC(const WeakHeapPtr<T*>& t); |
| 148 |
|
| 149 |
#endif // JSGC_HASH_TABLE_CHECKS |
| 150 |
|
| 151 |
} /* namespace gc */ |
| 152 |
|
| 153 |
} /* namespace js */ |
| 154 |
|
| 155 |
#endif /* gc_Marking_h */ |