g1OopClosures.hpp 11.1 KB
Newer Older
1
/*
2
 * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
22 23 24
 *
 */

25 26 27
#ifndef SHARE_VM_GC_IMPLEMENTATION_G1_G1OOPCLOSURES_HPP
#define SHARE_VM_GC_IMPLEMENTATION_G1_G1OOPCLOSURES_HPP

28 29 30 31 32 33 34 35
class HeapRegion;
class G1CollectedHeap;
class G1RemSet;
class ConcurrentMark;
class DirtyCardToOopClosure;
class CMBitMap;
class CMMarkStack;
class G1ParScanThreadState;
36
class CMTask;
37
class ReferenceProcessor;
38 39 40

// A class that scans oops in a given heap region (much as OopsInGenClosure
// scans oops in a generation.)
41
class OopsInHeapRegionClosure: public ExtendedOopClosure {
42 43 44
protected:
  HeapRegion* _from;
public:
45
  void set_region(HeapRegion* from) { _from = from; }
46 47 48 49 50 51 52 53
};

class G1ParClosureSuper : public OopsInHeapRegionClosure {
protected:
  G1CollectedHeap* _g1;
  G1RemSet* _g1_rem;
  ConcurrentMark* _cm;
  G1ParScanThreadState* _par_scan_state;
54
  uint _worker_id;
55 56
  bool _during_initial_mark;
  bool _mark_in_progress;
57 58 59 60 61
public:
  G1ParClosureSuper(G1CollectedHeap* g1, G1ParScanThreadState* par_scan_state);
  bool apply_to_weak_ref_discovered_field() { return true; }
};

62 63
class G1ParPushHeapRSClosure : public G1ParClosureSuper {
public:
64
  G1ParPushHeapRSClosure(G1CollectedHeap* g1,
65 66
                         G1ParScanThreadState* par_scan_state):
    G1ParClosureSuper(g1, par_scan_state) { }
67

68 69 70 71 72
  template <class T> void do_oop_nv(T* p);
  virtual void do_oop(oop* p)          { do_oop_nv(p); }
  virtual void do_oop(narrowOop* p)    { do_oop_nv(p); }
};

73 74
class G1ParScanClosure : public G1ParClosureSuper {
public:
75 76 77 78 79 80 81
  G1ParScanClosure(G1CollectedHeap* g1, G1ParScanThreadState* par_scan_state, ReferenceProcessor* rp) :
    G1ParClosureSuper(g1, par_scan_state)
  {
    assert(_ref_processor == NULL, "sanity");
    _ref_processor = rp;
  }

82
  template <class T> void do_oop_nv(T* p);
83 84 85 86
  virtual void do_oop(oop* p)          { do_oop_nv(p); }
  virtual void do_oop(narrowOop* p)    { do_oop_nv(p); }
};

87
#define G1_PARTIAL_ARRAY_MASK 0x2
88

89
inline bool has_partial_array_mask(oop* ref) {
90
  return ((uintptr_t)ref & G1_PARTIAL_ARRAY_MASK) == G1_PARTIAL_ARRAY_MASK;
91 92
}

93 94 95 96 97 98 99 100 101 102 103 104 105 106
// We never encode partial array oops as narrowOop*, so return false immediately.
// This allows the compiler to create optimized code when popping references from
// the work queue.
inline bool has_partial_array_mask(narrowOop* ref) {
  assert(((uintptr_t)ref & G1_PARTIAL_ARRAY_MASK) != G1_PARTIAL_ARRAY_MASK, "Partial array oop reference encoded as narrowOop*");
  return false;
}

// Only implement set_partial_array_mask() for regular oops, not for narrowOops.
// We always encode partial arrays as regular oop, to allow the
// specialization for has_partial_array_mask() for narrowOops above.
// This means that unintentional use of this method with narrowOops are caught
// by the compiler.
inline oop* set_partial_array_mask(oop obj) {
107
  assert(((uintptr_t)(void *)obj & G1_PARTIAL_ARRAY_MASK) == 0, "Information loss!");
108
  return (oop*) ((uintptr_t)(void *)obj | G1_PARTIAL_ARRAY_MASK);
109 110
}

111
template <class T> inline oop clear_partial_array_mask(T* ref) {
112
  return cast_to_oop((intptr_t)ref & ~G1_PARTIAL_ARRAY_MASK);
113 114
}

115 116
class G1ParScanPartialArrayClosure : public G1ParClosureSuper {
  G1ParScanClosure _scanner;
117

118
public:
119 120 121 122 123 124 125 126 127 128
  G1ParScanPartialArrayClosure(G1CollectedHeap* g1, G1ParScanThreadState* par_scan_state, ReferenceProcessor* rp) :
    G1ParClosureSuper(g1, par_scan_state), _scanner(g1, par_scan_state, rp)
  {
    assert(_ref_processor == NULL, "sanity");
  }

  G1ParScanClosure* scanner() {
    return &_scanner;
  }

129
  template <class T> void do_oop_nv(T* p);
130 131 132 133
  virtual void do_oop(oop* p)       { do_oop_nv(p); }
  virtual void do_oop(narrowOop* p) { do_oop_nv(p); }
};

134 135 136 137 138 139 140 141 142 143 144 145 146
// Add back base class for metadata
class G1ParCopyHelper : public G1ParClosureSuper {
  Klass* _scanned_klass;

 public:
  G1ParCopyHelper(G1CollectedHeap* g1,  G1ParScanThreadState* par_scan_state) :
      _scanned_klass(NULL),
      G1ParClosureSuper(g1, par_scan_state) {}

  void set_scanned_klass(Klass* k) { _scanned_klass = k; }
  template <class T> void do_klass_barrier(T* p, oop new_obj);
};

147
template <G1Barrier barrier, bool do_mark_object>
148
class G1ParCopyClosure : public G1ParCopyHelper {
B
brutisso 已提交
149 150
  G1ParScanClosure _scanner;
  template <class T> void do_oop_work(T* p);
151 152

protected:
153 154 155 156 157 158 159 160 161 162 163 164
  // Mark the object if it's not already marked. This is used to mark
  // objects pointed to by roots that are guaranteed not to move
  // during the GC (i.e., non-CSet objects). It is MT-safe.
  void mark_object(oop obj);

  // Mark the object if it's not already marked. This is used to mark
  // objects pointed to by roots that have been forwarded during a
  // GC. It is MT-safe.
  void mark_forwarded_object(oop from_obj, oop to_obj);

  oop copy_to_survivor_space(oop obj);

165
public:
166 167
  G1ParCopyClosure(G1CollectedHeap* g1, G1ParScanThreadState* par_scan_state,
                   ReferenceProcessor* rp) :
168
      _scanner(g1, par_scan_state, rp),
169
      G1ParCopyHelper(g1, par_scan_state) {
170 171 172 173 174
    assert(_ref_processor == NULL, "sanity");
  }

  G1ParScanClosure* scanner() { return &_scanner; }

175
  template <class T> void do_oop_nv(T* p) {
176 177 178 179 180 181
    do_oop_work(p);
  }
  virtual void do_oop(oop* p)       { do_oop_nv(p); }
  virtual void do_oop(narrowOop* p) { do_oop_nv(p); }
};

182 183
typedef G1ParCopyClosure<G1BarrierNone, false> G1ParScanExtRootClosure;
typedef G1ParCopyClosure<G1BarrierKlass, false> G1ParScanMetadataClosure;
184

185

186 187
typedef G1ParCopyClosure<G1BarrierNone, true> G1ParScanAndMarkExtRootClosure;
typedef G1ParCopyClosure<G1BarrierKlass, true> G1ParScanAndMarkMetadataClosure;
188 189 190

// The following closure type is defined in g1_specialized_oop_closures.hpp:
//
191
// typedef G1ParCopyClosure<G1BarrierEvac, false> G1ParScanHeapEvacClosure;
192 193 194 195 196 197 198

// We use a separate closure to handle references during evacuation
// failure processing.
// We could have used another instance of G1ParScanHeapEvacClosure
// (since that closure no longer assumes that the references it
// handles point into the collection set).

199
typedef G1ParCopyClosure<G1BarrierEvac, false> G1ParScanHeapEvacFailureClosure;
200

201
class FilterIntoCSClosure: public ExtendedOopClosure {
202 203 204 205 206
  G1CollectedHeap* _g1;
  OopClosure* _oc;
  DirtyCardToOopClosure* _dcto_cl;
public:
  FilterIntoCSClosure(  DirtyCardToOopClosure* dcto_cl,
207
                        G1CollectedHeap* g1,
208 209
                        OopClosure* oc) :
    _dcto_cl(dcto_cl), _g1(g1), _oc(oc) { }
210

211 212 213
  template <class T> void do_oop_nv(T* p);
  virtual void do_oop(oop* p)        { do_oop_nv(p); }
  virtual void do_oop(narrowOop* p)  { do_oop_nv(p); }
214 215 216
  bool apply_to_weak_ref_discovered_field() { return true; }
};

217
class FilterOutOfRegionClosure: public ExtendedOopClosure {
218 219 220 221 222
  HeapWord* _r_bottom;
  HeapWord* _r_end;
  OopClosure* _oc;
public:
  FilterOutOfRegionClosure(HeapRegion* r, OopClosure* oc);
223 224 225
  template <class T> void do_oop_nv(T* p);
  virtual void do_oop(oop* p) { do_oop_nv(p); }
  virtual void do_oop(narrowOop* p) { do_oop_nv(p); }
226 227
  bool apply_to_weak_ref_discovered_field() { return true; }
};
228

229
// Closure for iterating over object fields during concurrent marking
230
class G1CMOopClosure : public ExtendedOopClosure {
231
private:
232 233 234 235 236 237 238 239 240 241
  G1CollectedHeap*   _g1h;
  ConcurrentMark*    _cm;
  CMTask*            _task;
public:
  G1CMOopClosure(G1CollectedHeap* g1h, ConcurrentMark* cm, CMTask* task);
  template <class T> void do_oop_nv(T* p);
  virtual void do_oop(      oop* p) { do_oop_nv(p); }
  virtual void do_oop(narrowOop* p) { do_oop_nv(p); }
};

242
// Closure to scan the root regions during concurrent marking
243
class G1RootRegionScanClosure : public ExtendedOopClosure {
244 245 246 247 248 249 250 251 252 253 254 255 256
private:
  G1CollectedHeap* _g1h;
  ConcurrentMark*  _cm;
  uint _worker_id;
public:
  G1RootRegionScanClosure(G1CollectedHeap* g1h, ConcurrentMark* cm,
                          uint worker_id) :
    _g1h(g1h), _cm(cm), _worker_id(worker_id) { }
  template <class T> void do_oop_nv(T* p);
  virtual void do_oop(      oop* p) { do_oop_nv(p); }
  virtual void do_oop(narrowOop* p) { do_oop_nv(p); }
};

257 258 259 260 261
// Closure that applies the given two closures in sequence.
// Used by the RSet refinement code (when updating RSets
// during an evacuation pause) to record cards containing
// pointers into the collection set.

262
class G1Mux2Closure : public ExtendedOopClosure {
263 264 265 266 267 268 269 270 271 272 273 274
  OopClosure* _c1;
  OopClosure* _c2;
public:
  G1Mux2Closure(OopClosure *c1, OopClosure *c2);
  template <class T> void do_oop_nv(T* p);
  virtual void do_oop(oop* p)        { do_oop_nv(p); }
  virtual void do_oop(narrowOop* p)  { do_oop_nv(p); }
};

// A closure that returns true if it is actually applied
// to a reference

275
class G1TriggerClosure : public ExtendedOopClosure {
276 277 278 279 280 281 282 283 284 285 286 287
  bool _triggered;
public:
  G1TriggerClosure();
  bool triggered() const { return _triggered; }
  template <class T> void do_oop_nv(T* p);
  virtual void do_oop(oop* p)        { do_oop_nv(p); }
  virtual void do_oop(narrowOop* p)  { do_oop_nv(p); }
};

// A closure which uses a triggering closure to determine
// whether to apply an oop closure.

288
class G1InvokeIfNotTriggeredClosure: public ExtendedOopClosure {
289 290 291 292 293 294 295 296 297
  G1TriggerClosure* _trigger_cl;
  OopClosure* _oop_cl;
public:
  G1InvokeIfNotTriggeredClosure(G1TriggerClosure* t, OopClosure* oc);
  template <class T> void do_oop_nv(T* p);
  virtual void do_oop(oop* p)        { do_oop_nv(p); }
  virtual void do_oop(narrowOop* p)  { do_oop_nv(p); }
};

298
class G1UpdateRSOrPushRefOopClosure: public ExtendedOopClosure {
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
  G1CollectedHeap* _g1;
  G1RemSet* _g1_rem_set;
  HeapRegion* _from;
  OopsInHeapRegionClosure* _push_ref_cl;
  bool _record_refs_into_cset;
  int _worker_i;

public:
  G1UpdateRSOrPushRefOopClosure(G1CollectedHeap* g1h,
                                G1RemSet* rs,
                                OopsInHeapRegionClosure* push_ref_cl,
                                bool record_refs_into_cset,
                                int worker_i = 0);

  void set_from(HeapRegion* from) {
    assert(from != NULL, "from region must be non-NULL");
    _from = from;
  }

  bool self_forwarded(oop obj) {
    bool result = (obj->is_forwarded() && (obj->forwardee()== obj));
    return result;
  }

  bool apply_to_weak_ref_discovered_field() { return true; }

  template <class T> void do_oop_nv(T* p);
  virtual void do_oop(narrowOop* p) { do_oop_nv(p); }
  virtual void do_oop(oop* p)       { do_oop_nv(p); }
};

330
#endif // SHARE_VM_GC_IMPLEMENTATION_G1_G1OOPCLOSURES_HPP