1 /*
  2  * Copyright (c) 2018, 2020, 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_SHENANDOAHALLOCREQUEST_HPP
 26 #define SHARE_GC_SHENANDOAH_SHENANDOAHALLOCREQUEST_HPP
 27 
 28 #include "memory/allocation.hpp"
 29 #include "gc/shenandoah/mode/shenandoahGenerationalMode.hpp"
 30 
 31 class ShenandoahAllocRequest : StackObj {
 32 public:
 33   enum Type {
 34     _alloc_shared,      // Allocate common, outside of TLAB
 35     _alloc_shared_gc,   // Allocate common, outside of GCLAB/PLAB
 36     _alloc_tlab,        // Allocate TLAB
 37     _alloc_gclab,       // Allocate GCLAB
 38     _alloc_plab,        // Allocate PLAB
 39     _ALLOC_LIMIT
 40   };
 41 
 42   static const char* alloc_type_to_string(Type type) {
 43     switch (type) {
 44       case _alloc_shared:
 45         return "Shared";
 46       case _alloc_shared_gc:
 47         return "Shared GC";
 48       case _alloc_tlab:
 49         return "TLAB";
 50       case _alloc_gclab:
 51         return "GCLAB";
 52       case _alloc_plab:
 53         return "PLAB";
 54       default:
 55         ShouldNotReachHere();
 56         return "";
 57     }
 58   }
 59 
 60 private:
 61   size_t _min_size;
 62   size_t _requested_size;
 63   size_t _actual_size;
 64   Type _alloc_type;
 65   ShenandoahRegionAffiliation const _affiliation;
 66 #ifdef ASSERT
 67   bool _actual_size_set;
 68 #endif
 69 
 70   ShenandoahAllocRequest(size_t _min_size, size_t _requested_size, Type _alloc_type, ShenandoahRegionAffiliation affiliation) :
 71           _min_size(_min_size), _requested_size(_requested_size),
 72           _actual_size(0), _alloc_type(_alloc_type), _affiliation(affiliation)
 73 #ifdef ASSERT
 74           , _actual_size_set(false)
 75 #endif
 76   {}
 77 
 78 public:
 79   static inline ShenandoahAllocRequest for_tlab(size_t min_size, size_t requested_size) {
 80     return ShenandoahAllocRequest(min_size, requested_size, _alloc_tlab, ShenandoahRegionAffiliation::YOUNG_GENERATION);
 81   }
 82 
 83   static inline ShenandoahAllocRequest for_gclab(size_t min_size, size_t requested_size) {
 84     return ShenandoahAllocRequest(min_size, requested_size, _alloc_gclab, ShenandoahRegionAffiliation::YOUNG_GENERATION);
 85   }
 86 
 87   static inline ShenandoahAllocRequest for_plab(size_t min_size, size_t requested_size) {
 88     return ShenandoahAllocRequest(min_size, requested_size, _alloc_plab, ShenandoahRegionAffiliation::OLD_GENERATION);
 89   }
 90 
 91   static inline ShenandoahAllocRequest for_shared_gc(size_t requested_size, ShenandoahRegionAffiliation affiliation) {
 92     return ShenandoahAllocRequest(0, requested_size, _alloc_shared_gc, affiliation);
 93   }
 94 
 95   static inline ShenandoahAllocRequest for_shared(size_t requested_size) {
 96     return ShenandoahAllocRequest(0, requested_size, _alloc_shared, ShenandoahRegionAffiliation::YOUNG_GENERATION);
 97   }
 98 
 99   inline size_t size() {
100     return _requested_size;
101   }
102 
103   inline Type type() {
104     return _alloc_type;
105   }
106 
107   inline const char* type_string() {
108     return alloc_type_to_string(_alloc_type);
109   }
110 
111   inline size_t min_size() {
112     assert (is_lab_alloc(), "Only access for LAB allocs");
113     return _min_size;
114   }
115 
116   inline size_t actual_size() {
117     assert (_actual_size_set, "Should be set");
118     return _actual_size;
119   }
120 
121   inline void set_actual_size(size_t v) {
122 #ifdef ASSERT
123     assert (!_actual_size_set, "Should not be set");
124     _actual_size_set = true;
125 #endif
126     _actual_size = v;
127   }
128 
129   inline bool is_mutator_alloc() {
130     switch (_alloc_type) {
131       case _alloc_tlab:
132       case _alloc_shared:
133         return true;
134       case _alloc_gclab:
135       case _alloc_plab:
136       case _alloc_shared_gc:
137         return false;
138       default:
139         ShouldNotReachHere();
140         return false;
141     }
142   }
143 
144   inline bool is_gc_alloc() {
145     switch (_alloc_type) {
146       case _alloc_tlab:
147       case _alloc_shared:
148         return false;
149       case _alloc_gclab:
150       case _alloc_plab:
151       case _alloc_shared_gc:
152         return true;
153       default:
154         ShouldNotReachHere();
155         return false;
156     }
157   }
158 
159   inline bool is_lab_alloc() {
160     switch (_alloc_type) {
161       case _alloc_tlab:
162       case _alloc_gclab:
163       case _alloc_plab:
164         return true;
165       case _alloc_shared:
166       case _alloc_shared_gc:
167         return false;
168       default:
169         ShouldNotReachHere();
170         return false;
171     }
172   }
173 
174   bool is_old() {
175     return _affiliation == OLD_GENERATION;
176   }
177 
178   bool is_young() {
179     return _affiliation == YOUNG_GENERATION;
180   }
181 
182   ShenandoahRegionAffiliation affiliation() const {
183     return _affiliation;
184   }
185 };
186 
187 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHALLOCREQUEST_HPP