src/share/vm/utilities/bitMap.hpp

Tue, 05 Apr 2011 14:12:31 -0700

author
trims
date
Tue, 05 Apr 2011 14:12:31 -0700
changeset 2708
1d1603768966
parent 2314
f95d63e2154a
child 2968
842b840e67db
permissions
-rw-r--r--

7010070: Update all 2010 Oracle-changed OpenJDK files to have the proper copyright dates - second pass
Summary: Update the copyright to be 2010 on all changed files in OpenJDK
Reviewed-by: ohair

duke@435 1 /*
stefank@2314 2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #ifndef SHARE_VM_UTILITIES_BITMAP_HPP
stefank@2314 26 #define SHARE_VM_UTILITIES_BITMAP_HPP
stefank@2314 27
stefank@2314 28 #include "memory/allocation.hpp"
stefank@2314 29 #include "utilities/top.hpp"
stefank@2314 30
ysr@777 31 // Forward decl;
ysr@777 32 class BitMapClosure;
duke@435 33
ysr@777 34 // Operations for bitmaps represented as arrays of unsigned integers.
ysr@777 35 // Bit offsets are numbered from 0 to size-1.
duke@435 36
duke@435 37 class BitMap VALUE_OBJ_CLASS_SPEC {
duke@435 38 friend class BitMap2D;
duke@435 39
duke@435 40 public:
duke@435 41 typedef size_t idx_t; // Type used for bit and word indices.
ysr@777 42 typedef uintptr_t bm_word_t; // Element type of array that represents
ysr@777 43 // the bitmap.
duke@435 44
duke@435 45 // Hints for range sizes.
duke@435 46 typedef enum {
duke@435 47 unknown_range, small_range, large_range
duke@435 48 } RangeSizeHint;
duke@435 49
duke@435 50 private:
ysr@777 51 bm_word_t* _map; // First word in bitmap
ysr@777 52 idx_t _size; // Size of bitmap (in bits)
duke@435 53
duke@435 54 // Puts the given value at the given offset, using resize() to size
duke@435 55 // the bitmap appropriately if needed using factor-of-two expansion.
duke@435 56 void at_put_grow(idx_t index, bool value);
duke@435 57
duke@435 58 protected:
duke@435 59 // Return the position of bit within the word that contains it (e.g., if
duke@435 60 // bitmap words are 32 bits, return a number 0 <= n <= 31).
duke@435 61 static idx_t bit_in_word(idx_t bit) { return bit & (BitsPerWord - 1); }
duke@435 62
duke@435 63 // Return a mask that will select the specified bit, when applied to the word
duke@435 64 // containing the bit.
ysr@777 65 static bm_word_t bit_mask(idx_t bit) { return (bm_word_t)1 << bit_in_word(bit); }
duke@435 66
duke@435 67 // Return the index of the word containing the specified bit.
duke@435 68 static idx_t word_index(idx_t bit) { return bit >> LogBitsPerWord; }
duke@435 69
duke@435 70 // Return the bit number of the first bit in the specified word.
duke@435 71 static idx_t bit_index(idx_t word) { return word << LogBitsPerWord; }
duke@435 72
duke@435 73 // Return the array of bitmap words, or a specific word from it.
ysr@777 74 bm_word_t* map() const { return _map; }
ysr@777 75 bm_word_t map(idx_t word) const { return _map[word]; }
duke@435 76
duke@435 77 // Return a pointer to the word containing the specified bit.
ysr@777 78 bm_word_t* word_addr(idx_t bit) const { return map() + word_index(bit); }
duke@435 79
duke@435 80 // Set a word to a specified value or to all ones; clear a word.
ysr@777 81 void set_word (idx_t word, bm_word_t val) { _map[word] = val; }
duke@435 82 void set_word (idx_t word) { set_word(word, ~(uintptr_t)0); }
duke@435 83 void clear_word(idx_t word) { _map[word] = 0; }
duke@435 84
duke@435 85 // Utilities for ranges of bits. Ranges are half-open [beg, end).
duke@435 86
duke@435 87 // Ranges within a single word.
ysr@777 88 bm_word_t inverted_bit_mask_for_range(idx_t beg, idx_t end) const;
ysr@777 89 void set_range_within_word (idx_t beg, idx_t end);
ysr@777 90 void clear_range_within_word (idx_t beg, idx_t end);
ysr@777 91 void par_put_range_within_word (idx_t beg, idx_t end, bool value);
duke@435 92
duke@435 93 // Ranges spanning entire words.
ysr@777 94 void set_range_of_words (idx_t beg, idx_t end);
ysr@777 95 void clear_range_of_words (idx_t beg, idx_t end);
ysr@777 96 void set_large_range_of_words (idx_t beg, idx_t end);
ysr@777 97 void clear_large_range_of_words (idx_t beg, idx_t end);
duke@435 98
duke@435 99 // The index of the first full word in a range.
ysr@777 100 idx_t word_index_round_up(idx_t bit) const;
duke@435 101
jcoomes@1244 102 // Verification.
jcoomes@1244 103 inline void verify_index(idx_t index) const NOT_DEBUG_RETURN;
jcoomes@1244 104 inline void verify_range(idx_t beg_index, idx_t end_index) const
jcoomes@1244 105 NOT_DEBUG_RETURN;
duke@435 106
jcoomes@1244 107 // Statistics.
ysr@777 108 static idx_t* _pop_count_table;
ysr@777 109 static void init_pop_count_table();
ysr@777 110 static idx_t num_set_bits(bm_word_t w);
ysr@777 111 static idx_t num_set_bits_from_table(unsigned char c);
duke@435 112
duke@435 113 public:
duke@435 114
duke@435 115 // Constructs a bitmap with no map, and size 0.
duke@435 116 BitMap() : _map(NULL), _size(0) {}
duke@435 117
ysr@777 118 // Constructs a bitmap with the given map and size.
ysr@777 119 BitMap(bm_word_t* map, idx_t size_in_bits);
duke@435 120
ysr@777 121 // Constructs an empty bitmap of the given size (that is, this clears the
ysr@777 122 // new bitmap). Allocates the map array in resource area if
ysr@777 123 // "in_resource_area" is true, else in the C heap.
ysr@777 124 BitMap(idx_t size_in_bits, bool in_resource_area = true);
duke@435 125
ysr@777 126 // Set the map and size.
ysr@777 127 void set_map(bm_word_t* map) { _map = map; }
duke@435 128 void set_size(idx_t size_in_bits) { _size = size_in_bits; }
duke@435 129
ysr@777 130 // Allocates necessary data structure, either in the resource area
ysr@777 131 // or in the C heap, as indicated by "in_resource_area."
duke@435 132 // Preserves state currently in bit map by copying data.
duke@435 133 // Zeros any newly-addressable bits.
ysr@777 134 // If "in_resource_area" is false, frees the current map.
ysr@777 135 // (Note that this assumes that all calls to "resize" on the same BitMap
ysr@777 136 // use the same value for "in_resource_area".)
ysr@777 137 void resize(idx_t size_in_bits, bool in_resource_area = true);
duke@435 138
duke@435 139 // Accessing
duke@435 140 idx_t size() const { return _size; }
duke@435 141 idx_t size_in_words() const {
duke@435 142 return word_index(size() + BitsPerWord - 1);
duke@435 143 }
duke@435 144
duke@435 145 bool at(idx_t index) const {
duke@435 146 verify_index(index);
duke@435 147 return (*word_addr(index) & bit_mask(index)) != 0;
duke@435 148 }
duke@435 149
duke@435 150 // Align bit index up or down to the next bitmap word boundary, or check
duke@435 151 // alignment.
duke@435 152 static idx_t word_align_up(idx_t bit) {
duke@435 153 return align_size_up(bit, BitsPerWord);
duke@435 154 }
duke@435 155 static idx_t word_align_down(idx_t bit) {
duke@435 156 return align_size_down(bit, BitsPerWord);
duke@435 157 }
duke@435 158 static bool is_word_aligned(idx_t bit) {
duke@435 159 return word_align_up(bit) == bit;
duke@435 160 }
duke@435 161
duke@435 162 // Set or clear the specified bit.
duke@435 163 inline void set_bit(idx_t bit);
ysr@777 164 void clear_bit(idx_t bit);
duke@435 165
duke@435 166 // Atomically set or clear the specified bit.
ysr@777 167 bool par_set_bit(idx_t bit);
ysr@777 168 bool par_clear_bit(idx_t bit);
duke@435 169
duke@435 170 // Put the given value at the given offset. The parallel version
duke@435 171 // will CAS the value into the bitmap and is quite a bit slower.
duke@435 172 // The parallel version also returns a value indicating if the
duke@435 173 // calling thread was the one that changed the value of the bit.
duke@435 174 void at_put(idx_t index, bool value);
duke@435 175 bool par_at_put(idx_t index, bool value);
duke@435 176
duke@435 177 // Update a range of bits. Ranges are half-open [beg, end).
duke@435 178 void set_range (idx_t beg, idx_t end);
duke@435 179 void clear_range (idx_t beg, idx_t end);
duke@435 180 void set_large_range (idx_t beg, idx_t end);
duke@435 181 void clear_large_range (idx_t beg, idx_t end);
duke@435 182 void at_put_range(idx_t beg, idx_t end, bool value);
duke@435 183 void par_at_put_range(idx_t beg, idx_t end, bool value);
duke@435 184 void at_put_large_range(idx_t beg, idx_t end, bool value);
duke@435 185 void par_at_put_large_range(idx_t beg, idx_t end, bool value);
duke@435 186
duke@435 187 // Update a range of bits, using a hint about the size. Currently only
duke@435 188 // inlines the predominant case of a 1-bit range. Works best when hint is a
duke@435 189 // compile-time constant.
ysr@777 190 void set_range(idx_t beg, idx_t end, RangeSizeHint hint);
ysr@777 191 void clear_range(idx_t beg, idx_t end, RangeSizeHint hint);
ysr@777 192 void par_set_range(idx_t beg, idx_t end, RangeSizeHint hint);
ysr@777 193 void par_clear_range (idx_t beg, idx_t end, RangeSizeHint hint);
ysr@777 194
ysr@777 195 // It performs the union operation between subsets of equal length
ysr@777 196 // of two bitmaps (the target bitmap of the method and the
ysr@777 197 // from_bitmap) and stores the result to the target bitmap. The
ysr@777 198 // from_start_index represents the first bit index of the subrange
ysr@777 199 // of the from_bitmap. The to_start_index is the equivalent of the
ysr@777 200 // target bitmap. Both indexes should be word-aligned, i.e. they
ysr@777 201 // should correspond to the first bit on a bitmap word (it's up to
ysr@777 202 // the caller to ensure this; the method does check it). The length
ysr@777 203 // of the subset is specified with word_num and it is in number of
ysr@777 204 // bitmap words. The caller should ensure that this is at least 2
ysr@777 205 // (smaller ranges are not support to save extra checks). Again,
ysr@777 206 // this is checked in the method.
ysr@777 207 //
ysr@777 208 // Atomicity concerns: it is assumed that any contention on the
ysr@777 209 // target bitmap with other threads will happen on the first and
ysr@777 210 // last words; the ones in between will be "owned" exclusively by
ysr@777 211 // the calling thread and, in fact, they will already be 0. So, the
ysr@777 212 // method performs a CAS on the first word, copies the next
ysr@777 213 // word_num-2 words, and finally performs a CAS on the last word.
ysr@777 214 void mostly_disjoint_range_union(BitMap* from_bitmap,
ysr@777 215 idx_t from_start_index,
ysr@777 216 idx_t to_start_index,
ysr@777 217 size_t word_num);
ysr@777 218
duke@435 219
duke@435 220 // Clearing
duke@435 221 void clear_large();
ysr@777 222 inline void clear();
duke@435 223
ysr@777 224 // Iteration support. Returns "true" if the iteration completed, false
ysr@777 225 // if the iteration terminated early (because the closure "blk" returned
ysr@777 226 // false).
ysr@777 227 bool iterate(BitMapClosure* blk, idx_t leftIndex, idx_t rightIndex);
ysr@777 228 bool iterate(BitMapClosure* blk) {
duke@435 229 // call the version that takes an interval
ysr@777 230 return iterate(blk, 0, size());
duke@435 231 }
duke@435 232
ysr@777 233 // Looking for 1's and 0's at indices equal to or greater than "l_index",
ysr@777 234 // stopping if none has been found before "r_index", and returning
ysr@777 235 // "r_index" (which must be at most "size") in that case.
ysr@777 236 idx_t get_next_one_offset_inline (idx_t l_index, idx_t r_index) const;
ysr@777 237 idx_t get_next_zero_offset_inline(idx_t l_index, idx_t r_index) const;
ysr@777 238
ysr@777 239 // Like "get_next_one_offset_inline", except requires that "r_index" is
ysr@777 240 // aligned to bitsizeof(bm_word_t).
ysr@777 241 idx_t get_next_one_offset_inline_aligned_right(idx_t l_index,
ysr@777 242 idx_t r_index) const;
ysr@777 243
ysr@777 244 // Non-inline versionsof the above.
duke@435 245 idx_t get_next_one_offset (idx_t l_index, idx_t r_index) const;
duke@435 246 idx_t get_next_zero_offset(idx_t l_index, idx_t r_index) const;
duke@435 247
duke@435 248 idx_t get_next_one_offset(idx_t offset) const {
duke@435 249 return get_next_one_offset(offset, size());
duke@435 250 }
duke@435 251 idx_t get_next_zero_offset(idx_t offset) const {
duke@435 252 return get_next_zero_offset(offset, size());
duke@435 253 }
duke@435 254
ysr@777 255 // Returns the number of bits set in the bitmap.
ysr@777 256 idx_t count_one_bits() const;
duke@435 257
duke@435 258 // Set operations.
duke@435 259 void set_union(BitMap bits);
duke@435 260 void set_difference(BitMap bits);
duke@435 261 void set_intersection(BitMap bits);
duke@435 262 // Returns true iff "this" is a superset of "bits".
duke@435 263 bool contains(const BitMap bits) const;
duke@435 264 // Returns true iff "this and "bits" have a non-empty intersection.
duke@435 265 bool intersects(const BitMap bits) const;
duke@435 266
duke@435 267 // Returns result of whether this map changed
duke@435 268 // during the operation
duke@435 269 bool set_union_with_result(BitMap bits);
duke@435 270 bool set_difference_with_result(BitMap bits);
duke@435 271 bool set_intersection_with_result(BitMap bits);
duke@435 272
ysr@777 273 // Requires the submap of "bits" starting at offset to be at least as
ysr@777 274 // large as "this". Modifies "this" to be the intersection of its
ysr@777 275 // current contents and the submap of "bits" starting at "offset" of the
ysr@777 276 // same length as "this."
ysr@777 277 // (For expedience, currently requires the offset to be aligned to the
ysr@777 278 // bitsize of a uintptr_t. This should go away in the future though it
ysr@777 279 // will probably remain a good case to optimize.)
ysr@777 280 void set_intersection_at_offset(BitMap bits, idx_t offset);
ysr@777 281
duke@435 282 void set_from(BitMap bits);
duke@435 283
duke@435 284 bool is_same(BitMap bits);
duke@435 285
duke@435 286 // Test if all bits are set or cleared
duke@435 287 bool is_full() const;
duke@435 288 bool is_empty() const;
duke@435 289
duke@435 290
duke@435 291 #ifndef PRODUCT
duke@435 292 public:
duke@435 293 // Printing
duke@435 294 void print_on(outputStream* st) const;
duke@435 295 #endif
duke@435 296 };
duke@435 297
duke@435 298 // Convenience class wrapping BitMap which provides multiple bits per slot.
duke@435 299 class BitMap2D VALUE_OBJ_CLASS_SPEC {
duke@435 300 public:
ysr@777 301 typedef BitMap::idx_t idx_t; // Type used for bit and word indices.
ysr@777 302 typedef BitMap::bm_word_t bm_word_t; // Element type of array that
ysr@777 303 // represents the bitmap.
duke@435 304 private:
duke@435 305 BitMap _map;
duke@435 306 idx_t _bits_per_slot;
duke@435 307
duke@435 308 idx_t bit_index(idx_t slot_index, idx_t bit_within_slot_index) const {
duke@435 309 return slot_index * _bits_per_slot + bit_within_slot_index;
duke@435 310 }
duke@435 311
duke@435 312 void verify_bit_within_slot_index(idx_t index) const {
duke@435 313 assert(index < _bits_per_slot, "bit_within_slot index out of bounds");
duke@435 314 }
duke@435 315
duke@435 316 public:
duke@435 317 // Construction. bits_per_slot must be greater than 0.
ysr@777 318 BitMap2D(bm_word_t* map, idx_t size_in_slots, idx_t bits_per_slot);
duke@435 319
duke@435 320 // Allocates necessary data structure in resource area. bits_per_slot must be greater than 0.
duke@435 321 BitMap2D(idx_t size_in_slots, idx_t bits_per_slot);
duke@435 322
duke@435 323 idx_t size_in_bits() {
duke@435 324 return _map.size();
duke@435 325 }
duke@435 326
duke@435 327 // Returns number of full slots that have been allocated
duke@435 328 idx_t size_in_slots() {
duke@435 329 // Round down
duke@435 330 return _map.size() / _bits_per_slot;
duke@435 331 }
duke@435 332
duke@435 333 bool is_valid_index(idx_t slot_index, idx_t bit_within_slot_index) {
duke@435 334 verify_bit_within_slot_index(bit_within_slot_index);
duke@435 335 return (bit_index(slot_index, bit_within_slot_index) < size_in_bits());
duke@435 336 }
duke@435 337
duke@435 338 bool at(idx_t slot_index, idx_t bit_within_slot_index) const {
duke@435 339 verify_bit_within_slot_index(bit_within_slot_index);
duke@435 340 return _map.at(bit_index(slot_index, bit_within_slot_index));
duke@435 341 }
duke@435 342
duke@435 343 void set_bit(idx_t slot_index, idx_t bit_within_slot_index) {
duke@435 344 verify_bit_within_slot_index(bit_within_slot_index);
duke@435 345 _map.set_bit(bit_index(slot_index, bit_within_slot_index));
duke@435 346 }
duke@435 347
duke@435 348 void clear_bit(idx_t slot_index, idx_t bit_within_slot_index) {
duke@435 349 verify_bit_within_slot_index(bit_within_slot_index);
duke@435 350 _map.clear_bit(bit_index(slot_index, bit_within_slot_index));
duke@435 351 }
duke@435 352
duke@435 353 void at_put(idx_t slot_index, idx_t bit_within_slot_index, bool value) {
duke@435 354 verify_bit_within_slot_index(bit_within_slot_index);
duke@435 355 _map.at_put(bit_index(slot_index, bit_within_slot_index), value);
duke@435 356 }
duke@435 357
duke@435 358 void at_put_grow(idx_t slot_index, idx_t bit_within_slot_index, bool value) {
duke@435 359 verify_bit_within_slot_index(bit_within_slot_index);
duke@435 360 _map.at_put_grow(bit_index(slot_index, bit_within_slot_index), value);
duke@435 361 }
duke@435 362
ysr@777 363 void clear();
duke@435 364 };
duke@435 365
ysr@777 366 // Closure for iterating over BitMaps
duke@435 367
ysr@777 368 class BitMapClosure VALUE_OBJ_CLASS_SPEC {
ysr@777 369 public:
ysr@777 370 // Callback when bit in map is set. Should normally return "true";
ysr@777 371 // return of false indicates that the bitmap iteration should terminate.
ysr@777 372 virtual bool do_bit(BitMap::idx_t offset) = 0;
ysr@777 373 };
stefank@2314 374
stefank@2314 375 #endif // SHARE_VM_UTILITIES_BITMAP_HPP

mercurial