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