bitMap.hpp revision 11048:43f859c2d61c
16059Samurai/* 26059Samurai * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved. 36059Samurai * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 46059Samurai * 56059Samurai * This code is free software; you can redistribute it and/or modify it 66059Samurai * under the terms of the GNU General Public License version 2 only, as 76059Samurai * published by the Free Software Foundation. 86059Samurai * 96059Samurai * This code is distributed in the hope that it will be useful, but WITHOUT 106059Samurai * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 116059Samurai * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 126059Samurai * version 2 for more details (a copy is included in the LICENSE file that 136059Samurai * accompanied this code). 146059Samurai * 156059Samurai * You should have received a copy of the GNU General Public License version 166059Samurai * 2 along with this work; if not, write to the Free Software Foundation, 176059Samurai * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 186059Samurai * 196059Samurai * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2023114Sbrian * or visit www.oracle.com if you need additional information or have any 218857Srgrimes * questions. 226059Samurai * 236059Samurai */ 246059Samurai 256059Samurai#ifndef SHARE_VM_UTILITIES_BITMAP_HPP 266059Samurai#define SHARE_VM_UTILITIES_BITMAP_HPP 276059Samurai 2811336Samurai#include "memory/allocation.hpp" 296059Samurai 306059Samurai// Forward decl; 3123114Sbrianclass BitMapClosure; 3223114Sbrian 3322911Sbrian// Operations for bitmaps represented as arrays of unsigned integers. 346059Samurai// Bit offsets are numbered from 0 to size-1. 356059Samurai 366059Samuraiclass BitMap VALUE_OBJ_CLASS_SPEC { 3718786Sjkh friend class BitMap2D; 386059Samurai 396059Samurai public: 4020365Sjkh typedef size_t idx_t; // Type used for bit and word indices. 4120365Sjkh typedef uintptr_t bm_word_t; // Element type of array that represents 426059Samurai // the bitmap. 436059Samurai 446059Samurai // Hints for range sizes. 4513389Sphk typedef enum { 466059Samurai unknown_range, small_range, large_range 476059Samurai } RangeSizeHint; 486059Samurai 496735Samurai private: 507001Samurai bm_word_t* _map; // First word in bitmap 5113389Sphk idx_t _size; // Size of bitmap (in bits) 5213389Sphk 5320365Sjkh // Puts the given value at the given offset, using resize() to size 546059Samurai // the bitmap appropriately if needed using factor-of-two expansion. 556764Samurai void at_put_grow(idx_t index, bool value); 569410Sasami 576764Samurai protected: 586735Samurai // Return the position of bit within the word that contains it (e.g., if 596735Samurai // bitmap words are 32 bits, return a number 0 <= n <= 31). 606735Samurai static idx_t bit_in_word(idx_t bit) { return bit & (BitsPerWord - 1); } 616735Samurai 626735Samurai // Return a mask that will select the specified bit, when applied to the word 636735Samurai // containing the bit. 646059Samurai static bm_word_t bit_mask(idx_t bit) { return (bm_word_t)1 << bit_in_word(bit); } 656059Samurai 666059Samurai // Return the index of the word containing the specified bit. 676059Samurai static idx_t word_index(idx_t bit) { return bit >> LogBitsPerWord; } 686059Samurai 6918885Sjkh // Return the bit number of the first bit in the specified word. 706059Samurai static idx_t bit_index(idx_t word) { return word << LogBitsPerWord; } 716059Samurai 726059Samurai // Return the array of bitmap words, or a specific word from it. 7310528Samurai bm_word_t* map() { return _map; } 746059Samurai const bm_word_t* map() const { return _map; } 756059Samurai bm_word_t map(idx_t word) const { return _map[word]; } 766059Samurai 7714418Sache // Return a pointer to the word containing the specified bit. 7813379Sphk bm_word_t* word_addr(idx_t bit) { return map() + word_index(bit); } 7920813Sjkh const bm_word_t* word_addr(idx_t bit) const { return map() + word_index(bit); } 806059Samurai 8111336Samurai // Set a word to a specified value or to all ones; clear a word. 826059Samurai void set_word (idx_t word, bm_word_t val) { _map[word] = val; } 836059Samurai void set_word (idx_t word) { set_word(word, ~(bm_word_t)0); } 846059Samurai void clear_word(idx_t word) { _map[word] = 0; } 856059Samurai 866059Samurai // Utilities for ranges of bits. Ranges are half-open [beg, end). 876059Samurai 886059Samurai // Ranges within a single word. 896059Samurai bm_word_t inverted_bit_mask_for_range(idx_t beg, idx_t end) const; 906059Samurai void set_range_within_word (idx_t beg, idx_t end); 916059Samurai void clear_range_within_word (idx_t beg, idx_t end); 926059Samurai void par_put_range_within_word (idx_t beg, idx_t end, bool value); 936059Samurai 946059Samurai // Ranges spanning entire words. 956059Samurai void set_range_of_words (idx_t beg, idx_t end); 966059Samurai void clear_range_of_words (idx_t beg, idx_t end); 976059Samurai void set_large_range_of_words (idx_t beg, idx_t end); 986059Samurai void clear_large_range_of_words (idx_t beg, idx_t end); 996059Samurai 1006059Samurai // The index of the first full word in a range. 1016735Samurai idx_t word_index_round_up(idx_t bit) const; 1026059Samurai 1036059Samurai // Verification. 1046059Samurai void verify_index(idx_t index) const NOT_DEBUG_RETURN; 1056059Samurai void verify_range(idx_t beg_index, idx_t end_index) const NOT_DEBUG_RETURN; 1066059Samurai 1076059Samurai // Statistics. 10810528Samurai static idx_t* _pop_count_table; 10910528Samurai static void init_pop_count_table(); 11010528Samurai static idx_t num_set_bits(bm_word_t w); 1116059Samurai static idx_t num_set_bits_from_table(unsigned char c); 1126059Samurai 1136059Samurai public: 1146059Samurai 1156059Samurai // Constructs a bitmap with no map, and size 0. 1166059Samurai BitMap() : _map(NULL), _size(0) {} 1176735Samurai 11810528Samurai // Constructs a bitmap with the given map and size. 1196059Samurai BitMap(bm_word_t* map, idx_t size_in_bits) :_map(map), _size(size_in_bits) {} 1206059Samurai 1216735Samurai // Constructs an empty bitmap of the given size (that is, this clears the 1226059Samurai // new bitmap). Allocates the map array in resource area if 1236059Samurai // "in_resource_area" is true, else in the C heap. 1246059Samurai BitMap(idx_t size_in_bits, bool in_resource_area = true); 1256059Samurai 12610528Samurai // Set the map and size. 1276059Samurai void set_map(bm_word_t* map) { _map = map; } 1286059Samurai void set_size(idx_t size_in_bits) { _size = size_in_bits; } 1296059Samurai 1306059Samurai // Allocates necessary data structure, either in the resource area 1316059Samurai // or in the C heap, as indicated by "in_resource_area." 1326059Samurai // Preserves state currently in bit map by copying data. 1336059Samurai // Zeros any newly-addressable bits. 1346059Samurai // If "in_resource_area" is false, frees the current map. 1356059Samurai // (Note that this assumes that all calls to "resize" on the same BitMap 1366059Samurai // use the same value for "in_resource_area".) 1376735Samurai void resize(idx_t size_in_bits, bool in_resource_area = true); 1386059Samurai 1396059Samurai // Pretouch the entire range of memory this BitMap covers. 1406059Samurai void pretouch(); 1416059Samurai 1426059Samurai // Accessing 1436059Samurai idx_t size() const { return _size; } 1446059Samurai idx_t size_in_bytes() const { return size_in_words() * BytesPerWord; } 14510528Samurai idx_t size_in_words() const { 1466059Samurai return calc_size_in_words(size()); 1476059Samurai } 1486059Samurai 1496059Samurai static idx_t calc_size_in_words(size_t size_in_bits) { 1506059Samurai return word_index(size_in_bits + BitsPerWord - 1); 1516059Samurai } 1526735Samurai 15310528Samurai bool at(idx_t index) const { 15410528Samurai verify_index(index); 15510528Samurai return (*word_addr(index) & bit_mask(index)) != 0; 15610528Samurai } 15710528Samurai 15810528Samurai // Align bit index up or down to the next bitmap word boundary, or check 15910528Samurai // alignment. 16010528Samurai static idx_t word_align_up(idx_t bit) { 1616059Samurai return align_size_up(bit, BitsPerWord); 1626059Samurai } 16311336Samurai static idx_t word_align_down(idx_t bit) { 1646059Samurai return align_size_down(bit, BitsPerWord); 16520813Sjkh } 16620813Sjkh static bool is_word_aligned(idx_t bit) { 16711336Samurai return word_align_up(bit) == bit; 16811336Samurai } 1696059Samurai 17015738Sphk // Set or clear the specified bit. 1716059Samurai inline void set_bit(idx_t bit); 1726059Samurai inline void clear_bit(idx_t bit); 1736059Samurai 17410528Samurai // Atomically set or clear the specified bit. 1756059Samurai inline bool par_set_bit(idx_t bit); 1766059Samurai inline bool par_clear_bit(idx_t bit); 1776059Samurai 1786059Samurai // Put the given value at the given offset. The parallel version 1796059Samurai // will CAS the value into the bitmap and is quite a bit slower. 18014930Sache // The parallel version also returns a value indicating if the 18114930Sache // calling thread was the one that changed the value of the bit. 1826059Samurai void at_put(idx_t index, bool value); 18317044Sache bool par_at_put(idx_t index, bool value); 18417044Sache 18517044Sache // Update a range of bits. Ranges are half-open [beg, end). 18617044Sache void set_range (idx_t beg, idx_t end); 18717044Sache void clear_range (idx_t beg, idx_t end); 18820813Sjkh void set_large_range (idx_t beg, idx_t end); 18920813Sjkh void clear_large_range (idx_t beg, idx_t end); 19020813Sjkh void at_put_range(idx_t beg, idx_t end, bool value); 19120813Sjkh void par_at_put_range(idx_t beg, idx_t end, bool value); 19220813Sjkh void at_put_large_range(idx_t beg, idx_t end, bool value); 19320813Sjkh void par_at_put_large_range(idx_t beg, idx_t end, bool value); 19420813Sjkh 19520813Sjkh // Update a range of bits, using a hint about the size. Currently only 1966059Samurai // inlines the predominant case of a 1-bit range. Works best when hint is a 1976059Samurai // compile-time constant. 1986059Samurai void set_range(idx_t beg, idx_t end, RangeSizeHint hint); 19914930Sache void clear_range(idx_t beg, idx_t end, RangeSizeHint hint); 20014930Sache void par_set_range(idx_t beg, idx_t end, RangeSizeHint hint); 2016059Samurai void par_clear_range (idx_t beg, idx_t end, RangeSizeHint hint); 20220813Sjkh 20320813Sjkh // Clearing 20420813Sjkh void clear_large(); 20520813Sjkh inline void clear(); 20620813Sjkh 20720813Sjkh // Iteration support. Returns "true" if the iteration completed, false 20820813Sjkh // if the iteration terminated early (because the closure "blk" returned 20920813Sjkh // false). 21020813Sjkh bool iterate(BitMapClosure* blk, idx_t leftIndex, idx_t rightIndex); 2116059Samurai bool iterate(BitMapClosure* blk) { 2126059Samurai // call the version that takes an interval 21310528Samurai return iterate(blk, 0, size()); 21410528Samurai } 21510528Samurai 21622911Sbrian // Looking for 1's and 0's at indices equal to or greater than "l_index", 21722911Sbrian // stopping if none has been found before "r_index", and returning 21810528Samurai // "r_index" (which must be at most "size") in that case. 21910528Samurai idx_t get_next_one_offset_inline (idx_t l_index, idx_t r_index) const; 22010528Samurai idx_t get_next_zero_offset_inline(idx_t l_index, idx_t r_index) const; 22110528Samurai 22210528Samurai // Like "get_next_one_offset_inline", except requires that "r_index" is 22310528Samurai // aligned to bitsizeof(bm_word_t). 22410528Samurai idx_t get_next_one_offset_inline_aligned_right(idx_t l_index, 22522911Sbrian idx_t r_index) const; 22610528Samurai 22722911Sbrian // Non-inline versionsof the above. 22810528Samurai idx_t get_next_one_offset (idx_t l_index, idx_t r_index) const; 22910528Samurai idx_t get_next_zero_offset(idx_t l_index, idx_t r_index) const; 23010528Samurai 23110528Samurai idx_t get_next_one_offset(idx_t offset) const { 2326059Samurai return get_next_one_offset(offset, size()); 2336059Samurai } 2346059Samurai idx_t get_next_zero_offset(idx_t offset) const { 23520120Snate return get_next_zero_offset(offset, size()); 23620813Sjkh } 2376059Samurai 2386059Samurai // Returns the number of bits set in the bitmap. 2396059Samurai idx_t count_one_bits() const; 2406059Samurai 2416059Samurai // Set operations. 2426059Samurai void set_union(const BitMap& bits); 2436059Samurai void set_difference(const BitMap& bits); 2446059Samurai void set_intersection(const BitMap& bits); 2456059Samurai // Returns true iff "this" is a superset of "bits". 2466059Samurai bool contains(const BitMap& bits) const; 2476059Samurai // Returns true iff "this and "bits" have a non-empty intersection. 2486059Samurai bool intersects(const BitMap& bits) const; 2496059Samurai 2506059Samurai // Returns result of whether this map changed 25120813Sjkh // during the operation 25220813Sjkh bool set_union_with_result(const BitMap& bits); 2536059Samurai bool set_difference_with_result(const BitMap& bits); 2546059Samurai bool set_intersection_with_result(const BitMap& bits); 2556059Samurai 2566059Samurai // Requires the submap of "bits" starting at offset to be at least as 25720120Snate // large as "this". Modifies "this" to be the intersection of its 25820120Snate // current contents and the submap of "bits" starting at "offset" of the 25920365Sjkh // same length as "this." 26020365Sjkh // (For expedience, currently requires the offset to be aligned to the 26120365Sjkh // bitsize of a uintptr_t. This should go away in the future though it 26220365Sjkh // will probably remain a good case to optimize.) 2636059Samurai void set_intersection_at_offset(const BitMap& bits, idx_t offset); 2646059Samurai 2656059Samurai void set_from(const BitMap& bits); 2666059Samurai 2676059Samurai bool is_same(const BitMap& bits); 2686059Samurai 2696059Samurai // Test if all bits are set or cleared 2706059Samurai bool is_full() const; 2716059Samurai bool is_empty() const; 2726059Samurai 2736059Samurai void print_on_error(outputStream* st, const char* prefix) const; 2746059Samurai 2756059Samurai#ifndef PRODUCT 2766059Samurai public: 2776059Samurai // Printing 2786059Samurai void print_on(outputStream* st) const; 2796059Samurai#endif 2806059Samurai}; 2816059Samurai 2826059Samurai// Convenience class wrapping BitMap which provides multiple bits per slot. 2836059Samuraiclass BitMap2D VALUE_OBJ_CLASS_SPEC { 2846059Samurai public: 2856059Samurai typedef BitMap::idx_t idx_t; // Type used for bit and word indices. 2866059Samurai typedef BitMap::bm_word_t bm_word_t; // Element type of array that 2876059Samurai // represents the bitmap. 2886059Samurai private: 2896059Samurai BitMap _map; 2906059Samurai idx_t _bits_per_slot; 2916059Samurai 2926059Samurai idx_t bit_index(idx_t slot_index, idx_t bit_within_slot_index) const { 2936059Samurai return slot_index * _bits_per_slot + bit_within_slot_index; 2946059Samurai } 2956059Samurai 2966059Samurai void verify_bit_within_slot_index(idx_t index) const { 2976059Samurai assert(index < _bits_per_slot, "bit_within_slot index out of bounds"); 2986059Samurai } 2996059Samurai 3006059Samurai public: 3016059Samurai // Construction. bits_per_slot must be greater than 0. 30220365Sjkh BitMap2D(bm_word_t* map, idx_t size_in_slots, idx_t bits_per_slot); 3036059Samurai 3046059Samurai // Allocates necessary data structure in resource area. bits_per_slot must be greater than 0. 3056059Samurai BitMap2D(idx_t size_in_slots, idx_t bits_per_slot); 3066059Samurai 3076059Samurai idx_t size_in_bits() { 3086059Samurai return _map.size(); 3096059Samurai } 3106059Samurai 3116735Samurai // Returns number of full slots that have been allocated 3126735Samurai idx_t size_in_slots() { 3136764Samurai // Round down 3146764Samurai return _map.size() / _bits_per_slot; 3156764Samurai } 3166764Samurai 3176764Samurai bool is_valid_index(idx_t slot_index, idx_t bit_within_slot_index); 3186735Samurai bool at(idx_t slot_index, idx_t bit_within_slot_index) const; 3196735Samurai void set_bit(idx_t slot_index, idx_t bit_within_slot_index); 3206735Samurai void clear_bit(idx_t slot_index, idx_t bit_within_slot_index); 3216735Samurai void at_put(idx_t slot_index, idx_t bit_within_slot_index, bool value); 3226735Samurai void at_put_grow(idx_t slot_index, idx_t bit_within_slot_index, bool value); 3236735Samurai void clear(); 3246059Samurai}; 3256059Samurai 3266059Samurai// Closure for iterating over BitMaps 3276059Samurai 3286059Samuraiclass BitMapClosure VALUE_OBJ_CLASS_SPEC { 32920813Sjkh public: 3306059Samurai // Callback when bit in map is set. Should normally return "true"; 3316059Samurai // return of false indicates that the bitmap iteration should terminate. 3326059Samurai virtual bool do_bit(BitMap::idx_t offset) = 0; 3336059Samurai}; 3346059Samurai 33520120Snate#endif // SHARE_VM_UTILITIES_BITMAP_HPP 3366059Samurai