src/share/vm/utilities/bitMap.inline.hpp

Sat, 01 Dec 2007 00:00:00 +0000

author
duke
date
Sat, 01 Dec 2007 00:00:00 +0000
changeset 435
a61af66fc99e
child 777
37f87013dfd8
permissions
-rw-r--r--

Initial load

duke@435 1 /*
duke@435 2 * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
duke@435 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@435 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@435 21 * have any questions.
duke@435 22 *
duke@435 23 */
duke@435 24
duke@435 25 inline bool BitMap::par_set_bit(idx_t bit) {
duke@435 26 verify_index(bit);
duke@435 27 volatile idx_t* const addr = word_addr(bit);
duke@435 28 const idx_t mask = bit_mask(bit);
duke@435 29 idx_t old_val = *addr;
duke@435 30
duke@435 31 do {
duke@435 32 const idx_t new_val = old_val | mask;
duke@435 33 if (new_val == old_val) {
duke@435 34 return false; // Someone else beat us to it.
duke@435 35 }
duke@435 36 const idx_t cur_val = (idx_t) Atomic::cmpxchg_ptr((void*) new_val,
duke@435 37 (volatile void*) addr,
duke@435 38 (void*) old_val);
duke@435 39 if (cur_val == old_val) {
duke@435 40 return true; // Success.
duke@435 41 }
duke@435 42 old_val = cur_val; // The value changed, try again.
duke@435 43 } while (true);
duke@435 44 }
duke@435 45
duke@435 46 inline bool BitMap::par_clear_bit(idx_t bit) {
duke@435 47 verify_index(bit);
duke@435 48 volatile idx_t* const addr = word_addr(bit);
duke@435 49 const idx_t mask = ~bit_mask(bit);
duke@435 50 idx_t old_val = *addr;
duke@435 51
duke@435 52 do {
duke@435 53 const idx_t new_val = old_val & mask;
duke@435 54 if (new_val == old_val) {
duke@435 55 return false; // Someone else beat us to it.
duke@435 56 }
duke@435 57 const idx_t cur_val = (idx_t) Atomic::cmpxchg_ptr((void*) new_val,
duke@435 58 (volatile void*) addr,
duke@435 59 (void*) old_val);
duke@435 60 if (cur_val == old_val) {
duke@435 61 return true; // Success.
duke@435 62 }
duke@435 63 old_val = cur_val; // The value changed, try again.
duke@435 64 } while (true);
duke@435 65 }
duke@435 66
duke@435 67 inline BitMap::idx_t
duke@435 68 BitMap::find_next_one_bit(idx_t beg_bit, idx_t end_bit) const
duke@435 69 {
duke@435 70 verify_range(beg_bit, end_bit);
duke@435 71 assert(bit_in_word(end_bit) == 0, "end_bit not word-aligned");
duke@435 72
duke@435 73 if (beg_bit == end_bit) {
duke@435 74 return beg_bit;
duke@435 75 }
duke@435 76
duke@435 77 idx_t index = word_index(beg_bit);
duke@435 78 idx_t r_index = word_index(end_bit);
duke@435 79 idx_t res_bit = beg_bit;
duke@435 80
duke@435 81 // check bits including and to the _left_ of offset's position
duke@435 82 idx_t res = map(index) >> bit_in_word(res_bit);
duke@435 83 if (res != (uintptr_t) NoBits) {
duke@435 84 // find the position of the 1-bit
duke@435 85 for (; !(res & 1); res_bit++) {
duke@435 86 res = res >> 1;
duke@435 87 }
duke@435 88 assert(res_bit >= beg_bit && res_bit < end_bit, "just checking");
duke@435 89 return res_bit;
duke@435 90 }
duke@435 91 // skip over all word length 0-bit runs
duke@435 92 for (index++; index < r_index; index++) {
duke@435 93 res = map(index);
duke@435 94 if (res != (uintptr_t) NoBits) {
duke@435 95 // found a 1, return the offset
duke@435 96 for (res_bit = bit_index(index); !(res & 1); res_bit++) {
duke@435 97 res = res >> 1;
duke@435 98 }
duke@435 99 assert(res & 1, "tautology; see loop condition");
duke@435 100 assert(res_bit >= beg_bit && res_bit < end_bit, "just checking");
duke@435 101 return res_bit;
duke@435 102 }
duke@435 103 }
duke@435 104 return end_bit;
duke@435 105 }

mercurial