Update concepts branch to revision 131834
[official-gcc.git] / libjava / classpath / native / jni / classpath / jcl.c
blob0180ab9f0f58e756fc1dd35172d24fce2a72b172
1 /* jcl.c
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)
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
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)))
64 JNIEnv *env;
65 void *envp;
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");
82 #else
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");
93 #else
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)
105 jclass excClass;
106 if ((*env)->ExceptionOccurred (env))
108 (*env)->ExceptionClear (env);
110 excClass = (*env)->FindClass (env, className);
111 if (excClass == NULL)
113 jclass errExcClass;
114 errExcClass =
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);
125 return;
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);
143 if (mem == NULL)
145 JCL_ThrowException (env, "java/lang/OutOfMemoryError",
146 "malloc() failed.");
147 return NULL;
149 return mem;
152 JNIEXPORT void *JNICALL
153 JCL_realloc (JNIEnv * env, void *ptr, size_t size)
155 void *orig_ptr = ptr;
156 ptr = realloc (ptr, size);
157 if (ptr == 0)
159 free (orig_ptr);
160 JCL_ThrowException (env, "java/lang/OutOfMemoryError",
161 "malloc() failed.");
162 return NULL;
164 return (ptr);
167 JNIEXPORT void JNICALL
168 JCL_free (JNIEnv * env __attribute__ ((unused)), void *p)
170 if (p != NULL)
172 free (p);
176 JNIEXPORT const char *JNICALL
177 JCL_jstring_to_cstring (JNIEnv * env, jstring s)
179 const char *cstr;
180 if (s == NULL)
182 JCL_ThrowException (env, "java/lang/NullPointerException",
183 "Null string");
184 return NULL;
186 cstr = (const char *) (*env)->GetStringUTFChars (env, s, NULL);
187 if (cstr == NULL)
189 JCL_ThrowException (env, "java/lang/InternalError",
190 "GetStringUTFChars() failed.");
191 return NULL;
193 return cstr;
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);
206 if (retval != 0)
208 JCL_ThrowException (env, "java/lang/InternalError",
209 "MonitorEnter() failed.");
211 return retval;
214 JNIEXPORT jint JNICALL
215 JCL_MonitorExit (JNIEnv * env, jobject o)
217 jint retval = (*env)->MonitorExit (env, o);
218 if (retval != 0)
220 JCL_ThrowException (env, "java/lang/InternalError",
221 "MonitorExit() failed.");
223 return retval;
226 JNIEXPORT jclass JNICALL
227 JCL_FindClass (JNIEnv * env, const char *className)
229 jclass retval = (*env)->FindClass (env, className);
230 if (retval == NULL)
232 JCL_ThrowException (env, "java/lang/ClassNotFoundException", className);
234 return retval;
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");
249 return NULL;
252 #if SIZEOF_VOID_P == 8
253 return (*env)->NewObject (env, rawDataClass, rawData_mid, (jlong) data);
254 #else
255 return (*env)->NewObject (env, rawDataClass, rawData_mid, (jint) data);
256 #endif
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");
266 return NULL;
269 #if SIZEOF_VOID_P == 8
270 return (void *) (*env)->GetLongField (env, rawdata, rawData_fid);
271 #else
272 return (void *) (*env)->GetIntField (env, rawdata, rawData_fid);
273 #endif