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 /* Shadow definition of |JS::Zone| innards.  Do not use this directly! */
8 
9 #ifndef js_shadow_Zone_h
10 #define js_shadow_Zone_h
11 
12 #include "mozilla/Assertions.h"  // MOZ_ASSERT
13 
14 #include <stdint.h>  // uint8_t, uint32_t
15 
16 #include "jspubtd.h"  // js::CurrentThreadCanAccessRuntime
17 
18 struct JS_PUBLIC_API JSRuntime;
19 class JS_PUBLIC_API JSTracer;
20 
21 namespace JS {
22 
23 namespace shadow {
24 
25 struct Zone {
26   enum GCState : uint8_t {
27     NoGC,
28     Prepare,
29     MarkBlackOnly,
30     MarkBlackAndGray,
31     Sweep,
32     Finished,
33     Compact
34   };
35 
36   enum Kind : uint8_t { NormalZone, AtomsZone, SelfHostingZone, SystemZone };
37 
38  protected:
39   JSRuntime* const runtime_;
40   JSTracer* const barrierTracer_;  // A pointer to the JSRuntime's |gcMarker|.
41   uint32_t needsIncrementalBarrier_ = 0;
42   GCState gcState_ = NoGC;
43   const Kind kind_;
44 
ZoneZone45   Zone(JSRuntime* runtime, JSTracer* barrierTracerArg, Kind kind)
46       : runtime_(runtime), barrierTracer_(barrierTracerArg), kind_(kind) {}
47 
48  public:
needsIncrementalBarrierZone49   bool needsIncrementalBarrier() const { return needsIncrementalBarrier_; }
50 
barrierTracerZone51   JSTracer* barrierTracer() {
52     MOZ_ASSERT(needsIncrementalBarrier_);
53     MOZ_ASSERT(js::CurrentThreadCanAccessRuntime(runtime_));
54     return barrierTracer_;
55   }
56 
runtimeFromMainThreadZone57   JSRuntime* runtimeFromMainThread() const {
58     MOZ_ASSERT(js::CurrentThreadCanAccessRuntime(runtime_));
59     return runtime_;
60   }
61 
62   // Note: Unrestricted access to the zone's runtime from an arbitrary
63   // thread can easily lead to races. Use this method very carefully.
runtimeFromAnyThreadZone64   JSRuntime* runtimeFromAnyThread() const { return runtime_; }
65 
gcStateZone66   GCState gcState() const { return gcState_; }
wasGCStartedZone67   bool wasGCStarted() const { return gcState_ != NoGC; }
isGCPreparingZone68   bool isGCPreparing() const { return gcState_ == Prepare; }
isGCMarkingBlackOnlyZone69   bool isGCMarkingBlackOnly() const { return gcState_ == MarkBlackOnly; }
isGCMarkingBlackAndGrayZone70   bool isGCMarkingBlackAndGray() const { return gcState_ == MarkBlackAndGray; }
isGCSweepingZone71   bool isGCSweeping() const { return gcState_ == Sweep; }
isGCFinishedZone72   bool isGCFinished() const { return gcState_ == Finished; }
isGCCompactingZone73   bool isGCCompacting() const { return gcState_ == Compact; }
isGCMarkingZone74   bool isGCMarking() const {
75     return isGCMarkingBlackOnly() || isGCMarkingBlackAndGray();
76   }
isGCMarkingOrSweepingZone77   bool isGCMarkingOrSweeping() const {
78     return gcState_ >= MarkBlackOnly && gcState_ <= Sweep;
79   }
isGCSweepingOrCompactingZone80   bool isGCSweepingOrCompacting() const {
81     return gcState_ == Sweep || gcState_ == Compact;
82   }
83 
isAtomsZoneZone84   bool isAtomsZone() const { return kind_ == AtomsZone; }
isSelfHostingZoneZone85   bool isSelfHostingZone() const { return kind_ == SelfHostingZone; }
isSystemZoneZone86   bool isSystemZone() const { return kind_ == SystemZone; }
87 
fromZone88   static shadow::Zone* from(JS::Zone* zone) {
89     return reinterpret_cast<shadow::Zone*>(zone);
90   }
91 };
92 
93 }  // namespace shadow
94 
95 }  // namespace JS
96 
97 #endif  // js_shadow_Zone_h
98