1 /*
2  * Copyright (c) 2015, 2019, Red Hat, Inc. 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_SHENANDOAH_SHENANDOAHHEAPREGION_INLINE_HPP
26 #define SHARE_GC_SHENANDOAH_SHENANDOAHHEAPREGION_INLINE_HPP
27 
28 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
29 #include "gc/shenandoah/shenandoahHeapRegion.hpp"
30 #include "gc/shenandoah/shenandoahPacer.inline.hpp"
31 #include "runtime/atomic.hpp"
32 
allocate(size_t size,ShenandoahAllocRequest::Type type)33 HeapWord* ShenandoahHeapRegion::allocate(size_t size, ShenandoahAllocRequest::Type type) {
34   shenandoah_assert_heaplocked_or_safepoint();
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 
adjust_alloc_metadata(ShenandoahAllocRequest::Type type,size_t size)54 inline void ShenandoahHeapRegion::adjust_alloc_metadata(ShenandoahAllocRequest::Type type, size_t size) {
55   switch (type) {
56     case ShenandoahAllocRequest::_alloc_shared:
57     case ShenandoahAllocRequest::_alloc_shared_gc:
58       // Counted implicitly by tlab/gclab allocs
59       break;
60     case ShenandoahAllocRequest::_alloc_tlab:
61       _tlab_allocs += size;
62       break;
63     case ShenandoahAllocRequest::_alloc_gclab:
64       _gclab_allocs += size;
65       break;
66     default:
67       ShouldNotReachHere();
68   }
69 }
70 
increase_live_data_alloc_words(size_t s)71 inline void ShenandoahHeapRegion::increase_live_data_alloc_words(size_t s) {
72   internal_increase_live_data(s);
73 }
74 
increase_live_data_gc_words(size_t s)75 inline void ShenandoahHeapRegion::increase_live_data_gc_words(size_t s) {
76   internal_increase_live_data(s);
77   if (ShenandoahPacing) {
78     ShenandoahHeap::heap()->pacer()->report_mark(s);
79   }
80 }
81 
internal_increase_live_data(size_t s)82 inline void ShenandoahHeapRegion::internal_increase_live_data(size_t s) {
83   size_t new_live_data = Atomic::add(&_live_data, s);
84 #ifdef ASSERT
85   size_t live_bytes = new_live_data * HeapWordSize;
86   size_t used_bytes = used();
87   assert(live_bytes <= used_bytes,
88          "can't have more live data than used: " SIZE_FORMAT ", " SIZE_FORMAT, live_bytes, used_bytes);
89 #endif
90 }
91 
clear_live_data()92 inline void ShenandoahHeapRegion::clear_live_data() {
93   Atomic::release_store_fence(&_live_data, (size_t)0);
94 }
95 
get_live_data_words() const96 inline size_t ShenandoahHeapRegion::get_live_data_words() const {
97   return Atomic::load_acquire(&_live_data);
98 }
99 
get_live_data_bytes() const100 inline size_t ShenandoahHeapRegion::get_live_data_bytes() const {
101   return get_live_data_words() * HeapWordSize;
102 }
103 
has_live() const104 inline bool ShenandoahHeapRegion::has_live() const {
105   return get_live_data_words() != 0;
106 }
107 
garbage() const108 inline size_t ShenandoahHeapRegion::garbage() const {
109   assert(used() >= get_live_data_bytes(),
110          "Live Data must be a subset of used() live: " SIZE_FORMAT " used: " SIZE_FORMAT,
111          get_live_data_bytes(), used());
112 
113   size_t result = used() - get_live_data_bytes();
114   return result;
115 }
116 
get_update_watermark() const117 inline HeapWord* ShenandoahHeapRegion::get_update_watermark() const {
118   HeapWord* watermark = Atomic::load_acquire(&_update_watermark);
119   assert(bottom() <= watermark && watermark <= top(), "within bounds");
120   return watermark;
121 }
122 
set_update_watermark(HeapWord * w)123 inline void ShenandoahHeapRegion::set_update_watermark(HeapWord* w) {
124   assert(bottom() <= w && w <= top(), "within bounds");
125   Atomic::release_store(&_update_watermark, w);
126 }
127 
128 // Fast version that avoids synchronization, only to be used at safepoints.
set_update_watermark_at_safepoint(HeapWord * w)129 inline void ShenandoahHeapRegion::set_update_watermark_at_safepoint(HeapWord* w) {
130   assert(bottom() <= w && w <= top(), "within bounds");
131   assert(SafepointSynchronize::is_at_safepoint(), "Should be at Shenandoah safepoint");
132   _update_watermark = w;
133 }
134 
135 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHHEAPREGION_INLINE_HPP
136