4650bad4c8702d51042109955aeabb509c7f9c4d
[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    $Id: java_lang_VMObject.c 5153 2006-07-18 08:19:24Z twisti $
26
27 */
28
29
30 #include "config.h"
31
32 #include <stdint.h>
33 #include <unistd.h>
34
35 #include "mm/memory.h"
36
37 #include "native/jni.h"
38 #include "native/llni.h"
39 #include "native/native.h"
40
41 #include "native/include/java_lang_Object.h"                  /* before c.l.C */
42 #include "native/include/java_lang_String.h"            /* required by j.l.CL */
43
44 #if defined(WITH_CLASSPATH_SUN)
45 # include "native/include/java_nio_ByteBuffer.h"        /* required by j.l.CL */
46 #endif
47
48 #include "native/include/java_lang_ClassLoader.h"        /* required by j.l.C */
49 #include "native/include/java_lang_Class.h"
50 #include "native/include/java_lang_reflect_Field.h"
51 #include "native/include/java_lang_Thread.h"             /* required by s.m.U */
52 #include "native/include/java_lang_Throwable.h"
53
54 #include "native/include/java_security_ProtectionDomain.h" /* required by smU */
55
56 #include "native/include/sun_misc_Unsafe.h"
57
58 #include "vm/builtin.h"
59 #include "vm/exceptions.h"
60 #include "vm/initialize.h"
61 #include "vm/stringlocal.h"
62
63 #include "vmcore/utf8.h"
64
65
66 /* native methods implemented by this file ************************************/
67
68 static JNINativeMethod methods[] = {
69         { "registerNatives",        "()V",                                                        (void *) (intptr_t) &Java_sun_misc_Unsafe_registerNatives                },
70         { "getInt",                 "(Ljava/lang/Object;J)I",                                     (void *) (intptr_t) &Java_sun_misc_Unsafe_getInt__Ljava_lang_Object_2J   },
71         { "putInt",                 "(Ljava/lang/Object;JI)V",                                    (void *) (intptr_t) &Java_sun_misc_Unsafe_putInt__Ljava_lang_Object_2JI  },
72         { "getObject",              "(Ljava/lang/Object;J)Ljava/lang/Object;",                    (void *) (intptr_t) &Java_sun_misc_Unsafe_getObject                      },
73         { "putObject",              "(Ljava/lang/Object;JLjava/lang/Object;)V",                   (void *) (intptr_t) &Java_sun_misc_Unsafe_putObject                      },
74         { "getBoolean",             "(Ljava/lang/Object;J)Z",                                     (void *) (intptr_t) &Java_sun_misc_Unsafe_getBoolean                     },
75         { "putBoolean",             "(Ljava/lang/Object;JZ)V",                                    (void *) (intptr_t) &Java_sun_misc_Unsafe_putBoolean                     },
76         { "getByte",                "(Ljava/lang/Object;J)B",                                     (void *) (intptr_t) &Java_sun_misc_Unsafe_getByte__Ljava_lang_Object_2J  },
77         { "putByte",                "(Ljava/lang/Object;JB)V",                                    (void *) (intptr_t) &Java_sun_misc_Unsafe_putByte__Ljava_lang_Object_2JB },
78         { "putShort",               "(Ljava/lang/Object;JS)V",                                    (void *) (intptr_t) &Java_sun_misc_Unsafe_putShort__Ljava_lang_Object_2JS },
79         { "getChar",                "(Ljava/lang/Object;J)C",                                     (void *) (intptr_t) &Java_sun_misc_Unsafe_getChar__Ljava_lang_Object_2J  },
80         { "putChar",                "(Ljava/lang/Object;JC)V",                                    (void *) (intptr_t) &Java_sun_misc_Unsafe_putChar__Ljava_lang_Object_2JC },
81         { "putFloat",               "(Ljava/lang/Object;JF)V",                                    (void *) (intptr_t) &Java_sun_misc_Unsafe_putFloat__Ljava_lang_Object_2JF },
82         { "getByte",                "(J)B",                                                       (void *) (intptr_t) &Java_sun_misc_Unsafe_getByte__J                     },
83         { "getInt",                 "(J)I",                                                       (void *) (intptr_t) &Java_sun_misc_Unsafe_getInt__J                      },
84         { "putInt",                 "(JI)V",                                                      (void *) (intptr_t) &Java_sun_misc_Unsafe_putInt__JI                     },
85         { "getLong",                "(J)J",                                                       (void *) (intptr_t) &Java_sun_misc_Unsafe_getLong__J                     },
86         { "putLong",                "(JJ)V",                                                      (void *) (intptr_t) &Java_sun_misc_Unsafe_putLong__JJ                    },
87         { "objectFieldOffset",      "(Ljava/lang/reflect/Field;)J",                               (void *) (intptr_t) &Java_sun_misc_Unsafe_objectFieldOffset              },
88         { "allocateMemory",         "(J)J",                                                       (void *) (intptr_t) &Java_sun_misc_Unsafe_allocateMemory                 },
89         { "freeMemory",             "(J)V",                                                       (void *) (intptr_t) &Java_sun_misc_Unsafe_freeMemory                     },
90         { "staticFieldOffset",      "(Ljava/lang/reflect/Field;)J",                               (void *) (intptr_t) &Java_sun_misc_Unsafe_staticFieldOffset              },
91         { "staticFieldBase",        "(Ljava/lang/reflect/Field;)Ljava/lang/Object;",              (void *) (intptr_t) &Java_sun_misc_Unsafe_staticFieldBase                },
92         { "ensureClassInitialized", "(Ljava/lang/Class;)V",                                       (void *) (intptr_t) &Java_sun_misc_Unsafe_ensureClassInitialized         },
93         { "arrayBaseOffset",        "(Ljava/lang/Class;)I",                                       (void *) (intptr_t) &Java_sun_misc_Unsafe_arrayBaseOffset                },
94         { "arrayIndexScale",        "(Ljava/lang/Class;)I",                                       (void *) (intptr_t) &Java_sun_misc_Unsafe_arrayIndexScale                },
95         { "addressSize",            "()I",                                                        (void *) (intptr_t) &Java_sun_misc_Unsafe_addressSize                    },
96         { "pageSize",               "()I",                                                        (void *) (intptr_t) &Java_sun_misc_Unsafe_pageSize                       },
97         { "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 },
98         { "allocateInstance",       "(Ljava/lang/Class;)Ljava/lang/Object;",                      (void *) (intptr_t) &Java_sun_misc_Unsafe_allocateInstance               },
99         { "throwException",         "(Ljava/lang/Throwable;)V",                                   (void *) (intptr_t) &Java_sun_misc_Unsafe_throwException                 },
100         { "compareAndSwapObject",   "(Ljava/lang/Object;JLjava/lang/Object;Ljava/lang/Object;)Z", (void *) (intptr_t) &Java_sun_misc_Unsafe_compareAndSwapObject           },
101         { "compareAndSwapInt",      "(Ljava/lang/Object;JII)Z",                                   (void *) (intptr_t) &Java_sun_misc_Unsafe_compareAndSwapInt              },
102         { "compareAndSwapLong",     "(Ljava/lang/Object;JJJ)Z",                                   (void *) (intptr_t) &Java_sun_misc_Unsafe_compareAndSwapLong             },
103         { "getObjectVolatile",      "(Ljava/lang/Object;J)Ljava/lang/Object;",                    (void *) (intptr_t) &Java_sun_misc_Unsafe_getObjectVolatile              },
104         { "getIntVolatile",         "(Ljava/lang/Object;J)I",                                     (void *) (intptr_t) &Java_sun_misc_Unsafe_getIntVolatile                 },
105         { "getLongVolatile",        "(Ljava/lang/Object;J)J",                                     (void *) (intptr_t) &Java_sun_misc_Unsafe_getLongVolatile                },
106         { "unpark",                 "(Ljava/lang/Object;)V",                                      (void *) (intptr_t) &Java_sun_misc_Unsafe_unpark                         },
107         { "park",                   "(ZJ)V",                                                      (void *) (intptr_t) &Java_sun_misc_Unsafe_park                           },
108 };
109
110
111 /* _Jv_sun_misc_Unsafe_init ****************************************************
112
113    Register native functions.
114
115 *******************************************************************************/
116
117 void _Jv_sun_misc_Unsafe_init(void)
118 {
119         utf *u;
120
121         u = utf_new_char("sun/misc/Unsafe");
122
123         native_method_register(u, methods, NATIVE_METHODS_COUNT);
124 }
125
126
127 /*
128  * Class:     sun/misc/Unsafe
129  * Method:    registerNatives
130  * Signature: ()V
131  */
132 JNIEXPORT void JNICALL Java_sun_misc_Unsafe_registerNatives(JNIEnv *env, jclass clazz)
133 {
134         /* The native methods of this function are already registered in
135            _Jv_sun_misc_Unsafe_init() which is called during VM
136            startup. */
137 }
138
139
140 /*
141  * Class:     sun/misc/Unsafe
142  * Method:    getInt
143  * Signature: (Ljava/lang/Object;J)I
144  */
145 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)
146 {
147         int32_t *p;
148         int32_t  value;
149
150         p = (int32_t *) (((uint8_t *) o) + offset);
151
152         value = *p;
153
154         return value;
155 }
156
157
158 /*
159  * Class:     sun/misc/Unsafe
160  * Method:    putInt
161  * Signature: (Ljava/lang/Object;JI)V
162  */
163 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)
164 {
165         int32_t *p;
166
167         p = (int32_t *) (((uint8_t *) o) + offset);
168
169         *p = x;
170 }
171
172
173 /*
174  * Class:     sun/misc/Unsafe
175  * Method:    getObject
176  * Signature: (Ljava/lang/Object;J)Ljava/lang/Object;
177  */
178 JNIEXPORT java_lang_Object* JNICALL Java_sun_misc_Unsafe_getObject(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *o, int64_t offset)
179 {
180         void **p;
181         void  *value;
182
183         p = (void **) (((uint8_t *) o) + offset);
184
185         value = *p;
186
187         return value;
188 }
189
190
191 /*
192  * Class:     sun/misc/Unsafe
193  * Method:    putObject
194  * Signature: (Ljava/lang/Object;JLjava/lang/Object;)V
195  */
196 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)
197 {
198         void **p;
199
200         p = (void **) (((uint8_t *) o) + offset);
201
202         *p = (void *) x;
203 }
204
205
206 /*
207  * Class:     sun/misc/Unsafe
208  * Method:    getBoolean
209  * Signature: (Ljava/lang/Object;J)Z
210  */
211 JNIEXPORT int32_t JNICALL Java_sun_misc_Unsafe_getBoolean(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *o, int64_t offset)
212 {
213         int32_t *p;
214         int32_t  value;
215
216         p = (int32_t *) (((uint8_t *) o) + offset);
217
218         value = *p;
219
220         return value;
221 }
222
223
224 /*
225  * Class:     sun/misc/Unsafe
226  * Method:    putBoolean
227  * Signature: (Ljava/lang/Object;JZ)V
228  */
229 JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putBoolean(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *o, int64_t offset, int32_t x)
230 {
231         int32_t *p;
232
233         p = (int32_t *) (((uint8_t *) o) + offset);
234
235         *p = x;
236 }
237
238
239 /*
240  * Class:     sun/misc/Unsafe
241  * Method:    getByte
242  * Signature: (Ljava/lang/Object;J)B
243  */
244 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)
245 {
246         int32_t *p;
247         int32_t  value;
248
249         p = (int32_t *) (((uint8_t *) o) + offset);
250
251         value = *p;
252
253         return value;
254 }
255
256
257 /*
258  * Class:     sun/misc/Unsafe
259  * Method:    putByte
260  * Signature: (Ljava/lang/Object;JB)V
261  */
262 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)
263 {
264         int32_t *p;
265
266         p = (int32_t *) (((uint8_t *) o) + offset);
267
268         *p = x;
269 }
270
271
272 /*
273  * Class:     sun/misc/Unsafe
274  * Method:    putShort
275  * Signature: (Ljava/lang/Object;JS)V
276  */
277 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)
278 {
279         int32_t *p;
280
281         p = (int32_t *) (((uint8_t *) o) + offset);
282
283         *p = x;
284 }
285
286
287 /*
288  * Class:     sun/misc/Unsafe
289  * Method:    getChar
290  * Signature: (Ljava/lang/Object;J)C
291  */
292 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)
293 {
294         int32_t *p;
295         int32_t  value;
296
297         p = (int32_t *) (((uint8_t *) o) + offset);
298
299         value = *p;
300
301         return value;
302 }
303
304
305 /*
306  * Class:     sun/misc/Unsafe
307  * Method:    putChar
308  * Signature: (Ljava/lang/Object;JC)V
309  */
310 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)
311 {
312         int32_t *p;
313
314         p = (int32_t *) (((uint8_t *) o) + offset);
315
316         *p = x;
317 }
318
319
320 /*
321  * Class:     sun/misc/Unsafe
322  * Method:    putFloat
323  * Signature: (Ljava/lang/Object;JF)V
324  */
325 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)
326 {
327         float *p;
328
329         p = (float *) (((uint8_t *) o) + offset);
330
331         *p = x;
332 }
333
334
335 /*
336  * Class:     sun/misc/Unsafe
337  * Method:    getByte
338  * Signature: (J)B
339  */
340 JNIEXPORT int32_t JNICALL Java_sun_misc_Unsafe_getByte__J(JNIEnv *env, sun_misc_Unsafe *this, int64_t address)
341 {
342         int8_t *p;
343         int8_t  value;
344
345         p = (int8_t *) (intptr_t) address;
346
347         value = *p;
348
349         return (int32_t) value;
350 }
351
352
353 /*
354  * Class:     sun/misc/Unsafe
355  * Method:    getInt
356  * Signature: (J)I
357  */
358 JNIEXPORT int32_t JNICALL Java_sun_misc_Unsafe_getInt__J(JNIEnv *env, sun_misc_Unsafe *this, int64_t address)
359 {
360         int32_t *p;
361         int32_t  value;
362
363         p = (int32_t *) (intptr_t) address;
364
365         value = *p;
366
367         return value;
368 }
369
370
371 /*
372  * Class:     sun/misc/Unsafe
373  * Method:    putInt
374  * Signature: (JI)V
375  */
376 JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putInt__JI(JNIEnv *env, struct sun_misc_Unsafe* this, int64_t address, int32_t value)
377 {
378         int32_t *p;
379
380         p = (int32_t *) (intptr_t) address;
381
382         *p = value;
383 }
384
385
386 /*
387  * Class:     sun/misc/Unsafe
388  * Method:    getLong
389  * Signature: (J)J
390  */
391 JNIEXPORT int64_t JNICALL Java_sun_misc_Unsafe_getLong__J(JNIEnv *env, sun_misc_Unsafe *this, int64_t address)
392 {
393         int64_t *p;
394         int64_t  value;
395
396         p = (int64_t *) (intptr_t) address;
397
398         value = *p;
399
400         return value;
401 }
402
403
404 /*
405  * Class:     sun/misc/Unsafe
406  * Method:    putLong
407  * Signature: (JJ)V
408  */
409 JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putLong__JJ(JNIEnv *env, sun_misc_Unsafe *this, int64_t address, int64_t value)
410 {
411         int64_t *p;
412
413         p = (int64_t *) (intptr_t) address;
414
415         *p = value;
416 }
417
418
419 /*
420  * Class:     sun/misc/Unsafe
421  * Method:    objectFieldOffset
422  * Signature: (Ljava/lang/reflect/Field;)J
423  */
424 JNIEXPORT int64_t JNICALL Java_sun_misc_Unsafe_objectFieldOffset(JNIEnv *env, sun_misc_Unsafe* this, java_lang_reflect_Field* field)
425 {
426         classinfo *c;
427         fieldinfo *f;
428         int32_t    slot;
429
430         LLNI_field_get_cls(field, clazz, c);
431         LLNI_field_get_val(field, slot , slot);
432         f = &c->fields[slot];
433
434         return (int64_t) f->offset;
435 }
436
437
438 /*
439  * Class:     sun/misc/Unsafe
440  * Method:    allocateMemory
441  * Signature: (J)J
442  */
443 JNIEXPORT int64_t JNICALL Java_sun_misc_Unsafe_allocateMemory(JNIEnv *env, sun_misc_Unsafe *this, int64_t bytes)
444 {
445         size_t  length;
446         void   *p;
447
448         length = (size_t) bytes;
449
450         if ((length != (uint64_t) bytes) || (bytes < 0)) {
451                 exceptions_throw_illegalargumentexception();
452                 return 0;
453         }
454
455         p = MNEW(uint8_t, length);
456
457         return (int64_t) (intptr_t) p;
458 }
459
460
461 /*
462  * Class:     sun/misc/Unsafe
463  * Method:    freeMemory
464  * Signature: (J)V
465  */
466 JNIEXPORT void JNICALL Java_sun_misc_Unsafe_freeMemory(JNIEnv *env, sun_misc_Unsafe *this, int64_t address)
467 {
468         void *p;
469
470         p = (void *) (intptr_t) address;
471
472         if (p == NULL)
473                 return;
474
475         /* we pass length 1 to trick the free function */
476
477         MFREE(p, uint8_t, 1);
478 }
479
480
481 /*
482  * Class:     sun/misc/Unsafe
483  * Method:    staticFieldOffset
484  * Signature: (Ljava/lang/reflect/Field;)J
485  */
486 JNIEXPORT int64_t JNICALL Java_sun_misc_Unsafe_staticFieldOffset(JNIEnv *env, sun_misc_Unsafe *this, java_lang_reflect_Field *field)
487 {
488         classinfo *c;
489         fieldinfo *f;
490         int32_t    slot;
491
492         LLNI_field_get_cls(field, clazz, c);
493         LLNI_field_get_val(field, slot , slot);
494         f = &(c->fields[slot]);
495
496         return (int64_t) (intptr_t) f->value;
497 }
498
499
500 /*
501  * Class:     sun/misc/Unsafe
502  * Method:    staticFieldBase
503  * Signature: (Ljava/lang/reflect/Field;)Ljava/lang/Object;
504  */
505 JNIEXPORT java_lang_Object* JNICALL Java_sun_misc_Unsafe_staticFieldBase(JNIEnv *env, sun_misc_Unsafe *this, java_lang_reflect_Field *f)
506 {
507         /* In CACAO we return the absolute address in staticFieldOffset. */
508
509         return NULL;
510 }
511
512
513 /*
514  * Class:     sun/misc/Unsafe
515  * Method:    ensureClassInitialized
516  * Signature: (Ljava/lang/Class;)V
517  */
518 JNIEXPORT void JNICALL Java_sun_misc_Unsafe_ensureClassInitialized(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Class *class)
519 {
520         classinfo *c;
521
522         c = LLNI_classinfo_unwrap(class);
523
524         if (!(c->state & CLASS_INITIALIZED))
525                 initialize_class(c);
526 }
527
528
529 /*
530  * Class:     sun/misc/Unsafe
531  * Method:    arrayBaseOffset
532  * Signature: (Ljava/lang/Class;)I
533  */
534 JNIEXPORT int32_t JNICALL Java_sun_misc_Unsafe_arrayBaseOffset(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Class *arrayClass)
535 {
536         classinfo       *c;
537         arraydescriptor *ad;
538
539         c  = LLNI_classinfo_unwrap(arrayClass);
540         ad = c->vftbl->arraydesc;
541
542         if (ad == NULL) {
543                 /* XXX does that exception exist? */
544                 exceptions_throw_internalerror("java/lang/InvalidClassException");
545                 return 0;
546         }
547
548         return ad->dataoffset;
549 }
550
551
552 /*
553  * Class:     sun/misc/Unsafe
554  * Method:    arrayIndexScale
555  * Signature: (Ljava/lang/Class;)I
556  */
557 JNIEXPORT int32_t JNICALL Java_sun_misc_Unsafe_arrayIndexScale(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Class *arrayClass)
558 {
559         classinfo       *c;
560         arraydescriptor *ad;
561
562         c  = LLNI_classinfo_unwrap(arrayClass);
563         ad = c->vftbl->arraydesc;
564
565         if (ad == NULL) {
566                 /* XXX does that exception exist? */
567                 exceptions_throw_internalerror("java/lang/InvalidClassException");
568                 return 0;
569         }
570
571         return ad->componentsize;
572 }
573
574
575 /*
576  * Class:     sun/misc/Unsafe
577  * Method:    addressSize
578  * Signature: ()I
579  */
580 JNIEXPORT int32_t JNICALL Java_sun_misc_Unsafe_addressSize(JNIEnv *env, sun_misc_Unsafe *this)
581 {
582         return SIZEOF_VOID_P;
583 }
584
585
586 /*
587  * Class:     sun/misc/Unsafe
588  * Method:    pageSize
589  * Signature: ()I
590  */
591 JNIEXPORT int32_t JNICALL Java_sun_misc_Unsafe_pageSize(JNIEnv *env, sun_misc_Unsafe *this)
592 {
593         int sz;
594
595         sz = getpagesize();
596
597         return sz;
598 }
599
600
601 /*
602  * Class:     sun/misc/Unsafe
603  * Method:    defineClass
604  * Signature: (Ljava/lang/String;[BIILjava/lang/ClassLoader;Ljava/security/ProtectionDomain;)Ljava/lang/Class;
605  */
606 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)
607 {
608         classloader     *cl;
609         utf             *utfname;
610         classinfo       *c;
611         java_lang_Class *o;
612
613         cl = (classloader *) loader;
614
615         /* check if data was passed */
616
617         if (b == NULL) {
618                 exceptions_throw_nullpointerexception();
619                 return NULL;
620         }
621
622         /* check the indexes passed */
623
624         if ((off < 0) || (len < 0) || ((off + len) > LLNI_array_size(b))) {
625                 exceptions_throw_arrayindexoutofboundsexception();
626                 return NULL;
627         }
628
629         if (name != NULL) {
630                 /* convert '.' to '/' in java string */
631
632                 utfname = javastring_toutf((java_handle_t *) name, true);
633         } 
634         else {
635                 utfname = NULL;
636         }
637
638         /* define the class */
639
640         c = class_define(utfname, cl, len, (const uint8_t *) &b->data[off],
641                                          protectionDomain);
642
643         if (c == NULL)
644                 return NULL;
645
646         /* for convenience */
647
648         o = LLNI_classinfo_wrap(c);
649
650 #if defined(WITH_CLASSPATH_GNU)
651         /* set ProtectionDomain */
652
653         LLNI_field_set_ref(o, pd, protectionDomain);
654 #endif
655
656         return o;
657 }
658
659
660 /*
661  * Class:     sun/misc/Unsafe
662  * Method:    allocateInstance
663  * Signature: (Ljava/lang/Class;)Ljava/lang/Object;
664  */
665 JNIEXPORT java_lang_Object* JNICALL Java_sun_misc_Unsafe_allocateInstance(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Class *cls)
666 {
667         classinfo     *c;
668         java_object_t *o;
669
670         c = LLNI_classinfo_unwrap(cls);
671
672         o = builtin_new(c);
673
674         return (java_lang_Object *) o;
675 }
676
677
678 /*
679  * Class:     sun/misc/Unsafe
680  * Method:    throwException
681  * Signature: (Ljava/lang/Throwable;)V
682  */
683 JNIEXPORT void JNICALL Java_sun_misc_Unsafe_throwException(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Throwable *ee)
684 {
685         java_handle_t *o;
686
687         o = (java_handle_t *) ee;
688
689         exceptions_set_exception(o);
690 }
691
692
693 /*
694  * Class:     sun/misc/Unsafe
695  * Method:    compareAndSwapObject
696  * Signature: (Ljava/lang/Object;JLjava/lang/Object;Ljava/lang/Object;)Z
697  */
698 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)
699 {
700         void **p;
701         void  *value;
702
703         p = (void **) (((uint8_t *) o) + offset);
704
705         /* XXX this should be atomic */
706
707         value = *p;
708
709         if (value == expected) {
710                 *p = x;
711
712                 return true;
713         }
714
715         return false;
716 }
717
718
719 /*
720  * Class:     sun/misc/Unsafe
721  * Method:    compareAndSwapInt
722  * Signature: (Ljava/lang/Object;JII)Z
723  */
724 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)
725 {
726         int32_t *p;
727         int32_t  value;
728
729         p = (int32_t *) (((uint8_t *) obj) + offset);
730
731         /* XXX this should be atomic */
732
733         value = *p;
734
735         if (value == expect) {
736                 *p = update;
737
738                 return true;
739         }
740
741         return false;
742 }
743
744
745 /*
746  * Class:     sun/misc/Unsafe
747  * Method:    compareAndSwapLong
748  * Signature: (Ljava/lang/Object;JJJ)Z
749  */
750 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)
751 {
752         int64_t *p;
753         int64_t  value;
754
755         p = (int64_t *) (((uint8_t *) o) + offset);
756
757         /* XXX this should be atomic */
758
759         value = *p;
760
761         if (value == expected) {
762                 *p = x;
763
764                 return true;
765         }
766
767         return false;
768 }
769
770
771 /*
772  * Class:     sun/misc/Unsafe
773  * Method:    getObjectVolatile
774  * Signature: (Ljava/lang/Object;J)Ljava/lang/Object;
775  */
776 JNIEXPORT java_lang_Object* JNICALL Java_sun_misc_Unsafe_getObjectVolatile(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *o, int64_t offset)
777 {
778         volatile void **p;
779         volatile void  *value;
780
781         p = (volatile void **) (((uint8_t *) o) + offset);
782
783         value = *p;
784
785         return (java_lang_Object *) value;
786 }
787
788
789 /*
790  * Class:     sun/misc/Unsafe
791  * Method:    getIntVolatile
792  * Signature: (Ljava/lang/Object;J)I
793  */
794 JNIEXPORT int32_t JNICALL Java_sun_misc_Unsafe_getIntVolatile(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *o, int64_t offset)
795 {
796         volatile int32_t *p;
797         volatile int32_t  value;
798
799         p = (volatile int32_t *) (((uint8_t *) o) + offset);
800
801         value = *p;
802
803         return value;
804 }
805
806
807 /*
808  * Class:     sun/misc/Unsafe
809  * Method:    getLongVolatile
810  * Signature: (Ljava/lang/Object;J)J
811  */
812 JNIEXPORT int64_t JNICALL Java_sun_misc_Unsafe_getLongVolatile(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *o, int64_t offset)
813 {
814         volatile int64_t *p;
815         volatile int64_t  value;
816
817         p = (volatile int64_t *) (((uint8_t *) o) + offset);
818
819         value = *p;
820
821         return value;
822 }
823
824
825 /*
826  * Class:     sun/misc/Unsafe
827  * Method:    unpark
828  * Signature: (Ljava/lang/Object;)V
829  */
830 JNIEXPORT void JNICALL Java_sun_misc_Unsafe_unpark(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *thread)
831 {
832         /* XXX IMPLEMENT ME */
833 }
834
835
836 /*
837  * Class:     sun/misc/Unsafe
838  * Method:    park
839  * Signature: (ZJ)V
840  */
841 JNIEXPORT void JNICALL Java_sun_misc_Unsafe_park(JNIEnv *env, sun_misc_Unsafe *this, int32_t isAbsolute, int64_t time)
842 {
843         /* XXX IMPLEMENT ME */
844 }
845
846
847 /*
848  * These are local overrides for various environment variables in Emacs.
849  * Please do not remove this and leave it at the end of the file, where
850  * Emacs will automagically detect them.
851  * ---------------------------------------------------------------------
852  * Local variables:
853  * mode: c
854  * indent-tabs-mode: t
855  * c-basic-offset: 4
856  * tab-width: 4
857  * End:
858  */