Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : * vim: set ts=8 sts=4 et sw=4 tw=99:
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 : /* JS Garbage Collector. */
8 :
9 : #ifndef gc_Policy_h
10 : #define gc_Policy_h
11 :
12 : #include "mozilla/TypeTraits.h"
13 : #include "gc/Barrier.h"
14 : #include "gc/Marking.h"
15 : #include "js/GCPolicyAPI.h"
16 :
17 : // Forward declare the types we're defining policies for. This file is
18 : // included in all places that define GC things, so the real definitions
19 : // will be available when we do template expansion, allowing for use of
20 : // static members in the underlying types. We cannot, however, use
21 : // static_assert to verify relations between types.
22 : class JSLinearString;
23 : namespace js {
24 : class AccessorShape;
25 : class ArgumentsObject;
26 : class ArrayBufferObject;
27 : class ArrayBufferObjectMaybeShared;
28 : class ArrayBufferViewObject;
29 : class ArrayObject;
30 : class BaseShape;
31 : class DebugEnvironmentProxy;
32 : class DebuggerFrame;
33 : class ExportEntryObject;
34 : class EnvironmentObject;
35 : class GlobalObject;
36 : class ImportEntryObject;
37 : class LazyScript;
38 : class LexicalEnvironmentObject;
39 : class ModuleEnvironmentObject;
40 : class ModuleNamespaceObject;
41 : class ModuleObject;
42 : class NativeObject;
43 : class ObjectGroup;
44 : class PlainObject;
45 : class PropertyName;
46 : class RegExpObject;
47 : class SavedFrame;
48 : class Scope;
49 : class EnvironmentObject;
50 : class ScriptSourceObject;
51 : class Shape;
52 : class SharedArrayBufferObject;
53 : class StructTypeDescr;
54 : class UnownedBaseShape;
55 : class WasmFunctionScope;
56 : class WasmMemoryObject;
57 : namespace jit {
58 : class JitCode;
59 : } // namespace jit
60 : } // namespace js
61 :
62 : // Expand the given macro D for each valid GC reference type.
63 : #define FOR_EACH_INTERNAL_GC_POINTER_TYPE(D) \
64 : D(JSFlatString*) \
65 : D(JSLinearString*) \
66 : D(js::AccessorShape*) \
67 : D(js::ArgumentsObject*) \
68 : D(js::ArrayBufferObject*) \
69 : D(js::ArrayBufferObjectMaybeShared*) \
70 : D(js::ArrayBufferViewObject*) \
71 : D(js::ArrayObject*) \
72 : D(js::BaseShape*) \
73 : D(js::DebugEnvironmentProxy*) \
74 : D(js::DebuggerFrame*) \
75 : D(js::ExportEntryObject*) \
76 : D(js::EnvironmentObject*) \
77 : D(js::GlobalObject*) \
78 : D(js::ImportEntryObject*) \
79 : D(js::LazyScript*) \
80 : D(js::LexicalEnvironmentObject*) \
81 : D(js::ModuleEnvironmentObject*) \
82 : D(js::ModuleNamespaceObject*) \
83 : D(js::ModuleObject*) \
84 : D(js::NativeObject*) \
85 : D(js::ObjectGroup*) \
86 : D(js::PlainObject*) \
87 : D(js::PropertyName*) \
88 : D(js::RegExpObject*) \
89 : D(js::RegExpShared*) \
90 : D(js::SavedFrame*) \
91 : D(js::Scope*) \
92 : D(js::ScriptSourceObject*) \
93 : D(js::Shape*) \
94 : D(js::SharedArrayBufferObject*) \
95 : D(js::StructTypeDescr*) \
96 : D(js::UnownedBaseShape*) \
97 : D(js::WasmFunctionScope*) \
98 : D(js::WasmInstanceObject*) \
99 : D(js::WasmMemoryObject*) \
100 : D(js::WasmTableObject*) \
101 : D(js::jit::JitCode*)
102 :
103 : // Expand the given macro D for each internal tagged GC pointer type.
104 : #define FOR_EACH_INTERNAL_TAGGED_GC_POINTER_TYPE(D) \
105 : D(js::TaggedProto)
106 :
107 : // Expand the macro D for every GC reference type that we know about.
108 : #define FOR_EACH_GC_POINTER_TYPE(D) \
109 : FOR_EACH_PUBLIC_GC_POINTER_TYPE(D) \
110 : FOR_EACH_PUBLIC_TAGGED_GC_POINTER_TYPE(D) \
111 : FOR_EACH_INTERNAL_GC_POINTER_TYPE(D) \
112 : FOR_EACH_INTERNAL_TAGGED_GC_POINTER_TYPE(D)
113 :
114 : namespace js {
115 :
116 : // Define the GCPolicy for all internal pointers.
117 : template <typename T>
118 : struct InternalGCPointerPolicy {
119 : using Type = typename mozilla::RemovePointer<T>::Type;
120 1261198 : static T initial() { return nullptr; }
121 : static void preBarrier(T v) { Type::writeBarrierPre(v); }
122 : static void postBarrier(T* vp, T prev, T next) { Type::writeBarrierPost(vp, prev, next); }
123 : static void readBarrier(T v) { Type::readBarrier(v); }
124 0 : static void trace(JSTracer* trc, T* vp, const char* name) {
125 0 : TraceManuallyBarrieredEdge(trc, vp, name);
126 0 : }
127 : };
128 :
129 : } // namespace js
130 :
131 : namespace JS {
132 :
133 : #define DEFINE_INTERNAL_GC_POLICY(type) \
134 : template <> struct GCPolicy<type> : public js::InternalGCPointerPolicy<type> {};
135 : FOR_EACH_INTERNAL_GC_POINTER_TYPE(DEFINE_INTERNAL_GC_POLICY)
136 : #undef DEFINE_INTERNAL_GC_POLICY
137 :
138 : template <typename T>
139 : struct GCPolicy<js::HeapPtr<T>>
140 : {
141 0 : static void trace(JSTracer* trc, js::HeapPtr<T>* thingp, const char* name) {
142 0 : js::TraceEdge(trc, thingp, name);
143 0 : }
144 : static bool needsSweep(js::HeapPtr<T>* thingp) {
145 : return js::gc::IsAboutToBeFinalized(thingp);
146 : }
147 : };
148 :
149 : template <typename T>
150 : struct GCPolicy<js::ReadBarriered<T>>
151 : {
152 : static void trace(JSTracer* trc, js::ReadBarriered<T>* thingp, const char* name) {
153 : js::TraceEdge(trc, thingp, name);
154 : }
155 0 : static bool needsSweep(js::ReadBarriered<T>* thingp) {
156 0 : return js::gc::IsAboutToBeFinalized(thingp);
157 : }
158 : };
159 :
160 : } // namespace JS
161 :
162 : #endif // gc_Policy_h
|