You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2009/05/28 11:15:16 UTC

svn commit: r779498 - in /commons/sandbox/runtime/trunk/src: main/java/org/apache/commons/runtime/DirectByteBuffer.java main/native/shared/dbb.c test/org/apache/commons/runtime/TestDirectByteBuffer.java

Author: mturk
Date: Thu May 28 09:15:15 2009
New Revision: 779498

URL: http://svn.apache.org/viewvc?rev=779498&view=rev
Log:
Make some unsafe methods as private. The pubic API is unusable.

Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/DirectByteBuffer.java
    commons/sandbox/runtime/trunk/src/main/native/shared/dbb.c
    commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestDirectByteBuffer.java

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/DirectByteBuffer.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/DirectByteBuffer.java?rev=779498&r1=779497&r2=779498&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/DirectByteBuffer.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/DirectByteBuffer.java Thu May 28 09:15:15 2009
@@ -65,7 +65,9 @@
         throws OutOfMemoryError, IllegalArgumentException,
         IndexOutOfBoundsException;
 
-    private static native ByteBuffer    attach(long mem, long offset, long size)
+    private static native long addr0(ByteBuffer buf);
+
+    private static native ByteBuffer    attach0(long mem, long offset, long size)
         throws NullPointerException, IllegalArgumentException;
 
     private static native void copy0(ByteBuffer src, long srcPos, ByteBuffer dst,
@@ -251,7 +253,7 @@
         copy0(src, srcPos, dst, dstPos, length);
     }
 
-    // Package private methods
+    // Unsafe methods
     // TODO: See if those methods should be present at the first
     //       place because they can very easily crash the VM.
 
@@ -263,7 +265,7 @@
      * @throws OutOfMemoryError if memory cannot be allocated from the system.
      * @throws IllegalArgumentException if {@code size} is invalid.
      */
-    protected static ByteBuffer allocate(long size)
+    public static ByteBuffer allocate(long size)
         throws OutOfMemoryError, IllegalArgumentException
     {
         return alloc0(size);
@@ -278,7 +280,7 @@
      * @throws OutOfMemoryError if memory cannot be allocated from the system.
      * @throws IllegalArgumentException if {@code size} is invalid.
      */
-    protected static ByteBuffer allocateAndClear(long size)
+    public static ByteBuffer allocateAndClear(long size)
         throws OutOfMemoryError, IllegalArgumentException
     {
         return alloc1(size);
@@ -316,7 +318,7 @@
      * @throws IndexOutOfBoundsException if copying would cause access of
      *         data outside array bounds.
      */
-    protected static ByteBuffer[] allocate(long sizes[], int off, int len)
+    public static ByteBuffer[] allocate(long sizes[], int off, int len)
         throws NullPointerException, IllegalArgumentException,
         IndexOutOfBoundsException
     {
@@ -347,7 +349,7 @@
      * @throws IllegalArgumentException if {@code off} or {@code len}
      *         is invalid.
      */
-    protected static ByteBuffer[] allocate(long sizes[])
+    public static ByteBuffer[] allocate(long sizes[])
         throws NullPointerException, IllegalArgumentException
     {
         return alloc2(sizes, 0, sizes.length);
@@ -378,7 +380,7 @@
      * @throws IllegalArgumentException if {@code off} or {@code len}
      *         is invalid.
      */
-    protected static ByteBuffer[] allocateAndClear(long sizes[])
+    public static ByteBuffer[] allocateAndClear(long sizes[])
         throws NullPointerException, IllegalArgumentException
     {
         return alloc3(sizes, 0, sizes.length);
@@ -405,7 +407,7 @@
      * @throws IndexOutOfBoundsException if copying would cause access of
      *         data outside array bounds.
      */
-    protected static ByteBuffer[] allocateAndClear(long sizes[], int off, int len)
+    public static ByteBuffer[] allocateAndClear(long sizes[], int off, int len)
         throws NullPointerException, IllegalArgumentException,
         IndexOutOfBoundsException
     {
@@ -413,33 +415,6 @@
     }
 
     /**
-     * Allocate a new ByteBuffer from already allocated memory.
-     * <p>
-     * <b>Warning:</b><br/>
-     * Allocated memory must be provided from call to the
-     * {@code address} method or by some other valid method that returns
-     * the valid memory address.<br/>
-     * Providing invalid value to this call will almost certainly crash the
-     * running JVM because it represents the real memory address location and
-     * any operation on {@code ByteBuffer} will use it for internal storage
-     * location.
-     * </p>
-     * @param mem The memory to use
-     * @param offset Offset from the memory to use
-     * @param size The amount of memory to use
-     * @return The ByteBuffer with attached memory
-     *
-     * @throws NullPointerException if {@code mem} is zero
-     * @throws IllegalArgumentException if {@code offset} or
-     *         {@code size} are invalid.
-     */
-    protected static ByteBuffer allocate(long mem, long offset, long size)
-        throws NullPointerException, IllegalArgumentException
-    {
-        return attach(mem, offset, size);
-    }
-
-    /**
      * Allocate a new ByteBuffer from already allocated ByteBuffer.
      * @param buf The ByteBuffer who's memory to use
      * @param offset Offset from the memory to use
@@ -450,10 +425,10 @@
      * @throws IllegalArgumentException if {@code offset} or
      *         {@code size} are invalid.
      */
-    protected static ByteBuffer allocate(ByteBuffer buf, long offset, long size)
+    public static ByteBuffer allocate(ByteBuffer buf, long offset, long size)
         throws NullPointerException, IllegalArgumentException
     {
-        return attach(address(buf), offset, size);
+        return attach0(addr0(buf), offset, size);
     }
 
     /**
@@ -475,13 +450,7 @@
      *
      * @throws NullPointerException if {@code buf} is null
      */
-    protected static native void free(ByteBuffer buf)
+    public static native void free(ByteBuffer buf)
         throws NullPointerException;
 
-    /**
-     * Returns the memory address of the ByteBuffer.
-     * @param buf Previously allocated ByteBuffer.
-     */
-    protected static native long address(ByteBuffer buf);
-
 }

Modified: commons/sandbox/runtime/trunk/src/main/native/shared/dbb.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/dbb.c?rev=779498&r1=779497&r2=779498&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/dbb.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/dbb.c Thu May 28 09:15:15 2009
@@ -372,10 +372,10 @@
     return rv;
 }
 
-ACR_JNI_EXPORT_DECLARE(jobject, DirectByteBuffer, attach)(ACR_JNISTDARGS,
-                                                          jlong addr,
-                                                          jlong offset,
-                                                          jlong size)
+ACR_JNI_EXPORT_DECLARE(jobject, DirectByteBuffer, attach0)(ACR_JNISTDARGS,
+                                                           jlong addr,
+                                                           jlong offset,
+                                                           jlong size)
 {
     char *mem = J2P(addr, char *);
 
@@ -417,16 +417,8 @@
     }
 }
 
-/* XXX: Do we need 32/64 bit int/long methods? */
-ACR_JNI_EXPORT_DECLARE(jniptr, DirectByteBuffer, addressN)(ACR_JNISTDARGS,
-                                                           jobject bb)
-{
-    UNREFERENCED_O;
-    return P2N((*_E)->GetDirectBufferAddress(_E, bb));
-}
-
-ACR_JNI_EXPORT_DECLARE(jlong, DirectByteBuffer, address)(ACR_JNISTDARGS,
-                                                         jobject bb)
+ACR_JNI_EXPORT_DECLARE(jlong, DirectByteBuffer, addr0)(ACR_JNISTDARGS,
+                                                       jobject bb)
 {
     UNREFERENCED_O;
     return P2J((*_E)->GetDirectBufferAddress(_E, bb));

Modified: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestDirectByteBuffer.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestDirectByteBuffer.java?rev=779498&r1=779497&r2=779498&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestDirectByteBuffer.java (original)
+++ commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestDirectByteBuffer.java Thu May 28 09:15:15 2009
@@ -57,28 +57,6 @@
         DirectByteBuffer.free(bb);
     }
 
-    public void testAddress()
-        throws Exception
-    {
-        // bb1 and bb2 share the same memory
-        ByteBuffer bb1 = DirectByteBuffer.allocate(1000);
-        long a = DirectByteBuffer.address(bb1);
-        ByteBuffer bb2 = DirectByteBuffer.allocate(a, 0, 1000);
-        bb1.putInt(0xcafebabe);
-        assertTrue("Direct", bb2.isDirect());
-        assertEquals("Capacity", 1000, bb2.capacity());
-        assertEquals("Value", 0xcafebabe, bb2.getInt());
-        DirectByteBuffer.free(bb1);
-        /*
-         * Call to the second free crashes the JVM !
-         * Also call to the any operation on any
-         * Byte buffer will probably crash the JVM after the
-         * memory is free'd
-         * bb2.putInt(0xcafebabe);      might write to something else
-         * DirectByteBuffer.free(bb2);  will free already free'd memory
-         *                              causing core.
-         */
-    }
 
     public void testPointer()
         throws Exception
@@ -88,7 +66,6 @@
         ByteBuffer bb = DirectByteBuffer.allocate(ptr);
         assertTrue("Direct", bb.isDirect());
         assertEquals("Capacity", 1000, bb.capacity());
-        assertEquals("Address", DirectByteBuffer.address(bb), ptr.address().longValue());
         /*
          * Call to the free crashes the JVM !
          * Also call to the any operation on any
@@ -100,33 +77,6 @@
          */
     }
 
-    public void testPointerShare()
-        throws Exception
-    {
-        // ptr, bb1 and bb2 share the same memory
-        Pointer ptr = Memory.malloc(1000);
-        ByteBuffer bb1 = DirectByteBuffer.allocate(ptr, 0, 1000);
-        long a = DirectByteBuffer.address(bb1);
-        ByteBuffer bb2 = DirectByteBuffer.allocate(a, 0, 1000);
-        bb1.putInt(0xcafebabe);
-        assertTrue("Direct", bb2.isDirect());
-        assertEquals("Capacity", 1000, bb2.capacity());
-        assertEquals("Value", 0xcafebabe, bb2.getInt());
-        assertEquals("Address", a, ptr.address().longValue());
-        assertEquals("Address", DirectByteBuffer.address(bb2), ptr.address().longValue());
-        /*
-         * Call to the free crashes the JVM !
-         * Also call to the any operation on any
-         * Byte buffer will probably crash the JVM after the
-         * memory is free'd
-         * bb2.putInt(0xcafebabe);      might write to something else
-         * DirectByteBuffer.free(bb1);  will free already free'd memory
-         *                              causing core.
-         * DirectByteBuffer.free(bb2);  will free already free'd memory
-         *                              causing core.
-         */
-    }
-
     public void testMemset()
         throws Exception
     {