1 /*
   2  * Copyright (c) 2015, 2019, 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_GC_G1_G1ALLOCATOR_INLINE_HPP
  26 #define SHARE_GC_G1_G1ALLOCATOR_INLINE_HPP
  27 
  28 #include "gc/g1/g1Allocator.hpp"
  29 #include "gc/g1/g1AllocRegion.inline.hpp"
  30 #include "gc/shared/plab.inline.hpp"
  31 #include "memory/universe.hpp"
  32 
  33 inline MutatorAllocRegion* G1Allocator::mutator_alloc_region(uint node_index) {
  34   assert(_g1h->mem_node_mgr()->is_valid_node_index(node_index), "Invariant, index %u", node_index);
  35   return &_mutator_alloc_region[node_index];
  36 }
  37 
  38 inline SurvivorGCAllocRegion* G1Allocator::survivor_gc_alloc_region() {
  39   return &_survivor_gc_alloc_region;
  40 }
  41 
  42 inline OldGCAllocRegion* G1Allocator::old_gc_alloc_region() {
  43   return &_old_gc_alloc_region;
  44 }
  45 
  46 inline HeapWord* G1Allocator::attempt_allocation(size_t min_word_size,
  47                                                  size_t desired_word_size,
  48                                                  size_t* actual_word_size) {
  49   uint node_index = _g1h->mem_node_mgr()->index_of_current_thread();
  50   HeapWord* result = mutator_alloc_region(node_index)->attempt_retained_allocation(min_word_size, desired_word_size, actual_word_size);
  51   if (result != NULL) {
  52     return result;
  53   }
  54   return mutator_alloc_region(node_index)->attempt_allocation(min_word_size, desired_word_size, actual_word_size);
  55 }
  56 
  57 inline HeapWord* G1Allocator::attempt_allocation_locked(size_t word_size) {
  58   uint node_index = _g1h->mem_node_mgr()->index_of_current_thread();
  59   HeapWord* result = mutator_alloc_region(node_index)->attempt_allocation_locked(word_size);
  60   assert(result != NULL || mutator_alloc_region(node_index)->get() == NULL,
  61          "Must not have a mutator alloc region if there is no memory, but is " PTR_FORMAT, p2i(mutator_alloc_region(node_index)->get()));
  62   return result;
  63 }
  64 
  65 inline HeapWord* G1Allocator::attempt_allocation_force(size_t word_size) {
  66   uint node_index = _g1h->mem_node_mgr()->index_of_current_thread();
  67   return mutator_alloc_region(node_index)->attempt_allocation_force(word_size);
  68 }
  69 
  70 inline PLAB* G1PLABAllocator::alloc_buffer(G1HeapRegionAttr dest) {
  71   assert(dest.is_valid(),
  72          "Allocation buffer index out of bounds: %s", dest.get_type_str());
  73   assert(_alloc_buffers[dest.type()] != NULL,
  74          "Allocation buffer is NULL: %s", dest.get_type_str());
  75   return _alloc_buffers[dest.type()];
  76 }
  77 
  78 inline HeapWord* G1PLABAllocator::plab_allocate(G1HeapRegionAttr dest,
  79                                                 size_t word_sz) {
  80   PLAB* buffer = alloc_buffer(dest);
  81   if (_survivor_alignment_bytes == 0 || !dest.is_young()) {
  82     return buffer->allocate(word_sz);
  83   } else {
  84     return buffer->allocate_aligned(word_sz, _survivor_alignment_bytes);
  85   }
  86 }
  87 
  88 inline HeapWord* G1PLABAllocator::allocate(G1HeapRegionAttr dest,
  89                                            size_t word_sz,
  90                                            bool* refill_failed) {
  91   HeapWord* const obj = plab_allocate(dest, word_sz);
  92   if (obj != NULL) {
  93     return obj;
  94   }
  95   return allocate_direct_or_new_plab(dest, word_sz, refill_failed);
  96 }
  97 
  98 // Create the maps which is used to identify archive objects.
  99 inline void G1ArchiveAllocator::enable_archive_object_check() {
 100   if (_archive_check_enabled) {
 101     return;
 102   }
 103 
 104   _archive_check_enabled = true;
 105   size_t length = G1CollectedHeap::heap()->max_reserved_capacity();
 106   _closed_archive_region_map.initialize(G1CollectedHeap::heap()->base(),
 107                                         G1CollectedHeap::heap()->base() + length,
 108                                         HeapRegion::GrainBytes);
 109   _open_archive_region_map.initialize(G1CollectedHeap::heap()->base(),
 110                                       G1CollectedHeap::heap()->base() + length,
 111                                       HeapRegion::GrainBytes);
 112 }
 113 
 114 // Set the regions containing the specified address range as archive.
 115 inline void G1ArchiveAllocator::set_range_archive(MemRegion range, bool open) {
 116   assert(_archive_check_enabled, "archive range check not enabled");
 117   log_info(gc, cds)("Mark %s archive regions in map: [" PTR_FORMAT ", " PTR_FORMAT "]",
 118                      open ? "open" : "closed",
 119                      p2i(range.start()),
 120                      p2i(range.last()));
 121   if (open) {
 122     _open_archive_region_map.set_by_address(range, true);
 123   } else {
 124     _closed_archive_region_map.set_by_address(range, true);
 125   }
 126 }
 127 
 128 // Clear the archive regions map containing the specified address range.
 129 inline void G1ArchiveAllocator::clear_range_archive(MemRegion range, bool open) {
 130   assert(_archive_check_enabled, "archive range check not enabled");
 131   log_info(gc, cds)("Clear %s archive regions in map: [" PTR_FORMAT ", " PTR_FORMAT "]",
 132                     open ? "open" : "closed",
 133                     p2i(range.start()),
 134                     p2i(range.last()));
 135   if (open) {
 136     _open_archive_region_map.set_by_address(range, false);
 137   } else {
 138     _closed_archive_region_map.set_by_address(range, false);
 139   }
 140 }
 141 
 142 // Check if an object is in a closed archive region using the _archive_region_map.
 143 inline bool G1ArchiveAllocator::in_closed_archive_range(oop object) {
 144   // This is the out-of-line part of is_closed_archive_object test, done separately
 145   // to avoid additional performance impact when the check is not enabled.
 146   return _closed_archive_region_map.get_by_address((HeapWord*)object);
 147 }
 148 
 149 inline bool G1ArchiveAllocator::in_open_archive_range(oop object) {
 150   return _open_archive_region_map.get_by_address((HeapWord*)object);
 151 }
 152 
 153 // Check if archive object checking is enabled, to avoid calling in_open/closed_archive_range
 154 // unnecessarily.
 155 inline bool G1ArchiveAllocator::archive_check_enabled() {
 156   return _archive_check_enabled;
 157 }
 158 
 159 inline bool G1ArchiveAllocator::is_closed_archive_object(oop object) {
 160   return (archive_check_enabled() && in_closed_archive_range(object));
 161 }
 162 
 163 inline bool G1ArchiveAllocator::is_open_archive_object(oop object) {
 164   return (archive_check_enabled() && in_open_archive_range(object));
 165 }
 166 
 167 inline bool G1ArchiveAllocator::is_archived_object(oop object) {
 168   return (archive_check_enabled() && (in_closed_archive_range(object) ||
 169                                       in_open_archive_range(object)));
 170 }
 171 
 172 #endif // SHARE_GC_G1_G1ALLOCATOR_INLINE_HPP