1 /* 2 * Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved. 3 * Copyright (c) 2020, Red Hat, Inc. and/or its affiliates. 4 * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. 5 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 6 * 7 * This code is free software; you can redistribute it and/or modify it 8 * under the terms of the GNU General Public License version 2 only, as 9 * published by the Free Software Foundation. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 * 25 */ 26 27 #ifndef SHARE_GC_SHENANDOAH_SHENANDOAHMARKBITMAP_HPP 28 #define SHARE_GC_SHENANDOAH_SHENANDOAHMARKBITMAP_HPP 29 30 #include "memory/memRegion.hpp" 31 #include "runtime/atomic.hpp" 32 #include "utilities/globalDefinitions.hpp" 33 34 class ShenandoahMarkBitMap { 35 public: 36 typedef size_t idx_t; // Type used for bit and word indices. 37 typedef uintptr_t bm_word_t; // Element type of array that represents the 38 // bitmap, with BitsPerWord bits per element. 39 40 private: 41 // Values for get_next_bit_impl flip parameter. 42 static const bm_word_t find_ones_flip = 0; 43 static const bm_word_t find_zeros_flip = ~(bm_word_t)0; 44 45 int const _shift; 46 MemRegion _covered; 47 48 bm_word_t* _map; // First word in bitmap 49 idx_t _size; // Size of bitmap (in bits) 50 51 // Threshold for performing small range operation, even when large range 52 // operation was requested. Measured in words. 53 static const size_t small_range_words = 32; 54 55 static bool is_small_range_of_words(idx_t beg_full_word, idx_t end_full_word); 56 57 inline size_t address_to_index(const HeapWord* addr) const; 58 inline HeapWord* index_to_address(size_t offset) const; 59 60 void check_mark(HeapWord* addr) const NOT_DEBUG_RETURN; 61 62 // Return a mask that will select the specified bit, when applied to the word 63 // containing the bit. 64 static bm_word_t bit_mask(idx_t bit) { return (bm_word_t)1 << bit_in_word(bit); } 65 66 // Return the bit number of the first bit in the specified word. 67 static idx_t bit_index(idx_t word) { return word << LogBitsPerWord; } 68 69 // Return the position of bit within the word that contains it (e.g., if 70 // bitmap words are 32 bits, return a number 0 <= n <= 31). 71 static idx_t bit_in_word(idx_t bit) { return bit & (BitsPerWord - 1); } 72 73 bm_word_t* map() { return _map; } 74 const bm_word_t* map() const { return _map; } 75 bm_word_t map(idx_t word) const { return _map[word]; } 76 77 // Return a pointer to the word containing the specified bit. 78 bm_word_t* word_addr(idx_t bit) { 79 return map() + to_words_align_down(bit); 80 } 81 82 const bm_word_t* word_addr(idx_t bit) const { 83 return map() + to_words_align_down(bit); 84 } 85 86 bool at(idx_t index) const { 87 verify_index(index); 88 return (*word_addr(index) & bit_mask(index)) != 0; 89 } 90 91 // Assumes relevant validity checking for bit has already been done. 92 static idx_t raw_to_words_align_up(idx_t bit) { 93 return raw_to_words_align_down(bit + (BitsPerWord - 1)); 94 } 95 96 // Assumes relevant validity checking for bit has already been done. 97 static idx_t raw_to_words_align_down(idx_t bit) { 98 return bit >> LogBitsPerWord; 99 } 100 101 // Word-aligns bit and converts it to a word offset. 102 // precondition: bit <= size() 103 idx_t to_words_align_up(idx_t bit) const { 104 verify_limit(bit); 105 return raw_to_words_align_up(bit); 106 } 107 108 // Word-aligns bit and converts it to a word offset. 109 // precondition: bit <= size() 110 inline idx_t to_words_align_down(idx_t bit) const { 111 verify_limit(bit); 112 return raw_to_words_align_down(bit); 113 } 114 115 // Helper for get_next_{zero,one}_bit variants. 116 // - flip designates whether searching for 1s or 0s. Must be one of 117 // find_{zeros,ones}_flip. 118 // - aligned_right is true if r_index is a priori on a bm_word_t boundary. 119 template<bm_word_t flip, bool aligned_right> 120 inline idx_t get_next_bit_impl(idx_t l_index, idx_t r_index) const; 121 122 inline idx_t get_next_one_offset (idx_t l_index, idx_t r_index) const; 123 124 void clear_large_range (idx_t beg, idx_t end); 125 126 // Verify bit is less than size(). 127 void verify_index(idx_t bit) const NOT_DEBUG_RETURN; 128 // Verify bit is not greater than size(). 129 void verify_limit(idx_t bit) const NOT_DEBUG_RETURN; 130 // Verify [beg,end) is a valid range, e.g. beg <= end <= size(). 131 void verify_range(idx_t beg, idx_t end) const NOT_DEBUG_RETURN; 132 133 public: 134 static size_t compute_size(size_t heap_size); 135 // Returns the amount of bytes on the heap between two marks in the bitmap. 136 static size_t mark_distance(); 137 // Returns how many bytes (or bits) of the heap a single byte (or bit) of the 138 // mark bitmap corresponds to. This is the same as the mark distance above. 139 static size_t heap_map_factor() { 140 return mark_distance(); 141 } 142 143 ShenandoahMarkBitMap(MemRegion heap, MemRegion storage); 144 145 // Mark word as 'strong' if it hasn't been marked strong yet. 146 // Return true if the word has been marked strong, false if it has already been 147 // marked strong or if another thread has beat us by marking it 148 // strong. 149 // Words that have been marked final before or by a concurrent thread will be 150 // upgraded to strong. In this case, this method also returns true. 151 inline bool mark_strong(HeapWord* w, bool& was_upgraded); 152 153 // Mark word as 'weak' if it hasn't been marked weak or strong yet. 154 // Return true if the word has been marked weak, false if it has already been 155 // marked strong or weak or if another thread has beat us by marking it 156 // strong or weak. 157 inline bool mark_weak(HeapWord* heap_addr); 158 159 inline bool is_marked(HeapWord* addr) const; 160 inline bool is_marked_strong(HeapWord* w) const; 161 inline bool is_marked_weak(HeapWord* addr) const; 162 163 bool is_bitmap_clear_range(const HeapWord* start, const HeapWord* end) const; 164 165 // Return the address corresponding to the next marked bit at or after 166 // "addr", and before "limit", if "limit" is non-null. If there is no 167 // such bit, returns "limit" if that is non-null, or else "endWord()". 168 HeapWord* get_next_marked_addr(const HeapWord* addr, 169 const HeapWord* limit) const; 170 171 bm_word_t inverted_bit_mask_for_range(idx_t beg, idx_t end) const; 172 void clear_range_within_word (idx_t beg, idx_t end); 173 void clear_range (idx_t beg, idx_t end); 174 void clear_range_large(MemRegion mr); 175 176 void clear_range_of_words(idx_t beg, idx_t end); 177 void clear_large_range_of_words(idx_t beg, idx_t end); 178 static void clear_range_of_words(bm_word_t* map, idx_t beg, idx_t end); 179 180 }; 181 182 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHMARKBITMAP_HPP