1 /*
   2  * Copyright (c) 1997, 2018, 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/align.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 // The "abstract" base BitMap class.
  38 //
  39 // The constructor and destructor are protected to prevent
  40 // creation of BitMap instances outside of the BitMap class.
  41 //
  42 // The BitMap class doesn't use virtual calls on purpose,
  43 // this ensures that we don't get a vtable unnecessarily.
  44 //
  45 // The allocation of the backing storage for the BitMap are handled by
  46 // the subclasses. BitMap doesn't allocate or delete backing storage.
  47 class BitMap {
  48   friend class BitMap2D;
  49 
  50  public:
  51   typedef size_t idx_t;         // Type used for bit and word indices.
  52   typedef uintptr_t bm_word_t;  // Element type of array that represents
  53                                 // the bitmap.
  54 
  55   // Hints for range sizes.
  56   typedef enum {
  57     unknown_range, small_range, large_range
  58   } RangeSizeHint;
  59 
  60  private:
  61   bm_word_t* _map;     // First word in bitmap
  62   idx_t      _size;    // Size of bitmap (in bits)
  63 
  64   // Helper for get_next_{zero,one}_bit variants.
  65   // - flip designates whether searching for 1s or 0s.  Must be one of
  66   //   find_{zeros,ones}_flip.
  67   // - aligned_right is true if r_index is a priori on a bm_word_t boundary.
  68   template<bm_word_t flip, bool aligned_right>
  69   inline idx_t get_next_bit_impl(idx_t l_index, idx_t r_index) const;
  70 
  71   // Values for get_next_bit_impl flip parameter.
  72   static const bm_word_t find_ones_flip = 0;
  73   static const bm_word_t find_zeros_flip = ~(bm_word_t)0;
  74 
  75  protected:
  76   // Return the position of bit within the word that contains it (e.g., if
  77   // bitmap words are 32 bits, return a number 0 <= n <= 31).
  78   static idx_t bit_in_word(idx_t bit) { return bit & (BitsPerWord - 1); }
  79 
  80   // Return a mask that will select the specified bit, when applied to the word
  81   // containing the bit.
  82   static bm_word_t bit_mask(idx_t bit) { return (bm_word_t)1 << bit_in_word(bit); }
  83 
  84   // Return the index of the word containing the specified bit.
  85   static idx_t word_index(idx_t bit)  { return bit >> LogBitsPerWord; }
  86 
  87   // Return the bit number of the first bit in the specified word.
  88   static idx_t bit_index(idx_t word)  { return word << LogBitsPerWord; }
  89 
  90   // Return the array of bitmap words, or a specific word from it.
  91   bm_word_t* map()                 { return _map; }
  92   const bm_word_t* map() const     { return _map; }
  93   bm_word_t  map(idx_t word) const { return _map[word]; }
  94 
  95   // Return a pointer to the word containing the specified bit.
  96   bm_word_t* word_addr(idx_t bit)             { return map() + word_index(bit); }
  97   const bm_word_t* word_addr(idx_t bit) const { return map() + word_index(bit); }
  98 
  99   // Set a word to a specified value or to all ones; clear a word.
 100   void set_word  (idx_t word, bm_word_t val) { _map[word] = val; }
 101   void set_word  (idx_t word)            { set_word(word, ~(bm_word_t)0); }
 102   void clear_word(idx_t word)            { _map[word] = 0; }
 103 
 104   // Utilities for ranges of bits.  Ranges are half-open [beg, end).
 105 
 106   // Ranges within a single word.
 107   bm_word_t inverted_bit_mask_for_range(idx_t beg, idx_t end) const;
 108   void  set_range_within_word      (idx_t beg, idx_t end);
 109   void  clear_range_within_word    (idx_t beg, idx_t end);
 110   void  par_put_range_within_word  (idx_t beg, idx_t end, bool value);
 111 
 112   // Ranges spanning entire words.
 113   void      set_range_of_words         (idx_t beg, idx_t end);
 114   void      clear_range_of_words       (idx_t beg, idx_t end);
 115   void      set_large_range_of_words   (idx_t beg, idx_t end);
 116   void      clear_large_range_of_words (idx_t beg, idx_t end);
 117 
 118   static void clear_range_of_words(bm_word_t* map, idx_t beg, idx_t end);
 119 
 120   // The index of the first full word in a range.
 121   idx_t word_index_round_up(idx_t bit) const;
 122 
 123   // Verification.
 124   void verify_index(idx_t index) const NOT_DEBUG_RETURN;
 125   void verify_range(idx_t beg_index, idx_t end_index) const NOT_DEBUG_RETURN;
 126 
 127   // Statistics.
 128   static const idx_t* _pop_count_table;
 129   static void init_pop_count_table();
 130   static idx_t num_set_bits(bm_word_t w);
 131   static idx_t num_set_bits_from_table(unsigned char c);
 132 
 133   // Allocation Helpers.
 134 
 135   // Allocates and clears the bitmap memory.
 136   template <class Allocator>
 137   static bm_word_t* allocate(const Allocator&, idx_t size_in_bits, bool clear = true);
 138 
 139   // Reallocates and clears the new bitmap memory.
 140   template <class Allocator>
 141   static bm_word_t* reallocate(const Allocator&, bm_word_t* map, idx_t old_size_in_bits, idx_t new_size_in_bits, bool clear = true);
 142 
 143   // Free the bitmap memory.
 144   template <class Allocator>
 145   static void free(const Allocator&, bm_word_t* map, idx_t size_in_bits);
 146 
 147   // Protected functions, that are used by BitMap sub-classes that support them.
 148 
 149   // Resize the backing bitmap memory.
 150   //
 151   // Old bits are transfered to the new memory
 152   // and the extended memory is cleared.
 153   template <class Allocator>
 154   void resize(const Allocator& allocator, idx_t new_size_in_bits);
 155 
 156   // Set up and clear the bitmap memory.
 157   //
 158   // Precondition: The bitmap was default constructed and has
 159   // not yet had memory allocated via resize or (re)initialize.
 160   template <class Allocator>
 161   void initialize(const Allocator& allocator, idx_t size_in_bits);
 162 
 163   // Set up and clear the bitmap memory.
 164   //
 165   // Can be called on previously initialized bitmaps.
 166   template <class Allocator>
 167   void reinitialize(const Allocator& allocator, idx_t new_size_in_bits);
 168 
 169   // Set the map and size.
 170   void update(bm_word_t* map, idx_t size) {
 171     _map = map;
 172     _size = size;
 173   }
 174 
 175   // Protected constructor and destructor.
 176   BitMap(bm_word_t* map, idx_t size_in_bits) : _map(map), _size(size_in_bits) {}
 177   ~BitMap() {}
 178 
 179  public:
 180   // Pretouch the entire range of memory this BitMap covers.
 181   void pretouch();
 182 
 183   // Accessing
 184   static idx_t calc_size_in_words(size_t size_in_bits) {
 185     return word_index(size_in_bits + BitsPerWord - 1);
 186   }
 187 
 188   static idx_t calc_size_in_bytes(size_t size_in_bits) {
 189     return calc_size_in_words(size_in_bits) * BytesPerWord;
 190   }
 191 
 192   idx_t size() const          { return _size; }
 193   idx_t size_in_words() const { return calc_size_in_words(size()); }
 194   idx_t size_in_bytes() const { return calc_size_in_bytes(size()); }
 195 
 196   bool at(idx_t index) const {
 197     verify_index(index);
 198     return (*word_addr(index) & bit_mask(index)) != 0;
 199   }
 200 
 201   // Align bit index up or down to the next bitmap word boundary, or check
 202   // alignment.
 203   static idx_t word_align_up(idx_t bit) {
 204     return align_up(bit, BitsPerWord);
 205   }
 206   static idx_t word_align_down(idx_t bit) {
 207     return align_down(bit, BitsPerWord);
 208   }
 209   static bool is_word_aligned(idx_t bit) {
 210     return word_align_up(bit) == bit;
 211   }
 212 
 213   // Set or clear the specified bit.
 214   inline void set_bit(idx_t bit);
 215   inline void clear_bit(idx_t bit);
 216 
 217   // Atomically set or clear the specified bit.
 218   inline bool par_set_bit(idx_t bit);
 219   inline bool par_clear_bit(idx_t bit);
 220 
 221   // Put the given value at the given offset. The parallel version
 222   // will CAS the value into the bitmap and is quite a bit slower.
 223   // The parallel version also returns a value indicating if the
 224   // calling thread was the one that changed the value of the bit.
 225   void at_put(idx_t index, bool value);
 226   bool par_at_put(idx_t index, bool value);
 227 
 228   // Update a range of bits.  Ranges are half-open [beg, end).
 229   void set_range   (idx_t beg, idx_t end);
 230   void clear_range (idx_t beg, idx_t end);
 231   void set_large_range   (idx_t beg, idx_t end);
 232   void clear_large_range (idx_t beg, idx_t end);
 233   void at_put_range(idx_t beg, idx_t end, bool value);
 234   void par_at_put_range(idx_t beg, idx_t end, bool value);
 235   void at_put_large_range(idx_t beg, idx_t end, bool value);
 236   void par_at_put_large_range(idx_t beg, idx_t end, bool value);
 237 
 238   // Update a range of bits, using a hint about the size.  Currently only
 239   // inlines the predominant case of a 1-bit range.  Works best when hint is a
 240   // compile-time constant.
 241   void set_range(idx_t beg, idx_t end, RangeSizeHint hint);
 242   void clear_range(idx_t beg, idx_t end, RangeSizeHint hint);
 243   void par_set_range(idx_t beg, idx_t end, RangeSizeHint hint);
 244   void par_clear_range  (idx_t beg, idx_t end, RangeSizeHint hint);
 245 
 246   // Clearing
 247   void clear_large();
 248   inline void clear();
 249 
 250   // Iteration support.  Returns "true" if the iteration completed, false
 251   // if the iteration terminated early (because the closure "blk" returned
 252   // false).
 253   bool iterate(BitMapClosure* blk, idx_t leftIndex, idx_t rightIndex);
 254   bool iterate(BitMapClosure* blk) {
 255     // call the version that takes an interval
 256     return iterate(blk, 0, size());
 257   }
 258 
 259   // Looking for 1's and 0's at indices equal to or greater than "l_index",
 260   // stopping if none has been found before "r_index", and returning
 261   // "r_index" (which must be at most "size") in that case.
 262   idx_t get_next_one_offset (idx_t l_index, idx_t r_index) const;
 263   idx_t get_next_zero_offset(idx_t l_index, idx_t r_index) const;
 264 
 265   idx_t get_next_one_offset(idx_t offset) const {
 266     return get_next_one_offset(offset, size());
 267   }
 268   idx_t get_next_zero_offset(idx_t offset) const {
 269     return get_next_zero_offset(offset, size());
 270   }
 271 
 272   // Like "get_next_one_offset", except requires that "r_index" is
 273   // aligned to bitsizeof(bm_word_t).
 274   idx_t get_next_one_offset_aligned_right(idx_t l_index, idx_t r_index) const;
 275 
 276   // Returns the number of bits set in the bitmap.
 277   idx_t count_one_bits() const;
 278 
 279   // Set operations.
 280   void set_union(const BitMap& bits);
 281   void set_difference(const BitMap& bits);
 282   void set_intersection(const BitMap& bits);
 283   // Returns true iff "this" is a superset of "bits".
 284   bool contains(const BitMap& bits) const;
 285   // Returns true iff "this and "bits" have a non-empty intersection.
 286   bool intersects(const BitMap& bits) const;
 287 
 288   // Returns result of whether this map changed
 289   // during the operation
 290   bool set_union_with_result(const BitMap& bits);
 291   bool set_difference_with_result(const BitMap& bits);
 292   bool set_intersection_with_result(const BitMap& bits);
 293 
 294   void set_from(const BitMap& bits);
 295 
 296   bool is_same(const BitMap& bits) const;
 297 
 298   // Test if all bits are set or cleared
 299   bool is_full() const;
 300   bool is_empty() const;
 301 
 302   void write_to(bm_word_t* buffer, size_t buffer_size_in_bytes) const;
 303   void print_on_error(outputStream* st, const char* prefix) const;
 304 
 305 #ifndef PRODUCT
 306  public:
 307   // Printing
 308   void print_on(outputStream* st) const;
 309 #endif
 310 };
 311 
 312 // A concrete implementation of the the "abstract" BitMap class.
 313 //
 314 // The BitMapView is used when the backing storage is managed externally.
 315 class BitMapView : public BitMap {
 316  public:
 317   BitMapView() : BitMap(NULL, 0) {}
 318   BitMapView(bm_word_t* map, idx_t size_in_bits) : BitMap(map, size_in_bits) {}
 319 };
 320 
 321 // A BitMap with storage in a ResourceArea.
 322 class ResourceBitMap : public BitMap {
 323 
 324  public:
 325   ResourceBitMap() : BitMap(NULL, 0) {}
 326   // Clears the bitmap memory.
 327   ResourceBitMap(idx_t size_in_bits);
 328 
 329   // Resize the backing bitmap memory.
 330   //
 331   // Old bits are transfered to the new memory
 332   // and the extended memory is cleared.
 333   void resize(idx_t new_size_in_bits);
 334 
 335   // Set up and clear the bitmap memory.
 336   //
 337   // Precondition: The bitmap was default constructed and has
 338   // not yet had memory allocated via resize or initialize.
 339   void initialize(idx_t size_in_bits);
 340 
 341   // Set up and clear the bitmap memory.
 342   //
 343   // Can be called on previously initialized bitmaps.
 344   void reinitialize(idx_t size_in_bits);
 345 };
 346 
 347 // A BitMap with storage in a specific Arena.
 348 class ArenaBitMap : public BitMap {
 349  public:
 350   // Clears the bitmap memory.
 351   ArenaBitMap(Arena* arena, idx_t size_in_bits);
 352 
 353  private:
 354   // Don't allow copy or assignment.
 355   ArenaBitMap(const ArenaBitMap&);
 356   ArenaBitMap& operator=(const ArenaBitMap&);
 357 };
 358 
 359 // A BitMap with storage in the CHeap.
 360 class CHeapBitMap : public BitMap {
 361 
 362  private:
 363   // Don't allow copy or assignment, to prevent the
 364   // allocated memory from leaking out to other instances.
 365   CHeapBitMap(const CHeapBitMap&);
 366   CHeapBitMap& operator=(const CHeapBitMap&);
 367 
 368   // NMT memory type
 369   MEMFLAGS _flags;
 370 
 371  public:
 372   CHeapBitMap(MEMFLAGS flags = mtInternal) : BitMap(NULL, 0), _flags(flags) {}
 373   // Clears the bitmap memory.
 374   CHeapBitMap(idx_t size_in_bits, MEMFLAGS flags = mtInternal, bool clear = true);
 375   ~CHeapBitMap();
 376 
 377   // Resize the backing bitmap memory.
 378   //
 379   // Old bits are transfered to the new memory
 380   // and the extended memory is cleared.
 381   void resize(idx_t new_size_in_bits);
 382 
 383   // Set up and clear the bitmap memory.
 384   //
 385   // Precondition: The bitmap was default constructed and has
 386   // not yet had memory allocated via resize or initialize.
 387   void initialize(idx_t size_in_bits);
 388 
 389   // Set up and clear the bitmap memory.
 390   //
 391   // Can be called on previously initialized bitmaps.
 392   void reinitialize(idx_t size_in_bits);
 393 };
 394 
 395 // Convenience class wrapping BitMap which provides multiple bits per slot.
 396 class BitMap2D {
 397  public:
 398   typedef BitMap::idx_t idx_t;          // Type used for bit and word indices.
 399   typedef BitMap::bm_word_t bm_word_t;  // Element type of array that
 400                                         // represents the bitmap.
 401  private:
 402   ResourceBitMap _map;
 403   idx_t          _bits_per_slot;
 404 
 405   idx_t bit_index(idx_t slot_index, idx_t bit_within_slot_index) const {
 406     return slot_index * _bits_per_slot + bit_within_slot_index;
 407   }
 408 
 409   void verify_bit_within_slot_index(idx_t index) const {
 410     assert(index < _bits_per_slot, "bit_within_slot index out of bounds");
 411   }
 412 
 413  public:
 414   // Construction. bits_per_slot must be greater than 0.
 415   BitMap2D(idx_t bits_per_slot) :
 416       _map(), _bits_per_slot(bits_per_slot) {}
 417 
 418   // Allocates necessary data structure in resource area. bits_per_slot must be greater than 0.
 419   BitMap2D(idx_t size_in_slots, idx_t bits_per_slot) :
 420       _map(size_in_slots * bits_per_slot), _bits_per_slot(bits_per_slot) {}
 421 
 422   idx_t size_in_bits() {
 423     return _map.size();
 424   }
 425 
 426   // Returns number of full slots that have been allocated
 427   idx_t size_in_slots() {
 428     // Round down
 429     return _map.size() / _bits_per_slot;
 430   }
 431 
 432   bool is_valid_index(idx_t slot_index, idx_t bit_within_slot_index);
 433   bool at(idx_t slot_index, idx_t bit_within_slot_index) const;
 434   void set_bit(idx_t slot_index, idx_t bit_within_slot_index);
 435   void clear_bit(idx_t slot_index, idx_t bit_within_slot_index);
 436   void at_put(idx_t slot_index, idx_t bit_within_slot_index, bool value);
 437   void at_put_grow(idx_t slot_index, idx_t bit_within_slot_index, bool value);
 438 };
 439 
 440 // Closure for iterating over BitMaps
 441 
 442 class BitMapClosure {
 443  public:
 444   // Callback when bit in map is set.  Should normally return "true";
 445   // return of false indicates that the bitmap iteration should terminate.
 446   virtual bool do_bit(BitMap::idx_t offset) = 0;
 447 };
 448 
 449 #endif // SHARE_VM_UTILITIES_BITMAP_HPP