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