1 /*
   2  * Copyright (c) 2001, 2008, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 inline OopsInGenClosure::OopsInGenClosure(Generation* gen) :
  26   OopClosure(gen->ref_processor()), _orig_gen(gen), _rs(NULL) {
  27   set_generation(gen);
  28 }
  29 
  30 inline void OopsInGenClosure::set_generation(Generation* gen) {
  31   _gen = gen;
  32   _gen_boundary = _gen->reserved().start();
  33   // Barrier set for the heap, must be set after heap is initialized
  34   if (_rs == NULL) {
  35     GenRemSet* rs = SharedHeap::heap()->rem_set();
  36     assert(rs->rs_kind() == GenRemSet::CardTable, "Wrong rem set kind");
  37     _rs = (CardTableRS*)rs;
  38   }
  39 }
  40 
  41 template <class T> inline void OopsInGenClosure::do_barrier(T* p) {
  42   assert(generation()->is_in_reserved(p), "expected ref in generation");
  43   T heap_oop = oopDesc::load_heap_oop(p);
  44   assert(!oopDesc::is_null(heap_oop), "expected non-null oop");
  45   oop obj = oopDesc::decode_heap_oop_not_null(heap_oop);
  46   // If p points to a younger generation, mark the card.
  47   if ((HeapWord*)obj < _gen_boundary) {
  48     _rs->inline_write_ref_field_gc(p, obj);
  49   }
  50 }
  51 
  52 template <class T> inline void OopsInGenClosure::par_do_barrier(T* p) {
  53   assert(generation()->is_in_reserved(p), "expected ref in generation");
  54   T heap_oop = oopDesc::load_heap_oop(p);
  55   assert(!oopDesc::is_null(heap_oop), "expected non-null oop");
  56   oop obj = oopDesc::decode_heap_oop_not_null(heap_oop);
  57   // If p points to a younger generation, mark the card.
  58   if ((HeapWord*)obj < gen_boundary()) {
  59     rs()->write_ref_field_gc_par(p, obj);
  60   }
  61 }
  62 
  63 // NOTE! Any changes made here should also be made
  64 // in FastScanClosure::do_oop_work()
  65 template <class T> inline void ScanClosure::do_oop_work(T* p) {
  66   T heap_oop = oopDesc::load_heap_oop(p);
  67   // Should we copy the obj?
  68   if (!oopDesc::is_null(heap_oop)) {
  69     oop obj = oopDesc::decode_heap_oop_not_null(heap_oop);
  70     if ((HeapWord*)obj < _boundary) {
  71       assert(!_g->to()->is_in_reserved(obj), "Scanning field twice?");
  72       oop new_obj = obj->is_forwarded() ? obj->forwardee()
  73                                         : _g->copy_to_survivor_space(obj);
  74       oopDesc::encode_store_heap_oop_not_null(p, new_obj);
  75     }
  76     if (_gc_barrier) {
  77       // Now call parent closure
  78       do_barrier(p);
  79     }
  80   }
  81 }
  82 
  83 inline void ScanClosure::do_oop_nv(oop* p)       { ScanClosure::do_oop_work(p); }
  84 inline void ScanClosure::do_oop_nv(narrowOop* p) { ScanClosure::do_oop_work(p); }
  85 
  86 // NOTE! Any changes made here should also be made
  87 // in ScanClosure::do_oop_work()
  88 template <class T> inline void FastScanClosure::do_oop_work(T* p) {
  89   T heap_oop = oopDesc::load_heap_oop(p);
  90   // Should we copy the obj?
  91   if (!oopDesc::is_null(heap_oop)) {
  92     oop obj = oopDesc::decode_heap_oop_not_null(heap_oop);
  93     if ((HeapWord*)obj < _boundary) {
  94       assert(!_g->to()->is_in_reserved(obj), "Scanning field twice?");
  95       oop new_obj = obj->is_forwarded() ? obj->forwardee()
  96                                         : _g->copy_to_survivor_space(obj);
  97       oopDesc::encode_store_heap_oop_not_null(p, new_obj);
  98       if (_gc_barrier) {
  99         // Now call parent closure
 100         do_barrier(p);
 101       }
 102     }
 103   }
 104 }
 105 
 106 inline void FastScanClosure::do_oop_nv(oop* p)       { FastScanClosure::do_oop_work(p); }
 107 inline void FastScanClosure::do_oop_nv(narrowOop* p) { FastScanClosure::do_oop_work(p); }
 108 
 109 // Note similarity to ScanClosure; the difference is that
 110 // the barrier set is taken care of outside this closure.
 111 template <class T> inline void ScanWeakRefClosure::do_oop_work(T* p) {
 112   assert(!oopDesc::is_null(*p), "null weak reference?");
 113   oop obj = oopDesc::load_decode_heap_oop_not_null(p);
 114   // weak references are sometimes scanned twice; must check
 115   // that to-space doesn't already contain this object
 116   if ((HeapWord*)obj < _boundary && !_g->to()->is_in_reserved(obj)) {
 117     oop new_obj = obj->is_forwarded() ? obj->forwardee()
 118                                       : _g->copy_to_survivor_space(obj);
 119     oopDesc::encode_store_heap_oop_not_null(p, new_obj);
 120   }
 121 }
 122 
 123 inline void ScanWeakRefClosure::do_oop_nv(oop* p)       { ScanWeakRefClosure::do_oop_work(p); }
 124 inline void ScanWeakRefClosure::do_oop_nv(narrowOop* p) { ScanWeakRefClosure::do_oop_work(p); }