This is the mail archive of the
java-patches@gcc.gnu.org
mailing list for the Java project.
Re: [Patch] JNI support for direct buffers
- From: Michael Koch <konqueror at gmx dot de>
- To: java-patches at gcc dot gnu dot org
- Date: Thu, 6 Jan 2005 11:24:06 +0100
- Subject: Re: [Patch] JNI support for direct buffers
- References: <20050106083813.GA523@asterix.konqueror.de>
Am Donnerstag, 6. Januar 2005 09:38 schrieb Michael Koch:
> Hi list,
>
>
> I wrote the attached patch to fix the handling of direct buffers
> when using the JNI methods NewDirectByteBuffer,
> GetDirectBufferAddress and GetDirectBufferLength.
>
> OK to commit to trunk ?
There was little bug in the testsuite part of the patch. Here is the
new patch.
Michael
--
Homepage: http://www.worldforge.org/
Index: java/nio/Buffer.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/nio/Buffer.java,v
retrieving revision 1.10
diff -u -r1.10 Buffer.java
--- java/nio/Buffer.java 22 Oct 2004 17:14:14 -0000 1.10
+++ java/nio/Buffer.java 6 Jan 2005 10:20:47 -0000
@@ -38,12 +38,18 @@
package java.nio;
+import gnu.gcj.RawData;
+
+/**
+ * @since 1.4
+ */
public abstract class Buffer
{
int cap = 0;
int limit = 0;
int pos = 0;
int mark = -1;
+ RawData address;
/**
* Creates a new Buffer.
Index: java/nio/CharViewBufferImpl.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/nio/CharViewBufferImpl.java,v
retrieving revision 1.4
diff -u -r1.4 CharViewBufferImpl.java
--- java/nio/CharViewBufferImpl.java 9 Jul 2004 13:40:28 -0000 1.4
+++ java/nio/CharViewBufferImpl.java 6 Jan 2005 10:20:47 -0000
@@ -53,6 +53,8 @@
this.offset = bb.position();
this.readOnly = bb.isReadOnly();
this.endian = bb.order();
+ if (bb.isDirect())
+ this.address = VMDirectByteBuffer.adjustAddress(bb.address, offset);
}
public CharViewBufferImpl (ByteBuffer bb, int offset, int capacity,
@@ -64,6 +66,8 @@
this.offset = offset;
this.readOnly = readOnly;
this.endian = endian;
+ if (bb.isDirect())
+ this.address = VMDirectByteBuffer.adjustAddress(bb.address, offset);
}
/**
Index: java/nio/DirectByteBufferImpl.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/nio/DirectByteBufferImpl.java,v
retrieving revision 1.16
diff -u -r1.16 DirectByteBufferImpl.java
--- java/nio/DirectByteBufferImpl.java 24 Nov 2004 12:19:42 -0000 1.16
+++ java/nio/DirectByteBufferImpl.java 6 Jan 2005 10:20:47 -0000
@@ -55,7 +55,6 @@
* memory and should free it.
*/
private final Object owner;
- final RawData address;
final static class ReadOnly extends DirectByteBufferImpl
{
Index: java/nio/DoubleViewBufferImpl.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/nio/DoubleViewBufferImpl.java,v
retrieving revision 1.5
diff -u -r1.5 DoubleViewBufferImpl.java
--- java/nio/DoubleViewBufferImpl.java 9 Jul 2004 13:40:28 -0000 1.5
+++ java/nio/DoubleViewBufferImpl.java 6 Jan 2005 10:20:47 -0000
@@ -53,6 +53,8 @@
this.offset = bb.position();
this.readOnly = bb.isReadOnly();
this.endian = bb.order();
+ if (bb.isDirect())
+ this.address = VMDirectByteBuffer.adjustAddress(bb.address, offset);
}
public DoubleViewBufferImpl (ByteBuffer bb, int offset, int capacity,
@@ -64,6 +66,8 @@
this.offset = offset;
this.readOnly = readOnly;
this.endian = endian;
+ if (bb.isDirect())
+ this.address = VMDirectByteBuffer.adjustAddress(bb.address, offset);
}
/**
Index: java/nio/FloatViewBufferImpl.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/nio/FloatViewBufferImpl.java,v
retrieving revision 1.5
diff -u -r1.5 FloatViewBufferImpl.java
--- java/nio/FloatViewBufferImpl.java 9 Jul 2004 13:40:29 -0000 1.5
+++ java/nio/FloatViewBufferImpl.java 6 Jan 2005 10:20:47 -0000
@@ -53,6 +53,8 @@
this.offset = bb.position();
this.readOnly = bb.isReadOnly();
this.endian = bb.order();
+ if (bb.isDirect())
+ this.address = VMDirectByteBuffer.adjustAddress(bb.address, offset);
}
public FloatViewBufferImpl (ByteBuffer bb, int offset, int capacity,
@@ -64,6 +66,8 @@
this.offset = offset;
this.readOnly = readOnly;
this.endian = endian;
+ if (bb.isDirect())
+ this.address = VMDirectByteBuffer.adjustAddress(bb.address, offset);
}
/**
Index: java/nio/IntViewBufferImpl.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/nio/IntViewBufferImpl.java,v
retrieving revision 1.5
diff -u -r1.5 IntViewBufferImpl.java
--- java/nio/IntViewBufferImpl.java 9 Jul 2004 13:40:29 -0000 1.5
+++ java/nio/IntViewBufferImpl.java 6 Jan 2005 10:20:47 -0000
@@ -53,6 +53,8 @@
this.offset = bb.position();
this.readOnly = bb.isReadOnly();
this.endian = bb.order();
+ if (bb.isDirect())
+ this.address = VMDirectByteBuffer.adjustAddress(bb.address, offset);
}
public IntViewBufferImpl (ByteBuffer bb, int offset, int capacity,
@@ -64,6 +66,8 @@
this.offset = offset;
this.readOnly = readOnly;
this.endian = endian;
+ if (bb.isDirect())
+ this.address = VMDirectByteBuffer.adjustAddress(bb.address, offset);
}
/**
Index: java/nio/LongViewBufferImpl.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/nio/LongViewBufferImpl.java,v
retrieving revision 1.5
diff -u -r1.5 LongViewBufferImpl.java
--- java/nio/LongViewBufferImpl.java 9 Jul 2004 13:40:29 -0000 1.5
+++ java/nio/LongViewBufferImpl.java 6 Jan 2005 10:20:47 -0000
@@ -53,6 +53,8 @@
this.offset = bb.position();
this.readOnly = bb.isReadOnly();
this.endian = bb.order();
+ if (bb.isDirect())
+ this.address = VMDirectByteBuffer.adjustAddress(bb.address, offset);
}
public LongViewBufferImpl (ByteBuffer bb, int offset, int capacity,
@@ -64,6 +66,8 @@
this.offset = offset;
this.readOnly = readOnly;
this.endian = endian;
+ if (bb.isDirect())
+ this.address = VMDirectByteBuffer.adjustAddress(bb.address, offset);
}
/**
Index: java/nio/MappedByteBufferImpl.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/nio/MappedByteBufferImpl.java,v
retrieving revision 1.14
diff -u -r1.14 MappedByteBufferImpl.java
--- java/nio/MappedByteBufferImpl.java 24 Nov 2004 11:11:46 -0000 1.14
+++ java/nio/MappedByteBufferImpl.java 6 Jan 2005 10:20:47 -0000
@@ -45,7 +45,6 @@
final class MappedByteBufferImpl extends MappedByteBuffer
{
boolean readOnly;
- RawData address;
/** Posix uses this for the pointer returned by mmap;
* Win32 uses it for the pointer returned by MapViewOfFile. */
Index: java/nio/ShortViewBufferImpl.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/nio/ShortViewBufferImpl.java,v
retrieving revision 1.5
diff -u -r1.5 ShortViewBufferImpl.java
--- java/nio/ShortViewBufferImpl.java 9 Jul 2004 13:40:29 -0000 1.5
+++ java/nio/ShortViewBufferImpl.java 6 Jan 2005 10:20:47 -0000
@@ -53,6 +53,8 @@
this.offset = bb.position();
this.readOnly = bb.isReadOnly();
this.endian = bb.order();
+ if (bb.isDirect())
+ this.address = VMDirectByteBuffer.adjustAddress(bb.address, offset);
}
public ShortViewBufferImpl (ByteBuffer bb, int offset, int capacity,
@@ -64,6 +66,8 @@
this.offset = offset;
this.readOnly = readOnly;
this.endian = endian;
+ if (bb.isDirect())
+ this.address = VMDirectByteBuffer.adjustAddress(bb.address, offset);
}
/**
Index: testsuite/libjava.jni/directbuffer.c
===================================================================
RCS file: testsuite/libjava.jni/directbuffer.c
diff -N testsuite/libjava.jni/directbuffer.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ testsuite/libjava.jni/directbuffer.c 6 Jan 2005 10:20:47 -0000
@@ -0,0 +1,75 @@
+#include <stdlib.h>
+
+#include "directbuffer.h"
+
+#define BUFFER_SIZE 1024
+
+static void *address;
+
+JNIEXPORT jobject JNICALL
+Java_directbuffer_createDirectByteBuffer (JNIEnv *env, jclass k)
+{
+ address = malloc (BUFFER_SIZE);
+ return (*env)->NewDirectByteBuffer (env, address, 1024);
+}
+
+static void
+test_buffer (JNIEnv *env, jobject buffer, const char *name, int len)
+{
+ void *tmp = (*env)->GetDirectBufferAddress (env, buffer);
+
+ if (address == tmp)
+ printf ("PASS: address of %s\n", name);
+ else
+ printf ("FAIL: address of %s\n", name);
+
+ int tmplen = (*env)->GetDirectBufferCapacity (env, buffer);
+
+ if (len == tmplen)
+ printf ("PASS: length of %s\n", name);
+ else
+ printf ("FAIL: length of %s\n", name);
+}
+
+JNIEXPORT void JNICALL
+Java_directbuffer_testDirectByteBuffer (JNIEnv *env, jclass k, jobject buffer, jint len)
+{
+ test_buffer (env, buffer, "direct java.nio.ByteBuffer", len);
+}
+
+JNIEXPORT void JNICALL
+Java_directbuffer_testCharBuffer (JNIEnv *env, jclass k, jobject buffer, jint len)
+{
+ test_buffer (env, buffer, "java.nio.CharBuffer view", len);
+}
+
+JNIEXPORT void JNICALL
+Java_directbuffer_testDoubleBuffer (JNIEnv *env, jclass k, jobject buffer, jint len)
+{
+ test_buffer (env, buffer, "java.nio.DoubleBuffer view", len);
+}
+
+JNIEXPORT void JNICALL
+Java_directbuffer_testFloatBuffer (JNIEnv *env, jclass k, jobject buffer, jint len)
+{
+ test_buffer (env, buffer, "java.nio.FloatBuffer view", len);
+}
+
+JNIEXPORT void JNICALL
+Java_directbuffer_testIntBuffer (JNIEnv *env, jclass k, jobject buffer, jint len)
+{
+ test_buffer (env, buffer, "java.nio.IntBuffer view", len);
+}
+
+JNIEXPORT void JNICALL
+Java_directbuffer_testLongBuffer (JNIEnv *env, jclass k, jobject buffer, jint len)
+{
+ test_buffer (env, buffer, "java.nio.LongBuffer view", len);
+}
+
+JNIEXPORT void JNICALL
+Java_directbuffer_testShortBuffer (JNIEnv *env, jclass k, jobject buffer, jint len)
+{
+ test_buffer (env, buffer, "java.nio.ShortBuffer view", len);
+}
+
Index: testsuite/libjava.jni/directbuffer.java
===================================================================
RCS file: testsuite/libjava.jni/directbuffer.java
diff -N testsuite/libjava.jni/directbuffer.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ testsuite/libjava.jni/directbuffer.java 6 Jan 2005 10:20:47 -0000
@@ -0,0 +1,40 @@
+// Test to make sure JNI implementation catches exceptions.
+
+import java.nio.*;
+
+public class directbuffer
+{
+ static
+ {
+ System.loadLibrary ("directbuffer");
+ }
+
+ public static native ByteBuffer createDirectByteBuffer();
+
+ public static native void testDirectByteBuffer(ByteBuffer bb, int len);
+ public static native void testCharBuffer(CharBuffer b, int len);
+ public static native void testDoubleBuffer(DoubleBuffer b, int len);
+ public static native void testFloatBuffer(FloatBuffer b, int len);
+ public static native void testIntBuffer(IntBuffer b, int len);
+ public static native void testLongBuffer(LongBuffer b, int len);
+ public static native void testShortBuffer(ShortBuffer b, int len);
+
+ public static void main (String[] args)
+ {
+ ByteBuffer bb = createDirectByteBuffer();
+ CharBuffer cb = bb.asCharBuffer();
+ DoubleBuffer db = bb.asDoubleBuffer();
+ FloatBuffer fb = bb.asFloatBuffer();
+ IntBuffer ib = bb.asIntBuffer();
+ LongBuffer lb = bb.asLongBuffer();
+ ShortBuffer sb = bb.asShortBuffer();
+
+ testDirectByteBuffer(bb, 1024);
+ testCharBuffer(cb, 512);
+ testDoubleBuffer(db, 128);
+ testFloatBuffer(fb, 256);
+ testIntBuffer(ib, 256);
+ testLongBuffer(lb, 128);
+ testShortBuffer(sb, 512);
+ }
+}
Index: testsuite/libjava.jni/directbuffer.out
===================================================================
RCS file: testsuite/libjava.jni/directbuffer.out
diff -N testsuite/libjava.jni/directbuffer.out
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ testsuite/libjava.jni/directbuffer.out 6 Jan 2005 10:20:47 -0000
@@ -0,0 +1,14 @@
+PASS: address of direct java.nio.ByteBuffer
+PASS: length of direct java.nio.ByteBuffer
+PASS: address of java.nio.CharBuffer view
+PASS: length of java.nio.CharBuffer view
+PASS: address of java.nio.DoubleBuffer view
+PASS: length of java.nio.DoubleBuffer view
+PASS: address of java.nio.FloatBuffer view
+PASS: length of java.nio.FloatBuffer view
+PASS: address of java.nio.IntBuffer view
+PASS: length of java.nio.IntBuffer view
+PASS: address of java.nio.LongBuffer view
+PASS: length of java.nio.LongBuffer view
+PASS: address of java.nio.ShortBuffer view
+PASS: length of java.nio.ShortBuffer view