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