experiments with JNI;
authorHarald Steinlechner <haraldsteinlechner@gmail.com>
Thu, 23 Aug 2012 18:44:21 +0000 (20:44 +0200)
committerHarald Steinlechner <haraldsteinlechner@gmail.com>
Thu, 23 Aug 2012 18:44:21 +0000 (20:44 +0200)
Makefile
Mate/MethodPool.hs
Mate/Rts.hs [new file with mode: 0644]
jmate/lang/MateRuntime.java
jmate/lang/Runtime.java [new file with mode: 0644]
rts/mock/jmate_lang_MateRuntime.c [new file with mode: 0644]
rts/mock/jmate_lang_MateRuntime.h [new file with mode: 0644]
rts/mock/jmate_lang_MateRuntime.h.gch [new file with mode: 0644]
rts/mock/libMateRuntime.so [new file with mode: 0755]
tests/Garbage1.java
tools/openjdktest.sh

index b0a440ddfc97cbb1c05324873435cbe51931f14e..04685026df52dccae37e8038c26ef96ad9c5c21a 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -54,6 +54,11 @@ COMPILEF = $(basename $@).compile
 ffi/native.o: ffi/native.c
        ghc -Wall -O2 -c $< -o $@
 
 ffi/native.o: ffi/native.c
        ghc -Wall -O2 -c $< -o $@
 
+runtime: jmate/lang/MateRuntime.java
+       javac jmate/lang/MateRuntime.java
+       javah  -o rts/mock/jmate_lang_MateRuntime.h jmate.lang.MateRuntime
+       gcc -shared -fPIC -I$(JAVA_HOME)/include rts/mock/jmate_lang_MateRuntime.c -I./rts/mock -o rts/mock/libMateRuntime.so 
+
 GHCCALL = ghc --make $(GHC_OPT) Mate.hs ffi/trap.c -o $@ $(GHC_LD) -outputdir
 mate: Mate.hs ffi/trap.c $(HS_FILES) $(HS_BOOT) ffi/native.o $(CLASS_FILES)
        @mkdir -p $(B_RELEASE)
 GHCCALL = ghc --make $(GHC_OPT) Mate.hs ffi/trap.c -o $@ $(GHC_LD) -outputdir
 mate: Mate.hs ffi/trap.c $(HS_FILES) $(HS_BOOT) ffi/native.o $(CLASS_FILES)
        @mkdir -p $(B_RELEASE)
index da3e5e055808de09698423cc8173a4d3929178aa..4be3cf2577a7764660e59c19b1a55d01c33c6c1a 100644 (file)
@@ -32,7 +32,7 @@ import Mate.NativeMachine
 import Mate.ClassPool
 import Mate.Debug
 import Mate.Utilities
 import Mate.ClassPool
 import Mate.Debug
 import Mate.Utilities
-import Mate.GarbageAlloc
+import Mate.Rts()
 
 foreign import ccall "dynamic"
    code_void :: FunPtr (IO ()) -> IO ()
 
 foreign import ccall "dynamic"
    code_void :: FunPtr (IO ()) -> IO ()
@@ -43,6 +43,9 @@ foreign import ccall "&demoInterfaceCall"
 foreign import ccall "&printMemoryUsage"
   printMemoryUsageAddr :: FunPtr (IO ())
  
 foreign import ccall "&printMemoryUsage"
   printMemoryUsageAddr :: FunPtr (IO ())
  
+foreign import ccall "&loadLibrary"
+  loadLibraryAddr :: FunPtr (IO ())
+
 getMethodEntry :: CPtrdiff -> CPtrdiff -> IO CPtrdiff
 getMethodEntry signal_from methodtable = do
   mmap <- getMethodMap
 getMethodEntry :: CPtrdiff -> CPtrdiff -> IO CPtrdiff
 getMethodEntry signal_from methodtable = do
   mmap <- getMethodMap
@@ -74,6 +77,8 @@ getMethodEntry signal_from methodtable = do
                 let scm = toString cm; smethod = toString method
                 if scm == "jmate/lang/MateRuntime" then do
                   case smethod of
                 let scm = toString cm; smethod = toString method
                 if scm == "jmate/lang/MateRuntime" then do
                   case smethod of
+                    "loadLibrary" ->
+                       return . funPtrToAddr $ loadLibraryAddr
                     "demoInterfaceCall" ->
                        return . funPtrToAddr $ demoInterfaceCallAddr
                     "printMemoryUsage" ->
                     "demoInterfaceCall" ->
                        return . funPtrToAddr $ demoInterfaceCallAddr
                     "printMemoryUsage" ->
diff --git a/Mate/Rts.hs b/Mate/Rts.hs
new file mode 100644 (file)
index 0000000..844b44a
--- /dev/null
@@ -0,0 +1,5 @@
+module Mate.Rts (loadLibrary) where
+
+foreign export ccall loadLibrary :: IO ()
+loadLibrary :: IO ()
+loadLibrary = print "load lib" 
index 56f3a10595a10fa6ce919b770342d90ab41e29e5..678182bf7b713605cd086f5eef42a5957be905ad 100644 (file)
@@ -1,6 +1,7 @@
 package jmate.lang;
 
 public class MateRuntime {
 package jmate.lang;
 
 public class MateRuntime {
-       public static native void demoInterfaceCall(int val);
-        public static native void printMemoryUsage();
+
+       public static native void loadLibrary(String lib);
+        public static native int getCurrentHeapSize();
 }
 }
diff --git a/jmate/lang/Runtime.java b/jmate/lang/Runtime.java
new file mode 100644 (file)
index 0000000..d15200a
--- /dev/null
@@ -0,0 +1,17 @@
+package jmate.lang;
+
+public class Runtime
+{
+       public Runtime()
+       {
+       }
+
+       public void loadLibrary(String name)
+       {
+       }
+
+       public static Runtime getRuntime()
+       {
+               return new Runtime();
+       }
+}
diff --git a/rts/mock/jmate_lang_MateRuntime.c b/rts/mock/jmate_lang_MateRuntime.c
new file mode 100644 (file)
index 0000000..4b8a171
--- /dev/null
@@ -0,0 +1,9 @@
+#include "jmate_lang_MateRuntime.h"
+
+JNIEXPORT jint JNICALL Java_jmate_lang_MateRuntime_getCurrentHeapSize
+  (JNIEnv *env, jclass class)
+{
+       return 0;
+}
+
+
diff --git a/rts/mock/jmate_lang_MateRuntime.h b/rts/mock/jmate_lang_MateRuntime.h
new file mode 100644 (file)
index 0000000..991b5e8
--- /dev/null
@@ -0,0 +1,21 @@
+/* DO NOT EDIT THIS FILE - it is machine generated */
+#include <jni.h>
+/* Header for class jmate_lang_MateRuntime */
+
+#ifndef _Included_jmate_lang_MateRuntime
+#define _Included_jmate_lang_MateRuntime
+#ifdef __cplusplus
+extern "C" {
+#endif
+/*
+ * Class:     jmate_lang_MateRuntime
+ * Method:    getCurrentHeapSize
+ * Signature: ()I
+ */
+JNIEXPORT jint JNICALL Java_jmate_lang_MateRuntime_getCurrentHeapSize
+  (JNIEnv *, jclass);
+
+#ifdef __cplusplus
+}
+#endif
+#endif
diff --git a/rts/mock/jmate_lang_MateRuntime.h.gch b/rts/mock/jmate_lang_MateRuntime.h.gch
new file mode 100644 (file)
index 0000000..32d8c1d
Binary files /dev/null and b/rts/mock/jmate_lang_MateRuntime.h.gch differ
diff --git a/rts/mock/libMateRuntime.so b/rts/mock/libMateRuntime.so
new file mode 100755 (executable)
index 0000000..6ad2e1f
Binary files /dev/null and b/rts/mock/libMateRuntime.so differ
index 5ffea7b97300441f9acb5a1c6d2a875260a5e72a..4073cc808788dd5cefd16723bd1ae3e83d28bff8 100644 (file)
@@ -4,7 +4,15 @@ package tests;
 
 public class Garbage1
 {
 
 public class Garbage1
 {
-       
+       static
+       {
+               loadLibrary();
+       }
+
+       public static void loadLibrary()
+       {
+               //Runtime.getRuntime().loadLibrary("MateRuntime");
+       }       
 
        public Garbage1(){}
 
 
        public Garbage1(){}
 
@@ -33,7 +41,7 @@ class Big2
 
        public Big2()
        {
 
        public Big2()
        {
-               arr = new int[0x400];
+               arr = new int[0xF400];
                //System.out.println("foo");
        }
         
                //System.out.println("foo");
        }
         
index 605928d28e351606daf1c7aa4f1a1c9d73f08b4d..e07757254dd4a5c69896cfa52a11b631b8eec18f 100755 (executable)
@@ -8,7 +8,7 @@ fi
 
 class2test=$1
 
 
 class2test=$1
 
-openjdk="java -client"
+openjdk="java -client -Djava.library.path=rts/mock"
 openjdk_output=`mktemp`
 mate="./mate"
 mate_output=`mktemp`
 openjdk_output=`mktemp`
 mate="./mate"
 mate_output=`mktemp`