1 /* 2 * Copyright (c) 2001, 2023, 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_PARALLEL_MUTABLESPACE_HPP 26 #define SHARE_GC_PARALLEL_MUTABLESPACE_HPP 27 28 #include "memory/allocation.hpp" 29 #include "memory/iterator.hpp" 30 #include "memory/memRegion.hpp" 31 #include "utilities/copy.hpp" 32 #include "utilities/globalDefinitions.hpp" 33 #include "utilities/macros.hpp" 34 35 class WorkerThreads; 36 37 // A MutableSpace supports the concept of allocation. This includes the 38 // concepts that a space may be only partially full, and the query methods 39 // that go with such an assumption. 40 // 41 // MutableSpace is also responsible for minimizing the 42 // page allocation time by having the memory pretouched (with 43 // AlwaysPretouch) and for optimizing page placement on NUMA systems 44 // by make the underlying region interleaved (with UseNUMA). 45 // 46 // Invariant: bottom() <= top() <= end() 47 // top() and end() are exclusive. 48 49 class MutableSpace: public CHeapObj<mtGC> { 50 friend class VMStructs; 51 52 // The last region which page had been setup to be interleaved. 53 MemRegion _last_setup_region; 54 size_t _alignment; 55 HeapWord* _bottom; 56 HeapWord* volatile _top; 57 HeapWord* _end; 58 59 void numa_setup_pages(MemRegion mr, size_t page_size, bool clear_space); 60 61 void set_last_setup_region(MemRegion mr) { _last_setup_region = mr; } 62 MemRegion last_setup_region() const { return _last_setup_region; } 63 64 template<bool COMPACT_HEADERS> 65 void object_iterate_impl(ObjectClosure* cl); 66 67 public: 68 virtual ~MutableSpace() = default; 69 MutableSpace(size_t page_size); 70 71 // Accessors 72 HeapWord* bottom() const { return _bottom; } 73 HeapWord* top() const { return _top; } 74 HeapWord* end() const { return _end; } 75 76 void set_bottom(HeapWord* value) { _bottom = value; } 77 virtual void set_top(HeapWord* value) { _top = value; } 78 void set_end(HeapWord* value) { _end = value; } 79 80 HeapWord* volatile* top_addr() { return &_top; } 81 HeapWord** end_addr() { return &_end; } 82 83 size_t alignment() { return _alignment; } 84 85 MemRegion region() const { return MemRegion(bottom(), end()); } 86 87 size_t capacity_in_bytes() const { return capacity_in_words() * HeapWordSize; } 88 size_t capacity_in_words() const { return pointer_delta(end(), bottom()); } 89 90 // Returns a subregion containing all objects in this space. 91 MemRegion used_region() { return MemRegion(bottom(), top()); } 92 93 static const bool SetupPages = true; 94 static const bool DontSetupPages = false; 95 96 // Initialization 97 virtual void initialize(MemRegion mr, 98 bool clear_space, 99 bool mangle_space, 100 bool setup_pages = SetupPages, 101 WorkerThreads* pretouch_workers = nullptr); 102 103 virtual void clear(bool mangle_space); 104 virtual void update() { } 105 virtual void accumulate_statistics() { } 106 107 virtual void mangle_unused_area() PRODUCT_RETURN; 108 virtual void mangle_region(MemRegion mr) PRODUCT_RETURN; 109 110 virtual void ensure_parsability() { } 111 112 // Boolean queries. 113 bool is_empty() const { return used_in_words() == 0; } 114 bool not_empty() const { return used_in_words() > 0; } 115 bool contains(const void* p) const { return _bottom <= p && p < _end; } 116 117 // Size computations. Sizes are in bytes. 118 size_t used_in_bytes() const { return used_in_words() * HeapWordSize; } 119 size_t free_in_bytes() const { return free_in_words() * HeapWordSize; } 120 121 // Size computations. Sizes are in heapwords. 122 virtual size_t used_in_words() const { return pointer_delta(top(), bottom()); } 123 virtual size_t free_in_words() const { return pointer_delta(end(), top()); } 124 virtual size_t tlab_capacity(Thread* thr) const { return capacity_in_bytes(); } 125 virtual size_t tlab_used(Thread* thr) const { return used_in_bytes(); } 126 virtual size_t unsafe_max_tlab_alloc(Thread* thr) const { return free_in_bytes(); } 127 128 // Allocation (return null if full) 129 virtual HeapWord* cas_allocate(size_t word_size); 130 // Optional deallocation. Used in NUMA-allocator. 131 bool cas_deallocate(HeapWord *obj, size_t size); 132 // Return true if this space needs to be expanded in order to satisfy an 133 // allocation request of the indicated size. Concurrent allocations and 134 // resizes may change the result of a later call. Used by oldgen allocator. 135 // precondition: holding PSOldGenExpand_lock 136 bool needs_expand(size_t word_size) const; 137 138 // Iteration. 139 void oop_iterate(OopIterateClosure* cl); 140 void object_iterate(ObjectClosure* cl); 141 142 // Debugging 143 virtual void print() const; 144 virtual void print_on(outputStream* st) const; 145 virtual void print_short() const; 146 virtual void print_short_on(outputStream* st) const; 147 virtual void verify(); 148 }; 149 150 #endif // SHARE_GC_PARALLEL_MUTABLESPACE_HPP