1 /*
   2  * Copyright (c) 2018, Red Hat, Inc. All rights reserved.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 #include "precompiled.hpp"
  25 #include "gc/shared/markBitMap.inline.hpp"
  26 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
  27 #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp"
  28 #include "gc/shenandoah/shenandoahMarkingContext.hpp"
  29 
  30 ShenandoahMarkingContext::ShenandoahMarkingContext(MemRegion heap_region, MemRegion bitmap_region, size_t num_regions) :
  31   _top_bitmaps(NEW_C_HEAP_ARRAY(HeapWord*, num_regions, mtGC)),
  32   _top_at_mark_starts_base(NEW_C_HEAP_ARRAY(HeapWord*, num_regions, mtGC)),
  33   _top_at_mark_starts(_top_at_mark_starts_base -
  34                       ((uintx) heap_region.start() >> ShenandoahHeapRegion::region_size_bytes_shift())) {
  35   _mark_bit_map.initialize(heap_region, bitmap_region);
  36 }
  37 
  38 bool ShenandoahMarkingContext::is_bitmap_clear() const {
  39   ShenandoahHeap* heap = ShenandoahHeap::heap();
  40   size_t num_regions = heap->num_regions();
  41   for (size_t idx = 0; idx < num_regions; idx++) {
  42     ShenandoahHeapRegion* r = heap->get_region(idx);
  43     if (heap->is_bitmap_slice_committed(r) && !is_bitmap_clear_range(r->bottom(), r->end())) {
  44       return false;
  45     }
  46   }
  47   return true;
  48 }
  49 
  50 bool ShenandoahMarkingContext::is_bitmap_clear_range(HeapWord* start, HeapWord* end) const {
  51   return _mark_bit_map.getNextMarkedWordAddress(start, end) == end;
  52 }
  53 
  54 void ShenandoahMarkingContext::initialize_top_at_mark_start(ShenandoahHeapRegion* r) {
  55   size_t idx = r->index();
  56   HeapWord *bottom = r->bottom();
  57   _top_at_mark_starts_base[idx] = bottom;
  58   _top_bitmaps[idx] = bottom;
  59 }
  60 
  61 void ShenandoahMarkingContext::clear_bitmap(ShenandoahHeapRegion* r) {
  62   HeapWord* bottom = r->bottom();
  63   HeapWord* top_bitmap = _top_bitmaps[r->index()];
  64   if (top_bitmap > bottom) {
  65     _mark_bit_map.clear_range_large(MemRegion(bottom, top_bitmap));
  66     _top_bitmaps[r->index()] = bottom;
  67   }
  68   assert(is_bitmap_clear_range(bottom, r->end()),
  69          "Region " SIZE_FORMAT " should have no marks in bitmap", r->index());
  70 }
  71 
  72 bool ShenandoahMarkingContext::is_complete() {
  73   return _is_complete.is_set();
  74 }
  75 
  76 void ShenandoahMarkingContext::mark_complete() {
  77   _is_complete.set();
  78 }
  79 
  80 void ShenandoahMarkingContext::mark_incomplete() {
  81   _is_complete.unset();
  82 }