Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/utilities/bitMap.hpp
32285 views
/*1* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_VM_UTILITIES_BITMAP_HPP25#define SHARE_VM_UTILITIES_BITMAP_HPP2627#include "memory/allocation.hpp"28#include "utilities/top.hpp"2930// Forward decl;31class BitMapClosure;3233// Operations for bitmaps represented as arrays of unsigned integers.34// Bit offsets are numbered from 0 to size-1.3536class BitMap VALUE_OBJ_CLASS_SPEC {37friend class BitMap2D;3839public:40typedef size_t idx_t; // Type used for bit and word indices.41typedef uintptr_t bm_word_t; // Element type of array that represents42// the bitmap.4344// Hints for range sizes.45typedef enum {46unknown_range, small_range, large_range47} RangeSizeHint;4849private:50ArrayAllocator<bm_word_t, mtInternal> _map_allocator;51bm_word_t* _map; // First word in bitmap52idx_t _size; // Size of bitmap (in bits)5354// Puts the given value at the given offset, using resize() to size55// the bitmap appropriately if needed using factor-of-two expansion.56void at_put_grow(idx_t index, bool value);5758// Threshold for performing small range operation, even when large range59// operation was requested. Measured in words.60static const size_t small_range_words = 32;6162protected:63// Return the position of bit within the word that contains it (e.g., if64// bitmap words are 32 bits, return a number 0 <= n <= 31).65static idx_t bit_in_word(idx_t bit) { return bit & (BitsPerWord - 1); }6667// Return a mask that will select the specified bit, when applied to the word68// containing the bit.69static bm_word_t bit_mask(idx_t bit) { return (bm_word_t)1 << bit_in_word(bit); }7071// Return the index of the word containing the specified bit.72static idx_t word_index(idx_t bit) { return bit >> LogBitsPerWord; }7374// Return the bit number of the first bit in the specified word.75static idx_t bit_index(idx_t word) { return word << LogBitsPerWord; }7677// Return the array of bitmap words, or a specific word from it.78bm_word_t* map() const { return _map; }79bm_word_t map(idx_t word) const { return _map[word]; }8081// Return a pointer to the word containing the specified bit.82bm_word_t* word_addr(idx_t bit) const { return map() + word_index(bit); }8384// Set a word to a specified value or to all ones; clear a word.85void set_word (idx_t word, bm_word_t val) { _map[word] = val; }86void set_word (idx_t word) { set_word(word, ~(uintptr_t)0); }87void clear_word(idx_t word) { _map[word] = 0; }8889// Utilities for ranges of bits. Ranges are half-open [beg, end).9091// Ranges within a single word.92bm_word_t inverted_bit_mask_for_range(idx_t beg, idx_t end) const;93void set_range_within_word (idx_t beg, idx_t end);94void clear_range_within_word (idx_t beg, idx_t end);95void par_put_range_within_word (idx_t beg, idx_t end, bool value);9697// Ranges spanning entire words.98void set_range_of_words (idx_t beg, idx_t end);99void clear_range_of_words (idx_t beg, idx_t end);100void set_large_range_of_words (idx_t beg, idx_t end);101void clear_large_range_of_words (idx_t beg, idx_t end);102103static bool is_small_range_of_words(idx_t beg_full_word, idx_t end_full_word);104105// The index of the first full word in a range.106idx_t word_index_round_up(idx_t bit) const;107108// Verification.109inline void verify_index(idx_t index) const NOT_DEBUG_RETURN;110inline void verify_range(idx_t beg_index, idx_t end_index) const111NOT_DEBUG_RETURN;112113// Statistics.114static idx_t* _pop_count_table;115static void init_pop_count_table();116static idx_t num_set_bits(bm_word_t w);117static idx_t num_set_bits_from_table(unsigned char c);118119public:120121// Constructs a bitmap with no map, and size 0.122BitMap() : _map(NULL), _size(0), _map_allocator(false) {}123124// Constructs a bitmap with the given map and size.125BitMap(bm_word_t* map, idx_t size_in_bits);126127// Constructs an empty bitmap of the given size (that is, this clears the128// new bitmap). Allocates the map array in resource area if129// "in_resource_area" is true, else in the C heap.130BitMap(idx_t size_in_bits, bool in_resource_area = true);131132// Set the map and size.133void set_map(bm_word_t* map) { _map = map; }134void set_size(idx_t size_in_bits) { _size = size_in_bits; }135136// Allocates necessary data structure, either in the resource area137// or in the C heap, as indicated by "in_resource_area."138// Preserves state currently in bit map by copying data.139// Zeros any newly-addressable bits.140// If "in_resource_area" is false, frees the current map.141// (Note that this assumes that all calls to "resize" on the same BitMap142// use the same value for "in_resource_area".)143void resize(idx_t size_in_bits, bool in_resource_area = true);144145// Accessing146idx_t size() const { return _size; }147idx_t size_in_words() const {148return word_index(size() + BitsPerWord - 1);149}150151bool at(idx_t index) const {152verify_index(index);153return (*word_addr(index) & bit_mask(index)) != 0;154}155156// Align bit index up or down to the next bitmap word boundary, or check157// alignment.158static idx_t word_align_up(idx_t bit) {159return align_size_up(bit, BitsPerWord);160}161static idx_t word_align_down(idx_t bit) {162return align_size_down(bit, BitsPerWord);163}164static bool is_word_aligned(idx_t bit) {165return word_align_up(bit) == bit;166}167168// Set or clear the specified bit.169inline void set_bit(idx_t bit);170inline void clear_bit(idx_t bit);171172// Atomically set or clear the specified bit.173inline bool par_set_bit(idx_t bit);174inline bool par_clear_bit(idx_t bit);175176// Put the given value at the given offset. The parallel version177// will CAS the value into the bitmap and is quite a bit slower.178// The parallel version also returns a value indicating if the179// calling thread was the one that changed the value of the bit.180void at_put(idx_t index, bool value);181bool par_at_put(idx_t index, bool value);182183// Update a range of bits. Ranges are half-open [beg, end).184void set_range (idx_t beg, idx_t end);185void clear_range (idx_t beg, idx_t end);186void set_large_range (idx_t beg, idx_t end);187void clear_large_range (idx_t beg, idx_t end);188void at_put_range(idx_t beg, idx_t end, bool value);189void par_at_put_range(idx_t beg, idx_t end, bool value);190void at_put_large_range(idx_t beg, idx_t end, bool value);191void par_at_put_large_range(idx_t beg, idx_t end, bool value);192193// Update a range of bits, using a hint about the size. Currently only194// inlines the predominant case of a 1-bit range. Works best when hint is a195// compile-time constant.196void set_range(idx_t beg, idx_t end, RangeSizeHint hint);197void clear_range(idx_t beg, idx_t end, RangeSizeHint hint);198void par_set_range(idx_t beg, idx_t end, RangeSizeHint hint);199void par_clear_range (idx_t beg, idx_t end, RangeSizeHint hint);200201// Clearing202void clear_large();203inline void clear();204205// Iteration support. Returns "true" if the iteration completed, false206// if the iteration terminated early (because the closure "blk" returned207// false).208bool iterate(BitMapClosure* blk, idx_t leftIndex, idx_t rightIndex);209bool iterate(BitMapClosure* blk) {210// call the version that takes an interval211return iterate(blk, 0, size());212}213214// 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 returning216// "r_index" (which must be at most "size") in that case.217idx_t get_next_one_offset_inline (idx_t l_index, idx_t r_index) const;218idx_t get_next_zero_offset_inline(idx_t l_index, idx_t r_index) const;219220// Like "get_next_one_offset_inline", except requires that "r_index" is221// aligned to bitsizeof(bm_word_t).222idx_t get_next_one_offset_inline_aligned_right(idx_t l_index,223idx_t r_index) const;224225// Non-inline versionsof the above.226idx_t get_next_one_offset (idx_t l_index, idx_t r_index) const;227idx_t get_next_zero_offset(idx_t l_index, idx_t r_index) const;228229idx_t get_next_one_offset(idx_t offset) const {230return get_next_one_offset(offset, size());231}232idx_t get_next_zero_offset(idx_t offset) const {233return get_next_zero_offset(offset, size());234}235236// Returns the number of bits set in the bitmap.237idx_t count_one_bits() const;238239// Set operations.240void set_union(BitMap bits);241void set_difference(BitMap bits);242void set_intersection(BitMap bits);243// Returns true iff "this" is a superset of "bits".244bool contains(const BitMap bits) const;245// Returns true iff "this and "bits" have a non-empty intersection.246bool intersects(const BitMap bits) const;247248// Returns result of whether this map changed249// during the operation250bool set_union_with_result(BitMap bits);251bool set_difference_with_result(BitMap bits);252bool set_intersection_with_result(BitMap bits);253254// Requires the submap of "bits" starting at offset to be at least as255// large as "this". Modifies "this" to be the intersection of its256// current contents and the submap of "bits" starting at "offset" of the257// same length as "this."258// (For expedience, currently requires the offset to be aligned to the259// bitsize of a uintptr_t. This should go away in the future though it260// will probably remain a good case to optimize.)261void set_intersection_at_offset(BitMap bits, idx_t offset);262263void set_from(BitMap bits);264265bool is_same(BitMap bits);266267// Test if all bits are set or cleared268bool is_full() const;269bool is_empty() const;270271void print_on_error(outputStream* st, const char* prefix) const;272273#ifndef PRODUCT274public:275// Printing276void print_on(outputStream* st) const;277#endif278};279280// Convenience class wrapping BitMap which provides multiple bits per slot.281class BitMap2D VALUE_OBJ_CLASS_SPEC {282public:283typedef BitMap::idx_t idx_t; // Type used for bit and word indices.284typedef BitMap::bm_word_t bm_word_t; // Element type of array that285// represents the bitmap.286private:287BitMap _map;288idx_t _bits_per_slot;289290idx_t bit_index(idx_t slot_index, idx_t bit_within_slot_index) const {291return slot_index * _bits_per_slot + bit_within_slot_index;292}293294void verify_bit_within_slot_index(idx_t index) const {295assert(index < _bits_per_slot, "bit_within_slot index out of bounds");296}297298public:299// Construction. bits_per_slot must be greater than 0.300BitMap2D(bm_word_t* map, idx_t size_in_slots, idx_t bits_per_slot);301302// Allocates necessary data structure in resource area. bits_per_slot must be greater than 0.303BitMap2D(idx_t size_in_slots, idx_t bits_per_slot);304305idx_t size_in_bits() {306return _map.size();307}308309// Returns number of full slots that have been allocated310idx_t size_in_slots() {311// Round down312return _map.size() / _bits_per_slot;313}314315bool is_valid_index(idx_t slot_index, idx_t bit_within_slot_index) {316verify_bit_within_slot_index(bit_within_slot_index);317return (bit_index(slot_index, bit_within_slot_index) < size_in_bits());318}319320bool at(idx_t slot_index, idx_t bit_within_slot_index) const {321verify_bit_within_slot_index(bit_within_slot_index);322return _map.at(bit_index(slot_index, bit_within_slot_index));323}324325void set_bit(idx_t slot_index, idx_t bit_within_slot_index) {326verify_bit_within_slot_index(bit_within_slot_index);327_map.set_bit(bit_index(slot_index, bit_within_slot_index));328}329330void clear_bit(idx_t slot_index, idx_t bit_within_slot_index) {331verify_bit_within_slot_index(bit_within_slot_index);332_map.clear_bit(bit_index(slot_index, bit_within_slot_index));333}334335void at_put(idx_t slot_index, idx_t bit_within_slot_index, bool value) {336verify_bit_within_slot_index(bit_within_slot_index);337_map.at_put(bit_index(slot_index, bit_within_slot_index), value);338}339340void at_put_grow(idx_t slot_index, idx_t bit_within_slot_index, bool value) {341verify_bit_within_slot_index(bit_within_slot_index);342_map.at_put_grow(bit_index(slot_index, bit_within_slot_index), value);343}344345void clear();346};347348// Closure for iterating over BitMaps349350class BitMapClosure VALUE_OBJ_CLASS_SPEC {351public:352// Callback when bit in map is set. Should normally return "true";353// return of false indicates that the bitmap iteration should terminate.354virtual bool do_bit(BitMap::idx_t offset) = 0;355};356357#endif // SHARE_VM_UTILITIES_BITMAP_HPP358359360