src/share/vm/utilities/bitMap.cpp

Print this page
rev 6170 : 8037959: BitMap::resize frees old map before copying memory if !in_resource_area
Summary: Add reallocate functionality to ArrayAllocator and use it from BitMap::resize
Reviewed-by:
rev 6171 : imported patch alloclimit


   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 #include "precompiled.hpp"
  26 #include "memory/allocation.inline.hpp"

  27 #include "utilities/bitMap.inline.hpp"
  28 #include "utilities/copy.hpp"
  29 #ifdef TARGET_OS_FAMILY_linux
  30 # include "os_linux.inline.hpp"
  31 #endif
  32 #ifdef TARGET_OS_FAMILY_solaris
  33 # include "os_solaris.inline.hpp"
  34 #endif
  35 #ifdef TARGET_OS_FAMILY_windows
  36 # include "os_windows.inline.hpp"
  37 #endif
  38 #ifdef TARGET_OS_FAMILY_aix
  39 # include "os_aix.inline.hpp"
  40 #endif
  41 #ifdef TARGET_OS_FAMILY_bsd
  42 # include "os_bsd.inline.hpp"
  43 #endif
  44 
  45 
  46 BitMap::BitMap(bm_word_t* map, idx_t size_in_bits) :


  50   assert(size_in_bits >= 0, "just checking");
  51 }
  52 
  53 
  54 BitMap::BitMap(idx_t size_in_bits, bool in_resource_area) :
  55   _map(NULL), _size(0), _map_allocator(false)
  56 {
  57   assert(sizeof(bm_word_t) == BytesPerWord, "Implementation assumption.");
  58   resize(size_in_bits, in_resource_area);
  59 }
  60 
  61 void BitMap::resize(idx_t size_in_bits, bool in_resource_area) {
  62   assert(size_in_bits >= 0, "just checking");
  63   idx_t old_size_in_words = size_in_words();
  64   bm_word_t* old_map = map();
  65 
  66   _size = size_in_bits;
  67   idx_t new_size_in_words = size_in_words();
  68   if (in_resource_area) {
  69     _map = NEW_RESOURCE_ARRAY(bm_word_t, new_size_in_words);
  70   } else {
  71     if (old_map != NULL) {
  72       _map_allocator.free();
  73     }
  74     _map = _map_allocator.allocate(new_size_in_words);
  75   }
  76   Copy::disjoint_words((HeapWord*)old_map, (HeapWord*) _map,
  77                        MIN2(old_size_in_words, new_size_in_words));




  78   if (new_size_in_words > old_size_in_words) {
  79     clear_range_of_words(old_size_in_words, size_in_words());
  80   }
  81 }
  82 
  83 void BitMap::set_range_within_word(idx_t beg, idx_t end) {
  84   // With a valid range (beg <= end), this test ensures that end != 0, as
  85   // required by inverted_bit_mask_for_range.  Also avoids an unnecessary write.
  86   if (beg != end) {
  87     bm_word_t mask = inverted_bit_mask_for_range(beg, end);
  88     *word_addr(beg) |= ~mask;
  89   }
  90 }
  91 
  92 void BitMap::clear_range_within_word(idx_t beg, idx_t end) {
  93   // With a valid range (beg <= end), this test ensures that end != 0, as
  94   // required by inverted_bit_mask_for_range.  Also avoids an unnecessary write.
  95   if (beg != end) {
  96     bm_word_t mask = inverted_bit_mask_for_range(beg, end);
  97     *word_addr(beg) &= mask;
  98   }
  99 }


 519     }
 520   }
 521   return sum;
 522 }
 523 
 524 void BitMap::print_on_error(outputStream* st, const char* prefix) const {
 525   st->print_cr("%s[" PTR_FORMAT ", " PTR_FORMAT ")",
 526       prefix, map(), (char*)map() + (size() >> LogBitsPerByte));
 527 }
 528 
 529 #ifndef PRODUCT
 530 
 531 void BitMap::print_on(outputStream* st) const {
 532   tty->print("Bitmap(%d):", size());
 533   for (idx_t index = 0; index < size(); index++) {
 534     tty->print("%c", at(index) ? '1' : '0');
 535   }
 536   tty->cr();
 537 }
 538 













































































 539 #endif
 540 
 541 
 542 BitMap2D::BitMap2D(bm_word_t* map, idx_t size_in_slots, idx_t bits_per_slot)
 543   : _bits_per_slot(bits_per_slot)
 544   , _map(map, size_in_slots * bits_per_slot)
 545 {
 546 }
 547 
 548 
 549 BitMap2D::BitMap2D(idx_t size_in_slots, idx_t bits_per_slot)
 550   : _bits_per_slot(bits_per_slot)
 551   , _map(size_in_slots * bits_per_slot)
 552 {
 553 }


   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 #include "precompiled.hpp"
  26 #include "memory/allocation.inline.hpp"
  27 #include "memory/resourceArea.hpp"
  28 #include "utilities/bitMap.inline.hpp"
  29 #include "utilities/copy.hpp"
  30 #ifdef TARGET_OS_FAMILY_linux
  31 # include "os_linux.inline.hpp"
  32 #endif
  33 #ifdef TARGET_OS_FAMILY_solaris
  34 # include "os_solaris.inline.hpp"
  35 #endif
  36 #ifdef TARGET_OS_FAMILY_windows
  37 # include "os_windows.inline.hpp"
  38 #endif
  39 #ifdef TARGET_OS_FAMILY_aix
  40 # include "os_aix.inline.hpp"
  41 #endif
  42 #ifdef TARGET_OS_FAMILY_bsd
  43 # include "os_bsd.inline.hpp"
  44 #endif
  45 
  46 
  47 BitMap::BitMap(bm_word_t* map, idx_t size_in_bits) :


  51   assert(size_in_bits >= 0, "just checking");
  52 }
  53 
  54 
  55 BitMap::BitMap(idx_t size_in_bits, bool in_resource_area) :
  56   _map(NULL), _size(0), _map_allocator(false)
  57 {
  58   assert(sizeof(bm_word_t) == BytesPerWord, "Implementation assumption.");
  59   resize(size_in_bits, in_resource_area);
  60 }
  61 
  62 void BitMap::resize(idx_t size_in_bits, bool in_resource_area) {
  63   assert(size_in_bits >= 0, "just checking");
  64   idx_t old_size_in_words = size_in_words();
  65   bm_word_t* old_map = map();
  66 
  67   _size = size_in_bits;
  68   idx_t new_size_in_words = size_in_words();
  69   if (in_resource_area) {
  70     _map = NEW_RESOURCE_ARRAY(bm_word_t, new_size_in_words);






  71     Copy::disjoint_words((HeapWord*)old_map, (HeapWord*) _map,
  72                          MIN2(old_size_in_words, new_size_in_words));
  73   } else {
  74     _map = _map_allocator.reallocate(new_size_in_words);
  75   }
  76 
  77   if (new_size_in_words > old_size_in_words) {
  78     clear_range_of_words(old_size_in_words, new_size_in_words);
  79   }
  80 }
  81 
  82 void BitMap::set_range_within_word(idx_t beg, idx_t end) {
  83   // With a valid range (beg <= end), this test ensures that end != 0, as
  84   // required by inverted_bit_mask_for_range.  Also avoids an unnecessary write.
  85   if (beg != end) {
  86     bm_word_t mask = inverted_bit_mask_for_range(beg, end);
  87     *word_addr(beg) |= ~mask;
  88   }
  89 }
  90 
  91 void BitMap::clear_range_within_word(idx_t beg, idx_t end) {
  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     bm_word_t mask = inverted_bit_mask_for_range(beg, end);
  96     *word_addr(beg) &= mask;
  97   }
  98 }


 518     }
 519   }
 520   return sum;
 521 }
 522 
 523 void BitMap::print_on_error(outputStream* st, const char* prefix) const {
 524   st->print_cr("%s[" PTR_FORMAT ", " PTR_FORMAT ")",
 525       prefix, map(), (char*)map() + (size() >> LogBitsPerByte));
 526 }
 527 
 528 #ifndef PRODUCT
 529 
 530 void BitMap::print_on(outputStream* st) const {
 531   tty->print("Bitmap(%d):", size());
 532   for (idx_t index = 0; index < size(); index++) {
 533     tty->print("%c", at(index) ? '1' : '0');
 534   }
 535   tty->cr();
 536 }
 537 
 538 class TestBitMap : public AllStatic {
 539   const static BitMap::idx_t BITMAP_SIZE = 1024;
 540   static void fillBitMap(BitMap& map) {
 541     map.set_bit(1);
 542     map.set_bit(3);
 543     map.set_bit(17);
 544     map.set_bit(512);
 545   }
 546 
 547   static void testResize(bool in_resource_area) {
 548     {
 549       BitMap map(0, in_resource_area);
 550       map.resize(BITMAP_SIZE, in_resource_area);
 551       fillBitMap(map);
 552 
 553       BitMap map2(BITMAP_SIZE, in_resource_area);
 554       fillBitMap(map2);
 555       assert(map.is_same(map2), "could be");
 556     }
 557 
 558     {
 559       BitMap map(128, in_resource_area);
 560       map.resize(BITMAP_SIZE, in_resource_area);
 561       fillBitMap(map);
 562 
 563       BitMap map2(BITMAP_SIZE, in_resource_area);
 564       fillBitMap(map2);
 565       assert(map.is_same(map2), "could be");
 566     }
 567 
 568     {
 569       BitMap map(BITMAP_SIZE, in_resource_area);
 570       map.resize(BITMAP_SIZE, in_resource_area);
 571       fillBitMap(map);
 572 
 573       BitMap map2(BITMAP_SIZE, in_resource_area);
 574       fillBitMap(map2);
 575       assert(map.is_same(map2), "could be");
 576     }
 577   }
 578 
 579   static void testResizeResource() {
 580     ResourceMark rm;
 581     testResize(true);
 582   }
 583 
 584   static void testResizeNonResource() {
 585     const uintx bitmap_bytes = BITMAP_SIZE / BitsPerByte;
 586 
 587     // Test the default behavior
 588     testResize(false);
 589 
 590     {
 591       // Make sure that AllocatorMallocLimit is larger than our allocation request
 592       // forcing it to call standard malloc()
 593       UIntFlagSetting fs(ArrayAllocatorMallocLimit, bitmap_bytes * 4);
 594       testResize(false);
 595     }
 596     {
 597       // Make sure that AllocatorMallocLimit is smaller than our allocation request
 598       // forcing it to call mmap() (or equivalent)
 599       UIntFlagSetting fs(ArrayAllocatorMallocLimit, bitmap_bytes / 4);
 600       testResize(false);
 601     }
 602   }
 603 
 604  public:
 605   static void test() {
 606     testResizeResource();
 607     testResizeNonResource();
 608   }
 609 
 610 };
 611 
 612 void TestBitMap_test() {
 613   TestBitMap::test();
 614 }
 615 #endif
 616 
 617 
 618 BitMap2D::BitMap2D(bm_word_t* map, idx_t size_in_slots, idx_t bits_per_slot)
 619   : _bits_per_slot(bits_per_slot)
 620   , _map(map, size_in_slots * bits_per_slot)
 621 {
 622 }
 623 
 624 
 625 BitMap2D::BitMap2D(idx_t size_in_slots, idx_t bits_per_slot)
 626   : _bits_per_slot(bits_per_slot)
 627   , _map(size_in_slots * bits_per_slot)
 628 {
 629 }