1 /*
  2  * Copyright (c) 2000, 2026, 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 java.io.*;
 28 import sun.jvm.hotspot.debugger.*;
 29 import sun.jvm.hotspot.runtime.*;
 30 import sun.jvm.hotspot.types.*;
 31 import sun.jvm.hotspot.utilities.Observable;
 32 import sun.jvm.hotspot.utilities.Observer;
 33 
 34 // Oop represents the superclass for all types of
 35 // objects in the HotSpot object heap.
 36 
 37 public class Oop {
 38   static {
 39     VM.registerVMInitializedObserver(new Observer() {
 40         public void update(Observable o, Object data) {
 41           initialize(VM.getVM().getTypeDataBase());
 42         }
 43       });
 44   }
 45 
 46   private static synchronized void initialize(TypeDataBase db) throws WrongTypeException {
 47     Type type  = db.lookupType("oopDesc");
 48     mark       = new CIntField(type.getCIntegerField("_mark"), 0);
 49     if (VM.getVM().isCompactObjectHeadersEnabled()) {
 50       headerSize = 4;
 51     } else {
 52       headerSize = type.getSize();
 53       compressedKlass  = new NarrowKlassField(type.getAddressField("_compressed_klass"), 0);
 54     }
 55   }
 56 
 57   private OopHandle  handle;
 58   private ObjectHeap heap;
 59 
 60   Oop(OopHandle handle, ObjectHeap heap) {
 61     this.handle = handle;
 62     this.heap   = heap;
 63   }
 64 
 65   ObjectHeap getHeap()   { return heap; }
 66 
 67   /** Should not be used or needed by most clients outside this
 68       package; is needed, however, by {@link
 69       sun.jvm.hotspot.utilities.MarkBits}. */
 70   public OopHandle getHandle() { return handle; }
 71 
 72   private static long headerSize;
 73   public  static long getHeaderSize() { return headerSize; } // Header size in bytes.
 74 
 75   private static CIntField mark;
 76   private static NarrowKlassField compressedKlass;
 77 
 78   // Accessors for declared fields
 79   public Mark  getMark()   { return new Mark(getHandle()); }
 80 
 81   public Klass getKlass() {
 82     if (VM.getVM().isCompactObjectHeadersEnabled()) {
 83       return getMark().getKlass();
 84     } else {
 85       return (Klass)compressedKlass.getValue(getHandle());
 86     }
 87   }
 88 
 89   public boolean isA(Klass k) {
 90     return getKlass().isSubtypeOf(k);
 91   }
 92 
 93   // Returns the byte size of this object
 94   public long getObjectSize() {
 95     Klass k = getKlass();
 96     // All other types should be overriding getObjectSize directly
 97     return ((InstanceKlass)k).getObjectSize(this);
 98   }
 99 
100   // Type test operations
101   public boolean isInstance()          { return false; }
102   public boolean isInstanceRef()       { return false; }
103   public boolean isArray()             { return false; }
104   public boolean isObjArray()          { return false; }
105   public boolean isTypeArray()         { return false; }
106   public boolean isThread()            { return false; }
107 
108   // Align the object size.
109   public static long alignObjectSize(long size) {
110     return VM.getVM().alignUp(size, VM.getVM().getMinObjAlignmentInBytes());
111   }
112 
113   // All vm's align longs, so pad out certain offsets.
114   public static long alignObjectOffset(long offset) {
115     return VM.getVM().alignUp(offset, VM.getVM().getBytesPerLong());
116   }
117 
118   public boolean equals(Object obj) {
119     if (obj instanceof Oop other) {
120       return getHandle().equals(other.getHandle());
121     }
122     return false;
123  }
124 
125   public int hashCode() { return getHandle().hashCode(); }
126 
127   /** Identity hash in the target VM */
128   public long identityHash() {
129     if (VM.getVM().isCompactObjectHeadersEnabled()) {
130       System.exit(-23);
131         throw new InternalError("Not yet implemented");
132     }
133     Mark mark = getMark();
134     if (mark.isUnlocked() && (!mark.hasNoHash())) {
135       return (int) mark.hash();
136     } else if (mark.isMarked()) {
137       return (int) mark.hash();
138     } else {
139       return slowIdentityHash();
140     }
141   }
142 
143   public long slowIdentityHash() {
144     return VM.getVM().getObjectSynchronizer().identityHashValueFor(this);
145   }
146 
147   public void iterate(OopVisitor visitor, boolean doVMFields) {
148     visitor.setObj(this);
149     visitor.prologue();
150     iterateFields(visitor, doVMFields);
151     visitor.epilogue();
152   }
153 
154   void iterateFields(OopVisitor visitor, boolean doVMFields) {
155     if (doVMFields) {
156       visitor.doCInt(mark, true);
157       if (!VM.getVM().isCompactObjectHeadersEnabled()) {
158         visitor.doMetadata(compressedKlass, true);
159       }
160     }
161   }
162 
163   public void print()      { printOn(System.out); }
164   public void printValue() { printValueOn(System.out); }
165   public void printRaw()   { printRawOn(System.out); }
166 
167   public static void printOopValueOn(Oop obj, PrintStream tty) {
168     if (obj == null) {
169       tty.print("null");
170     } else {
171       obj.printValueOn(tty);
172       tty.print(" @ " + VM.getVM().getUniverse().heap().oopAddressDescription(obj.getHandle()));
173     }
174   }
175 
176   public static void printOopAddressOn(Oop obj, PrintStream tty) {
177     if (obj == null) {
178       tty.print("null");
179     } else {
180       tty.print(obj.getHandle().toString());
181     }
182   }
183 
184   public void printOn(PrintStream tty) {
185     OopPrinter printer = new OopPrinter(tty);
186     iterate(printer, true);
187   }
188 
189   public void printValueOn(PrintStream tty) {
190     try {
191       tty.print("Oop for " + getKlass().getName().asString());
192     } catch (java.lang.NullPointerException e) {
193       tty.print("Oop");
194     }
195   }
196 
197   public void printRawOn(PrintStream tty) {
198     tty.print("Dumping raw memory for ");
199     printValueOn(tty);
200     tty.println();
201     long size = getObjectSize() * 4;
202     for (long i = 0; i < size; i += 4) {
203       long memVal = getHandle().getCIntegerAt(i, 4, true);
204       tty.println(Long.toHexString(memVal));
205     }
206   }
207 
208   public boolean verify() { return true;}
209 
210   public static Klass getKlassForOopHandle(OopHandle handle) {
211     if (handle == null) {
212       return null;
213     }
214     if (VM.getVM().isCompactObjectHeadersEnabled()) {
215       Mark mark = new Mark(handle);
216       return mark.getKlass();
217     } else {
218       return (Klass)Metadata.instantiateWrapperFor(handle.getCompKlassAddressAt(compressedKlass.getOffset()));
219     }
220   }
221 };