Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Encapsulate the stdout port in a MachPort object

  • Loading branch information...
commit 27ff798acf6010b4262e54adab017065ab3cc2f4 1 parent adce55c
@jeremie-koenig authored
View
8 HelloMach.c
@@ -1,12 +1,10 @@
-#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
-#include <mach/port.h>
-#include <mach/message.h>
+#include <mach-java.h>
#include <hurd.h>
#include "HelloMach.h"
-JNIEXPORT void JNICALL Java_HelloMach_hello (JNIEnv *env, jclass cls)
+JNIEXPORT void JNICALL Java_HelloMach_hello (JNIEnv *env, jclass cls, jobject port)
{
static const char *hello = "Hello, World!\n";
mach_port_t stdoutp;
@@ -31,7 +29,7 @@ JNIEXPORT void JNICALL Java_HelloMach_hello (JNIEnv *env, jclass cls)
} rep;
} msg;
- stdoutp = getdport(1);
+ stdoutp = mach_java_getport(env, port);
mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &replyp);
msg.req.hdr.msgh_bits =
View
10 HelloMach.java
@@ -1,8 +1,12 @@
+import org.gnu.mach.MachPort;
+import org.gnu.hurd.Hurd;
+
public class HelloMach {
- private static native void hello();
+ private static native void hello(MachPort port);
public static void main(String argv[]) {
- System.loadLibrary("HelloMach");
- hello();
+ Hurd hurd = new Hurd();
+ System.loadLibrary("hurd-java");
+ hello(hurd.getdport(1));
}
}
View
31 Makefile
@@ -1,23 +1,40 @@
CC = gcc
CFLAGS = -Wall -g
+CPPFLAGS = -D_GNU_SOURCE -I.
JAVAH = javah
JAVAC = javac
+# Java class files
+JAVASRCS = $(shell find -name \*.java)
+CLASSES = $(patsubst %.java,%.class,$(JAVASRCS))
+
+# JNI shared library
+JNILIB = libhurd-java.so
+JNISRCS = $(shell find -name \*.c)
+JNIHDRS = $(patsubst %.c,%.h,$(JNISRCS))
+JNIOBJS = $(patsubst %.c,%.o,$(JNISRCS))
+
all: test
-%.class: %.java
- $(JAVAC) $<
+$(CLASSES): $(JAVASRCS)
+ $(JAVAC) $(JAVASRCS)
%.h: %.class
- $(JAVAH) -cp . $*
+ $(JAVAH) -cp . -o $@.n $*
+ mv $@.n $@
+
+%.o: %.c
+ $(CC) $(CFLAGS) $(CPPFLAGS) -o $@ -c $<
-lib%.so: %.c %.h
- $(CC) $(CFLAGS) -shared -o $@ $<
+$(JNILIB): $(JNIOBJS)
+ $(CC) $(CFLAGS) -shared -o $@ $(JNIOBJS)
-test: HelloMach.class libHelloMach.so
+test: $(CLASSES) $(JNILIB)
+ echo $(JAVASRCS)
LD_LIBRARY_PATH=. java HelloMach
clean:
- $(RM) HelloMach.class HelloMach.h libHelloMach.so
+ $(RM) $(CLASSES) $(JNILIB) $(JNIOBJS) $(JNIHDRS)
+$(JNIOBJS): $(JNIHDRS)
.PRECIOUS: %.h
View
1  gnu
View
9 hurd/Hurd.c
@@ -0,0 +1,9 @@
+#include <mach-java.h>
+#include <hurd.h>
+#include "Hurd.h"
+
+JNIEXPORT jobject JNICALL
+Java_org_gnu_hurd_Hurd_getdport(JNIEnv *env, jobject obj, jint fd)
+{
+ return mach_java_makeport(env, getdport(fd));
+}
View
13 hurd/Hurd.java
@@ -0,0 +1,13 @@
+package org.gnu.hurd;
+import org.gnu.mach.MachPort;
+
+/**
+ * Ambient authority of a Hurd process.
+ */
+public class Hurd {
+ /**
+ * Retreive the MachPort for a given file descriptor.
+ */
+ public native MachPort getdport(int fd);
+};
+
View
24 mach-java.h
@@ -0,0 +1,24 @@
+/**
+ * @file mach-java.h
+ * @brief JNI interfaces for the org.gnu.mach package.
+ */
+#ifndef __MACH_JAVA_H__
+#define __MACH_JAVA_H__
+
+#include <mach.h>
+#include <jni.h>
+
+/**
+ * Construct a MachPort object for the given mach_port_t.
+ *
+ * This consumes one reference to @p name. The reference will be released
+ * when deallocate() is called, or when the new object is collected.
+ */
+jobject mach_java_makeport(JNIEnv *env, mach_port_t name);
+
+/**
+ * Retreive a port name from a MachPort object.
+ */
+mach_port_t mach_java_getport(JNIEnv *env, jobject obj);
+
+#endif
View
38 mach/MachPort.c
@@ -0,0 +1,38 @@
+#include <assert.h>
+#include <mach.h>
+#include <mach-java.h>
+#include "MachPort.h"
+
+static jfieldID nameID;
+static jmethodID ctorID;
+static jclass cls_MachPort;
+
+JNIEXPORT void JNICALL
+Java_org_gnu_mach_MachPort_initIDs(JNIEnv *env, jclass cls)
+{
+ cls_MachPort = cls;
+ nameID = (*env)->GetFieldID(env, cls_MachPort, "name", "I");
+ ctorID = (*env)->GetMethodID(env, cls_MachPort, "<init>", "(I)V");
+ assert(nameID != NULL);
+}
+
+JNIEXPORT void JNICALL
+Java_org_gnu_mach_MachPort_nativeDeallocate (JNIEnv *env, jobject obj)
+{
+ mach_port_deallocate(mach_task_self(), mach_java_getport(env, obj));
+}
+
+jobject mach_java_makeport(JNIEnv *env, mach_port_t name)
+{
+ if(!cls_MachPort) {
+ /* Ensures initIDs() has been called. */
+ jclass cls = (*env)->FindClass(env, "org/gnu/mach/MachPort");
+ (*env)->DeleteLocalRef(env, cls);
+ }
+ return (*env)->NewObject(env, cls_MachPort, ctorID, (jint) name);
+}
+
+mach_port_t mach_java_getport(JNIEnv *env, jobject obj)
+{
+ return (*env)->GetIntField(env, obj, nameID);
+}
View
48 mach/MachPort.java
@@ -0,0 +1,48 @@
+package org.gnu.mach;
+
+/**
+ * Mach port name.
+ */
+public class MachPort {
+ private static final int NULL = 0;
+ private static final int DEAD = -1;
+
+ /**
+ * Encapsulated port name.
+ */
+ private int name;
+
+ /**
+ * Instanciate a new MachPort object for the given name.
+ *
+ * This consumes one reference to @p name. The reference will be released
+ * when deallocate() is called, or when the new object is collected.
+ */
+ private MachPort(int name) {
+ this.name = name;
+ }
+
+ /**
+ * Deallocate this port.
+ *
+ * The current port name is deallocated and replaced with @c MACH_PORT_DEAD.
+ */
+ public synchronized void deallocate() {
+ nativeDeallocate();
+ this.name = DEAD;
+ }
+
+ /**
+ * Call mach_port_dellocate().
+ *
+ * TODO: this could eventually be replaced by the Java-based RPC call.
+ */
+ private native void nativeDeallocate();
+
+ /* JNI code initialization */
+ private static native void initIDs();
+ static {
+ initIDs();
+ }
+};
+
View
1  org
Please sign in to comment.
Something went wrong with that request. Please try again.