2 Copyright (C) 1998, 2005 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 */
51 JNIEXPORT
void JNICALL
52 JCL_ThrowException (JNIEnv
* env
, const char *className
, const char *errMsg
)
55 if ((*env
)->ExceptionOccurred (env
))
57 (*env
)->ExceptionClear (env
);
59 excClass
= (*env
)->FindClass (env
, className
);
64 (*env
)->FindClass (env
, "java/lang/ClassNotFoundException");
65 if (errExcClass
== NULL
)
67 errExcClass
= (*env
)->FindClass (env
, "java/lang/InternalError");
68 if (errExcClass
== NULL
)
70 fprintf (stderr
, "JCL: Utterly failed to throw exeption ");
71 fprintf (stderr
, className
);
72 fprintf (stderr
, " with message ");
73 fprintf (stderr
, errMsg
);
77 /* Removed this (more comprehensive) error string to avoid the need for
78 * a static variable or allocation of a buffer for this message in this
79 * (unlikely) error case. --Fridi.
81 * sprintf(errstr,"JCL: Failed to throw exception %s with message %s: could not find exception class.", className, errMsg);
83 (*env
)->ThrowNew (env
, errExcClass
, className
);
85 (*env
)->ThrowNew (env
, excClass
, errMsg
);
88 JNIEXPORT
void *JNICALL
89 JCL_malloc (JNIEnv
* env
, size_t size
)
91 void *mem
= malloc (size
);
94 JCL_ThrowException (env
, "java/lang/OutOfMemoryError",
101 JNIEXPORT
void *JNICALL
102 JCL_realloc (JNIEnv
* env
, void *ptr
, size_t size
)
104 ptr
= realloc (ptr
, size
);
107 JCL_ThrowException (env
, "java/lang/OutOfMemoryError",
114 JNIEXPORT
void JNICALL
115 JCL_free (JNIEnv
* env
__attribute__ ((unused
)), void *p
)
123 JNIEXPORT
const char *JNICALL
124 JCL_jstring_to_cstring (JNIEnv
* env
, jstring s
)
129 JCL_ThrowException (env
, "java/lang/NullPointerException",
133 cstr
= (const char *) (*env
)->GetStringUTFChars (env
, s
, NULL
);
136 JCL_ThrowException (env
, "java/lang/InternalError",
137 "GetStringUTFChars() failed.");
143 JNIEXPORT
void JNICALL
144 JCL_free_cstring (JNIEnv
* env
, jstring s
, const char *cstr
)
146 (*env
)->ReleaseStringUTFChars (env
, s
, cstr
);
149 JNIEXPORT jint JNICALL
150 JCL_MonitorEnter (JNIEnv
* env
, jobject o
)
152 jint retval
= (*env
)->MonitorEnter (env
, o
);
155 JCL_ThrowException (env
, "java/lang/InternalError",
156 "MonitorEnter() failed.");
161 JNIEXPORT jint JNICALL
162 JCL_MonitorExit (JNIEnv
* env
, jobject o
)
164 jint retval
= (*env
)->MonitorExit (env
, o
);
167 JCL_ThrowException (env
, "java/lang/InternalError",
168 "MonitorExit() failed.");
173 JNIEXPORT jclass JNICALL
174 JCL_FindClass (JNIEnv
* env
, const char *className
)
176 jclass retval
= (*env
)->FindClass (env
, className
);
179 JCL_ThrowException (env
, "java/lang/ClassNotFoundException", className
);
186 * Build a Pointer object. The function caches the class type
189 static jclass rawDataClass
;
190 static jfieldID rawData_fid
;
191 static jmethodID rawData_mid
;
193 JNIEXPORT jobject JNICALL
194 JCL_NewRawDataObject (JNIEnv
* env
, void *data
)
196 if (rawDataClass
== NULL
)
199 #if SIZEOF_VOID_P == 8
200 rawDataClass
= (*env
)->FindClass (env
, "gnu/classpath/Pointer64");
201 if (rawDataClass
== NULL
)
203 JCL_ThrowException (env
, "java/lang/InternalError",
204 "unable to find internal class");
208 rawData_mid
= (*env
)->GetMethodID (env
, rawDataClass
, "<init>", "(J)V");
209 if (rawData_mid
== NULL
)
211 JCL_ThrowException (env
, "java/lang/InternalError",
212 "unable to find internal constructor");
216 rawData_fid
= (*env
)->GetFieldID (env
, rawDataClass
, "data", "J");
217 if (rawData_fid
== NULL
)
219 JCL_ThrowException (env
, "java/lang/InternalError",
220 "unable to find internal field");
224 rawDataClass
= (*env
)->FindClass (env
, "gnu/classpath/Pointer32");
225 if (rawDataClass
== NULL
)
227 JCL_ThrowException (env
, "java/lang/InternalError",
228 "unable to find internal class");
232 rawData_mid
= (*env
)->GetMethodID (env
, rawDataClass
, "<init>", "(I)V");
233 if (rawData_mid
== NULL
)
235 JCL_ThrowException (env
, "java/lang/InternalError",
236 "unable to find internal constructor");
240 rawData_fid
= (*env
)->GetFieldID (env
, rawDataClass
, "data", "I");
241 if (rawData_fid
== NULL
)
243 JCL_ThrowException (env
, "java/lang/InternalError",
244 "unable to find internal field");
249 tmp
= (*env
)->NewGlobalRef (env
, rawDataClass
);
252 JCL_ThrowException (env
, "java/lang/InternalError",
253 "unable to create an internal global ref");
256 (*env
)->DeleteLocalRef(env
, rawDataClass
);
260 #if SIZEOF_VOID_P == 8
261 return (*env
)->NewObject (env
, rawDataClass
, rawData_mid
, (jlong
) data
);
263 return (*env
)->NewObject (env
, rawDataClass
, rawData_mid
, (jint
) data
);
267 JNIEXPORT
void * JNICALL
268 JCL_GetRawData (JNIEnv
* env
, jobject rawdata
)
270 #if SIZEOF_VOID_P == 8
271 return (void *) (*env
)->GetLongField (env
, rawdata
, rawData_fid
);
273 return (void *) (*env
)->GetIntField (env
, rawdata
, rawData_fid
);