Debian Codegraph @

hawtjni-generator/src/main/resources/hawtjni-callback.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
16 /* define this to print out debug statements */
17 /* #define DEBUG_CALL_PRINTS */
18
19 /* --------------- callback globals ----------------- */
20 #if defined (_WIN32) || defined (_WIN32_WCE)
21 #include "windows.h"
22 #define RETURN_TYPE LRESULT CALLBACK
23 #define RETURN_CAST (LRESULT)
24 #endif
25
26 #ifndef RETURN_TYPE
27 #define RETURN_TYPE jintLong
28 #endif
29
30 #ifndef RETURN_CAST
31 #define RETURN_CAST
32 #endif
33
34 /*
35 * Note that only x86 assembler is supported
36 */
37 #if !(defined(__i386__) || defined(_M_IX86) || defined(_X86_))
38 #undef USE_ASSEMBLER
39 #endif
40
41 #ifdef REDUCED_CALLBACKS
42 #define MAX_CALLBACKS 16
43 #else
44 #ifdef USE_ASSEMBLER
45 #define MAX_CALLBACKS 256
46 #else
47 #define MAX_CALLBACKS 128
48 #endif
49 #endif /* REDUCED_CALLBACKS */
50
51 #define MAX_ARGS 12
52
53 typedef struct CALLBACK_DATA {
54 jobject callback;
55 jmethodID methodID;
56 jobject object;
57 jboolean isStatic;
58 jboolean isArrayBased;
59 jint argCount;
60 jlong errorResult;
61 } CALLBACK_DATA;
62
63 static JavaVM *jvm = NULL;
64 static CALLBACK_DATA callbackData[MAX_CALLBACKS];
65 static int callbackEnabled = 1;
66 static int callbackEntryCount = 0;
67 static int initialized = 0;
68 static jint JNI_VERSION = 0;
69
70 #ifdef DEBUG_CALL_PRINTS
71 static int counter = 0;
72 #endif
73
74 #ifdef ATOMIC
75 #include <libkern/OSAtomic.h>
76 #define ATOMIC_INC(value) OSAtomicIncrement32(&value);
77 #define ATOMIC_DEC(value) OSAtomicDecrement32(&value);
78 #else
79 #define ATOMIC_INC(value) value++;
80 #define ATOMIC_DEC(value) value--;
81 #endif
82
83 jintLong callback(int index, ...);
84
85 #ifdef USE_ASSEMBLER
86
87 #if !(defined (_WIN32) || defined (_WIN32_WCE))
88 #include <sys/mman.h>
89 #endif
90
91 static unsigned char *callbackCode = NULL;
92 #define CALLBACK_THUNK_SIZE 64
93
94 #else
95
96 /* ------------- Start: class Callback impl --------------- */
97
98
99 /* Function name from index and number of arguments */
100 #define FN(index, args) fn##index##_##args
101
102 /**
103 * Functions templates
104 *
105 * NOTE: If the maximum number of arguments changes (MAX_ARGS), the number
106 * of function templates has to change accordingly.
107 */
108
109 /* Function template with no arguments */
110 #define FN_0(index) RETURN_TYPE FN(index, 0)() { return RETURN_CAST callback(index); }
111
112 /* Function template with 1 argument */
113 #define FN_1(index) RETURN_TYPE FN(index, 1)(jintLong p1) { return RETURN_CAST callback(index, p1); }
114
115 /* Function template with 2 arguments */
116 #define FN_2(index) RETURN_TYPE FN(index, 2)(jintLong p1, jintLong p2) { return RETURN_CAST callback(index, p1, p2); }
117
118 /* Function template with 3 arguments */
119 #define FN_3(index) RETURN_TYPE FN(index, 3)(jintLong p1, jintLong p2, jintLong p3) { return RETURN_CAST callback(index, p1, p2, p3); }
120
121 /* Function template with 4 arguments */
122 #define FN_4(index) RETURN_TYPE FN(index, 4)(jintLong p1, jintLong p2, jintLong p3, jintLong p4) { return RETURN_CAST callback(index, p1, p2, p3, p4); }
123
124 /* Function template with 5 arguments */
125 #define FN_5(index) RETURN_TYPE FN(index, 5)(jintLong p1, jintLong p2, jintLong p3, jintLong p4, jintLong p5) { return RETURN_CAST callback(index, p1, p2, p3, p4, p5); }
126
127 /* Function template with 6 arguments */
128 #define FN_6(index) RETURN_TYPE FN(index, 6)(jintLong p1, jintLong p2, jintLong p3, jintLong p4, jintLong p5, jintLong p6) { return RETURN_CAST callback(index, p1, p2, p3, p4, p5, p6); }
129
130 /* Function template with 7 arguments */
131 #define FN_7(index) RETURN_TYPE FN(index, 7)(jintLong p1, jintLong p2, jintLong p3, jintLong p4, jintLong p5, jintLong p6, jintLong p7) { return RETURN_CAST callback(index, p1, p2, p3, p4, p5, p6, p7); }
132
133 /* Function template with 8 arguments */
134 #define FN_8(index) RETURN_TYPE FN(index, 8)(jintLong p1, jintLong p2, jintLong p3, jintLong p4, jintLong p5, jintLong p6, jintLong p7, jintLong p8) { return RETURN_CAST callback(index, p1, p2, p3, p4, p5, p6, p7, p8); }
135
136 /* Function template with 9 arguments */
137 #define FN_9(index) RETURN_TYPE FN(index, 9)(jintLong p1, jintLong p2, jintLong p3, jintLong p4, jintLong p5, jintLong p6, jintLong p7, jintLong p8, jintLong p9) { return RETURN_CAST callback(index, p1, p2, p3, p4, p5, p6, p7, p8, p9); }
138
139 /* Function template with 10 arguments */
140 #define FN_10(index) RETURN_TYPE FN(index, 10) (jintLong p1, jintLong p2, jintLong p3, jintLong p4, jintLong p5, jintLong p6, jintLong p7, jintLong p8, jintLong p9, jintLong p10) { return RETURN_CAST callback(index, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10); }
141
142 /* Function template with 11 arguments */
143 #define FN_11(index) RETURN_TYPE FN(index, 11) (jintLong p1, jintLong p2, jintLong p3, jintLong p4, jintLong p5, jintLong p6, jintLong p7, jintLong p8, jintLong p9, jintLong p10, jintLong p11) { return RETURN_CAST callback(index, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11); }
144
145 /* Function template with 12 arguments */
146 #define FN_12(index) RETURN_TYPE FN(index, 12) (jintLong p1, jintLong p2, jintLong p3, jintLong p4, jintLong p5, jintLong p6, jintLong p7, jintLong p8, jintLong p9, jintLong p10, jintLong p11, jintLong p12) { return RETURN_CAST callback(index, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12); }
147
148 /**
149 * Define all functions with the specified number of arguments.
150 *
151 * NOTE: If the maximum number of callbacks changes (MAX_CALLBACKS),
152 * this macro has to be updated.
153 */
154 #if MAX_CALLBACKS == 16
155 #define FN_BLOCK(args) \
156 FN_##args(0) \
157 FN_##args(1) \
158 FN_##args(2) \
159 FN_##args(3) \
160 FN_##args(4) \
161 FN_##args(5) \
162 FN_##args(6) \
163 FN_##args(7) \
164 FN_##args(8) \
165 FN_##args(9) \
166 FN_##args(10) \
167 FN_##args(11) \
168 FN_##args(12) \
169 FN_##args(13) \
170 FN_##args(14) \
171 FN_##args(15)
172 #elif MAX_CALLBACKS == 128
173 #define FN_BLOCK(args) \
174 FN_##args(0) \
175 FN_##args(1) \
176 FN_##args(2) \
177 FN_##args(3) \
178 FN_##args(4) \
179 FN_##args(5) \
180 FN_##args(6) \
181 FN_##args(7) \
182 FN_##args(8) \
183 FN_##args(9) \
184 FN_##args(10) \
185 FN_##args(11) \
186 FN_##args(12) \
187 FN_##args(13) \
188 FN_##args(14) \
189 FN_##args(15) \
190 FN_##args(16) \
191 FN_##args(17) \
192 FN_##args(18) \
193 FN_##args(19) \
194 FN_##args(20) \
195 FN_##args(21) \
196 FN_##args(22) \
197 FN_##args(23) \
198 FN_##args(24) \
199 FN_##args(25) \
200 FN_##args(26) \
201 FN_##args(27) \
202 FN_##args(28) \
203 FN_##args(29) \
204 FN_##args(30) \
205 FN_##args(31) \
206 FN_##args(32) \
207 FN_##args(33) \
208 FN_##args(34) \
209 FN_##args(35) \
210 FN_##args(36) \
211 FN_##args(37) \
212 FN_##args(38) \
213 FN_##args(39) \
214 FN_##args(40) \
215 FN_##args(41) \
216 FN_##args(42) \
217 FN_##args(43) \
218 FN_##args(44) \
219 FN_##args(45) \
220 FN_##args(46) \
221 FN_##args(47) \
222 FN_##args(48) \
223 FN_##args(49) \
224 FN_##args(50) \
225 FN_##args(51) \
226 FN_##args(52) \
227 FN_##args(53) \
228 FN_##args(54) \
229 FN_##args(55) \
230 FN_##args(56) \
231 FN_##args(57) \
232 FN_##args(58) \
233 FN_##args(59) \
234 FN_##args(60) \
235 FN_##args(61) \
236 FN_##args(62) \
237 FN_##args(63) \
238 FN_##args(64) \
239 FN_##args(65) \
240 FN_##args(66) \
241 FN_##args(67) \
242 FN_##args(68) \
243 FN_##args(69) \
244 FN_##args(70) \
245 FN_##args(71) \
246 FN_##args(72) \
247 FN_##args(73) \
248 FN_##args(74) \
249 FN_##args(75) \
250 FN_##args(76) \
251 FN_##args(77) \
252 FN_##args(78) \
253 FN_##args(79) \
254 FN_##args(80) \
255 FN_##args(81) \
256 FN_##args(82) \
257 FN_##args(83) \
258 FN_##args(84) \
259 FN_##args(85) \
260 FN_##args(86) \
261 FN_##args(87) \
262 FN_##args(88) \
263 FN_##args(89) \
264 FN_##args(90) \
265 FN_##args(91) \
266 FN_##args(92) \
267 FN_##args(93) \
268 FN_##args(94) \
269 FN_##args(95) \
270 FN_##args(96) \
271 FN_##args(97) \
272 FN_##args(98) \
273 FN_##args(99) \
274 FN_##args(100) \
275 FN_##args(101) \
276 FN_##args(102) \
277 FN_##args(103) \
278 FN_##args(104) \
279 FN_##args(105) \
280 FN_##args(106) \
281 FN_##args(107) \
282 FN_##args(108) \
283 FN_##args(109) \
284 FN_##args(110) \
285 FN_##args(111) \
286 FN_##args(112) \
287 FN_##args(113) \
288 FN_##args(114) \
289 FN_##args(115) \
290 FN_##args(116) \
291 FN_##args(117) \
292 FN_##args(118) \
293 FN_##args(119) \
294 FN_##args(120) \
295 FN_##args(121) \
296 FN_##args(122) \
297 FN_##args(123) \
298 FN_##args(124) \
299 FN_##args(125) \
300 FN_##args(126) \
301 FN_##args(127)
302 #else
303 #error Invalid MAX_CALLBACKS
304 #endif /* MAX_CALLBACKS == 16 */
305
306 /**
307 * Define all callback functions.
308 *
309 * NOTE: If the maximum number of arguments changes (MAX_ARGS), the following
310 * has to change accordinglly.
311 */
312 FN_BLOCK(0)
313 FN_BLOCK(1)
314 FN_BLOCK(2)
315 FN_BLOCK(3)
316 FN_BLOCK(4)
317 FN_BLOCK(5)
318 FN_BLOCK(6)
319 FN_BLOCK(7)
320 FN_BLOCK(8)
321 FN_BLOCK(9)
322 FN_BLOCK(10)
323 FN_BLOCK(11)
324 FN_BLOCK(12)
325
326 /**
327 * Initialize the function pointers for the callback routines.
328 *
329 * NOTE: If MAX_ARGS or MAX_CALLBACKS changes, the following has to be updated.
330 */
331 #if MAX_CALLBACKS == 16
332 #define FN_A_BLOCK(args) { \
333 (jintLong)FN(0, args), \
334 (jintLong)FN(1, args), \
335 (jintLong)FN(2, args), \
336 (jintLong)FN(3, args), \
337 (jintLong)FN(4, args), \
338 (jintLong)FN(5, args), \
339 (jintLong)FN(6, args), \
340 (jintLong)FN(7, args), \
341 (jintLong)FN(8, args), \
342 (jintLong)FN(9, args), \
343 (jintLong)FN(10, args), \
344 (jintLong)FN(11, args), \
345 (jintLong)FN(12, args), \
346 (jintLong)FN(13, args), \
347 (jintLong)FN(14, args), \
348 (jintLong)FN(15, args), \
349 },
350 #elif MAX_CALLBACKS == 128
351 #define FN_A_BLOCK(args) { \
352 (jintLong)FN(0, args), \
353 (jintLong)FN(1, args), \
354 (jintLong)FN(2, args), \
355 (jintLong)FN(3, args), \
356 (jintLong)FN(4, args), \
357 (jintLong)FN(5, args), \
358 (jintLong)FN(6, args), \
359 (jintLong)FN(7, args), \
360 (jintLong)FN(8, args), \
361 (jintLong)FN(9, args), \
362 (jintLong)FN(10, args), \
363 (jintLong)FN(11, args), \
364 (jintLong)FN(12, args), \
365 (jintLong)FN(13, args), \
366 (jintLong)FN(14, args), \
367 (jintLong)FN(15, args), \
368 (jintLong)FN(16, args), \
369 (jintLong)FN(17, args), \
370 (jintLong)FN(18, args), \
371 (jintLong)FN(19, args), \
372 (jintLong)FN(20, args), \
373 (jintLong)FN(21, args), \
374 (jintLong)FN(22, args), \
375 (jintLong)FN(23, args), \
376 (jintLong)FN(24, args), \
377 (jintLong)FN(25, args), \
378 (jintLong)FN(26, args), \
379 (jintLong)FN(27, args), \
380 (jintLong)FN(28, args), \
381 (jintLong)FN(29, args), \
382 (jintLong)FN(30, args), \
383 (jintLong)FN(31, args), \
384 (jintLong)FN(32, args), \
385 (jintLong)FN(33, args), \
386 (jintLong)FN(34, args), \
387 (jintLong)FN(35, args), \
388 (jintLong)FN(36, args), \
389 (jintLong)FN(37, args), \
390 (jintLong)FN(38, args), \
391 (jintLong)FN(39, args), \
392 (jintLong)FN(40, args), \
393 (jintLong)FN(41, args), \
394 (jintLong)FN(42, args), \
395 (jintLong)FN(43, args), \
396 (jintLong)FN(44, args), \
397 (jintLong)FN(45, args), \
398 (jintLong)FN(46, args), \
399 (jintLong)FN(47, args), \
400 (jintLong)FN(48, args), \
401 (jintLong)FN(49, args), \
402 (jintLong)FN(50, args), \
403 (jintLong)FN(51, args), \
404 (jintLong)FN(52, args), \
405 (jintLong)FN(53, args), \
406 (jintLong)FN(54, args), \
407 (jintLong)FN(55, args), \
408 (jintLong)FN(56, args), \
409 (jintLong)FN(57, args), \
410 (jintLong)FN(58, args), \
411 (jintLong)FN(59, args), \
412 (jintLong)FN(60, args), \
413 (jintLong)FN(61, args), \
414 (jintLong)FN(62, args), \
415 (jintLong)FN(63, args), \
416 (jintLong)FN(64, args), \
417 (jintLong)FN(65, args), \
418 (jintLong)FN(66, args), \
419 (jintLong)FN(67, args), \
420 (jintLong)FN(68, args), \
421 (jintLong)FN(69, args), \
422 (jintLong)FN(70, args), \
423 (jintLong)FN(71, args), \
424 (jintLong)FN(72, args), \
425 (jintLong)FN(73, args), \
426 (jintLong)FN(74, args), \
427 (jintLong)FN(75, args), \
428 (jintLong)FN(76, args), \
429 (jintLong)FN(77, args), \
430 (jintLong)FN(78, args), \
431 (jintLong)FN(79, args), \
432 (jintLong)FN(80, args), \
433 (jintLong)FN(81, args), \
434 (jintLong)FN(82, args), \
435 (jintLong)FN(83, args), \
436 (jintLong)FN(84, args), \
437 (jintLong)FN(85, args), \
438 (jintLong)FN(86, args), \
439 (jintLong)FN(87, args), \
440 (jintLong)FN(88, args), \
441 (jintLong)FN(89, args), \
442 (jintLong)FN(90, args), \
443 (jintLong)FN(91, args), \
444 (jintLong)FN(92, args), \
445 (jintLong)FN(93, args), \
446 (jintLong)FN(94, args), \
447 (jintLong)FN(95, args), \
448 (jintLong)FN(96, args), \
449 (jintLong)FN(97, args), \
450 (jintLong)FN(98, args), \
451 (jintLong)FN(99, args), \
452 (jintLong)FN(100, args), \
453 (jintLong)FN(101, args), \
454 (jintLong)FN(102, args), \
455 (jintLong)FN(103, args), \
456 (jintLong)FN(104, args), \
457 (jintLong)FN(105, args), \
458 (jintLong)FN(106, args), \
459 (jintLong)FN(107, args), \
460 (jintLong)FN(108, args), \
461 (jintLong)FN(109, args), \
462 (jintLong)FN(110, args), \
463 (jintLong)FN(111, args), \
464 (jintLong)FN(112, args), \
465 (jintLong)FN(113, args), \
466 (jintLong)FN(114, args), \
467 (jintLong)FN(115, args), \
468 (jintLong)FN(116, args), \
469 (jintLong)FN(117, args), \
470 (jintLong)FN(118, args), \
471 (jintLong)FN(119, args), \
472 (jintLong)FN(120, args), \
473 (jintLong)FN(121, args), \
474 (jintLong)FN(122, args), \
475 (jintLong)FN(123, args), \
476 (jintLong)FN(124, args), \
477 (jintLong)FN(125, args), \
478 (jintLong)FN(126, args), \
479 (jintLong)FN(127, args), \
480 },
481 #else
482 #error Invalid MAX_CALLBACKS
483 #endif /* MAX_CALLBACKS == 16 */
484
485 jintLong fnx_array[MAX_ARGS+1][MAX_CALLBACKS] = {
486 FN_A_BLOCK(0)
487 FN_A_BLOCK(1)
488 FN_A_BLOCK(2)
489 FN_A_BLOCK(3)
490 FN_A_BLOCK(4)
491 FN_A_BLOCK(5)
492 FN_A_BLOCK(6)
493 FN_A_BLOCK(7)
494 FN_A_BLOCK(8)
495 FN_A_BLOCK(9)
496 FN_A_BLOCK(10)
497 FN_A_BLOCK(11)
498 FN_A_BLOCK(12)
499 };
500
501 #endif /* USE_ASSEMBLER */
502
503 /* --------------- class Callback Native Methods --------------- */
504
505 JNIEXPORT jlong JNICALL Java_org_fusesource_hawtjni_runtime_Callback_bind
506 (JNIEnv *env, jclass that, jobject callbackObject, jobject object, jstring method, jstring signature, jint argCount, jboolean isStatic, jboolean isArrayBased, jlong errorResult)
507 {
508 int i;
509 jmethodID mid = NULL;
510 jclass javaClass = that;
511 const char *methodString = NULL, *sigString = NULL;
512 if (jvm == NULL) (*env)->GetJavaVM(env, &jvm);
513 if (JNI_VERSION == 0) JNI_VERSION = (*env)->GetVersion(env);
514 if (!initialized) {
515 memset(&callbackData, 0, sizeof(callbackData));
516 initialized = 1;
517 }
518 if (method) methodString = (const char *) (*env)->GetStringUTFChars(env, method, NULL);
519 if (signature) sigString = (const char *) (*env)->GetStringUTFChars(env, signature, NULL);
520 if (object && methodString && sigString) {
521 if (isStatic) {
522 mid = (*env)->GetStaticMethodID(env, object, methodString, sigString);
523 } else {
524 javaClass = (*env)->GetObjectClass(env, object);
525 mid = (*env)->GetMethodID(env, javaClass, methodString, sigString);
526 }
527 }
528 if (method && methodString) (*env)->ReleaseStringUTFChars(env, method, methodString);
529 if (signature && sigString) (*env)->ReleaseStringUTFChars(env, signature, sigString);
530 if (mid == 0) goto fail;
531 for (i=0; i<MAX_CALLBACKS; i++) {
532 if (!callbackData[i].callback) {
533 if ((callbackData[i].callback = (*env)->NewGlobalRef(env, callbackObject)) == NULL) goto fail;
534 if ((callbackData[i].object = (*env)->NewGlobalRef(env, object)) == NULL) goto fail;
535 callbackData[i].isStatic = isStatic;
536 callbackData[i].isArrayBased = isArrayBased;
537 callbackData[i].argCount = argCount;
538 callbackData[i].errorResult = errorResult;
539 callbackData[i].methodID = mid;
540 #ifndef USE_ASSEMBLER
541 return (jintLong) fnx_array[argCount][i];
542 #else
543 {
544 int j = 0, k;
545 unsigned char* code;
546 #ifdef __APPLE__
547 int pad = 0;
548 #endif
549 if (callbackCode == NULL) {
550 #if defined (_WIN32) || defined (_WIN32_WCE)
551 callbackCode = VirtualAlloc(NULL, CALLBACK_THUNK_SIZE * MAX_CALLBACKS, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
552 if (callbackCode == NULL) return 0;
553 #else
554 callbackCode = mmap(NULL, CALLBACK_THUNK_SIZE * MAX_CALLBACKS, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
555 if (callbackCode == MAP_FAILED) return 0;
556 #endif
557 }
558 code = (unsigned char *)(callbackCode + (i * CALLBACK_THUNK_SIZE));
559
560 //PUSH EBP - 1 byte
561 code[j++] = 0x55;
562
563 //MOV EBP,ESP - 2 bytes
564 code[j++] = 0x8b;
565 code[j++] = 0xec;
566
567 #ifdef __APPLE__
568 /* darwin calling conventions require that the stack be aligned on a 16-byte boundary. */
569 k = (argCount+3)*sizeof(jintLong);
570 pad = ((k + 15) & ~15) - k;
571 if (pad > 0) {
572 //SUB ESP,pad - 3 bytes
573 code[j++] = 0x83;
574 code[j++] = 0xec;
575 code[j++] = pad;
576 }
577 #endif
578
579 // 3*argCount bytes
580 for (k=(argCount + 1) * sizeof(jintLong); k >= sizeof(jintLong)*2; k -= sizeof(jintLong)) {
581 //PUSH SS:[EBP+k]
582 code[j++] = 0xff;
583 code[j++] = 0x75;
584 code[j++] = k;
585 }
586
587 if (i > 127) {
588 //PUSH i - 5 bytes
589 code[j++] = 0x68;
590 code[j++] = ((i >> 0) & 0xFF);
591 code[j++] = ((i >> 8) & 0xFF);
592 code[j++] = ((i >> 16) & 0xFF);
593 code[j++] = ((i >> 24) & 0xFF);
594 } else {
595 //PUSH i - 2 bytes
596 code[j++] = 0x6a;
597 code[j++] = i;
598 }
599
600 //MOV EAX callback - 1 + sizeof(jintLong) bytes
601 code[j++] = 0xb8;
602 ((jintLong *)&code[j])[0] = (jintLong)&callback;
603 j += sizeof(jintLong);
604
605 //CALL EAX - 2 bytes
606 code[j++] = 0xff;
607 code[j++] = 0xd0;
608
609 //ADD ESP,(argCount + 1) * sizeof(jintLong) - 3 bytes
610 code[j++] = 0x83;
611 code[j++] = 0xc4;
612 #ifdef __APPLE__
613 code[j++] = (unsigned char)(pad + ((argCount + 1) * sizeof(jintLong)));
614 #else
615 code[j++] = (unsigned char)((argCount + 1) * sizeof(jintLong));
616 #endif
617
618 //POP EBP - 1 byte
619 code[j++] = 0x5d;
620
621 #if defined (_WIN32) || defined (_WIN32_WCE)
622 //RETN argCount * sizeof(jintLong) - 3 bytes
623 code[j++] = 0xc2;
624 code[j++] = (unsigned char)(argCount * sizeof(jintLong));
625 code[j++] = 0x00;
626 #else
627 //RETN - 1 byte
628 code[j++] = 0xc3;
629 #endif
630
631 if (j > CALLBACK_THUNK_SIZE) {
632 jclass errorClass = (*env)->FindClass(env, "java/lang/Error");
633 (*env)->ThrowNew(env, errorClass, "Callback thunk overflow");
634 }
635
636 return (jintLong)code;
637 }
638 #endif /* USE_ASSEMBLER */
639 }
640 }
641 fail:
642 return 0;
643 }
644
645 JNIEXPORT void JNICALL Java_org_fusesource_hawtjni_runtime_Callback_unbind
646 (JNIEnv *env, jclass that, jobject callback)
647 {
648 int i;
649 for (i=0; i<MAX_CALLBACKS; i++) {
650 if (callbackData[i].callback != NULL && (*env)->IsSameObject(env, callback, callbackData[i].callback)) {
651 if (callbackData[i].callback != NULL) (*env)->DeleteGlobalRef(env, callbackData[i].callback);
652 if (callbackData[i].object != NULL) (*env)->DeleteGlobalRef(env, callbackData[i].object);
653 memset(&callbackData[i], 0, sizeof(CALLBACK_DATA));
654 }
655 }
656 }
657
658 JNIEXPORT jboolean JNICALL Java_org_eclipse_swt_internal_Callback_getEnabled
659 (JNIEnv *env, jclass that)
660 {
661 return (jboolean)callbackEnabled;
662 }
663
664 JNIEXPORT jint JNICALL Java_org_eclipse_swt_internal_Callback_getEntryCount
665 (JNIEnv *env, jclass that)
666 {
667 return (jint)callbackEntryCount;
668 }
669
670 JNIEXPORT void JNICALL Java_org_eclipse_swt_internal_Callback_setEnabled
671 (JNIEnv *env, jclass that, jboolean enable)
672 {
673 callbackEnabled = enable;
674 }
675
676 JNIEXPORT void JNICALL Java_org_eclipse_swt_internal_Callback_reset
677 (JNIEnv *env, jclass that)
678 {
679 memset((void *)&callbackData, 0, sizeof(callbackData));
680 }
681
682 jintLong callback(int index, ...)
683 {
684 if (!callbackEnabled) return 0;
685
686 {
687 JNIEnv *env = NULL;
688 jmethodID mid = callbackData[index].methodID;
689 jobject object = callbackData[index].object;
690 jboolean isStatic = callbackData[index].isStatic;
691 jboolean isArrayBased = callbackData[index].isArrayBased;
692 jint argCount = callbackData[index].argCount;
693 jlong result = callbackData[index].errorResult;
694 int detach = 0;
695 va_list vl;
696
697 #ifdef DEBUG_CALL_PRINTS
698 fprintf(stderr, "* callback starting %d\n", counter++);
699 #endif
700
701 #ifdef JNI_VERSION_1_2
702 if (IS_JNI_1_2) {
703 (*jvm)->GetEnv(jvm, (void **)&env, JNI_VERSION_1_2);
704 }
705 #endif
706
707 #ifdef JNI_VERSION_1_4
708 if (env == NULL) {
709 if (JNI_VERSION >= JNI_VERSION_1_4) {
710 (*jvm)->AttachCurrentThreadAsDaemon(jvm, (void **)&env, NULL);
711 }
712 }
713 #endif
714
715 if (env == NULL) {
716 (*jvm)->AttachCurrentThread(jvm, (void **)&env, NULL);
717 if (IS_JNI_1_2) detach = 1;
718 }
719
720 /* If the current thread is not attached to the VM, it is not possible to call into the VM */
721 if (env == NULL) {
722 #ifdef DEBUG_CALL_PRINTS
723 fprintf(stderr, "* could not get env\n");
724 #endif
725 goto noEnv;
726 }
727
728 /* If an exception has occurred in previous callbacks do not call into the VM. */
729 if ((*env)->ExceptionOccurred(env)) {
730 goto done;
731 }
732
733 /* Call into the VM. */
734 ATOMIC_INC(callbackEntryCount);
735 va_start(vl, index);
736 if (isArrayBased) {
737 int i;
738 jintLongArray argsArray = (*env)->NewIntLongArray(env, argCount);
739 if (argsArray != NULL) {
740 jintLong *elements = (*env)->GetIntLongArrayElements(env, argsArray, NULL);
741 if (elements != NULL) {
742 for (i=0; i<argCount; i++) {
743 elements[i] = va_arg(vl, jintLong);
744 }
745 (*env)->ReleaseIntLongArrayElements(env, argsArray, elements, 0);
746 if (isStatic) {
747 result = (*env)->CallStaticIntLongMethod(env, object, mid, argsArray);
748 } else {
749 result = (*env)->CallIntLongMethod(env, object, mid, argsArray);
750 }
751 }
752 /*
753 * This function may be called many times before returning to Java,
754 * explicitly delete local references to avoid GP's in certain VMs.
755 */
756 (*env)->DeleteLocalRef(env, argsArray);
757 }
758 } else {
759 if (isStatic) {
760 result = (*env)->CallStaticIntLongMethodV(env, object, mid, vl);
761 } else {
762 result = (*env)->CallIntLongMethodV(env, object, mid, vl);
763 }
764 }
765 va_end(vl);
766 ATOMIC_DEC(callbackEntryCount);
767
768 done:
769 /* If an exception has occurred in Java, return the error result. */
770 if ((*env)->ExceptionOccurred(env)) {
771 #ifdef DEBUG_CALL_PRINTS
772 fprintf(stderr, "* java exception occurred\n");
773 (*env)->ExceptionDescribe(env);
774 #endif
775 result = callbackData[index].errorResult;
776 }
777
778 if (detach) {
779 (*jvm)->DetachCurrentThread(jvm);
780 env = NULL;
781 }
782
783 noEnv:
784
785 #ifdef DEBUG_CALL_PRINTS
786 fprintf(stderr, "* callback exiting %d\n", --counter);
787 #endif
788
789 return result;
790 }
791 }
792
793 /* ------------- END: class Callback impl --------------- */