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