1 /*
   2  * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_UTILITIES_BITMAP_HPP
  26 #define SHARE_VM_UTILITIES_BITMAP_HPP
  27 
  28 #include "memory/allocation.hpp"
  29 #include "utilities/top.hpp"
  30 
  31 // Forward decl;
  32 class BitMapClosure;
  33 
  34 // Operations for bitmaps represented as arrays of unsigned integers.
  35 // Bit offsets are numbered from 0 to size-1.
  36 
  37 class BitMap VALUE_OBJ_CLASS_SPEC {
  38   friend class BitMap2D;
  39 
  40  public:
  41   typedef size_t idx_t;         // Type used for bit and word indices.
  42   typedef uintptr_t bm_word_t;  // Element type of array that represents
  43                                 // the bitmap.
  44 
  45   // Hints for range sizes.
  46   typedef enum {
  47     unknown_range, small_range, large_range
  48   } RangeSizeHint;
  49 
  50  private:
  51   ArrayAllocator<bm_word_t, mtInternal> _map_allocator;
  52   bm_word_t* _map;     // First word in bitmap
  53   idx_t      _size;    // Size of bitmap (in bits)
  54 
  55   // Puts the given value at the given offset, using resize() to size
  56   // the bitmap appropriately if needed using factor-of-two expansion.
  57   void at_put_grow(idx_t index, bool value);
  58 
  59  protected:
  60   // Return the position of bit within the word that contains it (e.g., if
  61   // bitmap words are 32 bits, return a number 0 <= n <= 31).
  62   static idx_t bit_in_word(idx_t bit) { return bit & (BitsPerWord - 1); }
  63 
  64   // Return a mask that will select the specified bit, when applied to the word
  65   // containing the bit.
  66   static bm_word_t bit_mask(idx_t bit) { return (bm_word_t)1 << bit_in_word(bit); }
  67 
  68   // Return the index of the word containing the specified bit.
  69   static idx_t word_index(idx_t bit)  { return bit >> LogBitsPerWord; }
  70 
  71   // Return the bit number of the first bit in the specified word.
  72   static idx_t bit_index(idx_t word)  { return word << LogBitsPerWord; }
  73 
  74   // Return the array of bitmap words, or a specific word from it.
  75   bm_word_t* map() const           { return _map; }
  76   bm_word_t  map(idx_t word) const { return _map[word]; }
  77 
  78   // Return a pointer to the word containing the specified bit.
  79   bm_word_t* word_addr(idx_t bit) const { return map() + word_index(bit); }
  80 
  81   // Set a word to a specified value or to all ones; clear a word.
  82   void set_word  (idx_t word, bm_word_t val) { _map[word] = val; }
  83   void set_word  (idx_t word)            { set_word(word, ~(uintptr_t)0); }
  84   void clear_word(idx_t word)            { _map[word] = 0; }
  85 
  86   // Utilities for ranges of bits.  Ranges are half-open [beg, end).
  87 
  88   // Ranges within a single word.
  89   bm_word_t inverted_bit_mask_for_range(idx_t beg, idx_t end) const;
  90   void  set_range_within_word      (idx_t beg, idx_t end);
  91   void  clear_range_within_word    (idx_t beg, idx_t end);
  92   void  par_put_range_within_word  (idx_t beg, idx_t end, bool value);
  93 
  94   // Ranges spanning entire words.
  95   void      set_range_of_words         (idx_t beg, idx_t end);
  96   void      clear_range_of_words       (idx_t beg, idx_t end);
  97   void      set_large_range_of_words   (idx_t beg, idx_t end);
  98   void      clear_large_range_of_words (idx_t beg, idx_t end);
  99 
 100   // The index of the first full word in a range.
 101   idx_t word_index_round_up(idx_t bit) const;
 102 
 103   // Verification.
 104   inline void verify_index(idx_t index) const NOT_DEBUG_RETURN;
 105   inline void verify_range(idx_t beg_index, idx_t end_index) const
 106     NOT_DEBUG_RETURN;
 107 
 108   // Statistics.
 109   static idx_t* _pop_count_table;
 110   static void init_pop_count_table();
 111   static idx_t num_set_bits(bm_word_t w);
 112   static idx_t num_set_bits_from_table(unsigned char c);
 113 
 114  public:
 115 
 116   // Constructs a bitmap with no map, and size 0.
 117   BitMap() : _map(NULL), _size(0), _map_allocator(false) {}
 118 
 119   // Constructs a bitmap with the given map and size.
 120   BitMap(bm_word_t* map, idx_t size_in_bits);
 121 
 122   // Constructs an empty bitmap of the given size (that is, this clears the
 123   // new bitmap).  Allocates the map array in resource area if
 124   // "in_resource_area" is true, else in the C heap.
 125   BitMap(idx_t size_in_bits, bool in_resource_area = true);
 126 
 127   // Set the map and size.
 128   void set_map(bm_word_t* map)      { _map = map; }
 129   void set_size(idx_t size_in_bits) { _size = size_in_bits; }
 130 
 131   // Allocates necessary data structure, either in the resource area
 132   // or in the C heap, as indicated by "in_resource_area."
 133   // Preserves state currently in bit map by copying data.
 134   // Zeros any newly-addressable bits.
 135   // If "in_resource_area" is false, frees the current map.
 136   // (Note that this assumes that all calls to "resize" on the same BitMap
 137   // use the same value for "in_resource_area".)
 138   void resize(idx_t size_in_bits, bool in_resource_area = true);
 139 
 140   // Accessing
 141   idx_t size() const                    { return _size; }
 142   idx_t size_in_words() const           {
 143     return word_index(size() + BitsPerWord - 1);
 144   }
 145 
 146   bool at(idx_t index) const {
 147     verify_index(index);
 148     return (*word_addr(index) & bit_mask(index)) != 0;
 149   }
 150 
 151   // Align bit index up or down to the next bitmap word boundary, or check
 152   // alignment.
 153   static idx_t word_align_up(idx_t bit) {
 154     return align_size_up(bit, BitsPerWord);
 155   }
 156   static idx_t word_align_down(idx_t bit) {
 157     return align_size_down(bit, BitsPerWord);
 158   }
 159   static bool is_word_aligned(idx_t bit) {
 160     return word_align_up(bit) == bit;
 161   }
 162 
 163   // Set or clear the specified bit.
 164   inline void set_bit(idx_t bit);
 165   inline void clear_bit(idx_t bit);
 166 
 167   // Atomically set or clear the specified bit.
 168   inline bool par_set_bit(idx_t bit);
 169   inline bool par_clear_bit(idx_t bit);
 170 
 171   // Put the given value at the given offset. The parallel version
 172   // will CAS the value into the bitmap and is quite a bit slower.
 173   // The parallel version also returns a value indicating if the
 174   // calling thread was the one that changed the value of the bit.
 175   void at_put(idx_t index, bool value);
 176   bool par_at_put(idx_t index, bool value);
 177 
 178   // Update a range of bits.  Ranges are half-open [beg, end).
 179   void set_range   (idx_t beg, idx_t end);
 180   void clear_range (idx_t beg, idx_t end);
 181   void set_large_range   (idx_t beg, idx_t end);
 182   void clear_large_range (idx_t beg, idx_t end);
 183   void at_put_range(idx_t beg, idx_t end, bool value);
 184   void par_at_put_range(idx_t beg, idx_t end, bool value);
 185   void at_put_large_range(idx_t beg, idx_t end, bool value);
 186   void par_at_put_large_range(idx_t beg, idx_t end, bool value);
 187 
 188   // Update a range of bits, using a hint about the size.  Currently only
 189   // inlines the predominant case of a 1-bit range.  Works best when hint is a
 190   // compile-time constant.
 191   void set_range(idx_t beg, idx_t end, RangeSizeHint hint);
 192   void clear_range(idx_t beg, idx_t end, RangeSizeHint hint);
 193   void par_set_range(idx_t beg, idx_t end, RangeSizeHint hint);
 194   void par_clear_range  (idx_t beg, idx_t end, RangeSizeHint hint);
 195 
 196   // Clearing
 197   void clear_large();
 198   inline void clear();
 199 
 200   // Iteration support.  Returns "true" if the iteration completed, false
 201   // if the iteration terminated early (because the closure "blk" returned
 202   // false).
 203   bool iterate(BitMapClosure* blk, idx_t leftIndex, idx_t rightIndex);
 204   bool iterate(BitMapClosure* blk) {
 205     // call the version that takes an interval
 206     return iterate(blk, 0, size());
 207   }
 208 
 209   // Looking for 1's and 0's at indices equal to or greater than "l_index",
 210   // stopping if none has been found before "r_index", and returning
 211   // "r_index" (which must be at most "size") in that case.
 212   idx_t get_next_one_offset_inline (idx_t l_index, idx_t r_index) const;
 213   idx_t get_next_zero_offset_inline(idx_t l_index, idx_t r_index) const;
 214 
 215   // Like "get_next_one_offset_inline", except requires that "r_index" is
 216   // aligned to bitsizeof(bm_word_t).
 217   idx_t get_next_one_offset_inline_aligned_right(idx_t l_index,
 218                                                         idx_t r_index) const;
 219 
 220   // Non-inline versionsof the above.
 221   idx_t get_next_one_offset (idx_t l_index, idx_t r_index) const;
 222   idx_t get_next_zero_offset(idx_t l_index, idx_t r_index) const;
 223 
 224   idx_t get_next_one_offset(idx_t offset) const {
 225     return get_next_one_offset(offset, size());
 226   }
 227   idx_t get_next_zero_offset(idx_t offset) const {
 228     return get_next_zero_offset(offset, size());
 229   }
 230 
 231   // Returns the number of bits set in the bitmap.
 232   idx_t count_one_bits() const;
 233 
 234   // Set operations.
 235   void set_union(BitMap bits);
 236   void set_difference(BitMap bits);
 237   void set_intersection(BitMap bits);
 238   // Returns true iff "this" is a superset of "bits".
 239   bool contains(const BitMap bits) const;
 240   // Returns true iff "this and "bits" have a non-empty intersection.
 241   bool intersects(const BitMap bits) const;
 242 
 243   // Returns result of whether this map changed
 244   // during the operation
 245   bool set_union_with_result(BitMap bits);
 246   bool set_difference_with_result(BitMap bits);
 247   bool set_intersection_with_result(BitMap bits);
 248 
 249   // Requires the submap of "bits" starting at offset to be at least as
 250   // large as "this".  Modifies "this" to be the intersection of its
 251   // current contents and the submap of "bits" starting at "offset" of the
 252   // same length as "this."
 253   // (For expedience, currently requires the offset to be aligned to the
 254   // bitsize of a uintptr_t.  This should go away in the future though it
 255   // will probably remain a good case to optimize.)
 256   void set_intersection_at_offset(BitMap bits, idx_t offset);
 257 
 258   void set_from(BitMap bits);
 259 
 260   bool is_same(BitMap bits);
 261 
 262   // Test if all bits are set or cleared
 263   bool is_full() const;
 264   bool is_empty() const;
 265 
 266   void print_on_error(outputStream* st, const char* prefix) const;
 267 
 268 #ifndef PRODUCT
 269  public:
 270   // Printing
 271   void print_on(outputStream* st) const;
 272 #endif
 273 };
 274 
 275 // A concrete implementation of the the "abstract" BitMap class.
 276 //
 277 // The BitMapView is used when the backing storage is managed externally.
 278 class BitMapView : public BitMap {
 279  public:
 280   BitMapView() : BitMap((bm_word_t*)NULL, (idx_t)0) {}
 281   BitMapView(bm_word_t* map, idx_t size_in_bits) : BitMap(map, size_in_bits) {}
 282 };
 283 
 284 // Convenience class wrapping BitMap which provides multiple bits per slot.
 285 class BitMap2D VALUE_OBJ_CLASS_SPEC {
 286  public:
 287   typedef BitMap::idx_t idx_t;          // Type used for bit and word indices.
 288   typedef BitMap::bm_word_t bm_word_t;  // Element type of array that
 289                                         // represents the bitmap.
 290  private:
 291   BitMap _map;
 292   idx_t  _bits_per_slot;
 293 
 294   idx_t bit_index(idx_t slot_index, idx_t bit_within_slot_index) const {
 295     return slot_index * _bits_per_slot + bit_within_slot_index;
 296   }
 297 
 298   void verify_bit_within_slot_index(idx_t index) const {
 299     assert(index < _bits_per_slot, "bit_within_slot index out of bounds");
 300   }
 301 
 302  public:
 303   // Construction. bits_per_slot must be greater than 0.
 304   BitMap2D(bm_word_t* map, idx_t size_in_slots, idx_t bits_per_slot);
 305 
 306   // Allocates necessary data structure in resource area. bits_per_slot must be greater than 0.
 307   BitMap2D(idx_t size_in_slots, idx_t bits_per_slot);
 308 
 309   idx_t size_in_bits() {
 310     return _map.size();
 311   }
 312 
 313   // Returns number of full slots that have been allocated
 314   idx_t size_in_slots() {
 315     // Round down
 316     return _map.size() / _bits_per_slot;
 317   }
 318 
 319   bool is_valid_index(idx_t slot_index, idx_t bit_within_slot_index) {
 320     verify_bit_within_slot_index(bit_within_slot_index);
 321     return (bit_index(slot_index, bit_within_slot_index) < size_in_bits());
 322   }
 323 
 324   bool at(idx_t slot_index, idx_t bit_within_slot_index) const {
 325     verify_bit_within_slot_index(bit_within_slot_index);
 326     return _map.at(bit_index(slot_index, bit_within_slot_index));
 327   }
 328 
 329   void set_bit(idx_t slot_index, idx_t bit_within_slot_index) {
 330     verify_bit_within_slot_index(bit_within_slot_index);
 331     _map.set_bit(bit_index(slot_index, bit_within_slot_index));
 332   }
 333 
 334   void clear_bit(idx_t slot_index, idx_t bit_within_slot_index) {
 335     verify_bit_within_slot_index(bit_within_slot_index);
 336     _map.clear_bit(bit_index(slot_index, bit_within_slot_index));
 337   }
 338 
 339   void at_put(idx_t slot_index, idx_t bit_within_slot_index, bool value) {
 340     verify_bit_within_slot_index(bit_within_slot_index);
 341     _map.at_put(bit_index(slot_index, bit_within_slot_index), value);
 342   }
 343 
 344   void at_put_grow(idx_t slot_index, idx_t bit_within_slot_index, bool value) {
 345     verify_bit_within_slot_index(bit_within_slot_index);
 346     _map.at_put_grow(bit_index(slot_index, bit_within_slot_index), value);
 347   }
 348 
 349   void clear();
 350 };
 351 
 352 // Closure for iterating over BitMaps
 353 
 354 class BitMapClosure VALUE_OBJ_CLASS_SPEC {
 355  public:
 356   // Callback when bit in map is set.  Should normally return "true";
 357   // return of false indicates that the bitmap iteration should terminate.
 358   virtual bool do_bit(BitMap::idx_t offset) = 0;
 359 };
 360 
 361 #endif // SHARE_VM_UTILITIES_BITMAP_HPP