You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by tr...@apache.org on 2008/04/07 08:52:21 UTC

svn commit: r645397 - in /mina/sandbox/native: ./ src/main/c/ src/main/java/org/ src/main/java/org/apache/ src/main/java/org/apache/mina/ src/main/java/org/apache/mina/jni/

Author: trustin
Date: Sun Apr  6 23:52:19 2008
New Revision: 645397

URL: http://svn.apache.org/viewvc?rev=645397&view=rev
Log:
The initial prototype of JNI-based buffer allocator

Added:
    mina/sandbox/native/src/main/c/org_apache_mina_jni_ByteBufferAllocator.c   (with props)
    mina/sandbox/native/src/main/c/org_apache_mina_jni_ByteBufferAllocator.h   (with props)
    mina/sandbox/native/src/main/java/org/
    mina/sandbox/native/src/main/java/org/apache/
    mina/sandbox/native/src/main/java/org/apache/mina/
    mina/sandbox/native/src/main/java/org/apache/mina/jni/
    mina/sandbox/native/src/main/java/org/apache/mina/jni/ByteBufferAllocator.java   (with props)
    mina/sandbox/native/src/main/java/org/apache/mina/jni/ByteBufferRegion.java   (with props)
    mina/sandbox/native/src/main/java/org/apache/mina/jni/JniBufferAllocator.java   (with props)
Modified:
    mina/sandbox/native/   (props changed)
    mina/sandbox/native/pom.xml

Propchange: mina/sandbox/native/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Sun Apr  6 23:52:19 2008
@@ -0,0 +1 @@
+target

Modified: mina/sandbox/native/pom.xml
URL: http://svn.apache.org/viewvc/mina/sandbox/native/pom.xml?rev=645397&r1=645396&r2=645397&view=diff
==============================================================================
--- mina/sandbox/native/pom.xml (original)
+++ mina/sandbox/native/pom.xml Sun Apr  6 23:52:19 2008
@@ -10,7 +10,7 @@
   <name>Apache MINA Native Extension</name>
   <packaging>bundle</packaging>  
   <properties>
-    <exportedPackage>${groupId}.native</exportedPackage>
+    <exportedPackage>${groupId}.jni</exportedPackage>
   </properties>
   <dependencies>
     <dependency>

Added: mina/sandbox/native/src/main/c/org_apache_mina_jni_ByteBufferAllocator.c
URL: http://svn.apache.org/viewvc/mina/sandbox/native/src/main/c/org_apache_mina_jni_ByteBufferAllocator.c?rev=645397&view=auto
==============================================================================
--- mina/sandbox/native/src/main/c/org_apache_mina_jni_ByteBufferAllocator.c (added)
+++ mina/sandbox/native/src/main/c/org_apache_mina_jni_ByteBufferAllocator.c Sun Apr  6 23:52:19 2008
@@ -0,0 +1,43 @@
+#include <stdlib.h>
+#include "org_apache_mina_jni_ByteBufferAllocator.h"
+
+/*
+ * Class:     org_apache_mina_jni_ByteBufferAllocator
+ * Method:    _allocate
+ * Signature: (I)Ljava/nio/ByteBuffer;
+ */
+JNIEXPORT jobject JNICALL Java_org_apache_mina_jni_ByteBufferAllocator__1allocate (JNIEnv *env, jclass class, jint capacity) {
+    void* region = malloc((size_t) capacity);
+    if (region == NULL) {
+        return NULL;
+    }
+    return (*env)->NewDirectByteBuffer(env, region, capacity);
+}
+
+/*
+ * Class:     org_apache_mina_jni_ByteBufferAllocator
+ * Method:    _map
+ * Signature: (JI)Ljava/nio/ByteBuffer;
+ */
+JNIEXPORT jobject JNICALL Java_org_apache_mina_jni_ByteBufferAllocator__1map (JNIEnv *env, jclass class, jlong address, jint capacity) {
+    return (*env)->NewDirectByteBuffer(env, (void*) address, (jlong) capacity);
+}
+
+/*
+ * Class:     org_apache_mina_jni_ByteBufferAllocator
+ * Method:    _address
+ * Signature: (Ljava/nio/ByteBuffer;)J
+ */
+JNIEXPORT jlong JNICALL Java_org_apache_mina_jni_ByteBufferAllocator__1address (JNIEnv *env, jclass class, jobject buffer) {
+    return (jlong) (*env)->GetDirectBufferAddress(env, buffer);
+}
+
+/*
+ * Class:     org_apache_mina_jni_ByteBufferAllocator
+ * Method:    _deallocate
+ * Signature: (J)V
+ */
+JNIEXPORT void JNICALL Java_org_apache_mina_jni_ByteBufferAllocator__1deallocate (JNIEnv *env, jclass class, jlong address) {
+    free((void*) address);
+}
+

Propchange: mina/sandbox/native/src/main/c/org_apache_mina_jni_ByteBufferAllocator.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: mina/sandbox/native/src/main/c/org_apache_mina_jni_ByteBufferAllocator.c
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: mina/sandbox/native/src/main/c/org_apache_mina_jni_ByteBufferAllocator.h
URL: http://svn.apache.org/viewvc/mina/sandbox/native/src/main/c/org_apache_mina_jni_ByteBufferAllocator.h?rev=645397&view=auto
==============================================================================
--- mina/sandbox/native/src/main/c/org_apache_mina_jni_ByteBufferAllocator.h (added)
+++ mina/sandbox/native/src/main/c/org_apache_mina_jni_ByteBufferAllocator.h Sun Apr  6 23:52:19 2008
@@ -0,0 +1,45 @@
+/* DO NOT EDIT THIS FILE - it is machine generated */
+#include <jni.h>
+/* Header for class org_apache_mina_jni_ByteBufferAllocator */
+
+#ifndef _Included_org_apache_mina_jni_ByteBufferAllocator
+#define _Included_org_apache_mina_jni_ByteBufferAllocator
+#ifdef __cplusplus
+extern "C" {
+#endif
+/*
+ * Class:     org_apache_mina_jni_ByteBufferAllocator
+ * Method:    _allocate
+ * Signature: (I)Ljava/nio/ByteBuffer;
+ */
+JNIEXPORT jobject JNICALL Java_org_apache_mina_jni_ByteBufferAllocator__1allocate
+  (JNIEnv *, jclass, jint);
+
+/*
+ * Class:     org_apache_mina_jni_ByteBufferAllocator
+ * Method:    _map
+ * Signature: (JI)Ljava/nio/ByteBuffer;
+ */
+JNIEXPORT jobject JNICALL Java_org_apache_mina_jni_ByteBufferAllocator__1map
+  (JNIEnv *, jclass, jlong, jint);
+
+/*
+ * Class:     org_apache_mina_jni_ByteBufferAllocator
+ * Method:    _address
+ * Signature: (Ljava/nio/ByteBuffer;)J
+ */
+JNIEXPORT jlong JNICALL Java_org_apache_mina_jni_ByteBufferAllocator__1address
+  (JNIEnv *, jclass, jobject);
+
+/*
+ * Class:     org_apache_mina_jni_ByteBufferAllocator
+ * Method:    _deallocate
+ * Signature: (J)V
+ */
+JNIEXPORT void JNICALL Java_org_apache_mina_jni_ByteBufferAllocator__1deallocate
+  (JNIEnv *, jclass, jlong);
+
+#ifdef __cplusplus
+}
+#endif
+#endif

Propchange: mina/sandbox/native/src/main/c/org_apache_mina_jni_ByteBufferAllocator.h
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: mina/sandbox/native/src/main/c/org_apache_mina_jni_ByteBufferAllocator.h
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: mina/sandbox/native/src/main/java/org/apache/mina/jni/ByteBufferAllocator.java
URL: http://svn.apache.org/viewvc/mina/sandbox/native/src/main/java/org/apache/mina/jni/ByteBufferAllocator.java?rev=645397&view=auto
==============================================================================
--- mina/sandbox/native/src/main/java/org/apache/mina/jni/ByteBufferAllocator.java (added)
+++ mina/sandbox/native/src/main/java/org/apache/mina/jni/ByteBufferAllocator.java Sun Apr  6 23:52:19 2008
@@ -0,0 +1,30 @@
+package org.apache.mina.jni;
+
+import java.nio.ByteBuffer;
+
+public class ByteBufferAllocator {
+    static {
+        System.loadLibrary("mina");
+    }
+
+    public static ByteBuffer allocate(int capacity) {
+        ByteBuffer buffer = _allocate(capacity);
+        if (buffer == null) {
+            throw new OutOfMemoryError("Insufficient system memory");
+        }
+
+        final long address = _address(buffer);
+
+        sun.misc.Cleaner.create(buffer, new Runnable() {
+            public void run() {
+                _deallocate(address);
+            }
+        });
+        return buffer;
+    }
+
+    private static native ByteBuffer _allocate(int capacity);
+    private static native ByteBuffer _map(long address, int capacity);
+    private static native long _address(ByteBuffer buffer);
+    private static native void _deallocate(long address);
+}

Propchange: mina/sandbox/native/src/main/java/org/apache/mina/jni/ByteBufferAllocator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: mina/sandbox/native/src/main/java/org/apache/mina/jni/ByteBufferAllocator.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: mina/sandbox/native/src/main/java/org/apache/mina/jni/ByteBufferRegion.java
URL: http://svn.apache.org/viewvc/mina/sandbox/native/src/main/java/org/apache/mina/jni/ByteBufferRegion.java?rev=645397&view=auto
==============================================================================
--- mina/sandbox/native/src/main/java/org/apache/mina/jni/ByteBufferRegion.java (added)
+++ mina/sandbox/native/src/main/java/org/apache/mina/jni/ByteBufferRegion.java Sun Apr  6 23:52:19 2008
@@ -0,0 +1,38 @@
+package org.apache.mina.jni;
+
+public class ByteBufferRegion {
+    private final long address;
+    private final int capacity;
+
+    public ByteBufferRegion(long address, int capacity) {
+        this.address = address;
+        this.capacity = capacity;
+    }
+
+    public long getAddress() {
+        return address;
+    }
+
+    public int getCapacity() {
+        return capacity;
+    }
+
+    @Override
+    public int hashCode() {
+        return (int) address;
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (o instanceof ByteBufferRegion) {
+            ByteBufferRegion that = (ByteBufferRegion) o;
+            return address == that.address;
+        }
+        return false;
+    }
+
+    @Override
+    public String toString() {
+        return "(" + address + ", " + capacity + ')';
+    }
+}

Propchange: mina/sandbox/native/src/main/java/org/apache/mina/jni/ByteBufferRegion.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: mina/sandbox/native/src/main/java/org/apache/mina/jni/ByteBufferRegion.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: mina/sandbox/native/src/main/java/org/apache/mina/jni/JniBufferAllocator.java
URL: http://svn.apache.org/viewvc/mina/sandbox/native/src/main/java/org/apache/mina/jni/JniBufferAllocator.java?rev=645397&view=auto
==============================================================================
--- mina/sandbox/native/src/main/java/org/apache/mina/jni/JniBufferAllocator.java (added)
+++ mina/sandbox/native/src/main/java/org/apache/mina/jni/JniBufferAllocator.java Sun Apr  6 23:52:19 2008
@@ -0,0 +1,12 @@
+package org.apache.mina.jni;
+
+import java.nio.ByteBuffer;
+
+import org.apache.mina.common.SimpleBufferAllocator;
+
+public class JniBufferAllocator extends SimpleBufferAllocator {
+    @Override
+    public ByteBuffer allocateNioBuffer(int capacity, boolean direct) {
+        return ByteBufferAllocator.allocate(capacity);
+    }
+}

Propchange: mina/sandbox/native/src/main/java/org/apache/mina/jni/JniBufferAllocator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: mina/sandbox/native/src/main/java/org/apache/mina/jni/JniBufferAllocator.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date