1 /*
   2  * Copyright (c) 2014, 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 #ifndef SHARE_VM_GC_IMPLEMENTATION_G1_G1PARSCANTHREADSTATE_INLINE_HPP
  26 #define SHARE_VM_GC_IMPLEMENTATION_G1_G1PARSCANTHREADSTATE_INLINE_HPP
  27 
  28 #include "gc_implementation/g1/g1ParScanThreadState.hpp"
  29 #include "gc_implementation/g1/g1RemSet.inline.hpp"
  30 
  31 template <class T> inline void G1ParScanThreadState::immediate_rs_update(HeapRegion* from, T* p, int tid) {
  32   if (!from->is_survivor()) {
  33     _g1_rem->par_write_ref(from, p, tid);
  34   }
  35 }
  36 
  37 template <class T> void G1ParScanThreadState::update_rs(HeapRegion* from, T* p, int tid) {
  38   if (G1DeferredRSUpdate) {
  39     deferred_rs_update(from, p, tid);
  40   } else {
  41     immediate_rs_update(from, p, tid);
  42   }
  43 }
  44 
  45 inline void G1ParScanThreadState::do_oop_partial_array(oop* p) {
  46   assert(has_partial_array_mask(p), "invariant");
  47   oop from_obj = clear_partial_array_mask(p);
  48 
  49   assert(Universe::heap()->is_in_reserved(from_obj), "must be in heap.");
  50   assert(from_obj->is_objArray(), "must be obj array");
  51   objArrayOop from_obj_array = objArrayOop(from_obj);
  52   // The from-space object contains the real length.
  53   int length                 = from_obj_array->length();
  54 
  55   assert(from_obj->is_forwarded(), "must be forwarded");
  56   oop to_obj                 = from_obj->forwardee();
  57   assert(from_obj != to_obj, "should not be chunking self-forwarded objects");
  58   objArrayOop to_obj_array   = objArrayOop(to_obj);
  59   // We keep track of the next start index in the length field of the
  60   // to-space object.
  61   int next_index             = to_obj_array->length();
  62   assert(0 <= next_index && next_index < length,
  63          err_msg("invariant, next index: %d, length: %d", next_index, length));
  64 
  65   int start                  = next_index;
  66   int end                    = length;
  67   int remainder              = end - start;
  68   // We'll try not to push a range that's smaller than ParGCArrayScanChunk.
  69   if (remainder > 2 * ParGCArrayScanChunk) {
  70     end = start + ParGCArrayScanChunk;
  71     to_obj_array->set_length(end);
  72     // Push the remainder before we process the range in case another
  73     // worker has run out of things to do and can steal it.
  74     oop* from_obj_p = set_partial_array_mask(from_obj);
  75     push_on_queue(from_obj_p);
  76   } else {
  77     assert(length == end, "sanity");
  78     // We'll process the final range for this object. Restore the length
  79     // so that the heap remains parsable in case of evacuation failure.
  80     to_obj_array->set_length(end);
  81   }
  82   _scanner.set_region(_g1h->heap_region_containing_raw(to_obj));
  83   // Process indexes [start,end). It will also process the header
  84   // along with the first chunk (i.e., the chunk with start == 0).
  85   // Note that at this point the length field of to_obj_array is not
  86   // correct given that we are using it to keep track of the next
  87   // start index. oop_iterate_range() (thankfully!) ignores the length
  88   // field and only relies on the start / end parameters.  It does
  89   // however return the size of the object which will be incorrect. So
  90   // we have to ignore it even if we wanted to use it.
  91   to_obj_array->oop_iterate_range(&_scanner, start, end);
  92 }
  93 
  94 template <class T> inline void G1ParScanThreadState::deal_with_reference(T* ref_to_scan) {
  95   if (!has_partial_array_mask(ref_to_scan)) {
  96     // Note: we can use "raw" versions of "region_containing" because
  97     // "obj_to_scan" is definitely in the heap, and is not in a
  98     // humongous region.
  99     HeapRegion* r = _g1h->heap_region_containing_raw(ref_to_scan);
 100     do_oop_evac(ref_to_scan, r);
 101   } else {
 102     do_oop_partial_array((oop*)ref_to_scan);
 103   }
 104 }
 105 
 106 inline void G1ParScanThreadState::deal_with_reference(StarTask ref) {
 107   assert(verify_task(ref), "sanity");
 108   if (ref.is_narrow()) {
 109     deal_with_reference((narrowOop*)ref);
 110   } else {
 111     deal_with_reference((oop*)ref);
 112   }
 113 }
 114 
 115 #endif  /* SHARE_VM_GC_IMPLEMENTATION_G1_G1PARSCANTHREADSTATE_INLINE_HPP */