Debian Codegraph @

hawtjni-generator/src/main/resources/hawtjni.c

raw
1 /*******************************************************************************
2 * Copyright (c) 2000, 2005 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 /* == HEADER-SNIP-LOCATION == */
12 #include "hawtjni.h"
13 #include <stdlib.h>
14 #include <string.h>
15 #include <stdio.h>
16
17 int IS_JNI_1_2 = 0;
18
19 #ifdef HAVE_PTHREAD_H
20 #include <pthread.h>
21 #endif
22
23 #ifdef HAVE_UNISTD_H
24 #include <unistd.h>
25 #endif
26
27 #ifdef JNI_VERSION_1_2
28 JavaVM *JVM;
29
30 #ifdef HAVE_PTHREAD_H
31 pthread_key_t JNI_ATTACH_THREAD_LOCAL_KEY;
32 #endif
33
34 jint hawtjni_attach_thread(JNIEnv **env, const char *thread_name) {
35 JavaVMAttachArgs args;
36 args.version = JNI_VERSION_1_2;
37 args.name = (char *)thread_name;
38 args.group = 0;
39 #ifdef HAVE_PTHREAD_H
40 if( JNI_ATTACH_THREAD_LOCAL_KEY ) {
41 *env = pthread_getspecific(JNI_ATTACH_THREAD_LOCAL_KEY);
42 if( ! *env ) {
43 if( (*JVM)->AttachCurrentThread(JVM, (void**)env, &args)==0 ) {
44 pthread_setspecific(JNI_ATTACH_THREAD_LOCAL_KEY, *env);
45 } else {
46 return -1;
47 }
48 }
49 return 0;
50 } else {
51 return (*JVM)->AttachCurrentThread(JVM, (void**)env, &args);
52 }
53 #else
54 return (*JVM)->AttachCurrentThread(JVM, (void**)env, &args);
55 #endif
56 }
57
58 jint hawtjni_detach_thread() {
59 #ifdef HAVE_PTHREAD_H
60 if( JNI_ATTACH_THREAD_LOCAL_KEY ) {
61 // Don't actually detach.. that will automatically
62 // happen when the the thread dies.
63 return 0;
64 } else {
65 return (*JVM)->DetachCurrentThread(JVM);
66 }
67 #else
68 return (*JVM)->DetachCurrentThread(JVM);
69 #endif
70 }
71
72 #ifdef HAVE_PTHREAD_H
73 void hawtjni_thread_cleanup(void *data) {
74 if( data ) {
75 (*JVM)->DetachCurrentThread(JVM);
76 pthread_setspecific(JNI_ATTACH_THREAD_LOCAL_KEY, 0);
77 }
78 }
79 #endif
80
81 JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
82 IS_JNI_1_2 = 1;
83 JVM = vm;
84 #ifdef HAVE_PTHREAD_H
85 if( pthread_key_create(&JNI_ATTACH_THREAD_LOCAL_KEY, hawtjni_thread_cleanup) ) {
86 JNI_ATTACH_THREAD_LOCAL_KEY = 0;
87 }
88 #endif
89 return JNI_VERSION_1_2;
90 }
91 #endif
92
93 void throwOutOfMemory(JNIEnv *env) {
94 jclass clazz = (*env)->FindClass(env, "java/lang/OutOfMemoryError");
95 if (clazz != NULL) {
96 (*env)->ThrowNew(env, clazz, "");
97 }
98 }
99
100 #ifndef JNI64
101
102 void **hawtjni_malloc_pointer_array(JNIEnv *env, jlongArray array) {
103 int i, size;
104 jlong *elems;
105 void **rc;
106
107 if( array==NULL ) {
108 return NULL;
109 }
110 #ifdef JNI_VERSION_1_2
111 if (IS_JNI_1_2) {
112 elems = (*env)->GetPrimitiveArrayCritical(env, array, NULL);
113 } else
114 #endif
115 {
116 elems = (*env)->GetLongArrayElements(env, array, NULL);
117 }
118 if( elems == NULL) {
119 return NULL;
120 }
121
122 size = (*env)->GetArrayLength(env, array);
123 rc=malloc(sizeof(void *)*(size+1));
124 if( rc!= NULL ) {
125 for( i=0; i < size; i++ ) {
126 rc[i]=(void *)(intptr_t)(elems[i]);
127 }
128 rc[size]=NULL;
129 }
130 #ifdef JNI_VERSION_1_2
131 if (IS_JNI_1_2) {
132 (*env)->ReleasePrimitiveArrayCritical(env, array, elems, JNI_ABORT);
133 } else
134 #endif
135 {
136 (*env)->ReleaseLongArrayElements(env, array, elems, JNI_ABORT);
137 }
138 return rc;
139 }
140
141 void hawtjni_free_pointer_array(JNIEnv *env, jlongArray array, void **elems, jint mode) {
142
143 // do we need to copy back the data??
144 if( mode != JNI_ABORT) {
145 int i, size;
146 jlong *tmp;
147
148 size = (*env)->GetArrayLength(env, array);
149 #ifdef JNI_VERSION_1_2
150 if (IS_JNI_1_2) {
151 tmp = (*env)->GetPrimitiveArrayCritical(env, array, NULL);
152 } else
153 #endif
154 {
155 tmp = (*env)->GetLongArrayElements(env, array, NULL);
156 }
157 if( tmp != NULL) {
158 for( i=0; i < size; i++ ) {
159 tmp[i]=(intptr_t)elems[i];
160 }
161 #ifdef JNI_VERSION_1_2
162 if (IS_JNI_1_2) {
163 (*env)->ReleasePrimitiveArrayCritical(env, array, tmp, 0);
164 } else
165 #endif
166 {
167 (*env)->ReleaseLongArrayElements(env, array, tmp, 0);
168 }
169 }
170 } /* mode != JNI_ABORTmode */
171
172 free(elems);
173 }
174
175 #endif /* JNI64 */