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