1 /* src/native/vm/sun_misc_Unsafe.c - sun/misc/Unsafe
3 Copyright (C) 2006, 2007, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
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.
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.
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
31 #include "machine-instr.h"
33 #include "mm/memory.h"
35 #include "native/jni.h"
36 #include "native/llni.h"
37 #include "native/native.h"
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 */
42 #if defined(WITH_CLASSPATH_SUN)
43 # include "native/include/java_nio_ByteBuffer.h" /* required by j.l.CL */
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"
52 #if defined(WITH_CLASSPATH_GNU)
53 # include "native/include/java_lang_reflect_VMField.h"
56 #include "native/include/java_security_ProtectionDomain.h" /* required by smU */
58 #include "native/include/sun_misc_Unsafe.h"
60 #include "vm/builtin.h"
61 #include "vm/exceptions.h"
62 #include "vm/initialize.h"
63 #include "vm/stringlocal.h"
65 #include "vmcore/system.h"
66 #include "vmcore/utf8.h"
69 /* native methods implemented by this file ************************************/
71 static JNINativeMethod methods[] = {
72 { "registerNatives", "()V", (void *) (intptr_t) &Java_sun_misc_Unsafe_registerNatives },
73 { "getInt", "(Ljava/lang/Object;J)I", (void *) (intptr_t) &Java_sun_misc_Unsafe_getInt__Ljava_lang_Object_2J },
74 { "putInt", "(Ljava/lang/Object;JI)V", (void *) (intptr_t) &Java_sun_misc_Unsafe_putInt__Ljava_lang_Object_2JI },
75 { "getObject", "(Ljava/lang/Object;J)Ljava/lang/Object;", (void *) (intptr_t) &Java_sun_misc_Unsafe_getObject },
76 { "putObject", "(Ljava/lang/Object;JLjava/lang/Object;)V", (void *) (intptr_t) &Java_sun_misc_Unsafe_putObject },
77 { "getBoolean", "(Ljava/lang/Object;J)Z", (void *) (intptr_t) &Java_sun_misc_Unsafe_getBoolean },
78 { "putBoolean", "(Ljava/lang/Object;JZ)V", (void *) (intptr_t) &Java_sun_misc_Unsafe_putBoolean },
79 { "getByte", "(Ljava/lang/Object;J)B", (void *) (intptr_t) &Java_sun_misc_Unsafe_getByte__Ljava_lang_Object_2J },
80 { "putByte", "(Ljava/lang/Object;JB)V", (void *) (intptr_t) &Java_sun_misc_Unsafe_putByte__Ljava_lang_Object_2JB },
81 { "getShort", "(Ljava/lang/Object;J)S", (void *) (intptr_t) &Java_sun_misc_Unsafe_getShort__Ljava_lang_Object_2J },
82 { "putShort", "(Ljava/lang/Object;JS)V", (void *) (intptr_t) &Java_sun_misc_Unsafe_putShort__Ljava_lang_Object_2JS },
83 { "getChar", "(Ljava/lang/Object;J)C", (void *) (intptr_t) &Java_sun_misc_Unsafe_getChar__Ljava_lang_Object_2J },
84 { "putChar", "(Ljava/lang/Object;JC)V", (void *) (intptr_t) &Java_sun_misc_Unsafe_putChar__Ljava_lang_Object_2JC },
85 { "getLong", "(Ljava/lang/Object;J)J", (void *) (intptr_t) &Java_sun_misc_Unsafe_getLong__Ljava_lang_Object_2J },
86 { "putLong", "(Ljava/lang/Object;JJ)V", (void *) (intptr_t) &Java_sun_misc_Unsafe_putLong__Ljava_lang_Object_2JJ },
87 { "getFloat", "(Ljava/lang/Object;J)F", (void *) (intptr_t) &Java_sun_misc_Unsafe_getFloat__Ljava_lang_Object_2J },
88 { "putFloat", "(Ljava/lang/Object;JF)V", (void *) (intptr_t) &Java_sun_misc_Unsafe_putFloat__Ljava_lang_Object_2JF },
89 { "getDouble", "(Ljava/lang/Object;J)D", (void *) (intptr_t) &Java_sun_misc_Unsafe_getDouble__Ljava_lang_Object_2J },
90 { "putDouble", "(Ljava/lang/Object;JD)V", (void *) (intptr_t) &Java_sun_misc_Unsafe_putDouble__Ljava_lang_Object_2JD },
91 { "getByte", "(J)B", (void *) (intptr_t) &Java_sun_misc_Unsafe_getByte__J },
92 { "putByte", "(JB)V", (void *) (intptr_t) &Java_sun_misc_Unsafe_putByte__JB },
93 { "getShort", "(J)S", (void *) (intptr_t) &Java_sun_misc_Unsafe_getShort__J },
94 { "putShort", "(JS)V", (void *) (intptr_t) &Java_sun_misc_Unsafe_putShort__JS },
95 { "getChar", "(J)C", (void *) (intptr_t) &Java_sun_misc_Unsafe_getChar__J },
96 { "putChar", "(JC)V", (void *) (intptr_t) &Java_sun_misc_Unsafe_putChar__JC },
97 { "getInt", "(J)I", (void *) (intptr_t) &Java_sun_misc_Unsafe_getInt__J },
98 { "putInt", "(JI)V", (void *) (intptr_t) &Java_sun_misc_Unsafe_putInt__JI },
99 { "getLong", "(J)J", (void *) (intptr_t) &Java_sun_misc_Unsafe_getLong__J },
100 { "putLong", "(JJ)V", (void *) (intptr_t) &Java_sun_misc_Unsafe_putLong__JJ },
101 { "getFloat", "(J)F", (void *) (intptr_t) &Java_sun_misc_Unsafe_getFloat__J },
102 { "objectFieldOffset", "(Ljava/lang/reflect/Field;)J", (void *) (intptr_t) &Java_sun_misc_Unsafe_objectFieldOffset },
103 { "allocateMemory", "(J)J", (void *) (intptr_t) &Java_sun_misc_Unsafe_allocateMemory },
106 { "setMemory", "(Ljava/lang/Object;JJB)V", (void *) (intptr_t) &Java_sun_misc_Unsafe_setMemory },
107 { "copyMemory", "(Ljava/lang/Object;JLjava/lang/Object;JJ)V", (void *) (intptr_t) &Java_sun_misc_Unsafe_copyMemory },
109 { "setMemory", "(JJB)V", (void *) (intptr_t) &Java_sun_misc_Unsafe_setMemory },
110 { "copyMemory", "(JJJ)V", (void *) (intptr_t) &Java_sun_misc_Unsafe_copyMemory },
112 { "freeMemory", "(J)V", (void *) (intptr_t) &Java_sun_misc_Unsafe_freeMemory },
113 { "staticFieldOffset", "(Ljava/lang/reflect/Field;)J", (void *) (intptr_t) &Java_sun_misc_Unsafe_staticFieldOffset },
114 { "staticFieldBase", "(Ljava/lang/reflect/Field;)Ljava/lang/Object;", (void *) (intptr_t) &Java_sun_misc_Unsafe_staticFieldBase },
115 { "ensureClassInitialized", "(Ljava/lang/Class;)V", (void *) (intptr_t) &Java_sun_misc_Unsafe_ensureClassInitialized },
116 { "arrayBaseOffset", "(Ljava/lang/Class;)I", (void *) (intptr_t) &Java_sun_misc_Unsafe_arrayBaseOffset },
117 { "arrayIndexScale", "(Ljava/lang/Class;)I", (void *) (intptr_t) &Java_sun_misc_Unsafe_arrayIndexScale },
118 { "addressSize", "()I", (void *) (intptr_t) &Java_sun_misc_Unsafe_addressSize },
119 { "pageSize", "()I", (void *) (intptr_t) &Java_sun_misc_Unsafe_pageSize },
120 { "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 },
121 { "allocateInstance", "(Ljava/lang/Class;)Ljava/lang/Object;", (void *) (intptr_t) &Java_sun_misc_Unsafe_allocateInstance },
122 { "throwException", "(Ljava/lang/Throwable;)V", (void *) (intptr_t) &Java_sun_misc_Unsafe_throwException },
123 { "compareAndSwapObject", "(Ljava/lang/Object;JLjava/lang/Object;Ljava/lang/Object;)Z", (void *) (intptr_t) &Java_sun_misc_Unsafe_compareAndSwapObject },
124 { "compareAndSwapInt", "(Ljava/lang/Object;JII)Z", (void *) (intptr_t) &Java_sun_misc_Unsafe_compareAndSwapInt },
125 { "compareAndSwapLong", "(Ljava/lang/Object;JJJ)Z", (void *) (intptr_t) &Java_sun_misc_Unsafe_compareAndSwapLong },
126 { "getObjectVolatile", "(Ljava/lang/Object;J)Ljava/lang/Object;", (void *) (intptr_t) &Java_sun_misc_Unsafe_getObjectVolatile },
127 { "putObjectVolatile", "(Ljava/lang/Object;JLjava/lang/Object;)V", (void *) (intptr_t) &Java_sun_misc_Unsafe_putObjectVolatile },
128 { "getIntVolatile", "(Ljava/lang/Object;J)I", (void *) (intptr_t) &Java_sun_misc_Unsafe_getIntVolatile },
129 { "putIntVolatile", "(Ljava/lang/Object;JI)V", (void *) (intptr_t) &Java_sun_misc_Unsafe_putIntVolatile },
130 { "getLongVolatile", "(Ljava/lang/Object;J)J", (void *) (intptr_t) &Java_sun_misc_Unsafe_getLongVolatile },
131 { "putLongVolatile", "(Ljava/lang/Object;JJ)V", (void *) (intptr_t) &Java_sun_misc_Unsafe_putLongVolatile },
132 { "putOrderedObject", "(Ljava/lang/Object;JLjava/lang/Object;)V", (void *) (intptr_t) &Java_sun_misc_Unsafe_putOrderedObject },
133 { "putOrderedInt", "(Ljava/lang/Object;JI)V", (void *) (intptr_t) &Java_sun_misc_Unsafe_putOrderedInt },
134 { "putOrderedLong", "(Ljava/lang/Object;JJ)V", (void *) (intptr_t) &Java_sun_misc_Unsafe_putOrderedLong },
135 { "unpark", "(Ljava/lang/Object;)V", (void *) (intptr_t) &Java_sun_misc_Unsafe_unpark },
136 { "park", "(ZJ)V", (void *) (intptr_t) &Java_sun_misc_Unsafe_park },
140 /* _Jv_sun_misc_Unsafe_init ****************************************************
142 Register native functions.
144 *******************************************************************************/
146 void _Jv_sun_misc_Unsafe_init(void)
150 u = utf_new_char("sun/misc/Unsafe");
152 native_method_register(u, methods, NATIVE_METHODS_COUNT);
157 * Class: sun/misc/Unsafe
158 * Method: registerNatives
161 JNIEXPORT void JNICALL Java_sun_misc_Unsafe_registerNatives(JNIEnv *env, jclass clazz)
163 /* The native methods of this function are already registered in
164 _Jv_sun_misc_Unsafe_init() which is called during VM
170 * Class: sun/misc/Unsafe
172 * Signature: (Ljava/lang/Object;J)I
174 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)
179 p = (int32_t *) (((uint8_t *) o) + offset);
188 * Class: sun/misc/Unsafe
190 * Signature: (Ljava/lang/Object;JI)V
192 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)
196 p = (int32_t *) (((uint8_t *) o) + offset);
203 * Class: sun/misc/Unsafe
205 * Signature: (Ljava/lang/Object;J)Ljava/lang/Object;
207 JNIEXPORT java_lang_Object* JNICALL Java_sun_misc_Unsafe_getObject(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *o, int64_t offset)
212 p = (void **) (((uint8_t *) o) + offset);
221 * Class: sun/misc/Unsafe
223 * Signature: (Ljava/lang/Object;JLjava/lang/Object;)V
225 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)
229 p = (void **) (((uint8_t *) o) + offset);
236 * Class: sun/misc/Unsafe
238 * Signature: (Ljava/lang/Object;J)Z
240 JNIEXPORT int32_t JNICALL Java_sun_misc_Unsafe_getBoolean(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *o, int64_t offset)
245 p = (int32_t *) (((uint8_t *) o) + offset);
254 * Class: sun/misc/Unsafe
256 * Signature: (Ljava/lang/Object;JZ)V
258 JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putBoolean(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *o, int64_t offset, int32_t x)
262 p = (int32_t *) (((uint8_t *) o) + offset);
269 * Class: sun/misc/Unsafe
271 * Signature: (Ljava/lang/Object;J)B
273 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)
278 p = (int32_t *) (((uint8_t *) o) + offset);
287 * Class: sun/misc/Unsafe
289 * Signature: (Ljava/lang/Object;JB)V
291 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)
295 p = (int32_t *) (((uint8_t *) o) + offset);
302 * Class: sun/misc/Unsafe
304 * Signature: (Ljava/lang/Object;J)S
306 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)
311 p = (int32_t *) (((uint8_t *) o) + offset);
320 * Class: sun/misc/Unsafe
322 * Signature: (Ljava/lang/Object;JS)V
324 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)
328 p = (int32_t *) (((uint8_t *) o) + offset);
335 * Class: sun/misc/Unsafe
337 * Signature: (Ljava/lang/Object;J)C
339 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)
344 p = (int32_t *) (((uint8_t *) o) + offset);
353 * Class: sun/misc/Unsafe
355 * Signature: (Ljava/lang/Object;JC)V
357 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)
361 p = (int32_t *) (((uint8_t *) o) + offset);
368 * Class: sun/misc/Unsafe
370 * Signature: (Ljava/lang/Object;J)J
372 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)
377 p = (int64_t *) (((uint8_t *) o) + offset);
386 * Class: sun/misc/Unsafe
388 * Signature: (Ljava/lang/Object;JJ)V
390 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)
394 p = (int64_t *) (((uint8_t *) o) + offset);
401 * Class: sun/misc/Unsafe
403 * Signature: (Ljava/lang/Object;J)F
405 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)
410 p = (float *) (((uint8_t *) o) + offset);
419 * Class: sun/misc/Unsafe
421 * Signature: (Ljava/lang/Object;JF)V
423 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)
427 p = (float *) (((uint8_t *) o) + offset);
434 * Class: sun/misc/Unsafe
436 * Signature: (Ljava/lang/Object;J)D
438 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)
443 p = (double *) (((uint8_t *) o) + offset);
452 * Class: sun/misc/Unsafe
454 * Signature: (Ljava/lang/Object;JD)V
456 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)
460 p = (double *) (((uint8_t *) o) + offset);
467 * Class: sun/misc/Unsafe
471 JNIEXPORT int32_t JNICALL Java_sun_misc_Unsafe_getByte__J(JNIEnv *env, sun_misc_Unsafe *this, int64_t address)
476 p = (int8_t *) (intptr_t) address;
480 return (int32_t) value;
485 * Class: sun/misc/Unsafe
489 JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putByte__JB(JNIEnv *env, sun_misc_Unsafe *this, int64_t address, int32_t value)
493 p = (int8_t *) (intptr_t) address;
500 * Class: sun/misc/Unsafe
504 JNIEXPORT int32_t JNICALL Java_sun_misc_Unsafe_getShort__J(JNIEnv *env, sun_misc_Unsafe *this, int64_t address)
509 p = (int16_t *) (intptr_t) address;
513 return (int32_t) value;
518 * Class: sun/misc/Unsafe
522 JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putShort__JS(JNIEnv *env, sun_misc_Unsafe *this, int64_t address, int32_t value)
526 p = (int16_t *) (intptr_t) address;
528 *p = (int16_t) value;
533 * Class: sun/misc/Unsafe
537 JNIEXPORT int32_t JNICALL Java_sun_misc_Unsafe_getChar__J(JNIEnv *env, sun_misc_Unsafe *this, int64_t address)
542 p = (uint16_t *) (intptr_t) address;
546 return (int32_t) value;
551 * Class: sun/misc/Unsafe
555 JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putChar__JC(JNIEnv *env, sun_misc_Unsafe *this, int64_t address, int32_t value)
559 p = (uint16_t *) (intptr_t) address;
561 *p = (uint16_t) value;
566 * Class: sun/misc/Unsafe
570 JNIEXPORT int32_t JNICALL Java_sun_misc_Unsafe_getInt__J(JNIEnv *env, sun_misc_Unsafe *this, int64_t address)
575 p = (int32_t *) (intptr_t) address;
584 * Class: sun/misc/Unsafe
588 JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putInt__JI(JNIEnv *env, struct sun_misc_Unsafe* this, int64_t address, int32_t value)
592 p = (int32_t *) (intptr_t) address;
599 * Class: sun/misc/Unsafe
603 JNIEXPORT int64_t JNICALL Java_sun_misc_Unsafe_getLong__J(JNIEnv *env, sun_misc_Unsafe *this, int64_t address)
608 p = (int64_t *) (intptr_t) address;
617 * Class: sun/misc/Unsafe
621 JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putLong__JJ(JNIEnv *env, sun_misc_Unsafe *this, int64_t address, int64_t value)
625 p = (int64_t *) (intptr_t) address;
632 * Class: sun/misc/Unsafe
636 JNIEXPORT float JNICALL Java_sun_misc_Unsafe_getFloat__J(JNIEnv *env, sun_misc_Unsafe *this, int64_t address)
641 p = (float *) (intptr_t) address;
650 * Class: sun/misc/Unsafe
651 * Method: objectFieldOffset
652 * Signature: (Ljava/lang/reflect/Field;)J
654 JNIEXPORT int64_t JNICALL Java_sun_misc_Unsafe_objectFieldOffset(JNIEnv *env, sun_misc_Unsafe *this, java_lang_reflect_Field *field)
660 #if defined(WITH_CLASSPATH_GNU)
661 java_lang_reflect_VMField *rvmf;
664 #if defined(WITH_CLASSPATH_GNU)
666 LLNI_field_get_ref(field, f, rvmf);
667 LLNI_field_get_cls(rvmf, clazz, c);
668 LLNI_field_get_val(rvmf, slot , slot);
670 #elif defined(WITH_CLASSPATH_SUN)
672 LLNI_field_get_cls(field, clazz, c);
673 LLNI_field_get_val(field, slot , slot);
676 # error unknown configuration
679 f = &(c->fields[slot]);
681 return (int64_t) f->offset;
686 * Class: sun/misc/Unsafe
687 * Method: allocateMemory
690 JNIEXPORT int64_t JNICALL Java_sun_misc_Unsafe_allocateMemory(JNIEnv *env, sun_misc_Unsafe *this, int64_t bytes)
695 length = (size_t) bytes;
697 if ((length != (uint64_t) bytes) || (bytes < 0)) {
698 exceptions_throw_illegalargumentexception();
702 p = MNEW(uint8_t, length);
704 return (int64_t) (intptr_t) p;
712 * Class: sun/misc/Unsafe
714 * Signature: (Ljava/lang/Object;JJB)V
716 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)
721 length = (size_t) bytes;
723 if ((length != (uint64_t) bytes) || (bytes < 0)) {
724 exceptions_throw_illegalargumentexception();
728 /* XXX Missing LLNI: we need to unwrap this object. */
730 p = (void *) (((uint8_t *) o) + offset);
732 /* XXX Not sure this is correct. */
734 system_memset(p, value, length);
739 * Class: sun/misc/Unsafe
741 * Signature: (Ljava/lang/Object;JLjava/lang/Object;JJ)V
743 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)
752 length = (size_t) bytes;
754 if ((length != (uint64_t) bytes) || (bytes < 0)) {
755 exceptions_throw_illegalargumentexception();
759 /* XXX Missing LLNI: We need to unwrap these objects. */
761 src = (void *) (((uint8_t *) srcBase) + srcOffset);
762 dest = (void *) (((uint8_t *) destBase) + destOffset);
764 system_memcpy(dest, src, length);
768 * Class: sun/misc/Unsafe
772 JNIEXPORT void JNICALL Java_sun_misc_Unsafe_setMemory(JNIEnv *env, sun_misc_Unsafe *this, int64_t address, int64_t bytes, int32_t value)
777 length = (size_t) bytes;
779 if ((length != (uint64_t) bytes) || (bytes < 0)) {
780 exceptions_throw_illegalargumentexception();
784 p = (void *) (intptr_t) address;
786 /* XXX Not sure this is correct. */
788 system_memset(p, value, length);
793 * Class: sun/misc/Unsafe
797 JNIEXPORT void JNICALL Java_sun_misc_Unsafe_copyMemory(JNIEnv *env, sun_misc_Unsafe *this, int64_t srcAddress, int64_t destAddress, int64_t bytes)
806 length = (size_t) bytes;
808 if ((length != (uint64_t) bytes) || (bytes < 0)) {
809 exceptions_throw_illegalargumentexception();
813 src = (void *) (intptr_t) srcAddress;
814 dest = (void *) (intptr_t) destAddress;
816 system_memcpy(dest, src, length);
822 * Class: sun/misc/Unsafe
826 JNIEXPORT void JNICALL Java_sun_misc_Unsafe_freeMemory(JNIEnv *env, sun_misc_Unsafe *this, int64_t address)
830 p = (void *) (intptr_t) address;
835 /* we pass length 1 to trick the free function */
837 MFREE(p, uint8_t, 1);
842 * Class: sun/misc/Unsafe
843 * Method: staticFieldOffset
844 * Signature: (Ljava/lang/reflect/Field;)J
846 JNIEXPORT int64_t JNICALL Java_sun_misc_Unsafe_staticFieldOffset(JNIEnv *env, sun_misc_Unsafe *this, java_lang_reflect_Field *f)
848 /* The offset of static fields is 0. */
855 * Class: sun/misc/Unsafe
856 * Method: staticFieldBase
857 * Signature: (Ljava/lang/reflect/Field;)Ljava/lang/Object;
859 JNIEXPORT java_lang_Object* JNICALL Java_sun_misc_Unsafe_staticFieldBase(JNIEnv *env, sun_misc_Unsafe *this, java_lang_reflect_Field *rf)
865 #if defined(WITH_CLASSPATH_GNU)
866 java_lang_reflect_VMField *rvmf;
869 #if defined(WITH_CLASSPATH_GNU)
871 LLNI_field_get_ref(rf, f, rvmf);
872 LLNI_field_get_cls(rvmf, clazz, c);
873 LLNI_field_get_val(rvmf, slot , slot);
875 #elif defined(WITH_CLASSPATH_SUN)
877 LLNI_field_get_cls(rf, clazz, c);
878 LLNI_field_get_val(rf, slot , slot);
881 # error unknown configuration
884 f = &(c->fields[slot]);
886 return (java_lang_Object *) (f->value);
891 * Class: sun/misc/Unsafe
892 * Method: ensureClassInitialized
893 * Signature: (Ljava/lang/Class;)V
895 JNIEXPORT void JNICALL Java_sun_misc_Unsafe_ensureClassInitialized(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Class *class)
899 c = LLNI_classinfo_unwrap(class);
901 if (!(c->state & CLASS_INITIALIZED))
907 * Class: sun/misc/Unsafe
908 * Method: arrayBaseOffset
909 * Signature: (Ljava/lang/Class;)I
911 JNIEXPORT int32_t JNICALL Java_sun_misc_Unsafe_arrayBaseOffset(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Class *arrayClass)
916 c = LLNI_classinfo_unwrap(arrayClass);
917 ad = c->vftbl->arraydesc;
920 /* XXX does that exception exist? */
921 exceptions_throw_internalerror("java/lang/InvalidClassException");
925 return ad->dataoffset;
930 * Class: sun/misc/Unsafe
931 * Method: arrayIndexScale
932 * Signature: (Ljava/lang/Class;)I
934 JNIEXPORT int32_t JNICALL Java_sun_misc_Unsafe_arrayIndexScale(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Class *arrayClass)
939 c = LLNI_classinfo_unwrap(arrayClass);
940 ad = c->vftbl->arraydesc;
943 /* XXX does that exception exist? */
944 exceptions_throw_internalerror("java/lang/InvalidClassException");
948 return ad->componentsize;
953 * Class: sun/misc/Unsafe
954 * Method: addressSize
957 JNIEXPORT int32_t JNICALL Java_sun_misc_Unsafe_addressSize(JNIEnv *env, sun_misc_Unsafe *this)
959 return SIZEOF_VOID_P;
964 * Class: sun/misc/Unsafe
968 JNIEXPORT int32_t JNICALL Java_sun_misc_Unsafe_pageSize(JNIEnv *env, sun_misc_Unsafe *this)
979 * Class: sun/misc/Unsafe
980 * Method: defineClass
981 * Signature: (Ljava/lang/String;[BIILjava/lang/ClassLoader;Ljava/security/ProtectionDomain;)Ljava/lang/Class;
983 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)
990 cl = loader_hashtable_classloader_add((java_handle_t *) loader);
992 /* check if data was passed */
995 exceptions_throw_nullpointerexception();
999 /* check the indexes passed */
1001 if ((off < 0) || (len < 0) || ((off + len) > LLNI_array_size(b))) {
1002 exceptions_throw_arrayindexoutofboundsexception();
1007 /* convert '.' to '/' in java string */
1009 utfname = javastring_toutf((java_handle_t *) name, true);
1015 /* define the class */
1017 c = class_define(utfname, cl, len, (uint8_t *) &(LLNI_array_direct(b, off)),
1018 (java_handle_t *) protectionDomain);
1023 /* for convenience */
1025 o = LLNI_classinfo_wrap(c);
1027 #if defined(WITH_CLASSPATH_GNU)
1028 /* set ProtectionDomain */
1030 LLNI_field_set_ref(o, pd, protectionDomain);
1038 * Class: sun/misc/Unsafe
1039 * Method: allocateInstance
1040 * Signature: (Ljava/lang/Class;)Ljava/lang/Object;
1042 JNIEXPORT java_lang_Object* JNICALL Java_sun_misc_Unsafe_allocateInstance(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Class *cls)
1047 c = LLNI_classinfo_unwrap(cls);
1051 return (java_lang_Object *) o;
1056 * Class: sun/misc/Unsafe
1057 * Method: throwException
1058 * Signature: (Ljava/lang/Throwable;)V
1060 JNIEXPORT void JNICALL Java_sun_misc_Unsafe_throwException(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Throwable *ee)
1064 o = (java_handle_t *) ee;
1066 exceptions_set_exception(o);
1071 * Class: sun/misc/Unsafe
1072 * Method: compareAndSwapObject
1073 * Signature: (Ljava/lang/Object;JLjava/lang/Object;Ljava/lang/Object;)Z
1075 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)
1081 p = (void **) (((uint8_t *) o) + offset);
1083 /* XXX this should be atomic */
1087 if (value == expected) {
1100 p = (volatile void **) (((uint8_t *) o) + offset);
1102 result = atomic_compare_and_swap_address(p, expected, x);
1104 if (result == expected)
1113 * Class: sun/misc/Unsafe
1114 * Method: compareAndSwapInt
1115 * Signature: (Ljava/lang/Object;JII)Z
1117 JNIEXPORT int32_t JNICALL Java_sun_misc_Unsafe_compareAndSwapInt(JNIEnv *env, sun_misc_Unsafe* this, java_lang_Object* o, int64_t offset, int32_t expected, int32_t x)
1123 p = (int32_t *) (((uint8_t *) o) + offset);
1125 /* XXX this should be atomic */
1129 if (value == expected) {
1142 p = (int32_t *) (((uint8_t *) o) + offset);
1144 result = atomic_compare_and_swap_int(p, expected, x);
1146 if (result == expected)
1155 * Class: sun/misc/Unsafe
1156 * Method: compareAndSwapLong
1157 * Signature: (Ljava/lang/Object;JJJ)Z
1159 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)
1164 p = (int64_t *) (((uint8_t *) o) + offset);
1166 /* XXX this should be atomic */
1170 if (value == expected) {
1181 * Class: sun/misc/Unsafe
1182 * Method: getObjectVolatile
1183 * Signature: (Ljava/lang/Object;J)Ljava/lang/Object;
1185 JNIEXPORT java_lang_Object* JNICALL Java_sun_misc_Unsafe_getObjectVolatile(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *o, int64_t offset)
1188 volatile void *value;
1190 p = (volatile void **) (((uint8_t *) o) + offset);
1194 return (java_lang_Object *) value;
1199 * Class: sun/misc/Unsafe
1200 * Method: putObjectVolatile
1201 * Signature: (Ljava/lang/Object;JLjava/lang/Object;)V
1203 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)
1207 p = (volatile void **) (((uint8_t *) o) + offset);
1213 #define UNSAFE_GET_VOLATILE(type) \
1214 java_handle_t *_h; \
1215 java_object_t *_o; \
1216 volatile type *_p; \
1219 _h = (java_handle_t *) o; \
1221 LLNI_CRITICAL_START; \
1223 _o = LLNI_UNWRAP(_h); \
1224 _p = (volatile type *) (((uint8_t *) _o) + offset); \
1228 LLNI_CRITICAL_END; \
1233 #define UNSAFE_PUT_VOLATILE(type) \
1234 java_handle_t *_h; \
1235 java_object_t *_o; \
1236 volatile type *_p; \
1238 _h = (java_handle_t *) o; \
1240 LLNI_CRITICAL_START; \
1242 _o = LLNI_UNWRAP(_h); \
1243 _p = (volatile type *) (((uint8_t *) _o) + offset); \
1253 * Class: sun/misc/Unsafe
1254 * Method: getIntVolatile
1255 * Signature: (Ljava/lang/Object;J)I
1257 JNIEXPORT int32_t JNICALL Java_sun_misc_Unsafe_getIntVolatile(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *o, int64_t offset)
1259 UNSAFE_GET_VOLATILE(int32_t);
1264 * Class: sun/misc/Unsafe
1265 * Method: putIntVolatile
1266 * Signature: (Ljava/lang/Object;JI)V
1268 JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putIntVolatile(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *o, int64_t offset, int32_t x)
1270 UNSAFE_PUT_VOLATILE(int32_t);
1275 * Class: sun/misc/Unsafe
1276 * Method: getLongVolatile
1277 * Signature: (Ljava/lang/Object;J)J
1279 JNIEXPORT int64_t JNICALL Java_sun_misc_Unsafe_getLongVolatile(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *o, int64_t offset)
1281 UNSAFE_GET_VOLATILE(int64_t);
1286 * Class: sun/misc/Unsafe
1287 * Method: putLongVolatile
1288 * Signature: (Ljava/lang/Object;JJ)V
1290 JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putLongVolatile(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *o, int64_t offset, int64_t x)
1292 UNSAFE_PUT_VOLATILE(int64_t);
1297 * Class: sun/misc/Unsafe
1298 * Method: putOrderedObject
1299 * Signature: (Ljava/lang/Object;JLjava/lang/Object;)V
1301 JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putOrderedObject(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *o, int64_t offset, java_lang_Object *x)
1309 _h = (java_handle_t *) o;
1310 _hx = (java_handle_t *) x;
1312 LLNI_CRITICAL_START;
1314 _o = LLNI_UNWRAP(_h);
1315 _x = LLNI_UNWRAP(_hx);
1316 _p = (volatile void **) (((uint8_t *) _o) + offset);
1327 * Class: sun/misc/Unsafe
1328 * Method: putOrderedInt
1329 * Signature: (Ljava/lang/Object;JI)V
1331 JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putOrderedInt(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *o, int64_t offset, int32_t x)
1333 UNSAFE_PUT_VOLATILE(int32_t);
1338 * Class: sun/misc/Unsafe
1339 * Method: putOrderedLong
1340 * Signature: (Ljava/lang/Object;JJ)V
1342 JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putOrderedLong(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *o, int64_t offset, int64_t x)
1344 UNSAFE_PUT_VOLATILE(int64_t);
1349 * Class: sun/misc/Unsafe
1351 * Signature: (Ljava/lang/Object;)V
1353 JNIEXPORT void JNICALL Java_sun_misc_Unsafe_unpark(JNIEnv *env, sun_misc_Unsafe *this, java_lang_Object *thread)
1355 /* XXX IMPLEMENT ME */
1360 * Class: sun/misc/Unsafe
1364 JNIEXPORT void JNICALL Java_sun_misc_Unsafe_park(JNIEnv *env, sun_misc_Unsafe *this, int32_t isAbsolute, int64_t time)
1366 /* XXX IMPLEMENT ME */
1371 * These are local overrides for various environment variables in Emacs.
1372 * Please do not remove this and leave it at the end of the file, where
1373 * Emacs will automagically detect them.
1374 * ---------------------------------------------------------------------
1377 * indent-tabs-mode: t