| 1 |
774 |
jeremybenn |
/* jcl.c
|
| 2 |
|
|
Copyright (C) 1998, 2005, 2006, 2008 Free Software Foundation, Inc.
|
| 3 |
|
|
|
| 4 |
|
|
This file is part of GNU Classpath.
|
| 5 |
|
|
|
| 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.
|
| 10 |
|
|
|
| 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.
|
| 15 |
|
|
|
| 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.
|
| 20 |
|
|
|
| 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.
|
| 25 |
|
|
|
| 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. */
|
| 37 |
|
|
|
| 38 |
|
|
/* do not move; needed here because of some macro definitions */
|
| 39 |
|
|
#include "config.h"
|
| 40 |
|
|
|
| 41 |
|
|
#include <stdlib.h>
|
| 42 |
|
|
#include <stdio.h>
|
| 43 |
|
|
#include <jcl.h>
|
| 44 |
|
|
|
| 45 |
|
|
#ifndef __GNUC__
|
| 46 |
|
|
#ifndef __attribute__
|
| 47 |
|
|
#define __attribute__(x) /* nothing */
|
| 48 |
|
|
#endif
|
| 49 |
|
|
#endif
|
| 50 |
|
|
|
| 51 |
|
|
/*
|
| 52 |
|
|
* Cached Pointer class info.
|
| 53 |
|
|
*/
|
| 54 |
|
|
static jclass rawDataClass = NULL;
|
| 55 |
|
|
static jfieldID rawData_fid = NULL;
|
| 56 |
|
|
static jmethodID rawData_mid = NULL;
|
| 57 |
|
|
|
| 58 |
|
|
/*
|
| 59 |
|
|
* JNI OnLoad constructor.
|
| 60 |
|
|
*/
|
| 61 |
|
|
JNIEXPORT jint JNICALL
|
| 62 |
|
|
JNI_OnLoad (JavaVM *vm, void *reserved __attribute__((unused)))
|
| 63 |
|
|
{
|
| 64 |
|
|
JNIEnv *env;
|
| 65 |
|
|
void *envp;
|
| 66 |
|
|
|
| 67 |
|
|
if ((*vm)->GetEnv (vm, &envp, JNI_VERSION_1_4) != JNI_OK)
|
| 68 |
|
|
{
|
| 69 |
|
|
return JNI_VERSION_1_4;
|
| 70 |
|
|
}
|
| 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);
|
| 76 |
|
|
|
| 77 |
|
|
if (rawDataClass != NULL)
|
| 78 |
|
|
{
|
| 79 |
|
|
rawData_fid = (*env)->GetFieldID (env, rawDataClass, "data", "J");
|
| 80 |
|
|
rawData_mid = (*env)->GetMethodID (env, rawDataClass, "<init>", "(J)V");
|
| 81 |
|
|
}
|
| 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);
|
| 87 |
|
|
|
| 88 |
|
|
if (rawDataClass != NULL)
|
| 89 |
|
|
{
|
| 90 |
|
|
rawData_fid = (*env)->GetFieldID (env, rawDataClass, "data", "I");
|
| 91 |
|
|
rawData_mid = (*env)->GetMethodID (env, rawDataClass, "<init>", "(I)V");
|
| 92 |
|
|
}
|
| 93 |
|
|
#else
|
| 94 |
|
|
#error "Pointer size is not supported."
|
| 95 |
|
|
#endif /* SIZEOF_VOID_P == 4 */
|
| 96 |
|
|
#endif /* SIZEOF_VOID_P == 8 */
|
| 97 |
|
|
|
| 98 |
|
|
return JNI_VERSION_1_4;
|
| 99 |
|
|
}
|
| 100 |
|
|
|
| 101 |
|
|
|
| 102 |
|
|
JNIEXPORT void JNICALL
|
| 103 |
|
|
JCL_ThrowException (JNIEnv * env, const char *className, const char *errMsg)
|
| 104 |
|
|
{
|
| 105 |
|
|
jclass excClass;
|
| 106 |
|
|
if ((*env)->ExceptionOccurred (env))
|
| 107 |
|
|
{
|
| 108 |
|
|
(*env)->ExceptionClear (env);
|
| 109 |
|
|
}
|
| 110 |
|
|
excClass = (*env)->FindClass (env, className);
|
| 111 |
|
|
if (excClass == NULL)
|
| 112 |
|
|
{
|
| 113 |
|
|
jclass errExcClass;
|
| 114 |
|
|
errExcClass =
|
| 115 |
|
|
(*env)->FindClass (env, "java/lang/ClassNotFoundException");
|
| 116 |
|
|
if (errExcClass == NULL)
|
| 117 |
|
|
{
|
| 118 |
|
|
errExcClass = (*env)->FindClass (env, "java/lang/InternalError");
|
| 119 |
|
|
if (errExcClass == NULL)
|
| 120 |
|
|
{
|
| 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;
|
| 126 |
|
|
}
|
| 127 |
|
|
}
|
| 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.
|
| 131 |
|
|
*
|
| 132 |
|
|
* sprintf(errstr,"JCL: Failed to throw exception %s with message %s: could not find exception class.", className, errMsg);
|
| 133 |
|
|
*/
|
| 134 |
|
|
(*env)->ThrowNew (env, errExcClass, className);
|
| 135 |
|
|
}
|
| 136 |
|
|
(*env)->ThrowNew (env, excClass, errMsg);
|
| 137 |
|
|
}
|
| 138 |
|
|
|
| 139 |
|
|
JNIEXPORT void *JNICALL
|
| 140 |
|
|
JCL_malloc (JNIEnv * env, size_t size)
|
| 141 |
|
|
{
|
| 142 |
|
|
void *mem = malloc (size);
|
| 143 |
|
|
if (mem == NULL)
|
| 144 |
|
|
{
|
| 145 |
|
|
JCL_ThrowException (env, "java/lang/OutOfMemoryError",
|
| 146 |
|
|
"malloc() failed.");
|
| 147 |
|
|
return NULL;
|
| 148 |
|
|
}
|
| 149 |
|
|
return mem;
|
| 150 |
|
|
}
|
| 151 |
|
|
|
| 152 |
|
|
JNIEXPORT void *JNICALL
|
| 153 |
|
|
JCL_realloc (JNIEnv * env, void *ptr, size_t size)
|
| 154 |
|
|
{
|
| 155 |
|
|
void *orig_ptr = ptr;
|
| 156 |
|
|
ptr = realloc (ptr, size);
|
| 157 |
|
|
if (ptr == 0)
|
| 158 |
|
|
{
|
| 159 |
|
|
free (orig_ptr);
|
| 160 |
|
|
JCL_ThrowException (env, "java/lang/OutOfMemoryError",
|
| 161 |
|
|
"malloc() failed.");
|
| 162 |
|
|
return NULL;
|
| 163 |
|
|
}
|
| 164 |
|
|
return (ptr);
|
| 165 |
|
|
}
|
| 166 |
|
|
|
| 167 |
|
|
JNIEXPORT void JNICALL
|
| 168 |
|
|
JCL_free (JNIEnv * env __attribute__ ((unused)), void *p)
|
| 169 |
|
|
{
|
| 170 |
|
|
if (p != NULL)
|
| 171 |
|
|
{
|
| 172 |
|
|
free (p);
|
| 173 |
|
|
}
|
| 174 |
|
|
}
|
| 175 |
|
|
|
| 176 |
|
|
JNIEXPORT const char *JNICALL
|
| 177 |
|
|
JCL_jstring_to_cstring (JNIEnv * env, jstring s)
|
| 178 |
|
|
{
|
| 179 |
|
|
const char *cstr;
|
| 180 |
|
|
if (s == NULL)
|
| 181 |
|
|
{
|
| 182 |
|
|
JCL_ThrowException (env, "java/lang/NullPointerException",
|
| 183 |
|
|
"Null string");
|
| 184 |
|
|
return NULL;
|
| 185 |
|
|
}
|
| 186 |
|
|
cstr = (const char *) (*env)->GetStringUTFChars (env, s, NULL);
|
| 187 |
|
|
if (cstr == NULL)
|
| 188 |
|
|
{
|
| 189 |
|
|
JCL_ThrowException (env, "java/lang/InternalError",
|
| 190 |
|
|
"GetStringUTFChars() failed.");
|
| 191 |
|
|
return NULL;
|
| 192 |
|
|
}
|
| 193 |
|
|
return cstr;
|
| 194 |
|
|
}
|
| 195 |
|
|
|
| 196 |
|
|
JNIEXPORT void JNICALL
|
| 197 |
|
|
JCL_free_cstring (JNIEnv * env, jstring s, const char *cstr)
|
| 198 |
|
|
{
|
| 199 |
|
|
(*env)->ReleaseStringUTFChars (env, s, cstr);
|
| 200 |
|
|
}
|
| 201 |
|
|
|
| 202 |
|
|
JNIEXPORT jint JNICALL
|
| 203 |
|
|
JCL_MonitorEnter (JNIEnv * env, jobject o)
|
| 204 |
|
|
{
|
| 205 |
|
|
jint retval = (*env)->MonitorEnter (env, o);
|
| 206 |
|
|
if (retval != 0)
|
| 207 |
|
|
{
|
| 208 |
|
|
JCL_ThrowException (env, "java/lang/InternalError",
|
| 209 |
|
|
"MonitorEnter() failed.");
|
| 210 |
|
|
}
|
| 211 |
|
|
return retval;
|
| 212 |
|
|
}
|
| 213 |
|
|
|
| 214 |
|
|
JNIEXPORT jint JNICALL
|
| 215 |
|
|
JCL_MonitorExit (JNIEnv * env, jobject o)
|
| 216 |
|
|
{
|
| 217 |
|
|
jint retval = (*env)->MonitorExit (env, o);
|
| 218 |
|
|
if (retval != 0)
|
| 219 |
|
|
{
|
| 220 |
|
|
JCL_ThrowException (env, "java/lang/InternalError",
|
| 221 |
|
|
"MonitorExit() failed.");
|
| 222 |
|
|
}
|
| 223 |
|
|
return retval;
|
| 224 |
|
|
}
|
| 225 |
|
|
|
| 226 |
|
|
JNIEXPORT jclass JNICALL
|
| 227 |
|
|
JCL_FindClass (JNIEnv * env, const char *className)
|
| 228 |
|
|
{
|
| 229 |
|
|
jclass retval = (*env)->FindClass (env, className);
|
| 230 |
|
|
if (retval == NULL)
|
| 231 |
|
|
{
|
| 232 |
|
|
JCL_ThrowException (env, "java/lang/ClassNotFoundException", className);
|
| 233 |
|
|
}
|
| 234 |
|
|
return retval;
|
| 235 |
|
|
}
|
| 236 |
|
|
|
| 237 |
|
|
|
| 238 |
|
|
/*
|
| 239 |
|
|
* Build a Pointer object.
|
| 240 |
|
|
*/
|
| 241 |
|
|
|
| 242 |
|
|
JNIEXPORT jobject JNICALL
|
| 243 |
|
|
JCL_NewRawDataObject (JNIEnv * env, void *data)
|
| 244 |
|
|
{
|
| 245 |
|
|
if (rawDataClass == NULL || rawData_mid == NULL)
|
| 246 |
|
|
{
|
| 247 |
|
|
JCL_ThrowException (env, "java/lang/InternalError",
|
| 248 |
|
|
"Pointer class was not properly initialized");
|
| 249 |
|
|
return NULL;
|
| 250 |
|
|
}
|
| 251 |
|
|
|
| 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
|
| 257 |
|
|
}
|
| 258 |
|
|
|
| 259 |
|
|
JNIEXPORT void * JNICALL
|
| 260 |
|
|
JCL_GetRawData (JNIEnv * env, jobject rawdata)
|
| 261 |
|
|
{
|
| 262 |
|
|
if (rawData_fid == NULL)
|
| 263 |
|
|
{
|
| 264 |
|
|
JCL_ThrowException (env, "java/lang/InternalError",
|
| 265 |
|
|
"Pointer class was not properly initialized");
|
| 266 |
|
|
return NULL;
|
| 267 |
|
|
}
|
| 268 |
|
|
|
| 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
|
| 274 |
|
|
}
|