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