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.shared;
  26 
  27 import java.io.*;
  28 import java.util.*;
  29 import sun.jvm.hotspot.debugger.*;
  30 import sun.jvm.hotspot.memory.*;
  31 import sun.jvm.hotspot.runtime.*;
  32 import sun.jvm.hotspot.types.*;
  33 
  34 /** <P> A Space describes a heap area. Class Space is an abstract base
  35     class. </P>
  36 
  37     <P> Space supports allocation, size computation and GC support is
  38     provided. </P>
  39 
  40     <P> Invariant: bottom() and end() are on page_size boundaries and: </P>
  41 
  42     <P> bottom() <= top() <= end() </P>
  43 
  44     <P> top() is inclusive and end() is exclusive. </P> */
  45 
  46 public abstract class Space extends VMObject {
  47   private static AddressField bottomField;
  48   private static AddressField endField;
  49 
  50   static {
  51     VM.registerVMInitializedObserver(new Observer() {
  52         public void update(Observable o, Object data) {
  53           initialize(VM.getVM().getTypeDataBase());
  54         }
  55       });
  56   }
  57 
  58   private static synchronized void initialize(TypeDataBase db) {
  59     Type type = db.lookupType("Space");
  60 
  61     bottomField = type.getAddressField("_bottom");
  62     endField    = type.getAddressField("_end");
  63   }
  64 
  65   public Space(Address addr) {
  66     super(addr);
  67   }
  68 
  69   public Address   bottom()       { return bottomField.getValue(addr); }
  70   public Address   end()          { return endField.getValue(addr);    }
  71 
  72   /** Returns a subregion of the space containing all the objects in
  73       the space. */
  74   public MemRegion usedRegion() {
  75     return new MemRegion(bottom(), end());
  76   }
  77 
  78   /** Support for iteration over heap -- not sure how this will
  79       interact with GC in reflective system, but necessary for the
  80       debugging mechanism */
  81   public OopHandle bottomAsOopHandle() {
  82     return bottomField.getOopHandle(addr);
  83   }
  84 
  85   /** Support for iteration over heap -- not sure how this will
  86       interact with GC in reflective system, but necessary for the
  87       debugging mechanism */
  88   public OopHandle nextOopHandle(OopHandle handle, long size) {
  89     return handle.addOffsetToAsOopHandle(size);
  90   }
  91 
  92   /** Returned value is in bytes */
  93   public long capacity() { return end().minus(bottom()); }
  94   /** Returned value is in bytes */
  95   public abstract long used();
  96   /** Returned value is in bytes */
  97   public abstract long free();
  98 
  99   /** Testers */
 100   public boolean contains(Address p) {
 101     return (bottom().lessThanOrEqual(p) && end().greaterThan(p));
 102   }
 103 
 104   public void print() { printOn(System.out); }
 105   public void printOn(PrintStream tty) {
 106     tty.print(" space capacity = ");
 107     tty.print(capacity());
 108     tty.print(", ");
 109     tty.print((double) used() * 100.0/ capacity());
 110     tty.print(" used");
 111   }
 112 }