1 /*
2  * Copyright (c) 2000, 2020, 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.oops;
26 
27 import sun.jvm.hotspot.utilities.Observable;
28 import sun.jvm.hotspot.utilities.Observer;
29 
30 import sun.jvm.hotspot.debugger.Address;
31 import sun.jvm.hotspot.runtime.VM;
32 import sun.jvm.hotspot.types.AddressField;
33 import sun.jvm.hotspot.types.CIntegerField;
34 import sun.jvm.hotspot.types.Type;
35 import sun.jvm.hotspot.types.TypeDataBase;
36 
37 public class CompressedOops {
38   private static AddressField baseField;
39   private static CIntegerField shiftField;
40 
41   public enum Mode {
42     UnscaledNarrowOop,
43     ZeroBasedNarrowOop,
44     HeapBasedNarrowOop
45   }
46 
47   static {
VM.registerVMInitializedObserver(new Observer() { public void update(Observable o, Object data) { initialize(VM.getVM().getTypeDataBase()); } })48     VM.registerVMInitializedObserver(new Observer() {
49         public void update(Observable o, Object data) {
50           initialize(VM.getVM().getTypeDataBase());
51         }
52       });
53   }
54 
typeExists(TypeDataBase db, String type)55   private static boolean typeExists(TypeDataBase db, String type) {
56       try {
57           db.lookupType(type);
58       } catch (RuntimeException e) {
59           return false;
60       }
61       return true;
62   }
63 
initialize(TypeDataBase db)64   private static synchronized void initialize(TypeDataBase db) {
65     Type type = db.lookupType("CompressedOops");
66 
67     baseField = type.getAddressField("_narrow_oop._base");
68     shiftField = type.getCIntegerField("_narrow_oop._shift");
69   }
70 
CompressedOops()71   public CompressedOops() {
72   }
modeToString(Mode mode)73   public static String modeToString(Mode mode) {
74     switch (mode) {
75     case UnscaledNarrowOop:
76       return "32-bits Oops";
77     case ZeroBasedNarrowOop:
78       return "zero based Compressed Oops";
79     case HeapBasedNarrowOop:
80       return "Compressed Oops with base";
81     }
82     return "";
83   }
84 
getBase()85   public static long getBase() {
86     if (baseField.getValue() == null) {
87       return 0;
88     } else {
89       return baseField.getValue().minus(null);
90     }
91   }
92 
getShift()93   public static int getShift() {
94     return (int)shiftField.getValue();
95   }
96 }
97