aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #ifndef SHARE_VM_UTILITIES_BITMAP_HPP aoqi@0: #define SHARE_VM_UTILITIES_BITMAP_HPP aoqi@0: aoqi@0: #include "memory/allocation.hpp" aoqi@0: #include "utilities/top.hpp" aoqi@0: aoqi@0: // Forward decl; aoqi@0: class BitMapClosure; aoqi@0: aoqi@0: // Operations for bitmaps represented as arrays of unsigned integers. aoqi@0: // Bit offsets are numbered from 0 to size-1. aoqi@0: aoqi@0: class BitMap VALUE_OBJ_CLASS_SPEC { aoqi@0: friend class BitMap2D; aoqi@0: aoqi@0: public: aoqi@0: typedef size_t idx_t; // Type used for bit and word indices. aoqi@0: typedef uintptr_t bm_word_t; // Element type of array that represents aoqi@0: // the bitmap. aoqi@0: aoqi@0: // Hints for range sizes. aoqi@0: typedef enum { aoqi@0: unknown_range, small_range, large_range aoqi@0: } RangeSizeHint; aoqi@0: aoqi@0: private: aoqi@0: ArrayAllocator _map_allocator; aoqi@0: bm_word_t* _map; // First word in bitmap aoqi@0: idx_t _size; // Size of bitmap (in bits) aoqi@0: aoqi@0: // Puts the given value at the given offset, using resize() to size aoqi@0: // the bitmap appropriately if needed using factor-of-two expansion. aoqi@0: void at_put_grow(idx_t index, bool value); aoqi@0: aoqi@0: protected: aoqi@0: // Return the position of bit within the word that contains it (e.g., if aoqi@0: // bitmap words are 32 bits, return a number 0 <= n <= 31). aoqi@0: static idx_t bit_in_word(idx_t bit) { return bit & (BitsPerWord - 1); } aoqi@0: aoqi@0: // Return a mask that will select the specified bit, when applied to the word aoqi@0: // containing the bit. aoqi@0: static bm_word_t bit_mask(idx_t bit) { return (bm_word_t)1 << bit_in_word(bit); } aoqi@0: aoqi@0: // Return the index of the word containing the specified bit. aoqi@0: static idx_t word_index(idx_t bit) { return bit >> LogBitsPerWord; } aoqi@0: aoqi@0: // Return the bit number of the first bit in the specified word. aoqi@0: static idx_t bit_index(idx_t word) { return word << LogBitsPerWord; } aoqi@0: aoqi@0: // Return the array of bitmap words, or a specific word from it. aoqi@0: bm_word_t* map() const { return _map; } aoqi@0: bm_word_t map(idx_t word) const { return _map[word]; } aoqi@0: aoqi@0: // Return a pointer to the word containing the specified bit. aoqi@0: bm_word_t* word_addr(idx_t bit) const { return map() + word_index(bit); } aoqi@0: aoqi@0: // Set a word to a specified value or to all ones; clear a word. aoqi@0: void set_word (idx_t word, bm_word_t val) { _map[word] = val; } aoqi@0: void set_word (idx_t word) { set_word(word, ~(uintptr_t)0); } aoqi@0: void clear_word(idx_t word) { _map[word] = 0; } aoqi@0: aoqi@0: // Utilities for ranges of bits. Ranges are half-open [beg, end). aoqi@0: aoqi@0: // Ranges within a single word. aoqi@0: bm_word_t inverted_bit_mask_for_range(idx_t beg, idx_t end) const; aoqi@0: void set_range_within_word (idx_t beg, idx_t end); aoqi@0: void clear_range_within_word (idx_t beg, idx_t end); aoqi@0: void par_put_range_within_word (idx_t beg, idx_t end, bool value); aoqi@0: aoqi@0: // Ranges spanning entire words. aoqi@0: void set_range_of_words (idx_t beg, idx_t end); aoqi@0: void clear_range_of_words (idx_t beg, idx_t end); aoqi@0: void set_large_range_of_words (idx_t beg, idx_t end); aoqi@0: void clear_large_range_of_words (idx_t beg, idx_t end); aoqi@0: aoqi@0: // The index of the first full word in a range. aoqi@0: idx_t word_index_round_up(idx_t bit) const; aoqi@0: aoqi@0: // Verification. aoqi@0: inline void verify_index(idx_t index) const NOT_DEBUG_RETURN; aoqi@0: inline void verify_range(idx_t beg_index, idx_t end_index) const aoqi@0: NOT_DEBUG_RETURN; aoqi@0: aoqi@0: // Statistics. aoqi@0: static idx_t* _pop_count_table; aoqi@0: static void init_pop_count_table(); aoqi@0: static idx_t num_set_bits(bm_word_t w); aoqi@0: static idx_t num_set_bits_from_table(unsigned char c); aoqi@0: aoqi@0: public: aoqi@0: aoqi@0: // Constructs a bitmap with no map, and size 0. aoqi@0: BitMap() : _map(NULL), _size(0), _map_allocator(false) {} aoqi@0: aoqi@0: // Constructs a bitmap with the given map and size. aoqi@0: BitMap(bm_word_t* map, idx_t size_in_bits); aoqi@0: aoqi@0: // Constructs an empty bitmap of the given size (that is, this clears the aoqi@0: // new bitmap). Allocates the map array in resource area if aoqi@0: // "in_resource_area" is true, else in the C heap. aoqi@0: BitMap(idx_t size_in_bits, bool in_resource_area = true); aoqi@0: aoqi@0: // Set the map and size. aoqi@0: void set_map(bm_word_t* map) { _map = map; } aoqi@0: void set_size(idx_t size_in_bits) { _size = size_in_bits; } aoqi@0: aoqi@0: // Allocates necessary data structure, either in the resource area aoqi@0: // or in the C heap, as indicated by "in_resource_area." aoqi@0: // Preserves state currently in bit map by copying data. aoqi@0: // Zeros any newly-addressable bits. aoqi@0: // If "in_resource_area" is false, frees the current map. aoqi@0: // (Note that this assumes that all calls to "resize" on the same BitMap aoqi@0: // use the same value for "in_resource_area".) aoqi@0: void resize(idx_t size_in_bits, bool in_resource_area = true); aoqi@0: aoqi@0: // Accessing aoqi@0: idx_t size() const { return _size; } aoqi@0: idx_t size_in_words() const { aoqi@0: return word_index(size() + BitsPerWord - 1); aoqi@0: } aoqi@0: aoqi@0: bool at(idx_t index) const { aoqi@0: verify_index(index); aoqi@0: return (*word_addr(index) & bit_mask(index)) != 0; aoqi@0: } aoqi@0: aoqi@0: // Align bit index up or down to the next bitmap word boundary, or check aoqi@0: // alignment. aoqi@0: static idx_t word_align_up(idx_t bit) { aoqi@0: return align_size_up(bit, BitsPerWord); aoqi@0: } aoqi@0: static idx_t word_align_down(idx_t bit) { aoqi@0: return align_size_down(bit, BitsPerWord); aoqi@0: } aoqi@0: static bool is_word_aligned(idx_t bit) { aoqi@0: return word_align_up(bit) == bit; aoqi@0: } aoqi@0: aoqi@0: // Set or clear the specified bit. aoqi@0: inline void set_bit(idx_t bit); aoqi@0: inline void clear_bit(idx_t bit); aoqi@0: aoqi@0: // Atomically set or clear the specified bit. aoqi@0: inline bool par_set_bit(idx_t bit); aoqi@0: inline bool par_clear_bit(idx_t bit); aoqi@0: aoqi@0: // Put the given value at the given offset. The parallel version aoqi@0: // will CAS the value into the bitmap and is quite a bit slower. aoqi@0: // The parallel version also returns a value indicating if the aoqi@0: // calling thread was the one that changed the value of the bit. aoqi@0: void at_put(idx_t index, bool value); aoqi@0: bool par_at_put(idx_t index, bool value); aoqi@0: aoqi@0: // Update a range of bits. Ranges are half-open [beg, end). aoqi@0: void set_range (idx_t beg, idx_t end); aoqi@0: void clear_range (idx_t beg, idx_t end); aoqi@0: void set_large_range (idx_t beg, idx_t end); aoqi@0: void clear_large_range (idx_t beg, idx_t end); aoqi@0: void at_put_range(idx_t beg, idx_t end, bool value); aoqi@0: void par_at_put_range(idx_t beg, idx_t end, bool value); aoqi@0: void at_put_large_range(idx_t beg, idx_t end, bool value); aoqi@0: void par_at_put_large_range(idx_t beg, idx_t end, bool value); aoqi@0: aoqi@0: // Update a range of bits, using a hint about the size. Currently only aoqi@0: // inlines the predominant case of a 1-bit range. Works best when hint is a aoqi@0: // compile-time constant. aoqi@0: void set_range(idx_t beg, idx_t end, RangeSizeHint hint); aoqi@0: void clear_range(idx_t beg, idx_t end, RangeSizeHint hint); aoqi@0: void par_set_range(idx_t beg, idx_t end, RangeSizeHint hint); aoqi@0: void par_clear_range (idx_t beg, idx_t end, RangeSizeHint hint); aoqi@0: aoqi@0: // Clearing aoqi@0: void clear_large(); aoqi@0: inline void clear(); aoqi@0: aoqi@0: // Iteration support. Returns "true" if the iteration completed, false aoqi@0: // if the iteration terminated early (because the closure "blk" returned aoqi@0: // false). aoqi@0: bool iterate(BitMapClosure* blk, idx_t leftIndex, idx_t rightIndex); aoqi@0: bool iterate(BitMapClosure* blk) { aoqi@0: // call the version that takes an interval aoqi@0: return iterate(blk, 0, size()); aoqi@0: } aoqi@0: aoqi@0: // Looking for 1's and 0's at indices equal to or greater than "l_index", aoqi@0: // stopping if none has been found before "r_index", and returning aoqi@0: // "r_index" (which must be at most "size") in that case. aoqi@0: idx_t get_next_one_offset_inline (idx_t l_index, idx_t r_index) const; aoqi@0: idx_t get_next_zero_offset_inline(idx_t l_index, idx_t r_index) const; aoqi@0: aoqi@0: // Like "get_next_one_offset_inline", except requires that "r_index" is aoqi@0: // aligned to bitsizeof(bm_word_t). aoqi@0: idx_t get_next_one_offset_inline_aligned_right(idx_t l_index, aoqi@0: idx_t r_index) const; aoqi@0: aoqi@0: // Non-inline versionsof the above. aoqi@0: idx_t get_next_one_offset (idx_t l_index, idx_t r_index) const; aoqi@0: idx_t get_next_zero_offset(idx_t l_index, idx_t r_index) const; aoqi@0: aoqi@0: idx_t get_next_one_offset(idx_t offset) const { aoqi@0: return get_next_one_offset(offset, size()); aoqi@0: } aoqi@0: idx_t get_next_zero_offset(idx_t offset) const { aoqi@0: return get_next_zero_offset(offset, size()); aoqi@0: } aoqi@0: aoqi@0: // Returns the number of bits set in the bitmap. aoqi@0: idx_t count_one_bits() const; aoqi@0: aoqi@0: // Set operations. aoqi@0: void set_union(BitMap bits); aoqi@0: void set_difference(BitMap bits); aoqi@0: void set_intersection(BitMap bits); aoqi@0: // Returns true iff "this" is a superset of "bits". aoqi@0: bool contains(const BitMap bits) const; aoqi@0: // Returns true iff "this and "bits" have a non-empty intersection. aoqi@0: bool intersects(const BitMap bits) const; aoqi@0: aoqi@0: // Returns result of whether this map changed aoqi@0: // during the operation aoqi@0: bool set_union_with_result(BitMap bits); aoqi@0: bool set_difference_with_result(BitMap bits); aoqi@0: bool set_intersection_with_result(BitMap bits); aoqi@0: aoqi@0: // Requires the submap of "bits" starting at offset to be at least as aoqi@0: // large as "this". Modifies "this" to be the intersection of its aoqi@0: // current contents and the submap of "bits" starting at "offset" of the aoqi@0: // same length as "this." aoqi@0: // (For expedience, currently requires the offset to be aligned to the aoqi@0: // bitsize of a uintptr_t. This should go away in the future though it aoqi@0: // will probably remain a good case to optimize.) aoqi@0: void set_intersection_at_offset(BitMap bits, idx_t offset); aoqi@0: aoqi@0: void set_from(BitMap bits); aoqi@0: aoqi@0: bool is_same(BitMap bits); aoqi@0: aoqi@0: // Test if all bits are set or cleared aoqi@0: bool is_full() const; aoqi@0: bool is_empty() const; aoqi@0: aoqi@0: void print_on_error(outputStream* st, const char* prefix) const; aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: public: aoqi@0: // Printing aoqi@0: void print_on(outputStream* st) const; aoqi@0: #endif aoqi@0: }; aoqi@0: aoqi@0: // Convenience class wrapping BitMap which provides multiple bits per slot. aoqi@0: class BitMap2D VALUE_OBJ_CLASS_SPEC { aoqi@0: public: aoqi@0: typedef BitMap::idx_t idx_t; // Type used for bit and word indices. aoqi@0: typedef BitMap::bm_word_t bm_word_t; // Element type of array that aoqi@0: // represents the bitmap. aoqi@0: private: aoqi@0: BitMap _map; aoqi@0: idx_t _bits_per_slot; aoqi@0: aoqi@0: idx_t bit_index(idx_t slot_index, idx_t bit_within_slot_index) const { aoqi@0: return slot_index * _bits_per_slot + bit_within_slot_index; aoqi@0: } aoqi@0: aoqi@0: void verify_bit_within_slot_index(idx_t index) const { aoqi@0: assert(index < _bits_per_slot, "bit_within_slot index out of bounds"); aoqi@0: } aoqi@0: aoqi@0: public: aoqi@0: // Construction. bits_per_slot must be greater than 0. aoqi@0: BitMap2D(bm_word_t* map, idx_t size_in_slots, idx_t bits_per_slot); aoqi@0: aoqi@0: // Allocates necessary data structure in resource area. bits_per_slot must be greater than 0. aoqi@0: BitMap2D(idx_t size_in_slots, idx_t bits_per_slot); aoqi@0: aoqi@0: idx_t size_in_bits() { aoqi@0: return _map.size(); aoqi@0: } aoqi@0: aoqi@0: // Returns number of full slots that have been allocated aoqi@0: idx_t size_in_slots() { aoqi@0: // Round down aoqi@0: return _map.size() / _bits_per_slot; aoqi@0: } aoqi@0: aoqi@0: bool is_valid_index(idx_t slot_index, idx_t bit_within_slot_index) { aoqi@0: verify_bit_within_slot_index(bit_within_slot_index); aoqi@0: return (bit_index(slot_index, bit_within_slot_index) < size_in_bits()); aoqi@0: } aoqi@0: aoqi@0: bool at(idx_t slot_index, idx_t bit_within_slot_index) const { aoqi@0: verify_bit_within_slot_index(bit_within_slot_index); aoqi@0: return _map.at(bit_index(slot_index, bit_within_slot_index)); aoqi@0: } aoqi@0: aoqi@0: void set_bit(idx_t slot_index, idx_t bit_within_slot_index) { aoqi@0: verify_bit_within_slot_index(bit_within_slot_index); aoqi@0: _map.set_bit(bit_index(slot_index, bit_within_slot_index)); aoqi@0: } aoqi@0: aoqi@0: void clear_bit(idx_t slot_index, idx_t bit_within_slot_index) { aoqi@0: verify_bit_within_slot_index(bit_within_slot_index); aoqi@0: _map.clear_bit(bit_index(slot_index, bit_within_slot_index)); aoqi@0: } aoqi@0: aoqi@0: void at_put(idx_t slot_index, idx_t bit_within_slot_index, bool value) { aoqi@0: verify_bit_within_slot_index(bit_within_slot_index); aoqi@0: _map.at_put(bit_index(slot_index, bit_within_slot_index), value); aoqi@0: } aoqi@0: aoqi@0: void at_put_grow(idx_t slot_index, idx_t bit_within_slot_index, bool value) { aoqi@0: verify_bit_within_slot_index(bit_within_slot_index); aoqi@0: _map.at_put_grow(bit_index(slot_index, bit_within_slot_index), value); aoqi@0: } aoqi@0: aoqi@0: void clear(); aoqi@0: }; aoqi@0: aoqi@0: // Closure for iterating over BitMaps aoqi@0: aoqi@0: class BitMapClosure VALUE_OBJ_CLASS_SPEC { aoqi@0: public: aoqi@0: // Callback when bit in map is set. Should normally return "true"; aoqi@0: // return of false indicates that the bitmap iteration should terminate. aoqi@0: virtual bool do_bit(BitMap::idx_t offset) = 0; aoqi@0: }; aoqi@0: aoqi@0: #endif // SHARE_VM_UTILITIES_BITMAP_HPP