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