1 /*
   2  * Copyright (c) 2018, 2019, 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 "gc/shared/collectedHeap.hpp"
  29 #include "memory/memRegion.hpp"
  30 #include "oops/oopsHierarchy.hpp"
  31 #include "utilities/exceptions.hpp"
  32 #include "utilities/macros.hpp"
  33 
  34 // These fascilities are used for allocating, and initializing newly allocated objects.
  35 
  36 class MemAllocator: StackObj {
  37 protected:
  38   class Allocation;
  39 
  40   CollectedHeap* const _heap;
  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     : _heap(Universe::heap()),
  54       _thread(thread),
  55       _klass(klass),
  56       _word_size(word_size)
  57   { }
  58 
  59   // This function clears the memory of the object
  60   void mem_clear(HeapWord* mem) const;
  61   // This finish constructing an oop by installing the mark word and the Klass* pointer
  62   // last. At the point when the Klass pointer is initialized, this is a constructed object
  63   // that must be parseable as an oop by concurrent collectors.
  64   inline void finish_mark(HeapWord* mem) const;
  65   oop finish(HeapWord* mem) const;
  66   // Encode any extra metadata properties for arrays
  67   oop finish_with_properties(HeapWord* mem, ArrayStorageProperties storage_props) const;
  68 
  69   // Raw memory allocation. This may or may not use TLAB allocations to satisfy the
  70   // allocation. A GC implementation may override this function to satisfy the allocation
  71   // in any way. But the default is to try a TLAB allocation, and otherwise perform
  72   // mem_allocate.
  73   virtual HeapWord* mem_allocate(Allocation& allocation) const;
  74 
  75   virtual MemRegion obj_memory_range(oop obj) const {
  76     return MemRegion((HeapWord*)obj, _word_size);
  77   }
  78 
  79 public:
  80   oop allocate() const;
  81   virtual oop initialize(HeapWord* mem) const = 0;
  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   virtual oop initialize(HeapWord* mem) const;
  89 };
  90 
  91 class ObjArrayAllocator: public MemAllocator {
  92   const int  _length;
  93   const bool _do_zero;
  94 protected:
  95   virtual MemRegion obj_memory_range(oop obj) const;
  96 
  97 public:
  98   ObjArrayAllocator(Klass* klass, size_t word_size, int length, bool do_zero,
  99                     Thread* thread = Thread::current())
 100     : MemAllocator(klass, word_size, thread),
 101       _length(length),
 102       _do_zero(do_zero) {}
 103   virtual oop initialize(HeapWord* mem) const;
 104 };
 105 
 106 class ClassAllocator: public MemAllocator {
 107 public:
 108   ClassAllocator(Klass* klass, size_t word_size, Thread* thread = Thread::current())
 109     : MemAllocator(klass, word_size, thread) {}
 110   virtual oop initialize(HeapWord* mem) const;
 111 };
 112 
 113 #endif // SHARE_GC_SHARED_MEMALLOCATOR_HPP