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