Merge from mainline
[official-gcc.git] / libjava / classpath / native / jni / classpath / jcl.c
blobdd6ca06a70e4330d893e3f153fd4c39ddbb5f6ec
1 /* jcl.c
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)
9 any later version.
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
19 02110-1301 USA.
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
24 combination.
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 */
39 #include "config.h"
41 #include <stdlib.h>
42 #include <stdio.h>
43 #include <jcl.h>
45 #ifndef __GNUC__
46 #ifndef __attribute__
47 #define __attribute__(x) /* nothing */
48 #endif
49 #endif
51 JNIEXPORT void JNICALL
52 JCL_ThrowException (JNIEnv * env, const char *className, const char *errMsg)
54 jclass excClass;
55 if ((*env)->ExceptionOccurred (env))
57 (*env)->ExceptionClear (env);
59 excClass = (*env)->FindClass (env, className);
60 if (excClass == NULL)
62 jclass errExcClass;
63 errExcClass =
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);
74 return;
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);
92 if (mem == NULL)
94 JCL_ThrowException (env, "java/lang/OutOfMemoryError",
95 "malloc() failed.");
96 return NULL;
98 return mem;
101 JNIEXPORT void *JNICALL
102 JCL_realloc (JNIEnv * env, void *ptr, size_t size)
104 ptr = realloc (ptr, size);
105 if (ptr == 0)
107 JCL_ThrowException (env, "java/lang/OutOfMemoryError",
108 "malloc() failed.");
109 return NULL;
111 return (ptr);
114 JNIEXPORT void JNICALL
115 JCL_free (JNIEnv * env __attribute__ ((unused)), void *p)
117 if (p != NULL)
119 free (p);
123 JNIEXPORT const char *JNICALL
124 JCL_jstring_to_cstring (JNIEnv * env, jstring s)
126 const char *cstr;
127 if (s == NULL)
129 JCL_ThrowException (env, "java/lang/NullPointerException",
130 "Null string");
131 return NULL;
133 cstr = (const char *) (*env)->GetStringUTFChars (env, s, NULL);
134 if (cstr == NULL)
136 JCL_ThrowException (env, "java/lang/InternalError",
137 "GetStringUTFChars() failed.");
138 return NULL;
140 return cstr;
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);
153 if (retval != 0)
155 JCL_ThrowException (env, "java/lang/InternalError",
156 "MonitorEnter() failed.");
158 return retval;
161 JNIEXPORT jint JNICALL
162 JCL_MonitorExit (JNIEnv * env, jobject o)
164 jint retval = (*env)->MonitorExit (env, o);
165 if (retval != 0)
167 JCL_ThrowException (env, "java/lang/InternalError",
168 "MonitorExit() failed.");
170 return retval;
173 JNIEXPORT jclass JNICALL
174 JCL_FindClass (JNIEnv * env, const char *className)
176 jclass retval = (*env)->FindClass (env, className);
177 if (retval == NULL)
179 JCL_ThrowException (env, "java/lang/ClassNotFoundException", className);
181 return retval;
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)
198 jclass tmp;
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");
205 return NULL;
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");
213 return NULL;
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");
221 return NULL;
223 #else
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");
229 return NULL;
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");
237 return NULL;
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");
245 return NULL;
248 #endif
249 tmp = (*env)->NewGlobalRef (env, rawDataClass);
250 if (tmp == NULL)
252 JCL_ThrowException (env, "java/lang/InternalError",
253 "unable to create an internal global ref");
254 return NULL;
256 (*env)->DeleteLocalRef(env, rawDataClass);
257 rawDataClass = tmp;
260 #if SIZEOF_VOID_P == 8
261 return (*env)->NewObject (env, rawDataClass, rawData_mid, (jlong) data);
262 #else
263 return (*env)->NewObject (env, rawDataClass, rawData_mid, (jint) data);
264 #endif
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);
272 #else
273 return (void *) (*env)->GetIntField (env, rawdata, rawData_fid);
274 #endif