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