1 #include <assert.h> 2 #include <jni.h> 3 #include <alloca.h> 4 5 #include <pthread.h> 6 7 union env_union 8 { 9 void *void_env; 10 JNIEnv *jni_env; 11 }; 12 13 union env_union tmp; 14 JNIEnv* env; 15 JavaVM* jvm; 16 JavaVMInitArgs vm_args; 17 JavaVMOption options[1]; 18 jclass class_id; 19 jmethodID method_id; 20 jint result; 21 22 long product(unsigned long n, unsigned long m) { 23 if (m == 1) { 24 return n; 25 } else { 26 int *p = alloca(sizeof (int)); 27 *p = n; 28 return product (n, m-1) + *p; 29 } 30 } 31 32 void * 33 floobydust (void *p) 34 { 35 (*jvm)->AttachCurrentThread(jvm, &tmp.void_env, NULL); 36 env = tmp.jni_env; 37 38 class_id = (*env)->FindClass (env, "T"); 39 assert (class_id); 40 41 method_id = (*env)->GetStaticMethodID (env, class_id, "printIt", "()V"); 42 assert (method_id); 43 44 (*env)->CallStaticVoidMethod (env, class_id, method_id, NULL); 45 46 (*jvm)->DetachCurrentThread(jvm); 47 48 printf("%ld\n", product(5000,5000)); 49 50 (*jvm)->AttachCurrentThread(jvm, &tmp.void_env, NULL); 51 env = tmp.jni_env; 52 53 class_id = (*env)->FindClass (env, "T"); 54 assert (class_id); 55 56 method_id = (*env)->GetStaticMethodID (env, class_id, "printIt", "()V"); 57 assert (method_id); 58 59 (*env)->CallStaticVoidMethod (env, class_id, method_id, NULL); 60 61 (*jvm)->DetachCurrentThread(jvm); 62 63 printf("%ld\n", product(5000,5000)); 64 65 return NULL; 66 } 67 68 int 69 main (int argc, const char** argv) 70 { 71 options[0].optionString = "-Xss320k"; 72 73 vm_args.version = JNI_VERSION_1_2; 74 vm_args.ignoreUnrecognized = JNI_TRUE; 75 vm_args.options = options; 76 vm_args.nOptions = 1; 77 78 result = JNI_CreateJavaVM (&jvm, &tmp.void_env, &vm_args); 79 assert (result >= 0); 80 81 env = tmp.jni_env; 82 83 floobydust (NULL); 84 85 pthread_t thr; 86 pthread_create (&thr, NULL, floobydust, NULL); 87 pthread_join (thr, NULL); 88 89 return 0; 90 }