2 Copyright (C) 1998, 2005, 2006, 2008 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 /* do not move; needed here because of some macro definitions */
47 #define __attribute__(x) /* nothing */
52 * Cached Pointer class info.
54 static jclass rawDataClass
= NULL
;
55 static jfieldID rawData_fid
= NULL
;
56 static jmethodID rawData_mid
= NULL
;
59 * JNI OnLoad constructor.
61 JNIEXPORT jint JNICALL
62 JNI_OnLoad (JavaVM
*vm
, void *reserved
__attribute__((unused
)))
67 if ((*vm
)->GetEnv (vm
, &envp
, JNI_VERSION_1_4
) != JNI_OK
)
69 return JNI_VERSION_1_4
;
71 env
= (JNIEnv
*) envp
;
72 #if SIZEOF_VOID_P == 8
73 rawDataClass
= (*env
)->FindClass (env
, "gnu/classpath/Pointer64");
74 if (rawDataClass
!= NULL
)
75 rawDataClass
= (*env
)->NewGlobalRef (env
, rawDataClass
);
77 if (rawDataClass
!= NULL
)
79 rawData_fid
= (*env
)->GetFieldID (env
, rawDataClass
, "data", "J");
80 rawData_mid
= (*env
)->GetMethodID (env
, rawDataClass
, "<init>", "(J)V");
83 #if SIZEOF_VOID_P == 4
84 rawDataClass
= (*env
)->FindClass (env
, "gnu/classpath/Pointer32");
85 if (rawDataClass
!= NULL
)
86 rawDataClass
= (*env
)->NewGlobalRef (env
, rawDataClass
);
88 if (rawDataClass
!= NULL
)
90 rawData_fid
= (*env
)->GetFieldID (env
, rawDataClass
, "data", "I");
91 rawData_mid
= (*env
)->GetMethodID (env
, rawDataClass
, "<init>", "(I)V");
94 #error "Pointer size is not supported."
95 #endif /* SIZEOF_VOID_P == 4 */
96 #endif /* SIZEOF_VOID_P == 8 */
98 return JNI_VERSION_1_4
;
102 JNIEXPORT
void JNICALL
103 JCL_ThrowException (JNIEnv
* env
, const char *className
, const char *errMsg
)
106 if ((*env
)->ExceptionOccurred (env
))
108 (*env
)->ExceptionClear (env
);
110 excClass
= (*env
)->FindClass (env
, className
);
111 if (excClass
== NULL
)
115 (*env
)->FindClass (env
, "java/lang/ClassNotFoundException");
116 if (errExcClass
== NULL
)
118 errExcClass
= (*env
)->FindClass (env
, "java/lang/InternalError");
119 if (errExcClass
== NULL
)
121 fprintf (stderr
, "JCL: Utterly failed to throw exeption ");
122 fprintf (stderr
, "%s", className
);
123 fprintf (stderr
, " with message ");
124 fprintf (stderr
, "%s", errMsg
);
128 /* Removed this (more comprehensive) error string to avoid the need for
129 * a static variable or allocation of a buffer for this message in this
130 * (unlikely) error case. --Fridi.
132 * sprintf(errstr,"JCL: Failed to throw exception %s with message %s: could not find exception class.", className, errMsg);
134 (*env
)->ThrowNew (env
, errExcClass
, className
);
136 (*env
)->ThrowNew (env
, excClass
, errMsg
);
139 JNIEXPORT
void *JNICALL
140 JCL_malloc (JNIEnv
* env
, size_t size
)
142 void *mem
= malloc (size
);
145 JCL_ThrowException (env
, "java/lang/OutOfMemoryError",
152 JNIEXPORT
void *JNICALL
153 JCL_realloc (JNIEnv
* env
, void *ptr
, size_t size
)
155 void *orig_ptr
= ptr
;
156 ptr
= realloc (ptr
, size
);
160 JCL_ThrowException (env
, "java/lang/OutOfMemoryError",
167 JNIEXPORT
void JNICALL
168 JCL_free (JNIEnv
* env
__attribute__ ((unused
)), void *p
)
176 JNIEXPORT
const char *JNICALL
177 JCL_jstring_to_cstring (JNIEnv
* env
, jstring s
)
182 JCL_ThrowException (env
, "java/lang/NullPointerException",
186 cstr
= (const char *) (*env
)->GetStringUTFChars (env
, s
, NULL
);
189 JCL_ThrowException (env
, "java/lang/InternalError",
190 "GetStringUTFChars() failed.");
196 JNIEXPORT
void JNICALL
197 JCL_free_cstring (JNIEnv
* env
, jstring s
, const char *cstr
)
199 (*env
)->ReleaseStringUTFChars (env
, s
, cstr
);
202 JNIEXPORT jint JNICALL
203 JCL_MonitorEnter (JNIEnv
* env
, jobject o
)
205 jint retval
= (*env
)->MonitorEnter (env
, o
);
208 JCL_ThrowException (env
, "java/lang/InternalError",
209 "MonitorEnter() failed.");
214 JNIEXPORT jint JNICALL
215 JCL_MonitorExit (JNIEnv
* env
, jobject o
)
217 jint retval
= (*env
)->MonitorExit (env
, o
);
220 JCL_ThrowException (env
, "java/lang/InternalError",
221 "MonitorExit() failed.");
226 JNIEXPORT jclass JNICALL
227 JCL_FindClass (JNIEnv
* env
, const char *className
)
229 jclass retval
= (*env
)->FindClass (env
, className
);
232 JCL_ThrowException (env
, "java/lang/ClassNotFoundException", className
);
239 * Build a Pointer object.
242 JNIEXPORT jobject JNICALL
243 JCL_NewRawDataObject (JNIEnv
* env
, void *data
)
245 if (rawDataClass
== NULL
|| rawData_mid
== NULL
)
247 JCL_ThrowException (env
, "java/lang/InternalError",
248 "Pointer class was not properly initialized");
252 #if SIZEOF_VOID_P == 8
253 return (*env
)->NewObject (env
, rawDataClass
, rawData_mid
, (jlong
) data
);
255 return (*env
)->NewObject (env
, rawDataClass
, rawData_mid
, (jint
) data
);
259 JNIEXPORT
void * JNICALL
260 JCL_GetRawData (JNIEnv
* env
, jobject rawdata
)
262 if (rawData_fid
== NULL
)
264 JCL_ThrowException (env
, "java/lang/InternalError",
265 "Pointer class was not properly initialized");
269 #if SIZEOF_VOID_P == 8
270 return (void *) (*env
)->GetLongField (env
, rawdata
, rawData_fid
);
272 return (void *) (*env
)->GetIntField (env
, rawdata
, rawData_fid
);