bitMap.inline.hpp revision 1879:f95d63e2154a
1/*
2 * Copyright (c) 2005, 2010, 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_INLINE_HPP
26#define SHARE_VM_UTILITIES_BITMAP_INLINE_HPP
27
28#include "runtime/atomic.hpp"
29#include "utilities/bitMap.hpp"
30
31#ifdef ASSERT
32inline void BitMap::verify_index(idx_t index) const {
33  assert(index < _size, "BitMap index out of bounds");
34}
35
36inline void BitMap::verify_range(idx_t beg_index, idx_t end_index) const {
37  assert(beg_index <= end_index, "BitMap range error");
38  // Note that [0,0) and [size,size) are both valid ranges.
39  if (end_index != _size) verify_index(end_index);
40}
41#endif // #ifdef ASSERT
42
43inline void BitMap::set_bit(idx_t bit) {
44  verify_index(bit);
45  *word_addr(bit) |= bit_mask(bit);
46}
47
48inline void BitMap::clear_bit(idx_t bit) {
49  verify_index(bit);
50  *word_addr(bit) &= ~bit_mask(bit);
51}
52
53inline bool BitMap::par_set_bit(idx_t bit) {
54  verify_index(bit);
55  volatile idx_t* const addr = word_addr(bit);
56  const idx_t mask = bit_mask(bit);
57  idx_t old_val = *addr;
58
59  do {
60    const idx_t new_val = old_val | mask;
61    if (new_val == old_val) {
62      return false;     // Someone else beat us to it.
63    }
64    const idx_t cur_val = (idx_t) Atomic::cmpxchg_ptr((void*) new_val,
65                                                      (volatile void*) addr,
66                                                      (void*) old_val);
67    if (cur_val == old_val) {
68      return true;      // Success.
69    }
70    old_val = cur_val;  // The value changed, try again.
71  } while (true);
72}
73
74inline bool BitMap::par_clear_bit(idx_t bit) {
75  verify_index(bit);
76  volatile idx_t* const addr = word_addr(bit);
77  const idx_t mask = ~bit_mask(bit);
78  idx_t old_val = *addr;
79
80  do {
81    const idx_t new_val = old_val & mask;
82    if (new_val == old_val) {
83      return false;     // Someone else beat us to it.
84    }
85    const idx_t cur_val = (idx_t) Atomic::cmpxchg_ptr((void*) new_val,
86                                                      (volatile void*) addr,
87                                                      (void*) old_val);
88    if (cur_val == old_val) {
89      return true;      // Success.
90    }
91    old_val = cur_val;  // The value changed, try again.
92  } while (true);
93}
94
95inline void BitMap::set_range(idx_t beg, idx_t end, RangeSizeHint hint) {
96  if (hint == small_range && end - beg == 1) {
97    set_bit(beg);
98  } else {
99    if (hint == large_range) {
100      set_large_range(beg, end);
101    } else {
102      set_range(beg, end);
103    }
104  }
105}
106
107inline void BitMap::clear_range(idx_t beg, idx_t end, RangeSizeHint hint) {
108  if (hint == small_range && end - beg == 1) {
109    clear_bit(beg);
110  } else {
111    if (hint == large_range) {
112      clear_large_range(beg, end);
113    } else {
114      clear_range(beg, end);
115    }
116  }
117}
118
119inline void BitMap::par_set_range(idx_t beg, idx_t end, RangeSizeHint hint) {
120  if (hint == small_range && end - beg == 1) {
121    par_at_put(beg, true);
122  } else {
123    if (hint == large_range) {
124      par_at_put_large_range(beg, end, true);
125    } else {
126      par_at_put_range(beg, end, true);
127    }
128  }
129}
130
131inline void BitMap::set_range_of_words(idx_t beg, idx_t end) {
132  bm_word_t* map = _map;
133  for (idx_t i = beg; i < end; ++i) map[i] = ~(uintptr_t)0;
134}
135
136
137inline void BitMap::clear_range_of_words(idx_t beg, idx_t end) {
138  bm_word_t* map = _map;
139  for (idx_t i = beg; i < end; ++i) map[i] = 0;
140}
141
142
143inline void BitMap::clear() {
144  clear_range_of_words(0, size_in_words());
145}
146
147
148inline void BitMap::par_clear_range(idx_t beg, idx_t end, RangeSizeHint hint) {
149  if (hint == small_range && end - beg == 1) {
150    par_at_put(beg, false);
151  } else {
152    if (hint == large_range) {
153      par_at_put_large_range(beg, end, false);
154    } else {
155      par_at_put_range(beg, end, false);
156    }
157  }
158}
159
160inline BitMap::idx_t
161BitMap::get_next_one_offset_inline(idx_t l_offset, idx_t r_offset) const {
162  assert(l_offset <= size(), "BitMap index out of bounds");
163  assert(r_offset <= size(), "BitMap index out of bounds");
164  assert(l_offset <= r_offset, "l_offset > r_offset ?");
165
166  if (l_offset == r_offset) {
167    return l_offset;
168  }
169  idx_t   index = word_index(l_offset);
170  idx_t r_index = word_index(r_offset-1) + 1;
171  idx_t res_offset = l_offset;
172
173  // check bits including and to the _left_ of offset's position
174  idx_t pos = bit_in_word(res_offset);
175  idx_t res = map(index) >> pos;
176  if (res != (uintptr_t)NoBits) {
177    // find the position of the 1-bit
178    for (; !(res & 1); res_offset++) {
179      res = res >> 1;
180    }
181    assert(res_offset >= l_offset &&
182           res_offset < r_offset, "just checking");
183    return MIN2(res_offset, r_offset);
184  }
185  // skip over all word length 0-bit runs
186  for (index++; index < r_index; index++) {
187    res = map(index);
188    if (res != (uintptr_t)NoBits) {
189      // found a 1, return the offset
190      for (res_offset = bit_index(index); !(res & 1); res_offset++) {
191        res = res >> 1;
192      }
193      assert(res & 1, "tautology; see loop condition");
194      assert(res_offset >= l_offset, "just checking");
195      return MIN2(res_offset, r_offset);
196    }
197  }
198  return r_offset;
199}
200
201inline BitMap::idx_t
202BitMap::get_next_zero_offset_inline(idx_t l_offset, idx_t r_offset) const {
203  assert(l_offset <= size(), "BitMap index out of bounds");
204  assert(r_offset <= size(), "BitMap index out of bounds");
205  assert(l_offset <= r_offset, "l_offset > r_offset ?");
206
207  if (l_offset == r_offset) {
208    return l_offset;
209  }
210  idx_t   index = word_index(l_offset);
211  idx_t r_index = word_index(r_offset-1) + 1;
212  idx_t res_offset = l_offset;
213
214  // check bits including and to the _left_ of offset's position
215  idx_t pos = res_offset & (BitsPerWord - 1);
216  idx_t res = (map(index) >> pos) | left_n_bits((int)pos);
217
218  if (res != (uintptr_t)AllBits) {
219    // find the position of the 0-bit
220    for (; res & 1; res_offset++) {
221      res = res >> 1;
222    }
223    assert(res_offset >= l_offset, "just checking");
224    return MIN2(res_offset, r_offset);
225  }
226  // skip over all word length 1-bit runs
227  for (index++; index < r_index; index++) {
228    res = map(index);
229    if (res != (uintptr_t)AllBits) {
230      // found a 0, return the offset
231      for (res_offset = index << LogBitsPerWord; res & 1;
232           res_offset++) {
233        res = res >> 1;
234      }
235      assert(!(res & 1), "tautology; see loop condition");
236      assert(res_offset >= l_offset, "just checking");
237      return MIN2(res_offset, r_offset);
238    }
239  }
240  return r_offset;
241}
242
243inline BitMap::idx_t
244BitMap::get_next_one_offset_inline_aligned_right(idx_t l_offset,
245                                                 idx_t r_offset) const
246{
247  verify_range(l_offset, r_offset);
248  assert(bit_in_word(r_offset) == 0, "r_offset not word-aligned");
249
250  if (l_offset == r_offset) {
251    return l_offset;
252  }
253  idx_t   index = word_index(l_offset);
254  idx_t r_index = word_index(r_offset);
255  idx_t res_offset = l_offset;
256
257  // check bits including and to the _left_ of offset's position
258  idx_t res = map(index) >> bit_in_word(res_offset);
259  if (res != (uintptr_t)NoBits) {
260    // find the position of the 1-bit
261    for (; !(res & 1); res_offset++) {
262      res = res >> 1;
263    }
264    assert(res_offset >= l_offset &&
265           res_offset < r_offset, "just checking");
266    return res_offset;
267  }
268  // skip over all word length 0-bit runs
269  for (index++; index < r_index; index++) {
270    res = map(index);
271    if (res != (uintptr_t)NoBits) {
272      // found a 1, return the offset
273      for (res_offset = bit_index(index); !(res & 1); res_offset++) {
274        res = res >> 1;
275      }
276      assert(res & 1, "tautology; see loop condition");
277      assert(res_offset >= l_offset && res_offset < r_offset, "just checking");
278      return res_offset;
279    }
280  }
281  return r_offset;
282}
283
284
285// Returns a bit mask for a range of bits [beg, end) within a single word.  Each
286// bit in the mask is 0 if the bit is in the range, 1 if not in the range.  The
287// returned mask can be used directly to clear the range, or inverted to set the
288// range.  Note:  end must not be 0.
289inline BitMap::bm_word_t
290BitMap::inverted_bit_mask_for_range(idx_t beg, idx_t end) const {
291  assert(end != 0, "does not work when end == 0");
292  assert(beg == end || word_index(beg) == word_index(end - 1),
293         "must be a single-word range");
294  bm_word_t mask = bit_mask(beg) - 1;   // low (right) bits
295  if (bit_in_word(end) != 0) {
296    mask |= ~(bit_mask(end) - 1);       // high (left) bits
297  }
298  return mask;
299}
300
301inline void BitMap::set_large_range_of_words(idx_t beg, idx_t end) {
302  memset(_map + beg, ~(unsigned char)0, (end - beg) * sizeof(uintptr_t));
303}
304
305inline void BitMap::clear_large_range_of_words(idx_t beg, idx_t end) {
306  memset(_map + beg, 0, (end - beg) * sizeof(uintptr_t));
307}
308
309inline BitMap::idx_t BitMap::word_index_round_up(idx_t bit) const {
310  idx_t bit_rounded_up = bit + (BitsPerWord - 1);
311  // Check for integer arithmetic overflow.
312  return bit_rounded_up > bit ? word_index(bit_rounded_up) : size_in_words();
313}
314
315inline BitMap::idx_t BitMap::get_next_one_offset(idx_t l_offset,
316                                          idx_t r_offset) const {
317  return get_next_one_offset_inline(l_offset, r_offset);
318}
319
320inline BitMap::idx_t BitMap::get_next_zero_offset(idx_t l_offset,
321                                           idx_t r_offset) const {
322  return get_next_zero_offset_inline(l_offset, r_offset);
323}
324
325inline void BitMap2D::clear() {
326  _map.clear();
327}
328
329#endif // SHARE_VM_UTILITIES_BITMAP_INLINE_HPP
330