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