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 : 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 min_size, size_t requested_size) {
  74     return ShenandoahAllocRequest(min_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 size_t min_size() {
  98     assert (is_lab_alloc(), "Only access for LAB allocs");
  99     return _min_size;
 100   }
 101 
 102   inline size_t actual_size() {
 103     assert (_actual_size_set, "Should be set");
 104     return _actual_size;
 105   }
 106 
 107   inline void set_actual_size(size_t v) {
 108 #ifdef ASSERT
 109     assert (!_actual_size_set, "Should not be set");
 110     _actual_size_set = true;
 111 #endif
 112     _actual_size = v;
 113   }
 114 
 115   inline bool is_mutator_alloc() {
 116     switch (_alloc_type) {
 117       case _alloc_tlab:
 118       case _alloc_shared:
 119         return true;
 120       case _alloc_gclab:
 121       case _alloc_shared_gc:
 122         return false;
 123       default:
 124         ShouldNotReachHere();
 125         return false;
 126     }
 127   }
 128 
 129   inline bool is_gc_alloc() {
 130     switch (_alloc_type) {
 131       case _alloc_tlab:
 132       case _alloc_shared:
 133         return false;
 134       case _alloc_gclab:
 135       case _alloc_shared_gc:
 136         return true;
 137       default:
 138         ShouldNotReachHere();
 139         return false;
 140     }
 141   }
 142 
 143   inline bool is_lab_alloc() {
 144     switch (_alloc_type) {
 145       case _alloc_tlab:
 146       case _alloc_gclab:
 147         return true;
 148       case _alloc_shared:
 149       case _alloc_shared_gc:
 150         return false;
 151       default:
 152         ShouldNotReachHere();
 153         return false;
 154     }
 155   }
 156 };
 157 
 158 #endif // SHARE_VM_GC_SHENANDOAH_SHENANDOAHALLOCREQUEST_HPP