1 /*
  2  * Copyright (c) 2017, 2021, 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 #include "precompiled.hpp"
 26 #include "gc/g1/g1FullGCCompactionPoint.hpp"
 27 #include "gc/g1/heapRegion.hpp"
 28 #include "gc/shared/slidingForwarding.inline.hpp"
 29 #include "oops/oop.inline.hpp"
 30 #include "utilities/debug.hpp"
 31 
 32 G1FullGCCompactionPoint::G1FullGCCompactionPoint() :
 33     _current_region(NULL),
 34     _compaction_top(NULL) {
 35   _compaction_regions = new (ResourceObj::C_HEAP, mtGC) GrowableArray<HeapRegion*>(32, mtGC);
 36   _compaction_region_iterator = _compaction_regions->begin();
 37 }
 38 
 39 G1FullGCCompactionPoint::~G1FullGCCompactionPoint() {
 40   delete _compaction_regions;
 41 }
 42 
 43 void G1FullGCCompactionPoint::update() {
 44   if (is_initialized()) {
 45     _current_region->set_compaction_top(_compaction_top);
 46   }
 47 }
 48 
 49 void G1FullGCCompactionPoint::initialize_values(bool init_threshold) {
 50   _compaction_top = _current_region->compaction_top();
 51   if (init_threshold) {
 52     _current_region->initialize_bot_threshold();
 53   }
 54 }
 55 
 56 bool G1FullGCCompactionPoint::has_regions() {
 57   return !_compaction_regions->is_empty();
 58 }
 59 
 60 bool G1FullGCCompactionPoint::is_initialized() {
 61   return _current_region != NULL;
 62 }
 63 
 64 void G1FullGCCompactionPoint::initialize(HeapRegion* hr, bool init_threshold) {
 65   _current_region = hr;
 66   initialize_values(init_threshold);
 67 }
 68 
 69 HeapRegion* G1FullGCCompactionPoint::current_region() {
 70   return *_compaction_region_iterator;
 71 }
 72 
 73 HeapRegion* G1FullGCCompactionPoint::next_region() {
 74   HeapRegion* next = *(++_compaction_region_iterator);
 75   assert(next != NULL, "Must return valid region");
 76   return next;
 77 }
 78 
 79 GrowableArray<HeapRegion*>* G1FullGCCompactionPoint::regions() {
 80   return _compaction_regions;
 81 }
 82 
 83 bool G1FullGCCompactionPoint::object_will_fit(size_t size) {
 84   size_t space_left = pointer_delta(_current_region->end(), _compaction_top);
 85   return size <= space_left;
 86 }
 87 
 88 void G1FullGCCompactionPoint::switch_region() {
 89   // Save compaction top in the region.
 90   _current_region->set_compaction_top(_compaction_top);
 91   // Get the next region and re-initialize the values.
 92   _current_region = next_region();
 93   initialize_values(true);
 94 }
 95 
 96 void G1FullGCCompactionPoint::forward(SlidingForwarding* const forwarding, oop object, size_t size) {
 97   assert(_current_region != NULL, "Must have been initialized");
 98 
 99   // Ensure the object fit in the current region.
100   while (!object_will_fit(size)) {
101     switch_region();
102   }
103 
104   // Store a forwarding pointer if the object should be moved.
105   if (cast_from_oop<HeapWord*>(object) != _compaction_top) {
106     forwarding->forward_to(object, cast_to_oop(_compaction_top));
107     assert(object->is_forwarded(), "must be forwarded");
108   } else {
109     assert(!object->is_forwarded(), "must not be forwarded");
110   }
111 
112   // Update compaction values.
113   _compaction_top += size;
114   _current_region->alloc_block_in_bot(_compaction_top - size, _compaction_top);
115 }
116 
117 void G1FullGCCompactionPoint::add(HeapRegion* hr) {
118   _compaction_regions->append(hr);
119 }
120 
121 HeapRegion* G1FullGCCompactionPoint::remove_last() {
122   return _compaction_regions->pop();
123 }