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 class SpaceDecorator: public AllStatic {
  26  public:
  27   // Initialization flags.
  28   static const bool Clear               = true;
  29   static const bool DontClear           = false;
  30   static const bool Mangle              = true;
  31   static const bool DontMangle          = false;
  32 };
  33 
  34 // Functionality for use with class Space and class MutableSpace.
  35 //   The approach taken with the mangling is to mangle all
  36 // the space initially and then to mangle areas that have
  37 // been allocated since the last collection.  Mangling is
  38 // done in the context of a generation and in the context
  39 // of a space.
  40 //   The space in a generation is mangled when it is first
  41 // initialized and when the generation grows.  The spaces
  42 // are not necessarily up-to-date when this mangling occurs
  43 // and the method mangle_region() is used.
  44 //   After allocations have been done in a space, the space generally
  45 // need to be remangled.  Remangling is only done on the
  46 // recently allocated regions in the space.  Typically, that is
  47 // the region between the new top and the top just before a
  48 // garbage collection.
  49 //   An exception to the usual mangling in a space is done when the
  50 // space is used for an extraordinary purpose.  Specifically, when
  51 // to-space is used as scratch space for a mark-sweep-compact
  52 // collection.
  53 //   Spaces are mangled after a collection.  If the generation
  54 // grows after a collection, the added space is mangled as part of
  55 // the growth of the generation.  No additional mangling is needed when the
  56 // spaces are resized after an expansion.
  57 //   The class SpaceMangler keeps a pointer to the top of the allocated
  58 // area and provides the methods for doing the piece meal mangling.
  59 // Methods for doing sparces and full checking of the mangling are
  60 // included.  The full checking is done if DEBUG_MANGLING is defined.
  61 //   GenSpaceMangler is used with the GenCollectedHeap collectors and
  62 // MutableSpaceMangler is used with the ParallelScavengeHeap collectors.
  63 // These subclasses abstract the differences in the types of spaces used
  64 // by each heap.
  65 
  66 class SpaceMangler: public CHeapObj {
  67   friend class VMStructs;
  68 
  69   // High water mark for allocations.  Typically, the space above
  70   // this point have been mangle previously and don't need to be
  71   // touched again.  Space belows this point has been allocated
  72   // and remangling is needed between the current top and this
  73   // high water mark.
  74   HeapWord* _top_for_allocations;
  75   HeapWord* top_for_allocations() { return _top_for_allocations; }
  76 
  77  public:
  78 
  79   // Setting _top_for_allocations to NULL at initialization
  80   // makes it always below top so that mangling done as part
  81   // of the initialize() call of a space does nothing (as it
  82   // should since the mangling is done as part of the constructor
  83   // for the space.
  84   SpaceMangler() : _top_for_allocations(NULL) {}
  85 
  86   // Methods for top and end that delegate to the specific
  87   // space type.
  88   virtual HeapWord* top() const = 0;
  89   virtual HeapWord* end() const = 0;
  90 
  91   // Return true if q matches the mangled pattern.
  92   static bool is_mangled(HeapWord* q) PRODUCT_RETURN0;
  93 
  94   // Used to save the an address in a space for later use during mangling.
  95   void set_top_for_allocations(HeapWord* v);
  96 
  97   // Overwrites the unused portion of this space.
  98   // Mangle only the region not previously mangled [top, top_previously_mangled)
  99   void mangle_unused_area();
 100   // Mangle all the unused region [top, end)
 101   void mangle_unused_area_complete();
 102   // Do some sparse checking on the area that should have been mangled.
 103   void check_mangled_unused_area(HeapWord* limit) PRODUCT_RETURN;
 104   // Do a complete check of the area that should be mangled.
 105   void check_mangled_unused_area_complete() PRODUCT_RETURN;
 106 
 107   // Mangle the MemRegion.  This is a non-space specific mangler.  It
 108   // is used during the initial mangling of a space before the space
 109   // is fully constructed.  Also is used when a generation is expanded
 110   // and possibly before the spaces have been reshaped to to the new
 111   // size of the generation.
 112   static void mangle_region(MemRegion mr) PRODUCT_RETURN;
 113 };
 114 
 115 class ContiguousSpace;
 116 
 117 // For use with GenCollectedHeap's
 118 class GenSpaceMangler: public SpaceMangler {
 119   ContiguousSpace* _sp;
 120 
 121   ContiguousSpace* sp() { return _sp; }
 122 
 123   HeapWord* top() const { return _sp->top(); }
 124   HeapWord* end() const { return _sp->end(); }
 125 
 126  public:
 127   GenSpaceMangler(ContiguousSpace* sp) : SpaceMangler(), _sp(sp) {}
 128 };
 129 
 130 // For use with ParallelScavengeHeap's.
 131 class MutableSpaceMangler: public SpaceMangler {
 132   MutableSpace* _sp;
 133 
 134   MutableSpace* sp() { return _sp; }
 135 
 136   HeapWord* top() const { return _sp->top(); }
 137   HeapWord* end() const { return _sp->end(); }
 138 
 139  public:
 140   MutableSpaceMangler(MutableSpace* sp) : SpaceMangler(), _sp(sp) {}
 141 };