1 /*
  2  * Copyright (c) 2018, 2021, 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/thread.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 private:
 46   // Allocate from the current thread's TLAB, with broken-out slow path.
 47   HeapWord* allocate_inside_tlab(Allocation& allocation) const;
 48   HeapWord* allocate_inside_tlab_slow(Allocation& allocation) const;
 49   HeapWord* allocate_outside_tlab(Allocation& allocation) const;
 50 
 51 protected:
 52   MemAllocator(Klass* klass, size_t word_size, Thread* thread)
 53     : _thread(thread),
 54       _klass(klass),
 55       _word_size(word_size)
 56   { }
 57 
 58   // This function clears the memory of the object
 59   void mem_clear(HeapWord* mem) const;
 60   // This finish constructing an oop by installing the mark word and the Klass* pointer
 61   // last. At the point when the Klass pointer is initialized, this is a constructed object
 62   // that must be parseable as an oop by concurrent collectors.
 63   virtual oop finish(HeapWord* mem) const;
 64 
 65   // Raw memory allocation. This will try to do a TLAB allocation, and otherwise fall
 66   // back to calling CollectedHeap::mem_allocate().
 67   HeapWord* mem_allocate(Allocation& allocation) const;
 68 
 69   virtual MemRegion obj_memory_range(oop obj) const {
 70     return MemRegion(cast_from_oop<HeapWord*>(obj), _word_size);
 71   }
 72 
 73 public:
 74   oop allocate() const;
 75   virtual oop initialize(HeapWord* mem) const = 0;
 76 };
 77 
 78 class ObjAllocator: public MemAllocator {
 79 public:
 80   ObjAllocator(Klass* klass, size_t word_size, Thread* thread = Thread::current())
 81     : MemAllocator(klass, word_size, thread) {}
 82   virtual oop initialize(HeapWord* mem) const;
 83 };
 84 
 85 class ObjArrayAllocator: public MemAllocator {
 86   const int  _length;
 87   const bool _do_zero;
 88 protected:
 89   virtual MemRegion obj_memory_range(oop obj) const;
 90 
 91 public:
 92   ObjArrayAllocator(Klass* klass, size_t word_size, int length, bool do_zero,
 93                     Thread* thread = Thread::current())
 94     : MemAllocator(klass, word_size, thread),
 95       _length(length),
 96       _do_zero(do_zero) {}
 97   virtual oop initialize(HeapWord* mem) const;
 98 };
 99 
100 class ClassAllocator: public MemAllocator {
101 public:
102   ClassAllocator(Klass* klass, size_t word_size, Thread* thread = Thread::current())
103     : MemAllocator(klass, word_size, thread) {}
104   virtual oop initialize(HeapWord* mem) const;
105 };
106 
107 class StackChunkAllocator : public MemAllocator {
108   const int _stack_size;
109 public:
110   StackChunkAllocator(Klass* klass, size_t word_size, int stack_size, Thread* thread = Thread::current())
111     : MemAllocator(klass, word_size, thread),
112       _stack_size(stack_size) {}
113   virtual oop initialize(HeapWord* mem) const;
114 };
115 
116 #endif // SHARE_GC_SHARED_MEMALLOCATOR_HPP