1 /* 2 * Copyright (c) 1997, 2013, 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_VM_UTILITIES_BITMAP_HPP 26 #define SHARE_VM_UTILITIES_BITMAP_HPP 27 28 #include "memory/allocation.hpp" 29 #include "utilities/top.hpp" 30 31 // Forward decl; 32 class BitMapClosure; 33 34 // Operations for bitmaps represented as arrays of unsigned integers. 35 // Bit offsets are numbered from 0 to size-1. 36 37 class BitMap VALUE_OBJ_CLASS_SPEC { 38 friend class BitMap2D; 39 40 public: 41 typedef size_t idx_t; // Type used for bit and word indices. 42 typedef uintptr_t bm_word_t; // Element type of array that represents 43 // the bitmap. 44 45 // Hints for range sizes. 46 typedef enum { 47 unknown_range, small_range, large_range 48 } RangeSizeHint; 49 50 private: 51 ArrayAllocator<bm_word_t, mtInternal> _map_allocator; 52 bm_word_t* _map; // First word in bitmap 53 idx_t _size; // Size of bitmap (in bits) 54 55 // Puts the given value at the given offset, using resize() to size 56 // the bitmap appropriately if needed using factor-of-two expansion. 57 void at_put_grow(idx_t index, bool value); 58 59 // Threshold for performing small range operation, even when large range 60 // operation was requested. Measured in words. 61 static const size_t small_range_words = 32; 62 63 protected: 64 // Return the position of bit within the word that contains it (e.g., if 65 // bitmap words are 32 bits, return a number 0 <= n <= 31). 66 static idx_t bit_in_word(idx_t bit) { return bit & (BitsPerWord - 1); } 67 68 // Return a mask that will select the specified bit, when applied to the word 69 // containing the bit. 70 static bm_word_t bit_mask(idx_t bit) { return (bm_word_t)1 << bit_in_word(bit); } 71 72 // Return the index of the word containing the specified bit. 73 static idx_t word_index(idx_t bit) { return bit >> LogBitsPerWord; } 74 75 // Return the bit number of the first bit in the specified word. 76 static idx_t bit_index(idx_t word) { return word << LogBitsPerWord; } 77 78 // Return the array of bitmap words, or a specific word from it. 79 bm_word_t* map() const { return _map; } 80 bm_word_t map(idx_t word) const { return _map[word]; } 81 82 // Return a pointer to the word containing the specified bit. 83 bm_word_t* word_addr(idx_t bit) const { return map() + word_index(bit); } 84 85 // Set a word to a specified value or to all ones; clear a word. 86 void set_word (idx_t word, bm_word_t val) { _map[word] = val; } 87 void set_word (idx_t word) { set_word(word, ~(uintptr_t)0); } 88 void clear_word(idx_t word) { _map[word] = 0; } 89 90 // Utilities for ranges of bits. Ranges are half-open [beg, end). 91 92 // Ranges within a single word. 93 bm_word_t inverted_bit_mask_for_range(idx_t beg, idx_t end) const; 94 void set_range_within_word (idx_t beg, idx_t end); 95 void clear_range_within_word (idx_t beg, idx_t end); 96 void par_put_range_within_word (idx_t beg, idx_t end, bool value); 97 98 // Ranges spanning entire words. 99 void set_range_of_words (idx_t beg, idx_t end); 100 void clear_range_of_words (idx_t beg, idx_t end); 101 void set_large_range_of_words (idx_t beg, idx_t end); 102 void clear_large_range_of_words (idx_t beg, idx_t end); 103 104 static bool is_small_range_of_words(idx_t beg_full_word, idx_t end_full_word); 105 106 // The index of the first full word in a range. 107 idx_t word_index_round_up(idx_t bit) const; 108 109 // Verification. 110 inline void verify_index(idx_t index) const NOT_DEBUG_RETURN; 111 inline void verify_range(idx_t beg_index, idx_t end_index) const 112 NOT_DEBUG_RETURN; 113 114 // Statistics. 115 static idx_t* _pop_count_table; 116 static void init_pop_count_table(); 117 static idx_t num_set_bits(bm_word_t w); 118 static idx_t num_set_bits_from_table(unsigned char c); 119 120 public: 121 122 // Constructs a bitmap with no map, and size 0. 123 BitMap() : _map(NULL), _size(0), _map_allocator(false) {} 124 125 // Constructs a bitmap with the given map and size. 126 BitMap(bm_word_t* map, idx_t size_in_bits); 127 128 // Constructs an empty bitmap of the given size (that is, this clears the 129 // new bitmap). Allocates the map array in resource area if 130 // "in_resource_area" is true, else in the C heap. 131 BitMap(idx_t size_in_bits, bool in_resource_area = true); 132 133 // Set the map and size. 134 void set_map(bm_word_t* map) { _map = map; } 135 void set_size(idx_t size_in_bits) { _size = size_in_bits; } 136 137 // Allocates necessary data structure, either in the resource area 138 // or in the C heap, as indicated by "in_resource_area." 139 // Preserves state currently in bit map by copying data. 140 // Zeros any newly-addressable bits. 141 // If "in_resource_area" is false, frees the current map. 142 // (Note that this assumes that all calls to "resize" on the same BitMap 143 // use the same value for "in_resource_area".) 144 void resize(idx_t size_in_bits, bool in_resource_area = true); 145 146 // Accessing 147 idx_t size() const { return _size; } 148 idx_t size_in_words() const { 149 return word_index(size() + BitsPerWord - 1); 150 } 151 152 bool at(idx_t index) const { 153 verify_index(index); 154 return (*word_addr(index) & bit_mask(index)) != 0; 155 } 156 157 // Align bit index up or down to the next bitmap word boundary, or check 158 // alignment. 159 static idx_t word_align_up(idx_t bit) { 160 return align_size_up(bit, BitsPerWord); 161 } 162 static idx_t word_align_down(idx_t bit) { 163 return align_size_down(bit, BitsPerWord); 164 } 165 static bool is_word_aligned(idx_t bit) { 166 return word_align_up(bit) == bit; 167 } 168 169 // Set or clear the specified bit. 170 inline void set_bit(idx_t bit); 171 inline void clear_bit(idx_t bit); 172 173 // Atomically set or clear the specified bit. 174 inline bool par_set_bit(idx_t bit); 175 inline bool par_clear_bit(idx_t bit); 176 177 // Put the given value at the given offset. The parallel version 178 // will CAS the value into the bitmap and is quite a bit slower. 179 // The parallel version also returns a value indicating if the 180 // calling thread was the one that changed the value of the bit. 181 void at_put(idx_t index, bool value); 182 bool par_at_put(idx_t index, bool value); 183 184 // Update a range of bits. Ranges are half-open [beg, end). 185 void set_range (idx_t beg, idx_t end); 186 void clear_range (idx_t beg, idx_t end); 187 void set_large_range (idx_t beg, idx_t end); 188 void clear_large_range (idx_t beg, idx_t end); 189 void at_put_range(idx_t beg, idx_t end, bool value); 190 void par_at_put_range(idx_t beg, idx_t end, bool value); 191 void at_put_large_range(idx_t beg, idx_t end, bool value); 192 void par_at_put_large_range(idx_t beg, idx_t end, bool value); 193 194 // Update a range of bits, using a hint about the size. Currently only 195 // inlines the predominant case of a 1-bit range. Works best when hint is a 196 // compile-time constant. 197 void set_range(idx_t beg, idx_t end, RangeSizeHint hint); 198 void clear_range(idx_t beg, idx_t end, RangeSizeHint hint); 199 void par_set_range(idx_t beg, idx_t end, RangeSizeHint hint); 200 void par_clear_range (idx_t beg, idx_t end, RangeSizeHint hint); 201 202 // Clearing 203 void clear_large(); 204 inline void clear(); 205 206 // Iteration support. Returns "true" if the iteration completed, false 207 // if the iteration terminated early (because the closure "blk" returned 208 // false). 209 bool iterate(BitMapClosure* blk, idx_t leftIndex, idx_t rightIndex); 210 bool iterate(BitMapClosure* blk) { 211 // call the version that takes an interval 212 return iterate(blk, 0, size()); 213 } 214 215 // Looking for 1's and 0's at indices equal to or greater than "l_index", 216 // stopping if none has been found before "r_index", and returning 217 // "r_index" (which must be at most "size") in that case. 218 idx_t get_next_one_offset_inline (idx_t l_index, idx_t r_index) const; 219 idx_t get_next_zero_offset_inline(idx_t l_index, idx_t r_index) const; 220 221 // Like "get_next_one_offset_inline", except requires that "r_index" is 222 // aligned to bitsizeof(bm_word_t). 223 idx_t get_next_one_offset_inline_aligned_right(idx_t l_index, 224 idx_t r_index) const; 225 226 // Non-inline versionsof the above. 227 idx_t get_next_one_offset (idx_t l_index, idx_t r_index) const; 228 idx_t get_next_zero_offset(idx_t l_index, idx_t r_index) const; 229 230 idx_t get_next_one_offset(idx_t offset) const { 231 return get_next_one_offset(offset, size()); 232 } 233 idx_t get_next_zero_offset(idx_t offset) const { 234 return get_next_zero_offset(offset, size()); 235 } 236 237 // Returns the number of bits set in the bitmap. 238 idx_t count_one_bits() const; 239 240 // Set operations. 241 void set_union(BitMap bits); 242 void set_difference(BitMap bits); 243 void set_intersection(BitMap bits); 244 // Returns true iff "this" is a superset of "bits". 245 bool contains(const BitMap bits) const; 246 // Returns true iff "this and "bits" have a non-empty intersection. 247 bool intersects(const BitMap bits) const; 248 249 // Returns result of whether this map changed 250 // during the operation 251 bool set_union_with_result(BitMap bits); 252 bool set_difference_with_result(BitMap bits); 253 bool set_intersection_with_result(BitMap bits); 254 255 // Requires the submap of "bits" starting at offset to be at least as 256 // large as "this". Modifies "this" to be the intersection of its 257 // current contents and the submap of "bits" starting at "offset" of the 258 // same length as "this." 259 // (For expedience, currently requires the offset to be aligned to the 260 // bitsize of a uintptr_t. This should go away in the future though it 261 // will probably remain a good case to optimize.) 262 void set_intersection_at_offset(BitMap bits, idx_t offset); 263 264 void set_from(BitMap bits); 265 266 bool is_same(BitMap bits); 267 268 // Test if all bits are set or cleared 269 bool is_full() const; 270 bool is_empty() const; 271 272 void print_on_error(outputStream* st, const char* prefix) const; 273 274 #ifndef PRODUCT 275 public: 276 // Printing 277 void print_on(outputStream* st) const; 278 #endif 279 }; 280 281 // Convenience class wrapping BitMap which provides multiple bits per slot. 282 class BitMap2D VALUE_OBJ_CLASS_SPEC { 283 public: 284 typedef BitMap::idx_t idx_t; // Type used for bit and word indices. 285 typedef BitMap::bm_word_t bm_word_t; // Element type of array that 286 // represents the bitmap. 287 private: 288 BitMap _map; 289 idx_t _bits_per_slot; 290 291 idx_t bit_index(idx_t slot_index, idx_t bit_within_slot_index) const { 292 return slot_index * _bits_per_slot + bit_within_slot_index; 293 } 294 295 void verify_bit_within_slot_index(idx_t index) const { 296 assert(index < _bits_per_slot, "bit_within_slot index out of bounds"); 297 } 298 299 public: 300 // Construction. bits_per_slot must be greater than 0. 301 BitMap2D(bm_word_t* map, idx_t size_in_slots, idx_t bits_per_slot); 302 303 // Allocates necessary data structure in resource area. bits_per_slot must be greater than 0. 304 BitMap2D(idx_t size_in_slots, idx_t bits_per_slot); 305 306 idx_t size_in_bits() { 307 return _map.size(); 308 } 309 310 // Returns number of full slots that have been allocated 311 idx_t size_in_slots() { 312 // Round down 313 return _map.size() / _bits_per_slot; 314 } 315 316 bool is_valid_index(idx_t slot_index, idx_t bit_within_slot_index) { 317 verify_bit_within_slot_index(bit_within_slot_index); 318 return (bit_index(slot_index, bit_within_slot_index) < size_in_bits()); 319 } 320 321 bool at(idx_t slot_index, idx_t bit_within_slot_index) const { 322 verify_bit_within_slot_index(bit_within_slot_index); 323 return _map.at(bit_index(slot_index, bit_within_slot_index)); 324 } 325 326 void set_bit(idx_t slot_index, idx_t bit_within_slot_index) { 327 verify_bit_within_slot_index(bit_within_slot_index); 328 _map.set_bit(bit_index(slot_index, bit_within_slot_index)); 329 } 330 331 void clear_bit(idx_t slot_index, idx_t bit_within_slot_index) { 332 verify_bit_within_slot_index(bit_within_slot_index); 333 _map.clear_bit(bit_index(slot_index, bit_within_slot_index)); 334 } 335 336 void at_put(idx_t slot_index, idx_t bit_within_slot_index, bool value) { 337 verify_bit_within_slot_index(bit_within_slot_index); 338 _map.at_put(bit_index(slot_index, bit_within_slot_index), value); 339 } 340 341 void at_put_grow(idx_t slot_index, idx_t bit_within_slot_index, bool value) { 342 verify_bit_within_slot_index(bit_within_slot_index); 343 _map.at_put_grow(bit_index(slot_index, bit_within_slot_index), value); 344 } 345 346 void clear(); 347 }; 348 349 // Closure for iterating over BitMaps 350 351 class BitMapClosure VALUE_OBJ_CLASS_SPEC { 352 public: 353 // Callback when bit in map is set. Should normally return "true"; 354 // return of false indicates that the bitmap iteration should terminate. 355 virtual bool do_bit(BitMap::idx_t offset) = 0; 356 }; 357 358 #endif // SHARE_VM_UTILITIES_BITMAP_HPP