1 /*
   2  * Copyright (c) 2011, 2017, 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_G1_G1ALLOCREGION_INLINE_HPP
  26 #define SHARE_VM_GC_G1_G1ALLOCREGION_INLINE_HPP
  27 
  28 #include "gc/g1/g1AllocRegion.hpp"
  29 #include "gc/g1/heapRegion.inline.hpp"
  30 
  31 #define assert_alloc_region(p, message)                                  \
  32   do {                                                                   \
  33     assert((p), "[%s] %s c: %u b: %s r: " PTR_FORMAT " u: " SIZE_FORMAT, \
  34            _name, (message), _count, BOOL_TO_STR(_bot_updates),          \
  35            p2i(_alloc_region), _used_bytes_before);                      \
  36   } while (0)
  37 
  38 
  39 inline void G1AllocRegion::reset_alloc_region() {
  40   _alloc_region = _dummy_region;
  41 }
  42 
  43 inline HeapWord* G1AllocRegion::allocate(HeapRegion* alloc_region,
  44                                          size_t word_size) {
  45   assert(alloc_region != NULL, "pre-condition");
  46 
  47   if (!_bot_updates) {
  48     return alloc_region->allocate_no_bot_updates(word_size);
  49   } else {
  50     return alloc_region->allocate(word_size);
  51   }
  52 }
  53 
  54 inline HeapWord* G1AllocRegion::par_allocate(HeapRegion* alloc_region, size_t word_size) {
  55   size_t temp;
  56   return par_allocate(alloc_region, word_size, word_size, &temp);
  57 }
  58 
  59 inline HeapWord* G1AllocRegion::par_allocate(HeapRegion* alloc_region,
  60                                              size_t min_word_size,
  61                                              size_t desired_word_size,
  62                                              size_t* actual_word_size) {
  63   assert(alloc_region != NULL, "pre-condition");
  64   assert(!alloc_region->is_empty(), "pre-condition");
  65 
  66   if (!_bot_updates) {
  67     return alloc_region->par_allocate_no_bot_updates(min_word_size, desired_word_size, actual_word_size);
  68   } else {
  69     return alloc_region->par_allocate(min_word_size, desired_word_size, actual_word_size);
  70   }
  71 }
  72 
  73 inline HeapWord* G1AllocRegion::attempt_allocation(size_t word_size) {
  74   size_t temp;
  75   return attempt_allocation(word_size, word_size, &temp);
  76 }
  77 
  78 inline HeapWord* G1AllocRegion::attempt_allocation(size_t min_word_size,
  79                                                    size_t desired_word_size,
  80                                                    size_t* actual_word_size) {
  81   HeapRegion* alloc_region = _alloc_region;
  82   assert_alloc_region(alloc_region != NULL, "not initialized properly");
  83 
  84   HeapWord* result = par_allocate(alloc_region, min_word_size, desired_word_size, actual_word_size);
  85   if (result != NULL) {
  86     trace("alloc", min_word_size, desired_word_size, *actual_word_size, result);
  87     return result;
  88   }
  89   trace("alloc failed", min_word_size, desired_word_size);
  90   return NULL;
  91 }
  92 
  93 inline HeapWord* G1AllocRegion::attempt_allocation_locked(size_t word_size) {
  94   size_t temp;
  95   return attempt_allocation_locked(word_size, word_size, &temp);
  96 }
  97 
  98 inline HeapWord* G1AllocRegion::attempt_allocation_locked(size_t min_word_size,
  99                                                           size_t desired_word_size,
 100                                                           size_t* actual_word_size) {
 101   // First we have to redo the allocation, assuming we're holding the
 102   // appropriate lock, in case another thread changed the region while
 103   // we were waiting to get the lock.
 104   HeapWord* result = attempt_allocation(min_word_size, desired_word_size, actual_word_size);
 105   if (result != NULL) {
 106     return result;
 107   }
 108 
 109   retire(true /* fill_up */);
 110   result = new_alloc_region_and_allocate(desired_word_size, false /* force */);
 111   if (result != NULL) {
 112     *actual_word_size = desired_word_size;
 113     trace("alloc locked (second attempt)", min_word_size, desired_word_size, *actual_word_size, result);
 114     return result;
 115   }
 116   trace("alloc locked failed", min_word_size, desired_word_size);
 117   return NULL;
 118 }
 119 
 120 inline HeapWord* G1AllocRegion::attempt_allocation_force(size_t word_size) {
 121   assert_alloc_region(_alloc_region != NULL, "not initialized properly");
 122 
 123   trace("forcing alloc", word_size, word_size);
 124   HeapWord* result = new_alloc_region_and_allocate(word_size, true /* force */);
 125   if (result != NULL) {
 126     trace("alloc forced", word_size, word_size, word_size, result);
 127     return result;
 128   }
 129   trace("alloc forced failed", word_size, word_size);
 130   return NULL;
 131 }
 132 
 133 inline HeapWord* MutatorAllocRegion::attempt_retained_allocation(size_t min_word_size,
 134                                                                  size_t desired_word_size,
 135                                                                  size_t* actual_word_size) {
 136   if (_retained_alloc_region != NULL) {
 137     HeapWord* result = par_allocate(_retained_alloc_region, min_word_size, desired_word_size, actual_word_size);
 138     if (result != NULL) {
 139       trace("alloc retained", min_word_size, desired_word_size, *actual_word_size, result);
 140       return result;
 141     }
 142   }
 143   return NULL;
 144 }
 145 
 146 #endif // SHARE_VM_GC_G1_G1ALLOCREGION_INLINE_HPP