Source code: ClassLib/Common/sun/misc/Unsafe.java
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 ClassLib.Common.sun.misc;
5
6 import ClassLib.ClassLibInterface;
7 import Clazz.jq_Array;
8 import Clazz.jq_Class;
9 import Clazz.jq_Field;
10 import Clazz.jq_InstanceField;
11 import Clazz.jq_StaticField;
12 import Clazz.jq_Type;
13 import Memory.HeapAddress;
14 import Run_Time.SystemInterface;
15
16 /**
17 * Unsafe
18 *
19 * @author John Whaley <jwhaley@alum.mit.edu>
20 * @version $Id: Unsafe.java,v 1.3 2003/05/12 10:04:53 joewhaley Exp $
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, (int) size, b);
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 int fieldOffset(java.lang.reflect.Field field) {
272 jq_Field f = (jq_Field) ClassLibInterface.DEFAULT.getJQField(field);
273 jq_Class c = f.getDeclaringClass();
274 c.load(); c.verify(); c.prepare();
275 if (f instanceof jq_InstanceField) {
276 return ((jq_InstanceField)f).getOffset();
277 } else {
278 HeapAddress a = ((jq_StaticField)f).getAddress();
279 HeapAddress b = HeapAddress.addressOf(c.getStaticData());
280 return b.difference(a);
281 }
282 }
283
284 public java.lang.Object staticFieldBase(java.lang.Class k) {
285 jq_Type t = ClassLibInterface.DEFAULT.getJQType(k);
286 if (t instanceof jq_Class) {
287 jq_Class c = (jq_Class) t;
288 return c.getStaticData();
289 }
290 return null;
291 }
292
293 public void ensureClassInitialized(java.lang.Class k) {
294 jq_Type t = ClassLibInterface.DEFAULT.getJQType(k);
295 t.load(); t.verify(); t.prepare(); t.sf_initialize(); t.cls_initialize();
296 }
297
298 public int arrayBaseOffset(java.lang.Class k) {
299 return 0;
300 }
301
302 public int arrayIndexScale(java.lang.Class k) {
303 jq_Type t = ClassLibInterface.DEFAULT.getJQType(k);
304 if (t instanceof jq_Array) {
305 int width = ((jq_Array)t).getElementType().getReferenceSize();
306 switch (width) {
307 case 4: return 2;
308 case 2: return 1;
309 case 1: return 0;
310 case 8: return 3;
311 }
312 }
313 return -1;
314 }
315
316 public int addressSize() {
317 return HeapAddress.size();
318 }
319
320 public int pageSize() {
321 return 1 << HeapAddress.pageAlign();
322 }
323
324 public java.lang.Class defineClass(java.lang.String name, byte[] b, int off, int len, ClassLib.Common.java.lang.ClassLoader cl, java.security.ProtectionDomain pd) {
325 return cl.defineClass0(name, b, off, len, pd);
326 }
327
328 public java.lang.Class defineClass(java.lang.String name, byte[] b, int off, int len, ClassLib.Common.java.lang.ClassLoader cl) {
329 return cl.defineClass0(name, b, off, len, null);
330 }
331
332 public java.lang.Object allocateInstance(java.lang.Class k)
333 throws java.lang.InstantiationException {
334 jq_Type t = ClassLibInterface.DEFAULT.getJQType(k);
335 if (t instanceof jq_Class) {
336 jq_Class c = (jq_Class) t;
337 return c.newInstance();
338 }
339 throw new java.lang.InstantiationException();
340 }
341
342 public void monitorEnter(java.lang.Object o) {
343 Run_Time.Monitor.monitorenter(o);
344 }
345
346 public void monitorExit(java.lang.Object o) {
347 Run_Time.Monitor.monitorexit(o);
348 }
349
350 public void throwException(java.lang.Throwable o) {
351 Run_Time.ExceptionDeliverer.athrow(o);
352 }
353 }