duke@435: /* duke@435: * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: // Closure for iterating over BitMaps duke@435: duke@435: class BitMapClosure VALUE_OBJ_CLASS_SPEC { duke@435: public: duke@435: // Callback when bit in map is set duke@435: virtual void do_bit(size_t offset) = 0; duke@435: }; duke@435: duke@435: duke@435: // Operations for bitmaps represented as arrays of unsigned 32- or 64-bit duke@435: // integers (uintptr_t). duke@435: // duke@435: // Bit offsets are numbered from 0 to size-1 duke@435: duke@435: class BitMap VALUE_OBJ_CLASS_SPEC { duke@435: friend class BitMap2D; duke@435: duke@435: public: duke@435: typedef size_t idx_t; // Type used for bit and word indices. duke@435: duke@435: // Hints for range sizes. duke@435: typedef enum { duke@435: unknown_range, small_range, large_range duke@435: } RangeSizeHint; duke@435: duke@435: private: duke@435: idx_t* _map; // First word in bitmap duke@435: idx_t _size; // Size of bitmap (in bits) duke@435: duke@435: // Puts the given value at the given offset, using resize() to size duke@435: // the bitmap appropriately if needed using factor-of-two expansion. duke@435: void at_put_grow(idx_t index, bool value); duke@435: duke@435: protected: duke@435: // Return the position of bit within the word that contains it (e.g., if duke@435: // bitmap words are 32 bits, return a number 0 <= n <= 31). duke@435: static idx_t bit_in_word(idx_t bit) { return bit & (BitsPerWord - 1); } duke@435: duke@435: // Return a mask that will select the specified bit, when applied to the word duke@435: // containing the bit. duke@435: static idx_t bit_mask(idx_t bit) { return (idx_t)1 << bit_in_word(bit); } duke@435: duke@435: // Return the index of the word containing the specified bit. duke@435: static idx_t word_index(idx_t bit) { return bit >> LogBitsPerWord; } duke@435: duke@435: // Return the bit number of the first bit in the specified word. duke@435: static idx_t bit_index(idx_t word) { return word << LogBitsPerWord; } duke@435: duke@435: // Return the array of bitmap words, or a specific word from it. duke@435: idx_t* map() const { return _map; } duke@435: idx_t map(idx_t word) const { return _map[word]; } duke@435: duke@435: // Return a pointer to the word containing the specified bit. duke@435: idx_t* word_addr(idx_t bit) const { return map() + word_index(bit); } duke@435: duke@435: // Set a word to a specified value or to all ones; clear a word. duke@435: void set_word (idx_t word, idx_t val) { _map[word] = val; } duke@435: void set_word (idx_t word) { set_word(word, ~(uintptr_t)0); } duke@435: void clear_word(idx_t word) { _map[word] = 0; } duke@435: duke@435: // Utilities for ranges of bits. Ranges are half-open [beg, end). duke@435: duke@435: // Ranges within a single word. duke@435: inline idx_t inverted_bit_mask_for_range(idx_t beg, idx_t end) const; duke@435: inline void set_range_within_word (idx_t beg, idx_t end); duke@435: inline void clear_range_within_word (idx_t beg, idx_t end); duke@435: inline void par_put_range_within_word (idx_t beg, idx_t end, bool value); duke@435: duke@435: // Ranges spanning entire words. duke@435: inline void set_range_of_words (idx_t beg, idx_t end); duke@435: inline void clear_range_of_words (idx_t beg, idx_t end); duke@435: inline void set_large_range_of_words (idx_t beg, idx_t end); duke@435: inline void clear_large_range_of_words (idx_t beg, idx_t end); duke@435: duke@435: // The index of the first full word in a range. duke@435: inline idx_t word_index_round_up(idx_t bit) const; duke@435: duke@435: // Verification, statistics. duke@435: void verify_index(idx_t index) const { duke@435: assert(index < _size, "BitMap index out of bounds"); duke@435: } duke@435: duke@435: void verify_range(idx_t beg_index, idx_t end_index) const { duke@435: #ifdef ASSERT duke@435: assert(beg_index <= end_index, "BitMap range error"); duke@435: // Note that [0,0) and [size,size) are both valid ranges. duke@435: if (end_index != _size) verify_index(end_index); duke@435: #endif duke@435: } duke@435: duke@435: public: duke@435: duke@435: // Constructs a bitmap with no map, and size 0. duke@435: BitMap() : _map(NULL), _size(0) {} duke@435: duke@435: // Construction duke@435: BitMap(idx_t* map, idx_t size_in_bits); duke@435: duke@435: // Allocates necessary data structure in resource area duke@435: BitMap(idx_t size_in_bits); duke@435: duke@435: void set_map(idx_t* map) { _map = map; } duke@435: void set_size(idx_t size_in_bits) { _size = size_in_bits; } duke@435: duke@435: // Allocates necessary data structure in resource area. duke@435: // Preserves state currently in bit map by copying data. duke@435: // Zeros any newly-addressable bits. duke@435: // Does not perform any frees (i.e., of current _map). duke@435: void resize(idx_t size_in_bits); duke@435: duke@435: // Accessing duke@435: idx_t size() const { return _size; } duke@435: idx_t size_in_words() const { duke@435: return word_index(size() + BitsPerWord - 1); duke@435: } duke@435: duke@435: bool at(idx_t index) const { duke@435: verify_index(index); duke@435: return (*word_addr(index) & bit_mask(index)) != 0; duke@435: } duke@435: duke@435: // Align bit index up or down to the next bitmap word boundary, or check duke@435: // alignment. duke@435: static idx_t word_align_up(idx_t bit) { duke@435: return align_size_up(bit, BitsPerWord); duke@435: } duke@435: static idx_t word_align_down(idx_t bit) { duke@435: return align_size_down(bit, BitsPerWord); duke@435: } duke@435: static bool is_word_aligned(idx_t bit) { duke@435: return word_align_up(bit) == bit; duke@435: } duke@435: duke@435: // Set or clear the specified bit. duke@435: inline void set_bit(idx_t bit); duke@435: inline void clear_bit(idx_t bit); duke@435: duke@435: // Atomically set or clear the specified bit. duke@435: inline bool par_set_bit(idx_t bit); duke@435: inline bool par_clear_bit(idx_t bit); duke@435: duke@435: // Put the given value at the given offset. The parallel version duke@435: // will CAS the value into the bitmap and is quite a bit slower. duke@435: // The parallel version also returns a value indicating if the duke@435: // calling thread was the one that changed the value of the bit. duke@435: void at_put(idx_t index, bool value); duke@435: bool par_at_put(idx_t index, bool value); duke@435: duke@435: // Update a range of bits. Ranges are half-open [beg, end). duke@435: void set_range (idx_t beg, idx_t end); duke@435: void clear_range (idx_t beg, idx_t end); duke@435: void set_large_range (idx_t beg, idx_t end); duke@435: void clear_large_range (idx_t beg, idx_t end); duke@435: void at_put_range(idx_t beg, idx_t end, bool value); duke@435: void par_at_put_range(idx_t beg, idx_t end, bool value); duke@435: void at_put_large_range(idx_t beg, idx_t end, bool value); duke@435: void par_at_put_large_range(idx_t beg, idx_t end, bool value); duke@435: duke@435: // Update a range of bits, using a hint about the size. Currently only duke@435: // inlines the predominant case of a 1-bit range. Works best when hint is a duke@435: // compile-time constant. duke@435: inline void set_range(idx_t beg, idx_t end, RangeSizeHint hint); duke@435: inline void clear_range(idx_t beg, idx_t end, RangeSizeHint hint); duke@435: inline void par_set_range(idx_t beg, idx_t end, RangeSizeHint hint); duke@435: inline void par_clear_range (idx_t beg, idx_t end, RangeSizeHint hint); duke@435: duke@435: // Clearing duke@435: void clear(); duke@435: void clear_large(); duke@435: duke@435: // Iteration support duke@435: void iterate(BitMapClosure* blk, idx_t leftIndex, idx_t rightIndex); duke@435: inline void iterate(BitMapClosure* blk) { duke@435: // call the version that takes an interval duke@435: iterate(blk, 0, size()); duke@435: } duke@435: duke@435: // Looking for 1's and 0's to the "right" duke@435: idx_t get_next_one_offset (idx_t l_index, idx_t r_index) const; duke@435: idx_t get_next_zero_offset(idx_t l_index, idx_t r_index) const; duke@435: duke@435: idx_t get_next_one_offset(idx_t offset) const { duke@435: return get_next_one_offset(offset, size()); duke@435: } duke@435: idx_t get_next_zero_offset(idx_t offset) const { duke@435: return get_next_zero_offset(offset, size()); duke@435: } duke@435: duke@435: duke@435: duke@435: // Find the next one bit in the range [beg_bit, end_bit), or return end_bit if duke@435: // no one bit is found. Equivalent to get_next_one_offset(), but inline for duke@435: // use in performance-critical code. duke@435: inline idx_t find_next_one_bit(idx_t beg_bit, idx_t end_bit) const; duke@435: duke@435: // Set operations. duke@435: void set_union(BitMap bits); duke@435: void set_difference(BitMap bits); duke@435: void set_intersection(BitMap bits); duke@435: // Returns true iff "this" is a superset of "bits". duke@435: bool contains(const BitMap bits) const; duke@435: // Returns true iff "this and "bits" have a non-empty intersection. duke@435: bool intersects(const BitMap bits) const; duke@435: duke@435: // Returns result of whether this map changed duke@435: // during the operation duke@435: bool set_union_with_result(BitMap bits); duke@435: bool set_difference_with_result(BitMap bits); duke@435: bool set_intersection_with_result(BitMap bits); duke@435: duke@435: void set_from(BitMap bits); duke@435: duke@435: bool is_same(BitMap bits); duke@435: duke@435: // Test if all bits are set or cleared duke@435: bool is_full() const; duke@435: bool is_empty() const; duke@435: duke@435: duke@435: #ifndef PRODUCT duke@435: public: duke@435: // Printing duke@435: void print_on(outputStream* st) const; duke@435: #endif duke@435: }; duke@435: duke@435: inline void BitMap::set_bit(idx_t bit) { duke@435: verify_index(bit); duke@435: *word_addr(bit) |= bit_mask(bit); duke@435: } duke@435: duke@435: inline void BitMap::clear_bit(idx_t bit) { duke@435: verify_index(bit); duke@435: *word_addr(bit) &= ~bit_mask(bit); duke@435: } duke@435: duke@435: inline void BitMap::set_range(idx_t beg, idx_t end, RangeSizeHint hint) { duke@435: if (hint == small_range && end - beg == 1) { duke@435: set_bit(beg); duke@435: } else { duke@435: if (hint == large_range) { duke@435: set_large_range(beg, end); duke@435: } else { duke@435: set_range(beg, end); duke@435: } duke@435: } duke@435: } duke@435: duke@435: inline void BitMap::clear_range(idx_t beg, idx_t end, RangeSizeHint hint) { duke@435: if (hint == small_range && end - beg == 1) { duke@435: clear_bit(beg); duke@435: } else { duke@435: if (hint == large_range) { duke@435: clear_large_range(beg, end); duke@435: } else { duke@435: clear_range(beg, end); duke@435: } duke@435: } duke@435: } duke@435: duke@435: inline void BitMap::par_set_range(idx_t beg, idx_t end, RangeSizeHint hint) { duke@435: if (hint == small_range && end - beg == 1) { duke@435: par_at_put(beg, true); duke@435: } else { duke@435: if (hint == large_range) { duke@435: par_at_put_large_range(beg, end, true); duke@435: } else { duke@435: par_at_put_range(beg, end, true); duke@435: } duke@435: } duke@435: } duke@435: duke@435: duke@435: // Convenience class wrapping BitMap which provides multiple bits per slot. duke@435: class BitMap2D VALUE_OBJ_CLASS_SPEC { duke@435: public: duke@435: typedef size_t idx_t; // Type used for bit and word indices. duke@435: duke@435: private: duke@435: BitMap _map; duke@435: idx_t _bits_per_slot; duke@435: duke@435: idx_t bit_index(idx_t slot_index, idx_t bit_within_slot_index) const { duke@435: return slot_index * _bits_per_slot + bit_within_slot_index; duke@435: } duke@435: duke@435: void verify_bit_within_slot_index(idx_t index) const { duke@435: assert(index < _bits_per_slot, "bit_within_slot index out of bounds"); duke@435: } duke@435: duke@435: public: duke@435: // Construction. bits_per_slot must be greater than 0. duke@435: BitMap2D(uintptr_t* map, idx_t size_in_slots, idx_t bits_per_slot); duke@435: duke@435: // Allocates necessary data structure in resource area. bits_per_slot must be greater than 0. duke@435: BitMap2D(idx_t size_in_slots, idx_t bits_per_slot); duke@435: duke@435: idx_t size_in_bits() { duke@435: return _map.size(); duke@435: } duke@435: duke@435: // Returns number of full slots that have been allocated duke@435: idx_t size_in_slots() { duke@435: // Round down duke@435: return _map.size() / _bits_per_slot; duke@435: } duke@435: duke@435: bool is_valid_index(idx_t slot_index, idx_t bit_within_slot_index) { duke@435: verify_bit_within_slot_index(bit_within_slot_index); duke@435: return (bit_index(slot_index, bit_within_slot_index) < size_in_bits()); duke@435: } duke@435: duke@435: bool at(idx_t slot_index, idx_t bit_within_slot_index) const { duke@435: verify_bit_within_slot_index(bit_within_slot_index); duke@435: return _map.at(bit_index(slot_index, bit_within_slot_index)); duke@435: } duke@435: duke@435: void set_bit(idx_t slot_index, idx_t bit_within_slot_index) { duke@435: verify_bit_within_slot_index(bit_within_slot_index); duke@435: _map.set_bit(bit_index(slot_index, bit_within_slot_index)); duke@435: } duke@435: duke@435: void clear_bit(idx_t slot_index, idx_t bit_within_slot_index) { duke@435: verify_bit_within_slot_index(bit_within_slot_index); duke@435: _map.clear_bit(bit_index(slot_index, bit_within_slot_index)); duke@435: } duke@435: duke@435: void at_put(idx_t slot_index, idx_t bit_within_slot_index, bool value) { duke@435: verify_bit_within_slot_index(bit_within_slot_index); duke@435: _map.at_put(bit_index(slot_index, bit_within_slot_index), value); duke@435: } duke@435: duke@435: void at_put_grow(idx_t slot_index, idx_t bit_within_slot_index, bool value) { duke@435: verify_bit_within_slot_index(bit_within_slot_index); duke@435: _map.at_put_grow(bit_index(slot_index, bit_within_slot_index), value); duke@435: } duke@435: duke@435: void clear() { duke@435: _map.clear(); duke@435: } duke@435: }; duke@435: duke@435: duke@435: duke@435: inline void BitMap::set_range_of_words(idx_t beg, idx_t end) { duke@435: uintptr_t* map = _map; duke@435: for (idx_t i = beg; i < end; ++i) map[i] = ~(uintptr_t)0; duke@435: } duke@435: duke@435: duke@435: inline void BitMap::clear_range_of_words(idx_t beg, idx_t end) { duke@435: uintptr_t* map = _map; duke@435: for (idx_t i = beg; i < end; ++i) map[i] = 0; duke@435: } duke@435: duke@435: duke@435: inline void BitMap::clear() { duke@435: clear_range_of_words(0, size_in_words()); duke@435: } duke@435: duke@435: duke@435: inline void BitMap::par_clear_range(idx_t beg, idx_t end, RangeSizeHint hint) { duke@435: if (hint == small_range && end - beg == 1) { duke@435: par_at_put(beg, false); duke@435: } else { duke@435: if (hint == large_range) { duke@435: par_at_put_large_range(beg, end, false); duke@435: } else { duke@435: par_at_put_range(beg, end, false); duke@435: } duke@435: } duke@435: }