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   // Verification.
165 
166   // Verify size_in_bits does not exceed max_size_in_bits().
167   static void verify_size(idx_t size_in_bits) NOT_DEBUG_RETURN;
168   // Verify bit is less than size().
169   void verify_index(idx_t bit) const NOT_DEBUG_RETURN;
170   // Verify bit is not greater than size().
171   void verify_limit(idx_t bit) const NOT_DEBUG_RETURN;
172   // Verify [beg,end) is a valid range, e.g. beg <= end <= size().
173   void verify_range(idx_t beg, idx_t end) const NOT_DEBUG_RETURN;
174 
175   // Allocation Helpers.
176 
177   // Allocates and clears the bitmap memory.
178   template <class Allocator>
179   static bm_word_t* allocate(const Allocator&, idx_t size_in_bits, bool clear = true);
180 
181   // Reallocates and clears the new bitmap memory.
182   template <class Allocator>
183   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);
184 
185   // Free the bitmap memory.
186   template <class Allocator>
187   static void free(const Allocator&, bm_word_t* map, idx_t size_in_bits);
188 
189   // Protected functions, that are used by BitMap sub-classes that support them.
190 
191   // Resize the backing bitmap memory.
192   //
193   // Old bits are transfered to the new memory
194   // and the extended memory is cleared.
195   template <class Allocator>
196   void resize(const Allocator& allocator, idx_t new_size_in_bits, bool clear);
197 
198   // Set up and clear the bitmap memory.
199   //
200   // Precondition: The bitmap was default constructed and has
201   // not yet had memory allocated via resize or (re)initialize.
202   template <class Allocator>
203   void initialize(const Allocator& allocator, idx_t size_in_bits, bool clear);
204 
205   // Set up and clear the bitmap memory.
206   //
207   // Can be called on previously initialized bitmaps.
208   template <class Allocator>
209   void reinitialize(const Allocator& allocator, idx_t new_size_in_bits, bool clear);
210 
211   // Set the map and size.
212   void update(bm_word_t* map, idx_t size) {
213     _map = map;
214     _size = size;
215   }
216 
217   // Protected constructor and destructor.
218   BitMap(bm_word_t* map, idx_t size_in_bits) : _map(map), _size(size_in_bits) {
219     verify_size(size_in_bits);
220   }
221   ~BitMap() {}
222 
223  public:
224   // Pretouch the entire range of memory this BitMap covers.
225   void pretouch();
226 
227   // Accessing
228   static idx_t calc_size_in_words(size_t size_in_bits) {
229     verify_size(size_in_bits);
230     return raw_to_words_align_up(size_in_bits);
231   }
232 
233   idx_t size() const          { return _size; }
234   idx_t size_in_words() const { return calc_size_in_words(size()); }
235   idx_t size_in_bytes() const { return size_in_words() * BytesPerWord; }
236 
237   bool at(idx_t index) const {
238     verify_index(index);
239     return (*word_addr(index) & bit_mask(index)) != 0;
240   }
241 
242   // memory_order must be memory_order_relaxed or memory_order_acquire.
243   bool par_at(idx_t index, atomic_memory_order memory_order = memory_order_acquire) const;
244 
245   // Set or clear the specified bit.
246   inline void set_bit(idx_t bit);
247   inline void clear_bit(idx_t bit);
248 
249   // Attempts to change a bit to a desired value. The operation returns true if
250   // this thread changed the value of the bit. It was changed with a RMW operation
251   // using the specified memory_order. The operation returns false if the change
252   // could not be set due to the bit already being observed in the desired state.
253   // The atomic access that observed the bit in the desired state has acquire
254   // semantics, unless memory_order is memory_order_relaxed or memory_order_release.
255   inline bool par_set_bit(idx_t bit, atomic_memory_order memory_order = memory_order_conservative);
256   inline bool par_clear_bit(idx_t bit, atomic_memory_order memory_order = memory_order_conservative);
257 
258   // Put the given value at the given index. The parallel version
259   // will CAS the value into the bitmap and is quite a bit slower.
260   // The parallel version also returns a value indicating if the
261   // calling thread was the one that changed the value of the bit.
262   void at_put(idx_t index, bool value);
263   bool par_at_put(idx_t index, bool value);
264 
265   // Update a range of bits.  Ranges are half-open [beg, end).
266   void set_range   (idx_t beg, idx_t end);
267   void clear_range (idx_t beg, idx_t end);
268   void set_large_range   (idx_t beg, idx_t end);
269   void clear_large_range (idx_t beg, idx_t end);
270   void at_put_range(idx_t beg, idx_t end, bool value);
271   void par_at_put_range(idx_t beg, idx_t end, bool value);
272   void at_put_large_range(idx_t beg, idx_t end, bool value);
273   void par_at_put_large_range(idx_t beg, idx_t end, bool value);
274 
275   // Update a range of bits, using a hint about the size.  Currently only
276   // inlines the predominant case of a 1-bit range.  Works best when hint is a
277   // compile-time constant.
278   void set_range(idx_t beg, idx_t end, RangeSizeHint hint);
279   void clear_range(idx_t beg, idx_t end, RangeSizeHint hint);
280   void par_set_range(idx_t beg, idx_t end, RangeSizeHint hint);
281   void par_clear_range  (idx_t beg, idx_t end, RangeSizeHint hint);
282 
283   // Clearing
284   void clear_large();
285   inline void clear();
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   bool iterate(BitMapClosure* cl, idx_t beg, idx_t end);
296   bool iterate(BitMapClosure* cl);
297 
298   // Looking for 1's and 0's at indices equal to or greater than "l_index",
299   // stopping if none has been found before "r_index", and returning
300   // "r_index" (which must be at most "size") in that case.
301   idx_t get_next_one_offset (idx_t l_index, idx_t r_index) const;
302   idx_t get_next_zero_offset(idx_t l_index, idx_t r_index) const;
303 
304   idx_t get_next_one_offset(idx_t offset) const {
305     return get_next_one_offset(offset, size());
306   }
307   idx_t get_next_zero_offset(idx_t offset) const {
308     return get_next_zero_offset(offset, size());
309   }
310 
311   // Like "get_next_one_offset", except requires that "r_index" is
312   // aligned to bitsizeof(bm_word_t).
313   idx_t get_next_one_offset_aligned_right(idx_t l_index, idx_t r_index) const;
314 
315   // Returns the number of bits set in the bitmap.
316   idx_t count_one_bits() const;
317 
318   // Returns the number of bits set within  [beg, end).
319   idx_t count_one_bits(idx_t beg, idx_t end) const;
320 
321   // Set operations.
322   void set_union(const BitMap& bits);
323   void set_difference(const BitMap& bits);
324   void set_intersection(const BitMap& bits);
325   // Returns true iff "this" is a superset of "bits".
326   bool contains(const BitMap& bits) const;
327   // Returns true iff "this and "bits" have a non-empty intersection.
328   bool intersects(const BitMap& bits) const;
329 
330   // Returns result of whether this map changed
331   // during the operation
332   bool set_union_with_result(const BitMap& bits);
333   bool set_difference_with_result(const BitMap& bits);
334   bool set_intersection_with_result(const BitMap& bits);
335 
336   void set_from(const BitMap& bits);
337 
338   bool is_same(const BitMap& bits) const;
339 
340   // Test if all bits are set or cleared
341   bool is_full() const;
342   bool is_empty() const;
343 
344   void write_to(bm_word_t* buffer, size_t buffer_size_in_bytes) const;
345   void print_on_error(outputStream* st, const char* prefix) const;
346 
347 #ifndef PRODUCT
348  public:
349   // Printing
350   void print_on(outputStream* st) const;
351 #endif
352 };
353 
354 // A concrete implementation of the the "abstract" BitMap class.
355 //
356 // The BitMapView is used when the backing storage is managed externally.
357 class BitMapView : public BitMap {
358  public:
359   BitMapView() : BitMap(NULL, 0) {}
360   BitMapView(bm_word_t* map, idx_t size_in_bits) : BitMap(map, size_in_bits) {}
361 };
362 
363 // A BitMap with storage in a ResourceArea.
364 class ResourceBitMap : public BitMap {
365 
366  public:
367   ResourceBitMap() : BitMap(NULL, 0) {}
368   // Conditionally clears the bitmap memory.
369   ResourceBitMap(idx_t size_in_bits, bool clear = true);
370 
371   // Resize the backing bitmap memory.
372   //
373   // Old bits are transfered to the new memory
374   // and the extended memory is cleared.
375   void resize(idx_t new_size_in_bits);
376 
377   // Set up and clear the bitmap memory.
378   //
379   // Precondition: The bitmap was default constructed and has
380   // not yet had memory allocated via resize or initialize.
381   void initialize(idx_t size_in_bits);
382 
383   // Set up and clear the bitmap memory.
384   //
385   // Can be called on previously initialized bitmaps.
386   void reinitialize(idx_t size_in_bits);
387 };
388 
389 // A BitMap with storage in a specific Arena.
390 class ArenaBitMap : public BitMap {
391  public:
392   // Clears the bitmap memory.
393   ArenaBitMap(Arena* arena, idx_t size_in_bits);
394 
395  private:
396   NONCOPYABLE(ArenaBitMap);
397 };
398 
399 // A BitMap with storage in the CHeap.
400 class CHeapBitMap : public BitMap {
401 
402  private:
403   // Don't allow copy or assignment, to prevent the
404   // allocated memory from leaking out to other instances.
405   NONCOPYABLE(CHeapBitMap);
406 
407   // NMT memory type
408   MEMFLAGS _flags;
409 
410  public:
411   CHeapBitMap(MEMFLAGS flags = mtInternal) : BitMap(NULL, 0), _flags(flags) {}
412   // Clears the bitmap memory.
413   CHeapBitMap(idx_t size_in_bits, MEMFLAGS flags = mtInternal, bool clear = true);
414   ~CHeapBitMap();
415 
416   // Resize the backing bitmap memory.
417   //
418   // Old bits are transfered to the new memory
419   // and the extended memory is (optionally) cleared.
420   void resize(idx_t new_size_in_bits, bool clear = true);
421 
422   // Set up and (optionally) clear the bitmap memory.
423   //
424   // Precondition: The bitmap was default constructed and has
425   // not yet had memory allocated via resize or initialize.
426   void initialize(idx_t size_in_bits, bool clear = true);
427 
428   // Set up and (optionally) clear the bitmap memory.
429   //
430   // Can be called on previously initialized bitmaps.
431   void reinitialize(idx_t size_in_bits, bool clear = true);
432 };
433 
434 // Convenience class wrapping BitMap which provides multiple bits per slot.
435 class BitMap2D {
436  public:
437   typedef BitMap::idx_t idx_t;          // Type used for bit and word indices.
438   typedef BitMap::bm_word_t bm_word_t;  // Element type of array that
439                                         // represents the bitmap.
440  private:
441   ResourceBitMap _map;
442   idx_t          _bits_per_slot;
443 
444   idx_t bit_index(idx_t slot_index, idx_t bit_within_slot_index) const {
445     return slot_index * _bits_per_slot + bit_within_slot_index;
446   }
447 
448   void verify_bit_within_slot_index(idx_t index) const {
449     assert(index < _bits_per_slot, "bit_within_slot index out of bounds");
450   }
451 
452  public:
453   // Construction. bits_per_slot must be greater than 0.
454   BitMap2D(idx_t bits_per_slot) :
455       _map(), _bits_per_slot(bits_per_slot) {}
456 
457   // Allocates necessary data structure in resource area. bits_per_slot must be greater than 0.
458   BitMap2D(idx_t size_in_slots, idx_t bits_per_slot) :
459       _map(size_in_slots * bits_per_slot), _bits_per_slot(bits_per_slot) {}
460 
461   idx_t size_in_bits() {
462     return _map.size();
463   }
464 
465   bool is_valid_index(idx_t slot_index, idx_t bit_within_slot_index);
466   bool at(idx_t slot_index, idx_t bit_within_slot_index) const;
467   void set_bit(idx_t slot_index, idx_t bit_within_slot_index);
468   void clear_bit(idx_t slot_index, idx_t bit_within_slot_index);
469   void at_put(idx_t slot_index, idx_t bit_within_slot_index, bool value);
470   void at_put_grow(idx_t slot_index, idx_t bit_within_slot_index, bool value);
471 };
472 
473 // Closure for iterating over BitMaps
474 
475 class BitMapClosure {
476  public:
477   // Callback when bit in map is set.  Should normally return "true";
478   // return of false indicates that the bitmap iteration should terminate.
479   virtual bool do_bit(BitMap::idx_t index) = 0;
480 };
481 
482 #endif // SHARE_UTILITIES_BITMAP_HPP