< prev index next >

src/jdk.vm.ci/share/classes/jdk.vm.ci.hotspot/src/jdk/vm/ci/hotspot/HotSpotJVMCIMetaAccessContext.java

Print this page




  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 package jdk.vm.ci.hotspot;
  24 
  25 import java.lang.ref.Reference;
  26 import java.lang.ref.ReferenceQueue;
  27 import java.lang.ref.WeakReference;
  28 import java.util.Arrays;
  29 import java.util.Iterator;
  30 import java.util.Map;
  31 import java.util.WeakHashMap;
  32 
  33 import jdk.vm.ci.meta.JVMCIMetaAccessContext;
  34 import jdk.vm.ci.meta.JavaKind;
  35 import jdk.vm.ci.meta.ResolvedJavaType;
  36 
  37 /**
  38  * This class manages the set of metadata roots that must be scanned during garbage collection.
  39  * Because of class redefinition Method* and ConstantPool* can be freed if they don't appear to be
  40  * in use so they must be tracked when there are live references to them from Java.
  41  *
  42  * The general theory of operation is that all {@link MetaspaceWrapperObject}s are created by
  43  * calling into the VM which calls back out to actually create the wrapper instance. During the call
  44  * the VM keeps the metadata reference alive through the use of metadata handles. Once the call
  45  * completes the wrapper object is registered here and will be scanned during metadata scanning. The
  46  * weakness of the reference to the wrapper object allows them to be reclaimed when they are no
  47  * longer used.
  48  *
  49  */
  50 public class HotSpotJVMCIMetaAccessContext implements JVMCIMetaAccessContext {
  51 
  52     /**
  53      * The set of currently live contexts used for tracking of live metadata. Examined from the VM
  54      * during garbage collection.
  55      */
  56     private static WeakReference<?>[] allContexts = new WeakReference<?>[0];
  57 
  58     /**
  59      * This is a chunked list of metadata roots. It can be read from VM native code so it's been
  60      * marked volatile to ensure the order of updates are respected.
  61      */
  62     private volatile Object[] metadataRoots;
  63 
  64     private ChunkedList<WeakReference<MetaspaceWrapperObject>> list = new ChunkedList<>();
  65 
  66     /**
  67      * The number of weak references freed since the last time the list was shrunk.
  68      */
  69     private int freed;
  70 


 132         list.add(new WeakReference<>(metaspaceObject, queue));
 133         if (list.getHead() != metadataRoots) {
 134             /*
 135              * The list enlarged so update the head.
 136              */
 137             metadataRoots = list.getHead();
 138         }
 139     }
 140 
 141     protected ResolvedJavaType createClass(Class<?> javaClass) {
 142         if (javaClass.isPrimitive()) {
 143             JavaKind kind = JavaKind.fromJavaClass(javaClass);
 144             return new HotSpotResolvedPrimitiveType(kind);
 145         } else {
 146             return new HotSpotResolvedObjectTypeImpl(javaClass, this);
 147         }
 148     }
 149 
 150     private final Map<Class<?>, WeakReference<ResolvedJavaType>> typeMap = new WeakHashMap<>();
 151 
 152     @Override




 153     public synchronized ResolvedJavaType fromClass(Class<?> javaClass) {
 154         WeakReference<ResolvedJavaType> typeRef = typeMap.get(javaClass);
 155         ResolvedJavaType type = typeRef != null ? typeRef.get() : null;
 156         if (type == null) {
 157             type = createClass(javaClass);
 158             typeMap.put(javaClass, new WeakReference<>(type));
 159         }
 160         return type;
 161     }
 162 
 163     /**
 164      * A very simple append only chunked list implementation.
 165      */
 166     static class ChunkedList<T> implements Iterable<T> {
 167         private static final int CHUNK_SIZE = 32;
 168 
 169         private static final int NEXT_CHUNK_INDEX = CHUNK_SIZE - 1;
 170 
 171         private Object[] head;
 172         private int index;




  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 package jdk.vm.ci.hotspot;
  24 
  25 import java.lang.ref.Reference;
  26 import java.lang.ref.ReferenceQueue;
  27 import java.lang.ref.WeakReference;
  28 import java.util.Arrays;
  29 import java.util.Iterator;
  30 import java.util.Map;
  31 import java.util.WeakHashMap;
  32 

  33 import jdk.vm.ci.meta.JavaKind;
  34 import jdk.vm.ci.meta.ResolvedJavaType;
  35 
  36 /**
  37  * This class manages the set of metadata roots that must be scanned during garbage collection.
  38  * Because of class redefinition Method* and ConstantPool* can be freed if they don't appear to be
  39  * in use so they must be tracked when there are live references to them from Java.
  40  *
  41  * The general theory of operation is that all {@link MetaspaceWrapperObject}s are created by
  42  * calling into the VM which calls back out to actually create the wrapper instance. During the call
  43  * the VM keeps the metadata reference alive through the use of metadata handles. Once the call
  44  * completes the wrapper object is registered here and will be scanned during metadata scanning. The
  45  * weakness of the reference to the wrapper object allows them to be reclaimed when they are no
  46  * longer used.
  47  *
  48  */
  49 public class HotSpotJVMCIMetaAccessContext {
  50 
  51     /**
  52      * The set of currently live contexts used for tracking of live metadata. Examined from the VM
  53      * during garbage collection.
  54      */
  55     private static WeakReference<?>[] allContexts = new WeakReference<?>[0];
  56 
  57     /**
  58      * This is a chunked list of metadata roots. It can be read from VM native code so it's been
  59      * marked volatile to ensure the order of updates are respected.
  60      */
  61     private volatile Object[] metadataRoots;
  62 
  63     private ChunkedList<WeakReference<MetaspaceWrapperObject>> list = new ChunkedList<>();
  64 
  65     /**
  66      * The number of weak references freed since the last time the list was shrunk.
  67      */
  68     private int freed;
  69 


 131         list.add(new WeakReference<>(metaspaceObject, queue));
 132         if (list.getHead() != metadataRoots) {
 133             /*
 134              * The list enlarged so update the head.
 135              */
 136             metadataRoots = list.getHead();
 137         }
 138     }
 139 
 140     protected ResolvedJavaType createClass(Class<?> javaClass) {
 141         if (javaClass.isPrimitive()) {
 142             JavaKind kind = JavaKind.fromJavaClass(javaClass);
 143             return new HotSpotResolvedPrimitiveType(kind);
 144         } else {
 145             return new HotSpotResolvedObjectTypeImpl(javaClass, this);
 146         }
 147     }
 148 
 149     private final Map<Class<?>, WeakReference<ResolvedJavaType>> typeMap = new WeakHashMap<>();
 150 
 151     /**
 152      * Gets the JVMCI mirror for a {@link Class} object.
 153      *
 154      * @return the {@link ResolvedJavaType} corresponding to {@code javaClass}
 155      */
 156     public synchronized ResolvedJavaType fromClass(Class<?> javaClass) {
 157         WeakReference<ResolvedJavaType> typeRef = typeMap.get(javaClass);
 158         ResolvedJavaType type = typeRef != null ? typeRef.get() : null;
 159         if (type == null) {
 160             type = createClass(javaClass);
 161             typeMap.put(javaClass, new WeakReference<>(type));
 162         }
 163         return type;
 164     }
 165 
 166     /**
 167      * A very simple append only chunked list implementation.
 168      */
 169     static class ChunkedList<T> implements Iterable<T> {
 170         private static final int CHUNK_SIZE = 32;
 171 
 172         private static final int NEXT_CHUNK_INDEX = CHUNK_SIZE - 1;
 173 
 174         private Object[] head;
 175         private int index;


< prev index next >