View Javadoc

1   // Unsafe.java, created Tue Dec 10 14:02:37 2002 by joewhaley
2   // Copyright (C) 2001-3 John Whaley <jwhaley@alum.mit.edu>
3   // Licensed under the terms of the GNU LGPL; see COPYING for details.
4   package joeq.ClassLib.Common.sun.misc;
5   
6   import joeq.ClassLib.ClassLibInterface;
7   import joeq.Class.jq_Array;
8   import joeq.Class.jq_Class;
9   import joeq.Class.jq_Field;
10  import joeq.Class.jq_InstanceField;
11  import joeq.Class.jq_StaticField;
12  import joeq.Class.jq_Type;
13  import joeq.Memory.HeapAddress;
14  import joeq.Runtime.SystemInterface;
15  
16  /***
17   * Unsafe
18   *
19   * @author  John Whaley <jwhaley@alum.mit.edu>
20   * @version $Id: Unsafe.java 1864 2004-08-07 07:30:50Z joewhaley $
21   */
22  public final class Unsafe {
23  
24      public java.lang.Object getObject(java.lang.Object o, int x) {
25          HeapAddress a = HeapAddress.addressOf(o);
26          return ((HeapAddress)a.offset(x).peek()).asObject();
27      }
28      public java.lang.Object getObject(java.lang.Object o, long x) {
29          return getObject(o, (int) x);
30      }
31      
32      public void putObject(java.lang.Object o1, int x, java.lang.Object v) {
33          HeapAddress a = HeapAddress.addressOf(o1);
34          a.offset(x).poke(HeapAddress.addressOf(v));
35      }
36      public void putObject(java.lang.Object o1, long x, java.lang.Object v) {
37          putObject(o1, (int) x, v);
38      }
39      
40      public boolean getBoolean(java.lang.Object o, int x) {
41          HeapAddress a = HeapAddress.addressOf(o);
42          return a.offset(x).peek1() != (byte)0;
43      }
44      public boolean getBoolean(java.lang.Object o, long x) {
45          return getBoolean(o, (int) x);
46      }
47      
48      public void putBoolean(java.lang.Object o, int x, boolean v) {
49          HeapAddress a = HeapAddress.addressOf(o);
50          a.offset(x).poke1(v?(byte)1:(byte)0);
51      }
52      public void putBoolean(java.lang.Object o1, long x, boolean v) {
53          putBoolean(o1, (int) x, v);
54      }
55      
56      public byte getByte(java.lang.Object o, int x) {
57          HeapAddress a = HeapAddress.addressOf(o);
58          return a.offset(x).peek1();
59      }
60      public byte getByte(java.lang.Object o, long x) {
61          return getByte(o, (int) x);
62      }
63      
64      public void putByte(java.lang.Object o, int x, byte v) {
65          HeapAddress a = HeapAddress.addressOf(o);
66          a.offset(x).poke1(v);
67      }
68      public void putByte(java.lang.Object o1, long x, byte v) {
69          putByte(o1, (int) x, v);
70      }
71      
72      public short getShort(java.lang.Object o, int x) {
73          HeapAddress a = HeapAddress.addressOf(o);
74          return a.offset(x).peek2();
75      }
76      public short getShort(java.lang.Object o, long x) {
77          return getShort(o, (int) x);
78      }
79      
80      public void putShort(java.lang.Object o, int x, short v) {
81          HeapAddress a = HeapAddress.addressOf(o);
82          a.offset(x).poke2(v);
83      }
84      public void putShort(java.lang.Object o1, long x, short v) {
85          putShort(o1, (int) x, v);
86      }
87      
88      public char getChar(java.lang.Object o, int x) {
89          HeapAddress a = HeapAddress.addressOf(o);
90          return (char) a.offset(x).peek2();
91      }
92      public char getChar(java.lang.Object o, long x) {
93          return getChar(o, (int) x);
94      }
95      
96      public void putChar(java.lang.Object o, int x, char v) {
97          HeapAddress a = HeapAddress.addressOf(o);
98          a.offset(x).poke2((short) v);
99      }
100     public void putChar(java.lang.Object o1, long x, char v) {
101         putChar(o1, (int) x, v);
102     }
103     
104     public int getInt(java.lang.Object o, int x) {
105         HeapAddress a = HeapAddress.addressOf(o);
106         return a.offset(x).peek4();
107     }
108     public int getInt(java.lang.Object o, long x) {
109         return getInt(o, (int) x);
110     }
111     
112     public void putInt(java.lang.Object o, int x, int v) {
113         HeapAddress a = HeapAddress.addressOf(o);
114         a.offset(x).poke4(v);
115     }
116     public void putInt(java.lang.Object o1, long x, int v) {
117         putInt(o1, (int) x, v);
118     }
119     
120     public long getLong(java.lang.Object o, int x) {
121         HeapAddress a = HeapAddress.addressOf(o);
122         return a.offset(x).peek8();
123     }
124     public long getLong(java.lang.Object o, long x) {
125         return getLong(o, (int) x);
126     }
127     
128     public void putLong(java.lang.Object o, int x, long v) {
129         HeapAddress a = HeapAddress.addressOf(o);
130         a.offset(x).poke8(v);
131     }
132     public void putLong(java.lang.Object o1, long x, long v) {
133         putLong(o1, (int) x, v);
134     }
135     
136     public float getFloat(java.lang.Object o, int x) {
137         HeapAddress a = HeapAddress.addressOf(o);
138         return Float.intBitsToFloat(a.offset(x).peek4());
139     }
140     public float getFloat(java.lang.Object o, long x) {
141         return getFloat(o, (int) x);
142     }
143     
144     public void putFloat(java.lang.Object o, int x, float v) {
145         HeapAddress a = HeapAddress.addressOf(o);
146         a.offset(x).poke4(Float.floatToRawIntBits(v));
147     }
148     public void putFloat(java.lang.Object o1, long x, float v) {
149         putFloat(o1, (int) x, v);
150     }
151     
152     public double getDouble(java.lang.Object o, int x) {
153         HeapAddress a = HeapAddress.addressOf(o);
154         return Double.longBitsToDouble(a.offset(x).peek8());
155     }
156     public double getDouble(java.lang.Object o, long x) {
157         return getDouble(o, (int) x);
158     }
159     
160     public void putDouble(java.lang.Object o, int x, double v) {
161         HeapAddress a = HeapAddress.addressOf(o);
162         a.offset(x).poke8(Double.doubleToRawLongBits(v));
163     }
164     public void putDouble(java.lang.Object o1, long x, double v) {
165         putDouble(o1, (int) x, v);
166     }
167     
168     public byte getByte(long addr) {
169         HeapAddress a = HeapAddress.address32((int) addr);
170         return a.peek1();
171     }
172     
173     public void putByte(long addr, byte v) {
174         HeapAddress a = HeapAddress.address32((int) addr);
175         a.poke1(v);
176     }
177     
178     public short getShort(long addr) {
179         HeapAddress a = HeapAddress.address32((int) addr);
180         return a.peek2();
181     }
182     
183     public void putShort(long addr, short v) {
184         HeapAddress a = HeapAddress.address32((int) addr);
185         a.poke2(v);
186     }
187     
188     public char getChar(long addr) {
189         HeapAddress a = HeapAddress.address32((int) addr);
190         return (char) a.peek2();
191     }
192     
193     public void putChar(long addr, char v) {
194         HeapAddress a = HeapAddress.address32((int) addr);
195         a.poke2((short)v);
196     }
197     
198     public int getInt(long addr) {
199         HeapAddress a = HeapAddress.address32((int) addr);
200         return a.peek4();
201     }
202     
203     public void putInt(long addr, int v) {
204         HeapAddress a = HeapAddress.address32((int) addr);
205         a.poke4(v);
206     }
207     
208     public long getLong(long addr) {
209         HeapAddress a = HeapAddress.address32((int) addr);
210         return a.peek8();
211     }
212     
213     public void putLong(long addr, long v) {
214         HeapAddress a = HeapAddress.address32((int) addr);
215         a.poke8(v);
216     }
217     
218     public float getFloat(long addr) {
219         HeapAddress a = HeapAddress.address32((int) addr);
220         return Float.intBitsToFloat(a.peek4());
221     }
222     
223     public void putFloat(long addr, float v) {
224         HeapAddress a = HeapAddress.address32((int) addr);
225         a.poke4(Float.floatToRawIntBits(v));
226     }
227     
228     public double getDouble(long addr) {
229         HeapAddress a = HeapAddress.address32((int) addr);
230         return Double.longBitsToDouble(a.peek8());
231     }
232     
233     public void putDouble(long addr, double v) {
234         HeapAddress a = HeapAddress.address32((int) addr);
235         a.poke8(Double.doubleToRawLongBits(v));
236     }
237     
238     public long getAddress(long addr) {
239         HeapAddress a = HeapAddress.address32((int) addr);
240         return (long) a.peek().to32BitValue();
241     }
242     
243     public void putAddress(long addr, long v) {
244         HeapAddress a = HeapAddress.address32((int) addr);
245         HeapAddress b = HeapAddress.address32((int) v);
246         a.poke(b);
247     }
248     
249     public long allocateMemory(long v) {
250         return SystemInterface.syscalloc((int) v).to32BitValue();
251     }
252     
253     //public long reallocateMemory(long addr, long size) {
254     
255     public void setMemory(long to, long size, byte b) {
256         HeapAddress a = HeapAddress.address32((int) to);
257         SystemInterface.mem_set(a, b, (int) size);
258     }
259     
260     public void copyMemory(long to, long from, long size) {
261         HeapAddress a = HeapAddress.address32((int) to);
262         HeapAddress b = HeapAddress.address32((int) from);
263         SystemInterface.mem_cpy(a, b, (int) size);
264     }
265     
266     public void freeMemory(long v) {
267         HeapAddress a = HeapAddress.address32((int) v);
268         SystemInterface.sysfree(a);
269     }
270     
271     public long objectFieldOffset(java.lang.reflect.Field field) {
272         return (long) fieldOffset(field);
273     }
274     public int fieldOffset(java.lang.reflect.Field field) {
275         jq_Field f = (jq_Field) ClassLibInterface.DEFAULT.getJQField(field);
276         jq_Class c = f.getDeclaringClass();
277         c.load(); c.verify(); c.prepare();
278         if (f instanceof jq_InstanceField) {
279             return ((jq_InstanceField)f).getOffset();
280         } else {
281             HeapAddress a = ((jq_StaticField)f).getAddress();
282             HeapAddress b = HeapAddress.addressOf(c.getStaticData());
283             return b.difference(a);
284         }
285     }
286     
287     public java.lang.Object staticFieldBase(java.lang.Class k) {
288         jq_Type t = ClassLibInterface.DEFAULT.getJQType(k);
289         if (t instanceof jq_Class) {
290             jq_Class c = (jq_Class) t;
291             return c.getStaticData();
292         }
293         return null;
294     }
295     
296     public void ensureClassInitialized(java.lang.Class k) {
297         jq_Type t = ClassLibInterface.DEFAULT.getJQType(k);
298         t.load(); t.verify(); t.prepare(); t.sf_initialize(); t.cls_initialize();
299     }
300     
301     public int arrayBaseOffset(java.lang.Class k) {
302         return 0;
303     }
304     
305     public int arrayIndexScale(java.lang.Class k) {
306         jq_Type t = ClassLibInterface.DEFAULT.getJQType(k);
307         if (t instanceof jq_Array) {
308             int width = ((jq_Array)t).getElementType().getReferenceSize();
309             switch (width) {
310             case 4: return 2;
311             case 2: return 1;
312             case 1: return 0;
313             case 8: return 3;
314             }
315         }
316         return -1;
317     }
318     
319     public int addressSize() {
320         return HeapAddress.size();
321     }
322     
323     public int pageSize() {
324         return 1 << HeapAddress.pageAlign();
325     }
326     
327     public java.lang.Class defineClass(java.lang.String name, byte[] b, int off, int len, joeq.ClassLib.Common.java.lang.ClassLoader cl, java.security.ProtectionDomain pd) {
328         return cl.defineClass0(name, b, off, len, pd);
329     }
330     
331     public java.lang.Class defineClass(java.lang.String name, byte[] b, int off, int len, joeq.ClassLib.Common.java.lang.ClassLoader cl) {
332         return cl.defineClass0(name, b, off, len, null);
333     }
334     
335     public java.lang.Object allocateInstance(java.lang.Class k)
336     throws java.lang.InstantiationException {
337         jq_Type t = ClassLibInterface.DEFAULT.getJQType(k);
338         if (t instanceof jq_Class) {
339             jq_Class c = (jq_Class) t;
340             return c.newInstance();
341         }
342         throw new java.lang.InstantiationException();
343     }
344     
345     public void monitorEnter(java.lang.Object o) {
346         joeq.Runtime.Monitor.monitorenter(o);
347     }
348     
349     public void monitorExit(java.lang.Object o) {
350         joeq.Runtime.Monitor.monitorexit(o);
351     }
352     
353     public void throwException(java.lang.Throwable o) {
354         joeq.Runtime.ExceptionDeliverer.athrow(o);
355     }
356 }