1 /*
   2  * Copyright (c) 2001, 2012, 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_HEAPREGION_INLINE_HPP
  26 #define SHARE_VM_GC_IMPLEMENTATION_G1_HEAPREGION_INLINE_HPP
  27 
  28 #include "gc_implementation/g1/g1BlockOffsetTable.inline.hpp"
  29 #include "gc_implementation/g1/g1CollectedHeap.hpp"
  30 #include "gc_implementation/g1/heapRegion.hpp"
  31 #include "memory/space.hpp"
  32 #include "runtime/atomic.inline.hpp"
  33 
  34 inline HeapWord* G1OffsetTableContigSpace::cas_allocate_inner(size_t size) {
  35   HeapWord* obj = top();
  36   do {
  37     if (pointer_delta(end(), obj) >= size) {
  38       HeapWord* new_top = obj + size;
  39       HeapWord* result = (HeapWord*)Atomic::cmpxchg_ptr(new_top, &_top, obj);
  40       if (result == obj) {
  41         assert(is_aligned(obj) && is_aligned(new_top), "checking alignment");
  42         return obj;
  43       }
  44       obj = result;
  45     } else {
  46       break;
  47     }
  48   } while (true);
  49   return NULL;
  50 }
  51 
  52 inline HeapWord* G1OffsetTableContigSpace::allocate_inner(size_t size) {
  53   HeapWord* obj = top();
  54   if (pointer_delta(end(), obj) >= size) {
  55     HeapWord* new_top = obj + size;
  56     assert(is_aligned(obj) && is_aligned(new_top), "checking alignment");
  57     set_top(new_top);
  58     return obj;
  59   }
  60   return NULL;
  61 }
  62 
  63 
  64 inline HeapWord* G1OffsetTableContigSpace::allocate(size_t size) {
  65   HeapWord* res = allocate_inner(size);
  66   if (res != NULL) {
  67     _offsets.alloc_block(res, size);
  68   }
  69   return res;
  70 }
  71 
  72 // Because of the requirement of keeping "_offsets" up to date with the
  73 // allocations, we sequentialize these with a lock.  Therefore, best if
  74 // this is used for larger LAB allocations only.
  75 inline HeapWord* G1OffsetTableContigSpace::par_allocate(size_t size) {
  76   MutexLocker x(&_par_alloc_lock);
  77   return allocate(size);
  78 }
  79 
  80 inline HeapWord* G1OffsetTableContigSpace::block_start(const void* p) {
  81   return _offsets.block_start(p);
  82 }
  83 
  84 inline HeapWord*
  85 G1OffsetTableContigSpace::block_start_const(const void* p) const {
  86   return _offsets.block_start_const(p);
  87 }
  88 
  89 inline bool
  90 HeapRegion::block_is_obj(const HeapWord* p) const {
  91   return p < top();
  92 }
  93 
  94 inline size_t
  95 HeapRegion::block_size(const HeapWord *addr) const {
  96   const HeapWord* current_top = top();
  97   if (addr < current_top) {
  98     return oop(addr)->size();
  99   } else {
 100     assert(addr == current_top, "just checking");
 101     return pointer_delta(end(), addr);
 102   }
 103 }
 104 
 105 inline HeapWord* HeapRegion::par_allocate_no_bot_updates(size_t word_size) {
 106   assert(is_young(), "we can only skip BOT updates on young regions");
 107   return cas_allocate_inner(word_size);
 108 }
 109 
 110 inline HeapWord* HeapRegion::allocate_no_bot_updates(size_t word_size) {
 111   assert(is_young(), "we can only skip BOT updates on young regions");
 112   return allocate_inner(word_size);
 113 }
 114 
 115 inline void HeapRegion::note_start_of_marking() {
 116   _next_marked_bytes = 0;
 117   _next_top_at_mark_start = top();
 118 }
 119 
 120 inline void HeapRegion::note_end_of_marking() {
 121   _prev_top_at_mark_start = _next_top_at_mark_start;
 122   _prev_marked_bytes = _next_marked_bytes;
 123   _next_marked_bytes = 0;
 124 
 125   assert(_prev_marked_bytes <=
 126          (size_t) pointer_delta(prev_top_at_mark_start(), bottom()) *
 127          HeapWordSize, "invariant");
 128 }
 129 
 130 inline void HeapRegion::note_start_of_copying(bool during_initial_mark) {
 131   if (is_survivor()) {
 132     // This is how we always allocate survivors.
 133     assert(_next_top_at_mark_start == bottom(), "invariant");
 134   } else {
 135     if (during_initial_mark) {
 136       // During initial-mark we'll explicitly mark any objects on old
 137       // regions that are pointed to by roots. Given that explicit
 138       // marks only make sense under NTAMS it'd be nice if we could
 139       // check that condition if we wanted to. Given that we don't
 140       // know where the top of this region will end up, we simply set
 141       // NTAMS to the end of the region so all marks will be below
 142       // NTAMS. We'll set it to the actual top when we retire this region.
 143       _next_top_at_mark_start = end();
 144     } else {
 145       // We could have re-used this old region as to-space over a
 146       // couple of GCs since the start of the concurrent marking
 147       // cycle. This means that [bottom,NTAMS) will contain objects
 148       // copied up to and including initial-mark and [NTAMS, top)
 149       // will contain objects copied during the concurrent marking cycle.
 150       assert(top() >= _next_top_at_mark_start, "invariant");
 151     }
 152   }
 153 }
 154 
 155 inline void HeapRegion::note_end_of_copying(bool during_initial_mark) {
 156   if (is_survivor()) {
 157     // This is how we always allocate survivors.
 158     assert(_next_top_at_mark_start == bottom(), "invariant");
 159   } else {
 160     if (during_initial_mark) {
 161       // See the comment for note_start_of_copying() for the details
 162       // on this.
 163       assert(_next_top_at_mark_start == end(), "pre-condition");
 164       _next_top_at_mark_start = top();
 165     } else {
 166       // See the comment for note_start_of_copying() for the details
 167       // on this.
 168       assert(top() >= _next_top_at_mark_start, "invariant");
 169     }
 170   }
 171 }
 172 
 173 #endif // SHARE_VM_GC_IMPLEMENTATION_G1_HEAPREGION_INLINE_HPP