* src/vm/jit/i386/darwin/md-asm.h: Repaired --enable-cycles-stats.
[cacao.git] / src / native / vm / sun_misc_Unsafe.c
1 /* src/native/vm/sun_misc_Unsafe.c - sun/misc/Unsafe
2
3    Copyright (C) 2006, 2007 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4    R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5    C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6    Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25 */
26
27
28 #include "config.h"
29
30 #include <stdint.h>
31 #include <unistd.h>
32
33 #include "mm/memory.h"
34
35 #include "native/jni.h"
36 #include "native/llni.h"
37 #include "native/native.h"
38
39 #include "native/include/java_lang_Object.h"                  /* before c.l.C */
40 #include "native/include/java_lang_String.h"            /* required by j.l.CL */
41
42 #if defined(WITH_CLASSPATH_SUN)
43 # include "native/include/java_nio_ByteBuffer.h"        /* required by j.l.CL */
44 #endif
45
46 #include "native/include/java_lang_ClassLoader.h"        /* required by j.l.C */
47 #include "native/include/java_lang_Class.h"
48 #include "native/include/java_lang_reflect_Field.h"
49 #include "native/include/java_lang_Thread.h"             /* required by s.m.U */
50 #include "native/include/java_lang_Throwable.h"
51
52 #include "native/include/java_security_ProtectionDomain.h" /* required by smU */
53
54 #include "native/include/sun_misc_Unsafe.h"
55
56 #include "vm/builtin.h"
57 #include "vm/exceptions.h"
58 #include "vm/initialize.h"
59 #include "vm/stringlocal.h"
60
61 #include "vmcore/utf8.h"
62
63
64 /* native methods implemented by this file ************************************/
65
66 static JNINativeMethod methods[] = {
67         { "registerNatives",        "()V",                                                        (void *) (intptr_t) &Java_sun_misc_Unsafe_registerNatives                },
68         { "getInt",                 "(Ljava/lang/Object;J)I",                                     (void *) (intptr_t) &Java_sun_misc_Unsafe_getInt__Ljava_lang_Object_2J   },
69         { "putInt",                 "(Ljava/lang/Object;JI)V",                                    (void *) (intptr_t) &Java_sun_misc_Unsafe_putInt__Ljava_lang_Object_2JI  },
70         { "getObject",              "(Ljava/lang/Object;J)Ljava/lang/Object;",                    (void *) (intptr_t) &Java_sun_misc_Unsafe_getObject                      },
71         { "putObject",              "(Ljava/lang/Object;JLjava/lang/Object;)V",                   (void *) (intptr_t) &Java_sun_misc_Unsafe_putObject                      },
72         { "getBoolean",             "(Ljava/lang/Object;J)Z",                                     (void *) (intptr_t) &Java_sun_misc_Unsafe_getBoolean                     },
73         { "putBoolean",             "(Ljava/lang/Object;JZ)V",                                    (void *) (intptr_t) &Java_sun_misc_Unsafe_putBoolean                     },
74         { "getByte",                "(Ljava/lang/Object;J)B",                                     (void *) (intptr_t) &Java_sun_misc_Unsafe_getByte__Ljava_lang_Object_2J  },
75         { "putByte",                "(Ljava/lang/Object;JB)V",                                    (void *) (intptr_t) &Java_sun_misc_Unsafe_putByte__Ljava_lang_Object_2JB },
76         { "getShort",               "(Ljava/lang/Object;J)S",                                     (void *) (intptr_t) &Java_sun_misc_Unsafe_getShort__Ljava_lang_Object_2J },
77         { "putShort",               "(Ljava/lang/Object;JS)V",                                    (void *) (intptr_t) &Java_sun_misc_Unsafe_putShort__Ljava_lang_Object_2JS },
78         { "getChar",                "(Ljava/lang/Object;J)C",                                     (void *) (intptr_t) &Java_sun_misc_Unsafe_getChar__Ljava_lang_Object_2J  },
79         { "putChar",                "(Ljava/lang/Object;JC)V",                                    (void *) (intptr_t) &Java_sun_misc_Unsafe_putChar__Ljava_lang_Object_2JC },
80         { "getLong",                "(Ljava/lang/Object;J)J",                                     (void *) (intptr_t) &Java_sun_misc_Unsafe_getLong__Ljava_lang_Object_2J  },
81         { "putLong",                "(Ljava/lang/Object;JJ)V",                                    (void *) (intptr_t) &Java_sun_misc_Unsafe_putLong__Ljava_lang_Object_2JJ },
82         { "getFloat",               "(Ljava/lang/Object;J)F",                                     (void *) (intptr_t) &Java_sun_misc_Unsafe_getFloat__Ljava_lang_Object_2J },
83         { "putFloat",               "(Ljava/lang/Object;JF)V",                                    (void *) (intptr_t) &Java_sun_misc_Unsafe_putFloat__Ljava_lang_Object_2JF },
84         { "getDouble",              "(Ljava/lang/Object;J)D",                                     (void *) (intptr_t) &Java_sun_misc_Unsafe_getDouble__Ljava_lang_Object_2J },
85         { "putDouble",              "(Ljava/lang/Object;JD)V",                                    (void *) (intptr_t) &Java_sun_misc_Unsafe_putDouble__Ljava_lang_Object_2JD },
86         { "getByte",                "(J)B",                                                       (void *) (intptr_t) &Java_sun_misc_Unsafe_getByte__J                     },
87         { "putByte",                "(JB)V",                                                      (void *) (intptr_t) &Java_sun_misc_Unsafe_putByte__JB                    },
88         { "getShort",               "(J)S",                                                       (void *) (intptr_t) &Java_sun_misc_Unsafe_getShort__J                    },
89         { "putShort",               "(JS)V",                                                      (void *) (intptr_t) &Java_sun_misc_Unsafe_putShort__JS                   },
90         { "getInt",                 "(J)I",                                                       (void *) (intptr_t) &Java_sun_misc_Unsafe_getInt__J                      },
91         { "putInt",                 "(JI)V",                                                      (void *) (intptr_t) &Java_sun_misc_Unsafe_putInt__JI                     },
92         { "getLong",                "(J)J",                                                       (void *) (intptr_t) &Java_sun_misc_Unsafe_getLong__J                     },
93         { "putLong",                "(JJ)V",                                                      (void *) (intptr_t) &Java_sun_misc_Unsafe_putLong__JJ                    },
94         { "getFloat",               "(J)F",                                                       (void *) (intptr_t) &Java_sun_misc_Unsafe_getFloat__J                    },
95         { "objectFieldOffset",      "(Ljava/lang/reflect/Field;)J",                               (void *) (intptr_t) &Java_sun_misc_Unsafe_objectFieldOffset              },
96         { "allocateMemory",         "(J)J",                                                       (void *) (intptr_t) &Java_sun_misc_Unsafe_allocateMemory                 },
97         { "setMemory",              "(Ljava/lang/Object;JJB)V",                                   (void *) (intptr_t) &Java_sun_misc_Unsafe_setMemory                      },
98         { "freeMemory",             "(J)V",                                                       (void *) (intptr_t) &Java_sun_misc_Unsafe_freeMemory                     },
99         { "staticFieldOffset",      "(Ljava/lang/reflect/Field;)J",                               (void *) (intptr_t) &Java_sun_misc_Unsafe_staticFieldOffset              },
100         { "staticFieldBase",        "(Ljava/lang/reflect/Field;)Ljava/lang/Object;",              (void *) (intptr_t) &Java_sun_misc_Unsafe_staticFieldBase                },
101         { "ensureClassInitialized", "(Ljava/lang/Class;)V",                                       (void *) (intptr_t) &Java_sun_misc_Unsafe_ensureClassInitialized         },
102         { "arrayBaseOffset",        "(Ljava/lang/Class;)I",                                       (void *) (intptr_t) &Java_sun_misc_Unsafe_arrayBaseOffset                },
103         { "arrayIndexScale",        "(Ljava/lang/Class;)I",                                       (void *) (intptr_t) &Java_sun_misc_Unsafe_arrayIndexScale                },
104         { "addressSize",            "()I",                                                        (void *) (intptr_t) &Java_sun_misc_Unsafe_addressSize                    },
105         { "pageSize",               "()I",                                                        (void *) (intptr_t) &Java_sun_misc_Unsafe_pageSize                       },
106         { "defineClass",            "(Ljava/lang/String;[BIILjava/lang/ClassLoader;Ljava/security/ProtectionDomain;)Ljava/lang/Class;", (void *) (intptr_t) &Java_sun_misc_Unsafe_defineClass__Ljava_lang_String_2_3BIILjava_lang_ClassLoader_2Ljava_security_ProtectionDomain_2 },
107         { "allocateInstance",       "(Ljava/lang/Class;)Ljava/lang/Object;",                      (void *) (intptr_t) &Java_sun_misc_Unsafe_allocateInstance               },
108         { "throwException",         "(Ljava/lang/Throwable;)V",                                   (void *) (intptr_t) &Java_sun_misc_Unsafe_throwException                 },
109         { "compareAndSwapObject",   "(Ljava/lang/Object;JLjava/lang/Object;Ljava/lang/Object;)Z", (void *) (intptr_t) &Java_sun_misc_Unsafe_compareAndSwapObject           },
110         { "compareAndSwapInt",      "(Ljava/lang/Object;JII)Z",                                   (void *) (intptr_t) &Java_sun_misc_Unsafe_compareAndSwapInt              },
111         { "compareAndSwapLong",     "(Ljava/lang/Object;JJJ)Z",                                   (void *) (intptr_t) &Java_sun_misc_Unsafe_compareAndSwapLong             },
112         { "getObjectVolatile",      "(Ljava/lang/Object;J)Ljava/lang/Object;",                    (void *) (intptr_t) &Java_sun_misc_Unsafe_getObjectVolatile              },
113         { "getIntVolatile",         "(Ljava/lang/Object;J)I",                                     (void *) (intptr_t) &Java_sun_misc_Unsafe_getIntVolatile                 },
114         { "getLongVolatile",        "(Ljava/lang/Object;J)J",                                     (void *) (intptr_t) &Java_sun_misc_Unsafe_getLongVolatile                },
115         { "unpark",                 "(Ljava/lang/Object;)V",                                      (void *) (intptr_t) &Java_sun_misc_Unsafe_unpark                         },
116         { "park",                   "(ZJ)V",                                                      (void *) (intptr_t) &Java_sun_misc_Unsafe_park                           },
117 };
118
119
120 /* _Jv_sun_misc_Unsafe_init ****************************************************
121
122    Register native functions.
123
124 *******************************************************************************/
125
126 void _Jv_sun_misc_Unsafe_init(void)
127 {
128         utf *u;
129
130         u = utf_new_char("sun/misc/Unsafe");
131
132         native_method_register(u, methods, NATIVE_METHODS_COUNT);
133 }
134
135
136 /*
137  * Class:     sun/misc/Unsafe
138  * Method:    registerNatives
139  * Signature: ()V
140  */
141 JNIEXPORT void JNICALL Java_sun_misc_Unsafe_registerNatives(JNIEnv *env, jclass clazz)
142 {
143         /* The native methods of this function are already registered in
144            _Jv_sun_misc_Unsafe_init() which is called during VM
145            startup. */
146 }
147
148
149 /*
150  * Class:     sun/misc/Unsafe
151  * Method:    getInt
152  * Signature: (Ljava/lang/Object;J)I
153  */
154 JNIEXPORT int32_t JNICALL Java_sun_misc_Unsafe_getInt__Ljava_lang_Object_2J(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *o, int64_t offset)
155 {
156         int32_t *p;
157         int32_t  value;
158
159         p = (int32_t *) (((uint8_t *) o) + offset);
160
161         value = *p;
162
163         return value;
164 }
165
166
167 /*
168  * Class:     sun/misc/Unsafe
169  * Method:    putInt
170  * Signature: (Ljava/lang/Object;JI)V
171  */
172 JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putInt__Ljava_lang_Object_2JI(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *o, int64_t offset, int32_t x)
173 {
174         int32_t *p;
175
176         p = (int32_t *) (((uint8_t *) o) + offset);
177
178         *p = x;
179 }
180
181
182 /*
183  * Class:     sun/misc/Unsafe
184  * Method:    getObject
185  * Signature: (Ljava/lang/Object;J)Ljava/lang/Object;
186  */
187 JNIEXPORT java_lang_Object* JNICALL Java_sun_misc_Unsafe_getObject(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *o, int64_t offset)
188 {
189         void **p;
190         void  *value;
191
192         p = (void **) (((uint8_t *) o) + offset);
193
194         value = *p;
195
196         return value;
197 }
198
199
200 /*
201  * Class:     sun/misc/Unsafe
202  * Method:    putObject
203  * Signature: (Ljava/lang/Object;JLjava/lang/Object;)V
204  */
205 JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putObject(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *o, int64_t offset, java_lang_Object *x)
206 {
207         void **p;
208
209         p = (void **) (((uint8_t *) o) + offset);
210
211         *p = (void *) x;
212 }
213
214
215 /*
216  * Class:     sun/misc/Unsafe
217  * Method:    getBoolean
218  * Signature: (Ljava/lang/Object;J)Z
219  */
220 JNIEXPORT int32_t JNICALL Java_sun_misc_Unsafe_getBoolean(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *o, int64_t offset)
221 {
222         int32_t *p;
223         int32_t  value;
224
225         p = (int32_t *) (((uint8_t *) o) + offset);
226
227         value = *p;
228
229         return value;
230 }
231
232
233 /*
234  * Class:     sun/misc/Unsafe
235  * Method:    putBoolean
236  * Signature: (Ljava/lang/Object;JZ)V
237  */
238 JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putBoolean(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *o, int64_t offset, int32_t x)
239 {
240         int32_t *p;
241
242         p = (int32_t *) (((uint8_t *) o) + offset);
243
244         *p = x;
245 }
246
247
248 /*
249  * Class:     sun/misc/Unsafe
250  * Method:    getByte
251  * Signature: (Ljava/lang/Object;J)B
252  */
253 JNIEXPORT int32_t JNICALL Java_sun_misc_Unsafe_getByte__Ljava_lang_Object_2J(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *o, int64_t offset)
254 {
255         int32_t *p;
256         int32_t  value;
257
258         p = (int32_t *) (((uint8_t *) o) + offset);
259
260         value = *p;
261
262         return value;
263 }
264
265
266 /*
267  * Class:     sun/misc/Unsafe
268  * Method:    putByte
269  * Signature: (Ljava/lang/Object;JB)V
270  */
271 JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putByte__Ljava_lang_Object_2JB(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *o, int64_t offset, int32_t x)
272 {
273         int32_t *p;
274
275         p = (int32_t *) (((uint8_t *) o) + offset);
276
277         *p = x;
278 }
279
280
281 /*
282  * Class:     sun/misc/Unsafe
283  * Method:    getShort
284  * Signature: (Ljava/lang/Object;J)S
285  */
286 JNIEXPORT int32_t JNICALL Java_sun_misc_Unsafe_getShort__Ljava_lang_Object_2J(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *o, int64_t offset)
287 {
288         int32_t *p;
289         int32_t  value;
290
291         p = (int32_t *) (((uint8_t *) o) + offset);
292
293         value = *p;
294
295         return value;
296 }
297
298
299 /*
300  * Class:     sun/misc/Unsafe
301  * Method:    putShort
302  * Signature: (Ljava/lang/Object;JS)V
303  */
304 JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putShort__Ljava_lang_Object_2JS(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *o, int64_t offset, int32_t x)
305 {
306         int32_t *p;
307
308         p = (int32_t *) (((uint8_t *) o) + offset);
309
310         *p = x;
311 }
312
313
314 /*
315  * Class:     sun/misc/Unsafe
316  * Method:    getChar
317  * Signature: (Ljava/lang/Object;J)C
318  */
319 JNIEXPORT int32_t JNICALL Java_sun_misc_Unsafe_getChar__Ljava_lang_Object_2J(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *o, int64_t offset)
320 {
321         int32_t *p;
322         int32_t  value;
323
324         p = (int32_t *) (((uint8_t *) o) + offset);
325
326         value = *p;
327
328         return value;
329 }
330
331
332 /*
333  * Class:     sun/misc/Unsafe
334  * Method:    putChar
335  * Signature: (Ljava/lang/Object;JC)V
336  */
337 JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putChar__Ljava_lang_Object_2JC(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *o, int64_t offset, int32_t x)
338 {
339         int32_t *p;
340
341         p = (int32_t *) (((uint8_t *) o) + offset);
342
343         *p = x;
344 }
345
346
347 /*
348  * Class:     sun/misc/Unsafe
349  * Method:    getLong
350  * Signature: (Ljava/lang/Object;J)J
351  */
352 JNIEXPORT int64_t JNICALL Java_sun_misc_Unsafe_getLong__Ljava_lang_Object_2J(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *o, int64_t offset)
353 {
354         int64_t *p;
355         int64_t  value;
356
357         p = (int64_t *) (((uint8_t *) o) + offset);
358
359         value = *p;
360
361         return value;
362 }
363
364
365 /*
366  * Class:     sun/misc/Unsafe
367  * Method:    putLong
368  * Signature: (Ljava/lang/Object;JJ)V
369  */
370 JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putLong__Ljava_lang_Object_2JJ(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *o, int64_t offset, int64_t x)
371 {
372         int64_t *p;
373
374         p = (int64_t *) (((uint8_t *) o) + offset);
375
376         *p = x;
377 }
378
379
380 /*
381  * Class:     sun/misc/Unsafe
382  * Method:    getFloat
383  * Signature: (Ljava/lang/Object;J)F
384  */
385 JNIEXPORT float JNICALL Java_sun_misc_Unsafe_getFloat__Ljava_lang_Object_2J(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *o, int64_t offset)
386 {
387         float *p;
388         float  value;
389
390         p = (float *) (((uint8_t *) o) + offset);
391
392         value = *p;
393
394         return value;
395 }
396
397
398 /*
399  * Class:     sun/misc/Unsafe
400  * Method:    putFloat
401  * Signature: (Ljava/lang/Object;JF)V
402  */
403 JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putFloat__Ljava_lang_Object_2JF(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *o, int64_t offset, float x)
404 {
405         float *p;
406
407         p = (float *) (((uint8_t *) o) + offset);
408
409         *p = x;
410 }
411
412
413 /*
414  * Class:     sun/misc/Unsafe
415  * Method:    getDouble
416  * Signature: (Ljava/lang/Object;J)D
417  */
418 JNIEXPORT double JNICALL Java_sun_misc_Unsafe_getDouble__Ljava_lang_Object_2J(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *o, int64_t offset)
419 {
420         double *p;
421         double  value;
422
423         p = (double *) (((uint8_t *) o) + offset);
424
425         value = *p;
426
427         return value;
428 }
429
430
431 /*
432  * Class:     sun/misc/Unsafe
433  * Method:    putDouble
434  * Signature: (Ljava/lang/Object;JD)V
435  */
436 JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putDouble__Ljava_lang_Object_2JD(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *o, int64_t offset, double x)
437 {
438         double *p;
439
440         p = (double *) (((uint8_t *) o) + offset);
441
442         *p = x;
443 }
444
445
446 /*
447  * Class:     sun/misc/Unsafe
448  * Method:    getByte
449  * Signature: (J)B
450  */
451 JNIEXPORT int32_t JNICALL Java_sun_misc_Unsafe_getByte__J(JNIEnv *env, sun_misc_Unsafe *this, int64_t address)
452 {
453         int8_t *p;
454         int8_t  value;
455
456         p = (int8_t *) (intptr_t) address;
457
458         value = *p;
459
460         return (int32_t) value;
461 }
462
463
464 /*
465  * Class:     sun/misc/Unsafe
466  * Method:    putByte
467  * Signature: (JB)V
468  */
469 JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putByte__JB(JNIEnv *env, sun_misc_Unsafe *this, int64_t address, int32_t value)
470 {
471         int8_t *p;
472
473         p = (int8_t *) (intptr_t) address;
474
475         *p = (int8_t) value;
476 }
477
478
479 /*
480  * Class:     sun/misc/Unsafe
481  * Method:    getShort
482  * Signature: (J)S
483  */
484 JNIEXPORT int32_t JNICALL Java_sun_misc_Unsafe_getShort__J(JNIEnv *env, sun_misc_Unsafe *this, int64_t address)
485 {
486         int16_t *p;
487         int16_t  value;
488
489         p = (int16_t *) (intptr_t) address;
490
491         value = *p;
492
493         return (int32_t) value;
494 }
495
496
497 /*
498  * Class:     sun/misc/Unsafe
499  * Method:    putShort
500  * Signature: (JS)V
501  */
502 JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putShort__JS(JNIEnv *env, sun_misc_Unsafe *this, int64_t address, int32_t value)
503 {
504         int16_t *p;
505
506         p = (int16_t *) (intptr_t) address;
507
508         *p = (int16_t) value;
509 }
510
511
512 /*
513  * Class:     sun/misc/Unsafe
514  * Method:    getInt
515  * Signature: (J)I
516  */
517 JNIEXPORT int32_t JNICALL Java_sun_misc_Unsafe_getInt__J(JNIEnv *env, sun_misc_Unsafe *this, int64_t address)
518 {
519         int32_t *p;
520         int32_t  value;
521
522         p = (int32_t *) (intptr_t) address;
523
524         value = *p;
525
526         return value;
527 }
528
529
530 /*
531  * Class:     sun/misc/Unsafe
532  * Method:    putInt
533  * Signature: (JI)V
534  */
535 JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putInt__JI(JNIEnv *env, struct sun_misc_Unsafe* this, int64_t address, int32_t value)
536 {
537         int32_t *p;
538
539         p = (int32_t *) (intptr_t) address;
540
541         *p = value;
542 }
543
544
545 /*
546  * Class:     sun/misc/Unsafe
547  * Method:    getLong
548  * Signature: (J)J
549  */
550 JNIEXPORT int64_t JNICALL Java_sun_misc_Unsafe_getLong__J(JNIEnv *env, sun_misc_Unsafe *this, int64_t address)
551 {
552         int64_t *p;
553         int64_t  value;
554
555         p = (int64_t *) (intptr_t) address;
556
557         value = *p;
558
559         return value;
560 }
561
562
563 /*
564  * Class:     sun/misc/Unsafe
565  * Method:    putLong
566  * Signature: (JJ)V
567  */
568 JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putLong__JJ(JNIEnv *env, sun_misc_Unsafe *this, int64_t address, int64_t value)
569 {
570         int64_t *p;
571
572         p = (int64_t *) (intptr_t) address;
573
574         *p = value;
575 }
576
577
578 /*
579  * Class:     sun/misc/Unsafe
580  * Method:    getFloat
581  * Signature: (J)F
582  */
583 JNIEXPORT float JNICALL Java_sun_misc_Unsafe_getFloat__J(JNIEnv *env, sun_misc_Unsafe *this, int64_t address)
584 {
585         float *p;
586         float  value;
587
588         p = (float *) (intptr_t) address;
589
590         value = *p;
591
592         return value;
593 }
594
595
596 /*
597  * Class:     sun/misc/Unsafe
598  * Method:    objectFieldOffset
599  * Signature: (Ljava/lang/reflect/Field;)J
600  */
601 JNIEXPORT int64_t JNICALL Java_sun_misc_Unsafe_objectFieldOffset(JNIEnv *env, sun_misc_Unsafe *this, java_lang_reflect_Field *field)
602 {
603         classinfo *c;
604         fieldinfo *f;
605         int32_t    slot;
606
607         LLNI_field_get_cls(field, clazz, c);
608         LLNI_field_get_val(field, slot , slot);
609
610         f = &c->fields[slot];
611
612         return (int64_t) f->offset;
613 }
614
615
616 /*
617  * Class:     sun/misc/Unsafe
618  * Method:    allocateMemory
619  * Signature: (J)J
620  */
621 JNIEXPORT int64_t JNICALL Java_sun_misc_Unsafe_allocateMemory(JNIEnv *env, sun_misc_Unsafe *this, int64_t bytes)
622 {
623         size_t  length;
624         void   *p;
625
626         length = (size_t) bytes;
627
628         if ((length != (uint64_t) bytes) || (bytes < 0)) {
629                 exceptions_throw_illegalargumentexception();
630                 return 0;
631         }
632
633         p = MNEW(uint8_t, length);
634
635         return (int64_t) (intptr_t) p;
636 }
637
638
639 /*
640  * Class:     sun/misc/Unsafe
641  * Method:    setMemory
642  * Signature: (Ljava/lang/Object;JJB)V
643  */
644 JNIEXPORT void JNICALL Java_sun_misc_Unsafe_setMemory(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *o, int64_t offset, int64_t bytes, int32_t value)
645 {
646         size_t  length;
647         void   *p;
648
649         length = (size_t) bytes;
650
651         if ((length != (uint64_t) bytes) || (bytes < 0)) {
652                 exceptions_throw_illegalargumentexception();
653                 return;
654         }
655
656         /* XXX Missing LLNI: we need to unwrap this object. */
657
658         p = (void *) (((uint8_t *) o) + offset);
659
660         /* XXX Not sure this is correct. */
661
662         MSET(p, value, uint8_t, length);
663 }
664
665
666 /*
667  * Class:     sun/misc/Unsafe
668  * Method:    freeMemory
669  * Signature: (J)V
670  */
671 JNIEXPORT void JNICALL Java_sun_misc_Unsafe_freeMemory(JNIEnv *env, sun_misc_Unsafe *this, int64_t address)
672 {
673         void *p;
674
675         p = (void *) (intptr_t) address;
676
677         if (p == NULL)
678                 return;
679
680         /* we pass length 1 to trick the free function */
681
682         MFREE(p, uint8_t, 1);
683 }
684
685
686 /*
687  * Class:     sun/misc/Unsafe
688  * Method:    staticFieldOffset
689  * Signature: (Ljava/lang/reflect/Field;)J
690  */
691 JNIEXPORT int64_t JNICALL Java_sun_misc_Unsafe_staticFieldOffset(JNIEnv *env, sun_misc_Unsafe *this, java_lang_reflect_Field *f)
692 {
693         /* The offset of static fields is 0. */
694
695         return 0;
696 }
697
698
699 /*
700  * Class:     sun/misc/Unsafe
701  * Method:    staticFieldBase
702  * Signature: (Ljava/lang/reflect/Field;)Ljava/lang/Object;
703  */
704 JNIEXPORT java_lang_Object* JNICALL Java_sun_misc_Unsafe_staticFieldBase(JNIEnv *env, sun_misc_Unsafe *this, java_lang_reflect_Field *rf)
705 {
706         classinfo *c;
707         fieldinfo *f;
708         int32_t    slot;
709
710         LLNI_field_get_cls(rf, clazz, c);
711         LLNI_field_get_val(rf, slot , slot);
712
713         f = &(c->fields[slot]);
714
715         return (java_lang_Object *) (f->value);
716 }
717
718
719 /*
720  * Class:     sun/misc/Unsafe
721  * Method:    ensureClassInitialized
722  * Signature: (Ljava/lang/Class;)V
723  */
724 JNIEXPORT void JNICALL Java_sun_misc_Unsafe_ensureClassInitialized(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Class *class)
725 {
726         classinfo *c;
727
728         c = LLNI_classinfo_unwrap(class);
729
730         if (!(c->state & CLASS_INITIALIZED))
731                 initialize_class(c);
732 }
733
734
735 /*
736  * Class:     sun/misc/Unsafe
737  * Method:    arrayBaseOffset
738  * Signature: (Ljava/lang/Class;)I
739  */
740 JNIEXPORT int32_t JNICALL Java_sun_misc_Unsafe_arrayBaseOffset(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Class *arrayClass)
741 {
742         classinfo       *c;
743         arraydescriptor *ad;
744
745         c  = LLNI_classinfo_unwrap(arrayClass);
746         ad = c->vftbl->arraydesc;
747
748         if (ad == NULL) {
749                 /* XXX does that exception exist? */
750                 exceptions_throw_internalerror("java/lang/InvalidClassException");
751                 return 0;
752         }
753
754         return ad->dataoffset;
755 }
756
757
758 /*
759  * Class:     sun/misc/Unsafe
760  * Method:    arrayIndexScale
761  * Signature: (Ljava/lang/Class;)I
762  */
763 JNIEXPORT int32_t JNICALL Java_sun_misc_Unsafe_arrayIndexScale(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Class *arrayClass)
764 {
765         classinfo       *c;
766         arraydescriptor *ad;
767
768         c  = LLNI_classinfo_unwrap(arrayClass);
769         ad = c->vftbl->arraydesc;
770
771         if (ad == NULL) {
772                 /* XXX does that exception exist? */
773                 exceptions_throw_internalerror("java/lang/InvalidClassException");
774                 return 0;
775         }
776
777         return ad->componentsize;
778 }
779
780
781 /*
782  * Class:     sun/misc/Unsafe
783  * Method:    addressSize
784  * Signature: ()I
785  */
786 JNIEXPORT int32_t JNICALL Java_sun_misc_Unsafe_addressSize(JNIEnv *env, sun_misc_Unsafe *this)
787 {
788         return SIZEOF_VOID_P;
789 }
790
791
792 /*
793  * Class:     sun/misc/Unsafe
794  * Method:    pageSize
795  * Signature: ()I
796  */
797 JNIEXPORT int32_t JNICALL Java_sun_misc_Unsafe_pageSize(JNIEnv *env, sun_misc_Unsafe *this)
798 {
799         int sz;
800
801         sz = getpagesize();
802
803         return sz;
804 }
805
806
807 /*
808  * Class:     sun/misc/Unsafe
809  * Method:    defineClass
810  * Signature: (Ljava/lang/String;[BIILjava/lang/ClassLoader;Ljava/security/ProtectionDomain;)Ljava/lang/Class;
811  */
812 JNIEXPORT java_lang_Class* JNICALL Java_sun_misc_Unsafe_defineClass__Ljava_lang_String_2_3BIILjava_lang_ClassLoader_2Ljava_security_ProtectionDomain_2(JNIEnv *env, sun_misc_Unsafe *this, java_lang_String *name, java_handle_bytearray_t *b, int32_t off, int32_t len, java_lang_ClassLoader *loader, java_security_ProtectionDomain *protectionDomain)
813 {
814         classloader     *cl;
815         utf             *utfname;
816         classinfo       *c;
817         java_lang_Class *o;
818
819         cl = loader_hashtable_classloader_add((java_handle_t *) loader);
820
821         /* check if data was passed */
822
823         if (b == NULL) {
824                 exceptions_throw_nullpointerexception();
825                 return NULL;
826         }
827
828         /* check the indexes passed */
829
830         if ((off < 0) || (len < 0) || ((off + len) > LLNI_array_size(b))) {
831                 exceptions_throw_arrayindexoutofboundsexception();
832                 return NULL;
833         }
834
835         if (name != NULL) {
836                 /* convert '.' to '/' in java string */
837
838                 utfname = javastring_toutf((java_handle_t *) name, true);
839         } 
840         else {
841                 utfname = NULL;
842         }
843
844         /* define the class */
845
846         c = class_define(utfname, cl, len, (const uint8_t *) &(LLNI_array_direct(b, off)),
847                                          protectionDomain);
848
849         if (c == NULL)
850                 return NULL;
851
852         /* for convenience */
853
854         o = LLNI_classinfo_wrap(c);
855
856 #if defined(WITH_CLASSPATH_GNU)
857         /* set ProtectionDomain */
858
859         LLNI_field_set_ref(o, pd, protectionDomain);
860 #endif
861
862         return o;
863 }
864
865
866 /*
867  * Class:     sun/misc/Unsafe
868  * Method:    allocateInstance
869  * Signature: (Ljava/lang/Class;)Ljava/lang/Object;
870  */
871 JNIEXPORT java_lang_Object* JNICALL Java_sun_misc_Unsafe_allocateInstance(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Class *cls)
872 {
873         classinfo     *c;
874         java_handle_t *o;
875
876         c = LLNI_classinfo_unwrap(cls);
877
878         o = builtin_new(c);
879
880         return (java_lang_Object *) o;
881 }
882
883
884 /*
885  * Class:     sun/misc/Unsafe
886  * Method:    throwException
887  * Signature: (Ljava/lang/Throwable;)V
888  */
889 JNIEXPORT void JNICALL Java_sun_misc_Unsafe_throwException(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Throwable *ee)
890 {
891         java_handle_t *o;
892
893         o = (java_handle_t *) ee;
894
895         exceptions_set_exception(o);
896 }
897
898
899 /*
900  * Class:     sun/misc/Unsafe
901  * Method:    compareAndSwapObject
902  * Signature: (Ljava/lang/Object;JLjava/lang/Object;Ljava/lang/Object;)Z
903  */
904 JNIEXPORT int32_t JNICALL Java_sun_misc_Unsafe_compareAndSwapObject(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *o, int64_t offset, java_lang_Object *expected, java_lang_Object *x)
905 {
906         void **p;
907         void  *value;
908
909         p = (void **) (((uint8_t *) o) + offset);
910
911         /* XXX this should be atomic */
912
913         value = *p;
914
915         if (value == expected) {
916                 *p = x;
917
918                 return true;
919         }
920
921         return false;
922 }
923
924
925 /*
926  * Class:     sun/misc/Unsafe
927  * Method:    compareAndSwapInt
928  * Signature: (Ljava/lang/Object;JII)Z
929  */
930 JNIEXPORT int32_t JNICALL Java_sun_misc_Unsafe_compareAndSwapInt(JNIEnv *env, sun_misc_Unsafe* this, java_lang_Object* obj, int64_t offset, int32_t expect, int32_t update)
931 {
932         int32_t *p;
933         int32_t  value;
934
935         p = (int32_t *) (((uint8_t *) obj) + offset);
936
937         /* XXX this should be atomic */
938
939         value = *p;
940
941         if (value == expect) {
942                 *p = update;
943
944                 return true;
945         }
946
947         return false;
948 }
949
950
951 /*
952  * Class:     sun/misc/Unsafe
953  * Method:    compareAndSwapLong
954  * Signature: (Ljava/lang/Object;JJJ)Z
955  */
956 JNIEXPORT int32_t JNICALL Java_sun_misc_Unsafe_compareAndSwapLong(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *o, int64_t offset, int64_t expected, int64_t x)
957 {
958         int64_t *p;
959         int64_t  value;
960
961         p = (int64_t *) (((uint8_t *) o) + offset);
962
963         /* XXX this should be atomic */
964
965         value = *p;
966
967         if (value == expected) {
968                 *p = x;
969
970                 return true;
971         }
972
973         return false;
974 }
975
976
977 /*
978  * Class:     sun/misc/Unsafe
979  * Method:    getObjectVolatile
980  * Signature: (Ljava/lang/Object;J)Ljava/lang/Object;
981  */
982 JNIEXPORT java_lang_Object* JNICALL Java_sun_misc_Unsafe_getObjectVolatile(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *o, int64_t offset)
983 {
984         volatile void **p;
985         volatile void  *value;
986
987         p = (volatile void **) (((uint8_t *) o) + offset);
988
989         value = *p;
990
991         return (java_lang_Object *) value;
992 }
993
994
995 /*
996  * Class:     sun/misc/Unsafe
997  * Method:    getIntVolatile
998  * Signature: (Ljava/lang/Object;J)I
999  */
1000 JNIEXPORT int32_t JNICALL Java_sun_misc_Unsafe_getIntVolatile(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *o, int64_t offset)
1001 {
1002         volatile int32_t *p;
1003         volatile int32_t  value;
1004
1005         p = (volatile int32_t *) (((uint8_t *) o) + offset);
1006
1007         value = *p;
1008
1009         return value;
1010 }
1011
1012
1013 /*
1014  * Class:     sun/misc/Unsafe
1015  * Method:    getLongVolatile
1016  * Signature: (Ljava/lang/Object;J)J
1017  */
1018 JNIEXPORT int64_t JNICALL Java_sun_misc_Unsafe_getLongVolatile(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *o, int64_t offset)
1019 {
1020         volatile int64_t *p;
1021         volatile int64_t  value;
1022
1023         p = (volatile int64_t *) (((uint8_t *) o) + offset);
1024
1025         value = *p;
1026
1027         return value;
1028 }
1029
1030
1031 /*
1032  * Class:     sun/misc/Unsafe
1033  * Method:    unpark
1034  * Signature: (Ljava/lang/Object;)V
1035  */
1036 JNIEXPORT void JNICALL Java_sun_misc_Unsafe_unpark(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *thread)
1037 {
1038         /* XXX IMPLEMENT ME */
1039 }
1040
1041
1042 /*
1043  * Class:     sun/misc/Unsafe
1044  * Method:    park
1045  * Signature: (ZJ)V
1046  */
1047 JNIEXPORT void JNICALL Java_sun_misc_Unsafe_park(JNIEnv *env, sun_misc_Unsafe *this, int32_t isAbsolute, int64_t time)
1048 {
1049         /* XXX IMPLEMENT ME */
1050 }
1051
1052
1053 /*
1054  * These are local overrides for various environment variables in Emacs.
1055  * Please do not remove this and leave it at the end of the file, where
1056  * Emacs will automagically detect them.
1057  * ---------------------------------------------------------------------
1058  * Local variables:
1059  * mode: c
1060  * indent-tabs-mode: t
1061  * c-basic-offset: 4
1062  * tab-width: 4
1063  * End:
1064  */