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