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