1 /*
   2  * Copyright (c) 2011, 2016, 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 #include "precompiled.hpp"
  25 #include "gc/g1/g1CollectedHeap.inline.hpp"
  26 #include "unittest.hpp"
  27 
  28 // @requires UseG1GC
  29 TEST_VM(FreeRegionList, length) {
  30   if (!UseG1GC) {
  31     return;
  32   }
  33 
  34   FreeRegionList l("test");
  35   const uint num_regions_in_test = 5;
  36 
  37   // Create a fake heap. It does not need to be valid, as the HeapRegion constructor
  38   // does not access it.
  39   MemRegion heap(NULL, num_regions_in_test * HeapRegion::GrainWords);
  40 
  41   // Allocate a fake BOT because the HeapRegion constructor initializes
  42   // the BOT.
  43   size_t bot_size = G1BlockOffsetTable::compute_size(heap.word_size());
  44   HeapWord* bot_data = NEW_C_HEAP_ARRAY(HeapWord, bot_size, mtGC);
  45   ReservedSpace bot_rs(G1BlockOffsetTable::compute_size(heap.word_size()));
  46   G1RegionToSpaceMapper* bot_storage =
  47     G1RegionToSpaceMapper::create_mapper(bot_rs,
  48                                          bot_rs.size(),
  49                                          os::vm_page_size(),
  50                                          HeapRegion::GrainBytes,
  51                                          BOTConstants::N_bytes,
  52                                          mtGC);
  53   G1BlockOffsetTable bot(heap, bot_storage);
  54   bot_storage->commit_regions(0, num_regions_in_test);
  55 
  56   // Set up memory regions for the heap regions.
  57   MemRegion mr0(heap.start(), HeapRegion::GrainWords);
  58   MemRegion mr1(mr0.end(), HeapRegion::GrainWords);
  59   MemRegion mr2(mr1.end(), HeapRegion::GrainWords);
  60   MemRegion mr3(mr2.end(), HeapRegion::GrainWords);
  61   MemRegion mr4(mr3.end(), HeapRegion::GrainWords);
  62 
  63   HeapRegion hr0(0, &bot, mr0);
  64   HeapRegion hr1(1, &bot, mr1);
  65   HeapRegion hr2(2, &bot, mr2);
  66   HeapRegion hr3(3, &bot, mr3);
  67   HeapRegion hr4(4, &bot, mr4);
  68   l.add_ordered(&hr1);
  69   l.add_ordered(&hr0);
  70   l.add_ordered(&hr3);
  71   l.add_ordered(&hr4);
  72   l.add_ordered(&hr2);
  73 
  74   EXPECT_EQ(l.length(), num_regions_in_test) << "Wrong free region list length";
  75   l.verify_list();
  76 
  77   bot_storage->uncommit_regions(0, num_regions_in_test);
  78   delete bot_storage;
  79   FREE_C_HEAP_ARRAY(HeapWord, bot_data);
  80 }