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   assert(is_object_aligned(size), "alloc size breaks alignment: " SIZE_FORMAT, size);
  36 
  37   HeapWord* obj = top();
  38   if (pointer_delta(end(), obj) >= size) {
  39     make_regular_allocation();
  40     adjust_alloc_metadata(type, size);
  41 
  42     HeapWord* new_top = obj + size;
  43     set_top(new_top);
  44 
  45     assert(is_object_aligned(new_top), "new top breaks alignment: " PTR_FORMAT, p2i(new_top));
  46     assert(is_object_aligned(obj),     "obj is not aligned: "       PTR_FORMAT, p2i(obj));
  47 
  48     return obj;
  49   } else {
  50     return NULL;
  51   }
  52 }
  53 
  54 inline void ShenandoahHeapRegion::adjust_alloc_metadata(ShenandoahAllocRequest::Type type, size_t size) {
  55   bool is_first_alloc = (top() == bottom());
  56 
  57   switch (type) {
  58     case ShenandoahAllocRequest::_alloc_shared:
  59     case ShenandoahAllocRequest::_alloc_tlab:
  60       _seqnum_last_alloc_mutator = _alloc_seq_num.value++;
  61       if (is_first_alloc) {
  62         assert (_seqnum_first_alloc_mutator == 0, "Region " SIZE_FORMAT " metadata is correct", _region_number);
  63         _seqnum_first_alloc_mutator = _seqnum_last_alloc_mutator;
  64       }
  65       break;
  66     case ShenandoahAllocRequest::_alloc_shared_gc:
  67     case ShenandoahAllocRequest::_alloc_gclab:
  68       _seqnum_last_alloc_gc = _alloc_seq_num.value++;
  69       if (is_first_alloc) {
  70         assert (_seqnum_first_alloc_gc == 0, "Region " SIZE_FORMAT " metadata is correct", _region_number);
  71         _seqnum_first_alloc_gc = _seqnum_last_alloc_gc;
  72       }
  73       break;
  74     default:
  75       ShouldNotReachHere();
  76   }
  77 
  78   switch (type) {
  79     case ShenandoahAllocRequest::_alloc_shared:
  80     case ShenandoahAllocRequest::_alloc_shared_gc:
  81       _shared_allocs += size;
  82       break;
  83     case ShenandoahAllocRequest::_alloc_tlab:
  84       _tlab_allocs += size;
  85       break;
  86     case ShenandoahAllocRequest::_alloc_gclab:
  87       _gclab_allocs += size;
  88       break;
  89     default:
  90       ShouldNotReachHere();
  91   }
  92 }
  93 
  94 inline void ShenandoahHeapRegion::increase_live_data_alloc_words(size_t s) {
  95   internal_increase_live_data(s);
  96 }
  97 
  98 inline void ShenandoahHeapRegion::increase_live_data_gc_words(size_t s) {
  99   internal_increase_live_data(s);
 100   if (ShenandoahPacing) {
 101     _heap->pacer()->report_mark(s);
 102   }
 103 }
 104 
 105 inline void ShenandoahHeapRegion::internal_increase_live_data(size_t s) {
 106   size_t new_live_data = Atomic::add(s, &_live_data);
 107 #ifdef ASSERT
 108   size_t live_bytes = new_live_data * HeapWordSize;
 109   size_t used_bytes = used();
 110   assert(live_bytes <= used_bytes,
 111          "can't have more live data than used: " SIZE_FORMAT ", " SIZE_FORMAT, live_bytes, used_bytes);
 112 #endif
 113 }
 114 
 115 #endif // SHARE_VM_GC_SHENANDOAH_SHENANDOAHHEAPREGION_INLINE_HPP