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/_spaceDecorator.cpp.incl"
  27 
  28 // Catch-all file for utility classes
  29 
  30 #ifndef PRODUCT
  31 
  32 // Returns true is the location q matches the mangling
  33 // pattern.
  34 bool SpaceMangler::is_mangled(HeapWord* q) {
  35   // This test loses precision but is good enough
  36   return badHeapWord == (max_juint & (uintptr_t) q->value());
  37 }
  38 
  39 
  40 void SpaceMangler::set_top_for_allocations(HeapWord* v)  {
  41   if (v < end()) {
  42     assert(!CheckZapUnusedHeapArea || is_mangled(v),
  43       "The high water mark is not mangled");
  44   }
  45   _top_for_allocations = v;
  46 }
  47 
  48 // Mangle only the unused space that has not previously
  49 // been mangled and that has not been allocated since being
  50 // mangled.
  51 void SpaceMangler::mangle_unused_area() {
  52   assert(ZapUnusedHeapArea, "Mangling should not be in use");
  53   // Mangle between top and the high water mark.  Safeguard
  54   // against the space changing since top_for_allocations was
  55   // set.
  56   HeapWord* mangled_end = MIN2(top_for_allocations(), end());
  57   if (top() < mangled_end) {
  58     MemRegion mangle_mr(top(), mangled_end);
  59     SpaceMangler::mangle_region(mangle_mr);
  60     // Light weight check of mangling.
  61     check_mangled_unused_area(end());
  62   }
  63   // Complete check of unused area which is functional when
  64   // DEBUG_MANGLING is defined.
  65   check_mangled_unused_area_complete();
  66 }
  67 
  68 // A complete mangle is expected in the
  69 // exceptional case where top_for_allocations is not
  70 // properly tracking the high water mark for mangling.
  71 // This can be the case when to-space is being used for
  72 // scratch space during a mark-sweep-compact.  See
  73 // contribute_scratch() and PSMarkSweep::allocate_stacks().
  74 void SpaceMangler::mangle_unused_area_complete() {
  75   assert(ZapUnusedHeapArea, "Mangling should not be in use");
  76   MemRegion mangle_mr(top(), end());
  77   SpaceMangler::mangle_region(mangle_mr);
  78 }
  79 
  80 // Simply mangle the MemRegion mr.
  81 void SpaceMangler::mangle_region(MemRegion mr) {
  82   assert(ZapUnusedHeapArea, "Mangling should not be in use");
  83 #ifdef ASSERT
  84   if(TraceZapUnusedHeapArea) {
  85     gclog_or_tty->print("Mangling [0x%x to 0x%x)", mr.start(), mr.end());
  86   }
  87   Copy::fill_to_words(mr.start(), mr.word_size(), badHeapWord);
  88   if(TraceZapUnusedHeapArea) {
  89     gclog_or_tty->print_cr(" done");
  90   }
  91 #endif
  92 }
  93 
  94 // Check that top, top_for_allocations and the last
  95 // word of the space are mangled.  In a tight memory
  96 // situation even this light weight mangling could
  97 // cause paging by touching the end of the space.
  98 void  SpaceMangler::check_mangled_unused_area(HeapWord* limit) {
  99   if (CheckZapUnusedHeapArea) {
 100     // This method can be called while the spaces are
 101     // being reshaped so skip the test if the end of the
 102     // space is beyond the specified limit;
 103     if (end() > limit) return;
 104 
 105     assert(top() == end() ||
 106            (is_mangled(top())), "Top not mangled");
 107     assert((top_for_allocations() < top()) ||
 108            (top_for_allocations() >= end()) ||
 109            (is_mangled(top_for_allocations())),
 110            "Older unused not mangled");
 111     assert(top() == end() ||
 112            (is_mangled(end() - 1)), "End not properly mangled");
 113     // Only does checking when DEBUG_MANGLING is defined.
 114     check_mangled_unused_area_complete();
 115   }
 116 }
 117 
 118 #undef DEBUG_MANGLING
 119 // This should only be used while debugging the mangling
 120 // because of the high cost of checking the completeness.
 121 void  SpaceMangler::check_mangled_unused_area_complete() {
 122   if (CheckZapUnusedHeapArea) {
 123     assert(ZapUnusedHeapArea, "Not mangling unused area");
 124 #ifdef DEBUG_MANGLING
 125     HeapWord* q = top();
 126     HeapWord* limit = end();
 127 
 128     bool passed = true;
 129     while (q < limit) {
 130       if (!is_mangled(q)) {
 131         passed = false;
 132         break;
 133       }
 134       q++;
 135     }
 136     assert(passed, "Mangling is not complete");
 137 #endif
 138   }
 139 }
 140 #undef DEBUG_MANGLING
 141 #endif // not PRODUCT