1 /*
   2  * Copyright (c) 2015, 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 #ifndef SHARE_VM_GC_SHENANDOAH_SHENANDOAHHEAPREGION_INLINE_HPP
  25 #define SHARE_VM_GC_SHENANDOAH_SHENANDOAHHEAPREGION_INLINE_HPP
  26 
  27 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
  28 #include "gc/shenandoah/shenandoahHeapRegion.hpp"
  29 #include "gc/shenandoah/shenandoahPacer.inline.hpp"
  30 #include "runtime/atomic.hpp"
  31 
  32 HeapWord* ShenandoahHeapRegion::allocate(size_t size, ShenandoahAllocRequest::Type type) {
  33   _heap->assert_heaplock_or_safepoint();
  34 
  35   HeapWord* obj = top();
  36   if (pointer_delta(end(), obj) >= size) {
  37     make_regular_allocation();
  38     adjust_alloc_metadata(type, size);
  39 
  40     HeapWord* new_top = obj + size;
  41     set_top(new_top);
  42     assert(is_aligned(obj) && is_aligned(new_top), "checking alignment");
  43 
  44     return obj;
  45   } else {
  46     return NULL;
  47   }
  48 }
  49 
  50 inline void ShenandoahHeapRegion::adjust_alloc_metadata(ShenandoahAllocRequest::Type type, size_t size) {
  51   bool is_first_alloc = (top() == bottom());
  52 
  53   switch (type) {
  54     case ShenandoahAllocRequest::_alloc_shared:
  55     case ShenandoahAllocRequest::_alloc_tlab:
  56       _seqnum_last_alloc_mutator = _alloc_seq_num.value++;
  57       if (is_first_alloc) {
  58         assert (_seqnum_first_alloc_mutator == 0, "Region " SIZE_FORMAT " metadata is correct", _region_number);
  59         _seqnum_first_alloc_mutator = _seqnum_last_alloc_mutator;
  60       }
  61       break;
  62     case ShenandoahAllocRequest::_alloc_shared_gc:
  63     case ShenandoahAllocRequest::_alloc_gclab:
  64       _seqnum_last_alloc_gc = _alloc_seq_num.value++;
  65       if (is_first_alloc) {
  66         assert (_seqnum_first_alloc_gc == 0, "Region " SIZE_FORMAT " metadata is correct", _region_number);
  67         _seqnum_first_alloc_gc = _seqnum_last_alloc_gc;
  68       }
  69       break;
  70     default:
  71       ShouldNotReachHere();
  72   }
  73 
  74   switch (type) {
  75     case ShenandoahAllocRequest::_alloc_shared:
  76     case ShenandoahAllocRequest::_alloc_shared_gc:
  77       _shared_allocs += size;
  78       break;
  79     case ShenandoahAllocRequest::_alloc_tlab:
  80       _tlab_allocs += size;
  81       break;
  82     case ShenandoahAllocRequest::_alloc_gclab:
  83       _gclab_allocs += size;
  84       break;
  85     default:
  86       ShouldNotReachHere();
  87   }
  88 }
  89 
  90 inline void ShenandoahHeapRegion::increase_live_data_alloc_words(size_t s) {
  91   internal_increase_live_data(s);
  92 }
  93 
  94 inline void ShenandoahHeapRegion::increase_live_data_gc_words(size_t s) {
  95   internal_increase_live_data(s);
  96   if (ShenandoahPacing) {
  97     _pacer->report_mark(s);
  98   }
  99 }
 100 
 101 inline void ShenandoahHeapRegion::internal_increase_live_data(size_t s) {
 102   size_t new_live_data = Atomic::add(s, &_live_data);
 103 #ifdef ASSERT
 104   size_t live_bytes = new_live_data * HeapWordSize;
 105   size_t used_bytes = used();
 106   assert(live_bytes <= used_bytes,
 107          "can't have more live data than used: " SIZE_FORMAT ", " SIZE_FORMAT, live_bytes, used_bytes);
 108 #endif
 109 }
 110 
 111 #endif // SHARE_VM_GC_SHENANDOAH_SHENANDOAHHEAPREGION_INLINE_HPP