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(Allocation& allocation) const; 51 HeapWord* mem_allocate_inside_tlab_slow(Allocation& allocation) const; 52 53 // Allocate outside a TLAB. Could safepoint. 54 HeapWord* mem_allocate_outside_tlab(Allocation& allocation) const; 55 56 // Fast-path TLAB allocation failed. Takes a slow-path and potentially safepoint. 57 HeapWord* mem_allocate_slow(Allocation& allocation) const; 58 59 protected: 60 MemAllocator(Klass* klass, size_t word_size, Thread* thread) 61 : _thread(thread), 62 _klass(klass), 63 _word_size(word_size) 64 { 65 assert(_thread == Thread::current(), "must be"); 66 } 67 68 // Initialization provided by subclasses. 69 virtual oop initialize(HeapWord* mem) const = 0; 70 71 // This function clears the memory of the object. 72 void mem_clear(HeapWord* mem) const; 73 74 // This finish constructing an oop by installing the mark word and the Klass* pointer 75 // last. At the point when the Klass pointer is initialized, this is a constructed object 76 // that must be parseable as an oop by concurrent collectors. 77 oop finish(HeapWord* mem) const; 78 79 // Raw memory allocation. This will try to do a TLAB allocation, and otherwise fall 80 // back to calling CollectedHeap::mem_allocate(). 81 HeapWord* mem_allocate(Allocation& allocation) const; 82 83 public: 84 // Allocate and fully construct the object, and perform various instrumentation. Could safepoint. 85 oop allocate() const; 86 }; 87 88 class ObjAllocator: public MemAllocator { 89 public: 90 ObjAllocator(Klass* klass, size_t word_size, Thread* thread = Thread::current()) 91 : MemAllocator(klass, word_size, thread) {} 92 93 virtual oop initialize(HeapWord* mem) const; 94 }; 95 96 class ObjBufferAllocator: public MemAllocator { 97 public: 98 ObjBufferAllocator(Klass* klass, size_t word_size, Thread* thread = Thread::current()) 99 : MemAllocator(klass, word_size, thread) {} 100 virtual oop initialize(HeapWord* mem) const; 101 }; 102 103 104 class ObjArrayAllocator: public MemAllocator { 105 protected: 106 const int _length; 107 const bool _do_zero; 108 109 void mem_zap_end_padding(HeapWord* mem) const PRODUCT_RETURN; 110 111 public: 112 ObjArrayAllocator(Klass* klass, size_t word_size, int length, bool do_zero, 113 Thread* thread = Thread::current()) 114 : MemAllocator(klass, word_size, thread), 115 _length(length), 116 _do_zero(do_zero) {} 117 118 virtual oop initialize(HeapWord* mem) const; 119 }; 120 121 class ClassAllocator: public MemAllocator { 122 public: 123 ClassAllocator(Klass* klass, size_t word_size, Thread* thread = Thread::current()) 124 : MemAllocator(klass, word_size, thread) {} 125 126 virtual oop initialize(HeapWord* mem) const; 127 }; 128 129 #endif // SHARE_GC_SHARED_MEMALLOCATOR_HPP