src/share/vm/utilities/bitMap.cpp

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

     1 /*
     2  * Copyright 1997-2006 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
    21  * have any questions.
    22  *
    23  */
    25 # include "incls/_precompiled.incl"
    26 # include "incls/_bitMap.cpp.incl"
    29 BitMap::BitMap(idx_t* map, idx_t size_in_bits) {
    30   assert(size_in_bits >= 0, "just checking");
    31   _map = map;
    32   _size = size_in_bits;
    33 }
    36 BitMap::BitMap(idx_t size_in_bits) {
    37   assert(size_in_bits >= 0, "just checking");
    38   _size = size_in_bits;
    39   _map = NEW_RESOURCE_ARRAY(idx_t, size_in_words());
    40 }
    43 void BitMap::resize(idx_t size_in_bits) {
    44   assert(size_in_bits >= 0, "just checking");
    45   size_t old_size_in_words = size_in_words();
    46   uintptr_t* old_map = map();
    47   _size = size_in_bits;
    48   size_t new_size_in_words = size_in_words();
    49   _map = NEW_RESOURCE_ARRAY(idx_t, new_size_in_words);
    50   Copy::disjoint_words((HeapWord*) old_map, (HeapWord*) _map, MIN2(old_size_in_words, new_size_in_words));
    51   if (new_size_in_words > old_size_in_words) {
    52     clear_range_of_words(old_size_in_words, size_in_words());
    53   }
    54 }
    56 // Returns a bit mask for a range of bits [beg, end) within a single word.  Each
    57 // bit in the mask is 0 if the bit is in the range, 1 if not in the range.  The
    58 // returned mask can be used directly to clear the range, or inverted to set the
    59 // range.  Note:  end must not be 0.
    60 inline BitMap::idx_t
    61 BitMap::inverted_bit_mask_for_range(idx_t beg, idx_t end) const {
    62   assert(end != 0, "does not work when end == 0");
    63   assert(beg == end || word_index(beg) == word_index(end - 1),
    64          "must be a single-word range");
    65   idx_t mask = bit_mask(beg) - 1;       // low (right) bits
    66   if (bit_in_word(end) != 0) {
    67     mask |= ~(bit_mask(end) - 1);       // high (left) bits
    68   }
    69   return mask;
    70 }
    72 void BitMap::set_range_within_word(idx_t beg, idx_t end) {
    73   // With a valid range (beg <= end), this test ensures that end != 0, as
    74   // required by inverted_bit_mask_for_range.  Also avoids an unnecessary write.
    75   if (beg != end) {
    76     idx_t mask = inverted_bit_mask_for_range(beg, end);
    77     *word_addr(beg) |= ~mask;
    78   }
    79 }
    81 void BitMap::clear_range_within_word(idx_t beg, idx_t end) {
    82   // With a valid range (beg <= end), this test ensures that end != 0, as
    83   // required by inverted_bit_mask_for_range.  Also avoids an unnecessary write.
    84   if (beg != end) {
    85     idx_t mask = inverted_bit_mask_for_range(beg, end);
    86     *word_addr(beg) &= mask;
    87   }
    88 }
    90 void BitMap::par_put_range_within_word(idx_t beg, idx_t end, bool value) {
    91   assert(value == 0 || value == 1, "0 for clear, 1 for set");
    92   // With a valid range (beg <= end), this test ensures that end != 0, as
    93   // required by inverted_bit_mask_for_range.  Also avoids an unnecessary write.
    94   if (beg != end) {
    95     intptr_t* pw  = (intptr_t*)word_addr(beg);
    96     intptr_t  w   = *pw;
    97     intptr_t  mr  = (intptr_t)inverted_bit_mask_for_range(beg, end);
    98     intptr_t  nw  = value ? (w | ~mr) : (w & mr);
    99     while (true) {
   100       intptr_t res = Atomic::cmpxchg_ptr(nw, pw, w);
   101       if (res == w) break;
   102       w  = *pw;
   103       nw = value ? (w | ~mr) : (w & mr);
   104     }
   105   }
   106 }
   108 inline void BitMap::set_large_range_of_words(idx_t beg, idx_t end) {
   109   memset(_map + beg, ~(unsigned char)0, (end - beg) * sizeof(uintptr_t));
   110 }
   112 inline void BitMap::clear_large_range_of_words(idx_t beg, idx_t end) {
   113   memset(_map + beg, 0, (end - beg) * sizeof(uintptr_t));
   114 }
   116 inline BitMap::idx_t BitMap::word_index_round_up(idx_t bit) const {
   117   idx_t bit_rounded_up = bit + (BitsPerWord - 1);
   118   // Check for integer arithmetic overflow.
   119   return bit_rounded_up > bit ? word_index(bit_rounded_up) : size_in_words();
   120 }
   122 void BitMap::set_range(idx_t beg, idx_t end) {
   123   verify_range(beg, end);
   125   idx_t beg_full_word = word_index_round_up(beg);
   126   idx_t end_full_word = word_index(end);
   128   if (beg_full_word < end_full_word) {
   129     // The range includes at least one full word.
   130     set_range_within_word(beg, bit_index(beg_full_word));
   131     set_range_of_words(beg_full_word, end_full_word);
   132     set_range_within_word(bit_index(end_full_word), end);
   133   } else {
   134     // The range spans at most 2 partial words.
   135     idx_t boundary = MIN2(bit_index(beg_full_word), end);
   136     set_range_within_word(beg, boundary);
   137     set_range_within_word(boundary, end);
   138   }
   139 }
   141 void BitMap::clear_range(idx_t beg, idx_t end) {
   142   verify_range(beg, end);
   144   idx_t beg_full_word = word_index_round_up(beg);
   145   idx_t end_full_word = word_index(end);
   147   if (beg_full_word < end_full_word) {
   148     // The range includes at least one full word.
   149     clear_range_within_word(beg, bit_index(beg_full_word));
   150     clear_range_of_words(beg_full_word, end_full_word);
   151     clear_range_within_word(bit_index(end_full_word), end);
   152   } else {
   153     // The range spans at most 2 partial words.
   154     idx_t boundary = MIN2(bit_index(beg_full_word), end);
   155     clear_range_within_word(beg, boundary);
   156     clear_range_within_word(boundary, end);
   157   }
   158 }
   160 void BitMap::set_large_range(idx_t beg, idx_t end) {
   161   verify_range(beg, end);
   163   idx_t beg_full_word = word_index_round_up(beg);
   164   idx_t end_full_word = word_index(end);
   166   assert(end_full_word - beg_full_word >= 32,
   167          "the range must include at least 32 bytes");
   169   // The range includes at least one full word.
   170   set_range_within_word(beg, bit_index(beg_full_word));
   171   set_large_range_of_words(beg_full_word, end_full_word);
   172   set_range_within_word(bit_index(end_full_word), end);
   173 }
   175 void BitMap::clear_large_range(idx_t beg, idx_t end) {
   176   verify_range(beg, end);
   178   idx_t beg_full_word = word_index_round_up(beg);
   179   idx_t end_full_word = word_index(end);
   181   assert(end_full_word - beg_full_word >= 32,
   182          "the range must include at least 32 bytes");
   184   // The range includes at least one full word.
   185   clear_range_within_word(beg, bit_index(beg_full_word));
   186   clear_large_range_of_words(beg_full_word, end_full_word);
   187   clear_range_within_word(bit_index(end_full_word), end);
   188 }
   190 void BitMap::at_put(idx_t offset, bool value) {
   191   if (value) {
   192     set_bit(offset);
   193   } else {
   194     clear_bit(offset);
   195   }
   196 }
   198 // Return true to indicate that this thread changed
   199 // the bit, false to indicate that someone else did.
   200 // In either case, the requested bit is in the
   201 // requested state some time during the period that
   202 // this thread is executing this call. More importantly,
   203 // if no other thread is executing an action to
   204 // change the requested bit to a state other than
   205 // the one that this thread is trying to set it to,
   206 // then the the bit is in the expected state
   207 // at exit from this method. However, rather than
   208 // make such a strong assertion here, based on
   209 // assuming such constrained use (which though true
   210 // today, could change in the future to service some
   211 // funky parallel algorithm), we encourage callers
   212 // to do such verification, as and when appropriate.
   213 bool BitMap::par_at_put(idx_t bit, bool value) {
   214   return value ? par_set_bit(bit) : par_clear_bit(bit);
   215 }
   217 void BitMap::at_put_grow(idx_t offset, bool value) {
   218   if (offset >= size()) {
   219     resize(2 * MAX2(size(), offset));
   220   }
   221   at_put(offset, value);
   222 }
   224 void BitMap::at_put_range(idx_t start_offset, idx_t end_offset, bool value) {
   225   if (value) {
   226     set_range(start_offset, end_offset);
   227   } else {
   228     clear_range(start_offset, end_offset);
   229   }
   230 }
   232 void BitMap::par_at_put_range(idx_t beg, idx_t end, bool value) {
   233   verify_range(beg, end);
   235   idx_t beg_full_word = word_index_round_up(beg);
   236   idx_t end_full_word = word_index(end);
   238   if (beg_full_word < end_full_word) {
   239     // The range includes at least one full word.
   240     par_put_range_within_word(beg, bit_index(beg_full_word), value);
   241     if (value) {
   242       set_range_of_words(beg_full_word, end_full_word);
   243     } else {
   244       clear_range_of_words(beg_full_word, end_full_word);
   245     }
   246     par_put_range_within_word(bit_index(end_full_word), end, value);
   247   } else {
   248     // The range spans at most 2 partial words.
   249     idx_t boundary = MIN2(bit_index(beg_full_word), end);
   250     par_put_range_within_word(beg, boundary, value);
   251     par_put_range_within_word(boundary, end, value);
   252   }
   254 }
   256 void BitMap::at_put_large_range(idx_t beg, idx_t end, bool value) {
   257   if (value) {
   258     set_large_range(beg, end);
   259   } else {
   260     clear_large_range(beg, end);
   261   }
   262 }
   264 void BitMap::par_at_put_large_range(idx_t beg, idx_t end, bool value) {
   265   verify_range(beg, end);
   267   idx_t beg_full_word = word_index_round_up(beg);
   268   idx_t end_full_word = word_index(end);
   270   assert(end_full_word - beg_full_word >= 32,
   271          "the range must include at least 32 bytes");
   273   // The range includes at least one full word.
   274   par_put_range_within_word(beg, bit_index(beg_full_word), value);
   275   if (value) {
   276     set_large_range_of_words(beg_full_word, end_full_word);
   277   } else {
   278     clear_large_range_of_words(beg_full_word, end_full_word);
   279   }
   280   par_put_range_within_word(bit_index(end_full_word), end, value);
   281 }
   283 bool BitMap::contains(const BitMap other) const {
   284   assert(size() == other.size(), "must have same size");
   285   uintptr_t* dest_map = map();
   286   uintptr_t* other_map = other.map();
   287   idx_t size = size_in_words();
   288   for (idx_t index = 0; index < size_in_words(); index++) {
   289     uintptr_t word_union = dest_map[index] | other_map[index];
   290     // If this has more bits set than dest_map[index], then other is not a
   291     // subset.
   292     if (word_union != dest_map[index]) return false;
   293   }
   294   return true;
   295 }
   297 bool BitMap::intersects(const BitMap other) const {
   298   assert(size() == other.size(), "must have same size");
   299   uintptr_t* dest_map = map();
   300   uintptr_t* other_map = other.map();
   301   idx_t size = size_in_words();
   302   for (idx_t index = 0; index < size_in_words(); index++) {
   303     if ((dest_map[index] & other_map[index]) != 0) return true;
   304   }
   305   // Otherwise, no intersection.
   306   return false;
   307 }
   309 void BitMap::set_union(BitMap other) {
   310   assert(size() == other.size(), "must have same size");
   311   idx_t* dest_map = map();
   312   idx_t* other_map = other.map();
   313   idx_t size = size_in_words();
   314   for (idx_t index = 0; index < size_in_words(); index++) {
   315     dest_map[index] = dest_map[index] | other_map[index];
   316   }
   317 }
   320 void BitMap::set_difference(BitMap other) {
   321   assert(size() == other.size(), "must have same size");
   322   idx_t* dest_map = map();
   323   idx_t* other_map = other.map();
   324   idx_t size = size_in_words();
   325   for (idx_t index = 0; index < size_in_words(); index++) {
   326     dest_map[index] = dest_map[index] & ~(other_map[index]);
   327   }
   328 }
   331 void BitMap::set_intersection(BitMap other) {
   332   assert(size() == other.size(), "must have same size");
   333   idx_t* dest_map = map();
   334   idx_t* other_map = other.map();
   335   idx_t size = size_in_words();
   336   for (idx_t index = 0; index < size; index++) {
   337     dest_map[index]  = dest_map[index] & other_map[index];
   338   }
   339 }
   342 bool BitMap::set_union_with_result(BitMap other) {
   343   assert(size() == other.size(), "must have same size");
   344   bool changed = false;
   345   idx_t* dest_map = map();
   346   idx_t* other_map = other.map();
   347   idx_t size = size_in_words();
   348   for (idx_t index = 0; index < size; index++) {
   349     idx_t temp = map(index) | other_map[index];
   350     changed = changed || (temp != map(index));
   351     map()[index] = temp;
   352   }
   353   return changed;
   354 }
   357 bool BitMap::set_difference_with_result(BitMap other) {
   358   assert(size() == other.size(), "must have same size");
   359   bool changed = false;
   360   idx_t* dest_map = map();
   361   idx_t* other_map = other.map();
   362   idx_t size = size_in_words();
   363   for (idx_t index = 0; index < size; index++) {
   364     idx_t temp = dest_map[index] & ~(other_map[index]);
   365     changed = changed || (temp != dest_map[index]);
   366     dest_map[index] = temp;
   367   }
   368   return changed;
   369 }
   372 bool BitMap::set_intersection_with_result(BitMap other) {
   373   assert(size() == other.size(), "must have same size");
   374   bool changed = false;
   375   idx_t* dest_map = map();
   376   idx_t* other_map = other.map();
   377   idx_t size = size_in_words();
   378   for (idx_t index = 0; index < size; index++) {
   379     idx_t orig = dest_map[index];
   380     idx_t temp = orig & other_map[index];
   381     changed = changed || (temp != orig);
   382     dest_map[index]  = temp;
   383   }
   384   return changed;
   385 }
   388 void BitMap::set_from(BitMap other) {
   389   assert(size() == other.size(), "must have same size");
   390   idx_t* dest_map = map();
   391   idx_t* other_map = other.map();
   392   idx_t size = size_in_words();
   393   for (idx_t index = 0; index < size; index++) {
   394     dest_map[index] = other_map[index];
   395   }
   396 }
   399 bool BitMap::is_same(BitMap other) {
   400   assert(size() == other.size(), "must have same size");
   401   idx_t* dest_map = map();
   402   idx_t* other_map = other.map();
   403   idx_t size = size_in_words();
   404   for (idx_t index = 0; index < size; index++) {
   405     if (dest_map[index] != other_map[index]) return false;
   406   }
   407   return true;
   408 }
   410 bool BitMap::is_full() const {
   411   uintptr_t* word = map();
   412   idx_t rest = size();
   413   for (; rest >= (idx_t) BitsPerWord; rest -= BitsPerWord) {
   414     if (*word != (uintptr_t) AllBits) return false;
   415     word++;
   416   }
   417   return rest == 0 || (*word | ~right_n_bits((int)rest)) == (uintptr_t) AllBits;
   418 }
   421 bool BitMap::is_empty() const {
   422   uintptr_t* word = map();
   423   idx_t rest = size();
   424   for (; rest >= (idx_t) BitsPerWord; rest -= BitsPerWord) {
   425     if (*word != (uintptr_t) NoBits) return false;
   426     word++;
   427   }
   428   return rest == 0 || (*word & right_n_bits((int)rest)) == (uintptr_t) NoBits;
   429 }
   431 void BitMap::clear_large() {
   432   clear_large_range_of_words(0, size_in_words());
   433 }
   435 // Note that if the closure itself modifies the bitmap
   436 // then modifications in and to the left of the _bit_ being
   437 // currently sampled will not be seen. Note also that the
   438 // interval [leftOffset, rightOffset) is right open.
   439 void BitMap::iterate(BitMapClosure* blk, idx_t leftOffset, idx_t rightOffset) {
   440   verify_range(leftOffset, rightOffset);
   442   idx_t startIndex = word_index(leftOffset);
   443   idx_t endIndex   = MIN2(word_index(rightOffset) + 1, size_in_words());
   444   for (idx_t index = startIndex, offset = leftOffset;
   445        offset < rightOffset && index < endIndex;
   446        offset = (++index) << LogBitsPerWord) {
   447     idx_t rest = map(index) >> (offset & (BitsPerWord - 1));
   448     for (; offset < rightOffset && rest != (uintptr_t)NoBits; offset++) {
   449       if (rest & 1) {
   450         blk->do_bit(offset);
   451         //  resample at each closure application
   452         // (see, for instance, CMS bug 4525989)
   453         rest = map(index) >> (offset & (BitsPerWord -1));
   454         // XXX debugging: remove
   455         // The following assertion assumes that closure application
   456         // doesn't clear bits (may not be true in general, e.g. G1).
   457         assert(rest & 1,
   458                "incorrect shift or closure application can clear bits?");
   459       }
   460       rest = rest >> 1;
   461     }
   462   }
   463 }
   465 BitMap::idx_t BitMap::get_next_one_offset(idx_t l_offset,
   466                                           idx_t r_offset) const {
   467   assert(l_offset <= size(), "BitMap index out of bounds");
   468   assert(r_offset <= size(), "BitMap index out of bounds");
   469   assert(l_offset <= r_offset, "l_offset > r_offset ?");
   471   if (l_offset == r_offset) {
   472     return l_offset;
   473   }
   474   idx_t   index = word_index(l_offset);
   475   idx_t r_index = word_index(r_offset-1) + 1;
   476   idx_t res_offset = l_offset;
   478   // check bits including and to the _left_ of offset's position
   479   idx_t pos = bit_in_word(res_offset);
   480   idx_t res = map(index) >> pos;
   481   if (res != (uintptr_t)NoBits) {
   482     // find the position of the 1-bit
   483     for (; !(res & 1); res_offset++) {
   484       res = res >> 1;
   485     }
   486     assert(res_offset >= l_offset, "just checking");
   487     return MIN2(res_offset, r_offset);
   488   }
   489   // skip over all word length 0-bit runs
   490   for (index++; index < r_index; index++) {
   491     res = map(index);
   492     if (res != (uintptr_t)NoBits) {
   493       // found a 1, return the offset
   494       for (res_offset = index << LogBitsPerWord; !(res & 1);
   495            res_offset++) {
   496         res = res >> 1;
   497       }
   498       assert(res & 1, "tautology; see loop condition");
   499       assert(res_offset >= l_offset, "just checking");
   500       return MIN2(res_offset, r_offset);
   501     }
   502   }
   503   return r_offset;
   504 }
   506 BitMap::idx_t BitMap::get_next_zero_offset(idx_t l_offset,
   507                                            idx_t r_offset) const {
   508   assert(l_offset <= size(), "BitMap index out of bounds");
   509   assert(r_offset <= size(), "BitMap index out of bounds");
   510   assert(l_offset <= r_offset, "l_offset > r_offset ?");
   512   if (l_offset == r_offset) {
   513     return l_offset;
   514   }
   515   idx_t   index = word_index(l_offset);
   516   idx_t r_index = word_index(r_offset-1) + 1;
   517   idx_t res_offset = l_offset;
   519   // check bits including and to the _left_ of offset's position
   520   idx_t pos = res_offset & (BitsPerWord - 1);
   521   idx_t res = (map(index) >> pos) | left_n_bits((int)pos);
   523   if (res != (uintptr_t)AllBits) {
   524     // find the position of the 0-bit
   525     for (; res & 1; res_offset++) {
   526       res = res >> 1;
   527     }
   528     assert(res_offset >= l_offset, "just checking");
   529     return MIN2(res_offset, r_offset);
   530   }
   531   // skip over all word length 1-bit runs
   532   for (index++; index < r_index; index++) {
   533     res = map(index);
   534     if (res != (uintptr_t)AllBits) {
   535       // found a 0, return the offset
   536       for (res_offset = index << LogBitsPerWord; res & 1;
   537            res_offset++) {
   538         res = res >> 1;
   539       }
   540       assert(!(res & 1), "tautology; see loop condition");
   541       assert(res_offset >= l_offset, "just checking");
   542       return MIN2(res_offset, r_offset);
   543     }
   544   }
   545   return r_offset;
   546 }
   548 #ifndef PRODUCT
   550 void BitMap::print_on(outputStream* st) const {
   551   tty->print("Bitmap(%d):", size());
   552   for (idx_t index = 0; index < size(); index++) {
   553     tty->print("%c", at(index) ? '1' : '0');
   554   }
   555   tty->cr();
   556 }
   558 #endif
   561 BitMap2D::BitMap2D(uintptr_t* map, idx_t size_in_slots, idx_t bits_per_slot)
   562   : _bits_per_slot(bits_per_slot)
   563   , _map(map, size_in_slots * bits_per_slot)
   564 {
   565 }
   568 BitMap2D::BitMap2D(idx_t size_in_slots, idx_t bits_per_slot)
   569   : _bits_per_slot(bits_per_slot)
   570   , _map(size_in_slots * bits_per_slot)
   571 {
   572 }

mercurial