1 /*
   2  * Copyright (c) 2000, 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 package sun.jvm.hotspot.gc.serial;
  26 
  27 import java.io.*;
  28 import java.util.*;
  29 
  30 import sun.jvm.hotspot.debugger.*;
  31 import sun.jvm.hotspot.gc.shared.*;
  32 import sun.jvm.hotspot.runtime.*;
  33 import sun.jvm.hotspot.types.*;
  34 
  35 /** DefNewGeneration is a young generation containing eden, from- and
  36     to-space. */
  37 
  38 public class DefNewGeneration extends Generation {
  39   protected static AddressField edenSpaceField;
  40   protected static AddressField fromSpaceField;
  41   protected static AddressField toSpaceField;
  42 
  43   static {
  44     VM.registerVMInitializedObserver(new Observer() {
  45         public void update(Observable o, Object data) {
  46           initialize(VM.getVM().getTypeDataBase());
  47         }
  48       });
  49   }
  50 
  51   private static synchronized void initialize(TypeDataBase db) {
  52     Type type = db.lookupType("DefNewGeneration");
  53 
  54     edenSpaceField = type.getAddressField("_eden_space");
  55     fromSpaceField = type.getAddressField("_from_space");
  56     toSpaceField   = type.getAddressField("_to_space");
  57   }
  58 
  59   public DefNewGeneration(Address addr) {
  60     super(addr);
  61   }
  62 
  63   public Generation.Name kind() {
  64     return Generation.Name.DEF_NEW;
  65   }
  66 
  67   // Accessing spaces
  68   public ContiguousSpace eden() {
  69     return (ContiguousSpace) VMObjectFactory.newObject(ContiguousSpace.class, edenSpaceField.getValue(addr));
  70   }
  71 
  72   public ContiguousSpace from() {
  73     return (ContiguousSpace) VMObjectFactory.newObject(ContiguousSpace.class, fromSpaceField.getValue(addr));
  74   }
  75 
  76   public ContiguousSpace to() {
  77     return (ContiguousSpace) VMObjectFactory.newObject(ContiguousSpace.class, toSpaceField.getValue(addr));
  78   }
  79 
  80   public long capacity()            { return eden().capacity() + from().capacity(); /* to() is only used during scavenge */ }
  81   public long used()                { return eden().used()     + from().used();     /* to() is only used during scavenge */ }
  82   public long free()                { return eden().free()     + from().free();     /* to() is only used during scavenge */ }
  83   public long contiguousAvailable() { return eden().free(); }
  84 
  85   public String name() {
  86     return "default new generation";
  87   }
  88 
  89   public void spaceIterate(SpaceClosure blk, boolean usedOnly) {
  90     blk.doSpace(eden());
  91     blk.doSpace(from());
  92     if (!usedOnly) {
  93       blk.doSpace(to());
  94     }
  95   }
  96 
  97   public void liveRegionsIterate(LiveRegionsClosure closure) {
  98     closure.doLiveRegions(eden());
  99     closure.doLiveRegions(from());
 100   }
 101 
 102   public void printOn(PrintStream tty) {
 103     tty.print("  eden");
 104     eden().printOn(tty);
 105     tty.print("\n  from");
 106     from().printOn(tty);
 107     tty.print("\n  to  ");
 108     to().printOn(tty);
 109   }
 110 }