1 /*
  2  * Copyright (c) 2018, 2024, Oracle and/or its affiliates. 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_SHARED_MEMALLOCATOR_HPP
 26 #define SHARE_GC_SHARED_MEMALLOCATOR_HPP
 27 
 28 #include "memory/memRegion.hpp"
 29 #include "oops/oopsHierarchy.hpp"
 30 #include "runtime/javaThread.hpp"
 31 #include "utilities/exceptions.hpp"
 32 #include "utilities/globalDefinitions.hpp"
 33 #include "utilities/macros.hpp"
 34 
 35 // These fascilities are used for allocating, and initializing newly allocated objects.
 36 
 37 class MemAllocator: StackObj {
 38 protected:
 39   class Allocation;
 40 
 41   Thread* const        _thread;
 42   Klass* const         _klass;
 43   const size_t         _word_size;
 44 
 45   // Allocate from the current thread's TLAB, without taking a new TLAB (no safepoint).
 46  HeapWord* mem_allocate_inside_tlab_fast() const;
 47 
 48 private:
 49   // Allocate in a TLAB. Could allocate a new TLAB, and therefore potentially safepoint.
 50   HeapWord* mem_allocate_inside_tlab_slow(Allocation& allocation) const;
 51 
 52   // Allocate outside a TLAB. Could safepoint.
 53   HeapWord* mem_allocate_outside_tlab(Allocation& allocation) const;
 54 
 55 protected:
 56   MemAllocator(Klass* klass, size_t word_size, Thread* thread)
 57     : _thread(thread),
 58       _klass(klass),
 59       _word_size(word_size)
 60   {
 61     assert(_thread == Thread::current(), "must be");
 62   }
 63 
 64   // Initialization provided by subclasses.
 65   virtual oop initialize(HeapWord* mem) const = 0;
 66 
 67   // This function clears the memory of the object.
 68   void mem_clear(HeapWord* mem) const;
 69 
 70   // This finish constructing an oop by installing the mark word and the Klass* pointer
 71   // last. At the point when the Klass pointer is initialized, this is a constructed object
 72   // that must be parseable as an oop by concurrent collectors.
 73   oop finish(HeapWord* mem) const;
 74 
 75   // Raw memory allocation. This will try to do a TLAB allocation, and otherwise fall
 76   // back to calling CollectedHeap::mem_allocate().
 77   HeapWord* mem_allocate(Allocation& allocation) const;
 78 
 79 public:
 80   // Allocate and fully construct the object, and perform various instrumentation. Could safepoint.
 81   oop allocate() const;
 82 };
 83 
 84 class ObjAllocator: public MemAllocator {
 85 public:
 86   ObjAllocator(Klass* klass, size_t word_size, Thread* thread = Thread::current())
 87     : MemAllocator(klass, word_size, thread) {}
 88 
 89   virtual oop initialize(HeapWord* mem) const;
 90 };
 91 
 92 class ObjArrayAllocator: public MemAllocator {
 93 protected:
 94   const int  _length;
 95   const bool _do_zero;
 96 
 97   void mem_zap_start_padding(HeapWord* mem) const PRODUCT_RETURN;
 98   void mem_zap_end_padding(HeapWord* mem) const PRODUCT_RETURN;
 99 
100 public:
101   ObjArrayAllocator(Klass* klass, size_t word_size, int length, bool do_zero,
102                     Thread* thread = Thread::current())
103     : MemAllocator(klass, word_size, thread),
104       _length(length),
105       _do_zero(do_zero) {}
106 
107   virtual oop initialize(HeapWord* mem) const;
108 };
109 
110 class ClassAllocator: public MemAllocator {
111 public:
112   ClassAllocator(Klass* klass, size_t word_size, Thread* thread = Thread::current())
113     : MemAllocator(klass, word_size, thread) {}
114 
115   virtual oop initialize(HeapWord* mem) const;
116 };
117 
118 // Manages a scope where a failed heap allocation results in
119 // suppression of JVMTI "resource exhausted" events and
120 // throwing a shared, backtrace-less OOME instance.
121 // Used for OOMEs that will not be propagated to user code.
122 class InternalOOMEMark: public StackObj {
123  private:
124   bool _outer;
125   JavaThread* _thread;
126 
127  public:
128   explicit InternalOOMEMark(JavaThread* thread) {
129     assert(thread != nullptr, "nullptr is not supported");
130     _outer = thread->is_in_internal_oome_mark();
131     thread->set_is_in_internal_oome_mark(true);
132     _thread = thread;
133   }
134 
135   ~InternalOOMEMark() {
136     // Check that only InternalOOMEMark sets
137     // JavaThread::_is_in_internal_oome_mark
138     assert(_thread->is_in_internal_oome_mark(), "must be");
139     _thread->set_is_in_internal_oome_mark(_outer);
140   }
141 
142   JavaThread* thread() const  { return _thread; }
143 };
144 
145 #endif // SHARE_GC_SHARED_MEMALLOCATOR_HPP