1 /*
   2  * Copyright (c) 2002, 2008, 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 #include "incls/_precompiled.incl"
  26 #include "incls/_psPromotionLAB.cpp.incl"
  27 
  28 size_t PSPromotionLAB::filler_header_size;
  29 
  30 // This is the shared initialization code. It sets up the basic pointers,
  31 // and allows enough extra space for a filler object. We call a virtual
  32 // method, "lab_is_valid()" to handle the different asserts the old/young
  33 // labs require.
  34 void PSPromotionLAB::initialize(MemRegion lab) {
  35   assert(lab_is_valid(lab), "Sanity");
  36 
  37   HeapWord* bottom = lab.start();
  38   HeapWord* end    = lab.end();
  39 
  40   set_bottom(bottom);
  41   set_end(end);
  42   set_top(bottom);
  43 
  44   // Initialize after VM starts up because header_size depends on compressed
  45   // oops.
  46   filler_header_size = align_object_size(typeArrayOopDesc::header_size(T_INT));
  47 
  48   // We can be initialized to a zero size!
  49   if (free() > 0) {
  50     if (ZapUnusedHeapArea) {
  51       debug_only(Copy::fill_to_words(top(), free()/HeapWordSize, badHeapWord));
  52     }
  53 
  54     // NOTE! We need to allow space for a filler object.
  55     assert(lab.word_size() >= filler_header_size, "lab is too small");
  56     end = end - filler_header_size;
  57     set_end(end);
  58 
  59     _state = needs_flush;
  60   } else {
  61     _state = zero_size;
  62   }
  63 
  64   assert(this->top() <= this->end(), "pointers out of order");
  65 }
  66 
  67 // Fill all remaining lab space with an unreachable object.
  68 // The goal is to leave a contiguous parseable span of objects.
  69 void PSPromotionLAB::flush() {
  70   assert(_state != flushed, "Attempt to flush PLAB twice");
  71   assert(top() <= end(), "pointers out of order");
  72 
  73   // If we were initialized to a zero sized lab, there is
  74   // nothing to flush
  75   if (_state == zero_size)
  76     return;
  77 
  78   // PLAB's never allocate the last aligned_header_size
  79   // so they can always fill with an array.
  80   HeapWord* tlab_end = end() + filler_header_size;
  81   typeArrayOop filler_oop = (typeArrayOop) top();
  82   filler_oop->set_mark(markOopDesc::prototype());
  83   filler_oop->set_klass(Universe::intArrayKlassObj());
  84   const size_t array_length =
  85     pointer_delta(tlab_end, top()) - typeArrayOopDesc::header_size(T_INT);
  86   assert( (array_length * (HeapWordSize/sizeof(jint))) < (size_t)max_jint, "array too big in PSPromotionLAB");
  87   filler_oop->set_length((int)(array_length * (HeapWordSize/sizeof(jint))));
  88 
  89 #ifdef ASSERT
  90   // Note that we actually DO NOT want to use the aligned header size!
  91   HeapWord* elt_words = ((HeapWord*)filler_oop) + typeArrayOopDesc::header_size(T_INT);
  92   Copy::fill_to_words(elt_words, array_length, 0xDEAABABE);
  93 #endif
  94 
  95   set_bottom(NULL);
  96   set_end(NULL);
  97   set_top(NULL);
  98 
  99   _state = flushed;
 100 }
 101 
 102 bool PSPromotionLAB::unallocate_object(oop obj) {
 103   assert(Universe::heap()->is_in(obj), "Object outside heap");
 104 
 105   if (contains(obj)) {
 106     HeapWord* object_end = (HeapWord*)obj + obj->size();
 107     assert(object_end <= top(), "Object crosses promotion LAB boundary");
 108 
 109     if (object_end == top()) {
 110       set_top((HeapWord*)obj);
 111       return true;
 112     }
 113   }
 114 
 115   return false;
 116 }
 117 
 118 // Fill all remaining lab space with an unreachable object.
 119 // The goal is to leave a contiguous parseable span of objects.
 120 void PSOldPromotionLAB::flush() {
 121   assert(_state != flushed, "Attempt to flush PLAB twice");
 122   assert(top() <= end(), "pointers out of order");
 123 
 124   if (_state == zero_size)
 125     return;
 126 
 127   HeapWord* obj = top();
 128 
 129   PSPromotionLAB::flush();
 130 
 131   assert(_start_array != NULL, "Sanity");
 132 
 133   _start_array->allocate_block(obj);
 134 }
 135 
 136 #ifdef ASSERT
 137 
 138 bool PSYoungPromotionLAB::lab_is_valid(MemRegion lab) {
 139   ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
 140   assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
 141 
 142   MutableSpace* to_space = heap->young_gen()->to_space();
 143   MemRegion used = to_space->used_region();
 144   if (used.contains(lab)) {
 145     return true;
 146   }
 147 
 148   return false;
 149 }
 150 
 151 bool PSOldPromotionLAB::lab_is_valid(MemRegion lab) {
 152   ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
 153   assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
 154   assert(_start_array->covered_region().contains(lab), "Sanity");
 155 
 156   PSOldGen* old_gen = heap->old_gen();
 157   MemRegion used = old_gen->object_space()->used_region();
 158 
 159   if (used.contains(lab)) {
 160     return true;
 161   }
 162 
 163   return false;
 164 }
 165 
 166 #endif /* ASSERT */