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/07/02 17:08:02 UTC
svn commit: r790616 - in /commons/sandbox/runtime/trunk/src:
main/java/org/apache/commons/runtime/ main/native/include/
main/native/shared/ main/native/test/ test/org/apache/commons/runtime/
Author: mturk
Date: Thu Jul 2 15:08:02 2009
New Revision: 790616
URL: http://svn.apache.org/viewvc?rev=790616&view=rev
Log:
Add DirectBuffer classes
Added:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/DirectBuffer.java (with props)
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/DirectBuffer32.java (with props)
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/DirectBuffer64.java (with props)
Modified:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer32.java
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer64.java
commons/sandbox/runtime/trunk/src/main/native/include/acr_pointer.h
commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c
commons/sandbox/runtime/trunk/src/main/native/test/testcase.c
commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestPrivate.java
Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/DirectBuffer.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/DirectBuffer.java?rev=790616&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/DirectBuffer.java (added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/DirectBuffer.java Thu Jul 2 15:08:02 2009
@@ -0,0 +1,45 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.runtime;
+
+import org.apache.commons.runtime.util.Utils;
+
+/**
+ * Represents the {@code Pointer} as buffer.
+ *
+ * @since Runtime 1.0
+ */
+public interface DirectBuffer {
+
+ /**
+ * Cast {@code this} buffer to {@link Pointer Pointer}.
+ *
+ * @return Pointer
+ */
+ public Pointer asPointer();
+
+ /**
+ * Free the allocated resource by the Operating system.
+ *
+ * @see Pointer#free()
+ * @throws Throwable the {@code Exception} raised by this method.
+ */
+ public void free()
+ throws Throwable;
+
+}
+
Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/DirectBuffer.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/DirectBuffer32.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/DirectBuffer32.java?rev=790616&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/DirectBuffer32.java (added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/DirectBuffer32.java Thu Jul 2 15:08:02 2009
@@ -0,0 +1,49 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.runtime;
+
+import org.apache.commons.runtime.util.Utils;
+
+/** Represents the Operating System 32-bit pointer C/C++ pointer.
+ * <p>
+ * <b>Warning:</b><br/>Using this class improperly may crash the running JVM.
+ * </p>
+ * @since Runtime 1.0
+ */
+class DirectBuffer32 extends Pointer32 implements DirectBuffer {
+
+ protected DirectBuffer32()
+ {
+ }
+
+ /*
+ * Only created from JNI code.
+ */
+ private DirectBuffer32(int ptr, int clr, int len)
+ {
+ POINTER = ptr;
+ CLEANUP = clr;
+ PLENGTH = len;
+ }
+
+ public Pointer asPointer()
+ {
+ return this;
+ }
+
+}
+
Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/DirectBuffer32.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/DirectBuffer64.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/DirectBuffer64.java?rev=790616&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/DirectBuffer64.java (added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/DirectBuffer64.java Thu Jul 2 15:08:02 2009
@@ -0,0 +1,49 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.runtime;
+
+import org.apache.commons.runtime.util.Utils;
+
+/** Represents the Operating System 64-bit pointer C/C++ pointer.
+ * <p>
+ * <b>Warning:</b><br/>Using this class improperly may crash the running JVM.
+ * </p>
+ * @since Runtime 1.0
+ */
+class DirectBuffer64 extends Pointer64 implements DirectBuffer {
+
+ protected DirectBuffer64()
+ {
+ }
+
+ /*
+ * Only created from JNI code.
+ */
+ private DirectBuffer64(long ptr, long clr, long len)
+ {
+ POINTER = ptr;
+ CLEANUP = clr;
+ PLENGTH = len;
+ }
+
+ public Pointer asPointer()
+ {
+ return this;
+ }
+
+}
+
Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/DirectBuffer64.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer32.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer32.java?rev=790616&r1=790615&r2=790616&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer32.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer32.java Thu Jul 2 15:08:02 2009
@@ -26,7 +26,7 @@
*/
class Pointer32 extends Pointer {
- private int CLEANUP;
+ protected int CLEANUP;
protected int POINTER;
protected int PLENGTH;
Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer64.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer64.java?rev=790616&r1=790615&r2=790616&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer64.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer64.java Thu Jul 2 15:08:02 2009
@@ -26,7 +26,7 @@
*/
class Pointer64 extends Pointer {
- private long CLEANUP;
+ protected long CLEANUP;
protected long POINTER;
protected long PLENGTH;
Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr_pointer.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_pointer.h?rev=790616&r1=790615&r2=790616&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr_pointer.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr_pointer.h Thu Jul 2 15:08:02 2009
@@ -84,6 +84,16 @@
ACR_DECLARE(int) ACR_PointerSet(JNIEnv *env, jobject ptr, void *val,
size_t len);
+/**
+ * Create new DirectBuffer class instance
+ * @param env Current JNI environment
+ * @param p Native pointer to wrap into Pointer class
+ * @param len Length of the pointer.
+ * @param cb callback function to use on Pointer.finalize()
+ */
+ACR_DECLARE(jobject) ACR_DirectBufferCreate(JNIEnv *_E, void *p, size_t len,
+ acr_pointer_cleanup_fn_t *cb);
+
#ifdef __cplusplus
}
#endif
Modified: commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c?rev=790616&r1=790615&r2=790616&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c Thu Jul 2 15:08:02 2009
@@ -372,6 +372,7 @@
/* Forward class loading declarations */
ACR_CLASS_LDEC(Descriptor);
ACR_CLASS_LDEC(Pointer);
+ACR_CLASS_LDEC(DirectBuffer);
ACR_CLASS_LDEC(MbString);
ACR_CLASS_LDEC(Group);
ACR_CLASS_LDEC(User);
@@ -382,6 +383,7 @@
ACR_CLASS_LRUN(Descriptor);
ACR_CLASS_LRUN(Pointer);
+ ACR_CLASS_LRUN(DirectBuffer);
ACR_CLASS_LRUN(MbString);
ACR_CLASS_LRUN(Group);
ACR_CLASS_LRUN(User);
@@ -398,6 +400,7 @@
ACR_CLASS_URUN(Descriptor);
ACR_CLASS_URUN(Pointer);
+ ACR_CLASS_URUN(DirectBuffer);
ACR_CLASS_URUN(MbString);
ACR_CLASS_URUN(Group);
ACR_CLASS_URUN(User);
Modified: commons/sandbox/runtime/trunk/src/main/native/test/testcase.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/test/testcase.c?rev=790616&r1=790615&r2=790616&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/test/testcase.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/test/testcase.c Thu Jul 2 15:08:02 2009
@@ -220,12 +220,25 @@
return 0;
}
+static int dbbcallback(void *p, size_t s)
+{
+ fprintf(stderr, "[native] DirectBuffer callback called: %p - %d\n", p, s);
+ fflush(stderr);
+ return 0;
+}
+
ACR_JNI_EXPORT_DECLARE(jobject, TestPrivate, test017)(ACR_JNISTDARGS, jint d)
{
return ACR_PointerCreate(_E, I2P(d, void *), 0, callback);
}
+ACR_JNI_EXPORT_DECLARE(jobject, TestPrivate, test038)(ACR_JNISTDARGS, jint d)
+{
+
+ return ACR_DirectBufferCreate(_E, I2P(d, void *), 0, dbbcallback);
+}
+
ACR_JNI_EXPORT_DECLARE(jint, TestPrivate, test018)(ACR_JNISTDARGS, jobject p)
{
Modified: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestPrivate.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestPrivate.java?rev=790616&r1=790615&r2=790616&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestPrivate.java (original)
+++ commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestPrivate.java Thu Jul 2 15:08:02 2009
@@ -63,6 +63,7 @@
// Pointer
private static native int test016(int d);
private static native Pointer test017(int d);
+ private static native DirectBuffer test038(int d);
private static native int test018(Pointer p);
private static native int test019(Pointer p);
@@ -296,6 +297,19 @@
Thread.sleep(200);
}
+ public void testDirectBufferCb()
+ throws Throwable
+ {
+ DirectBuffer b = test038(0xcafebabe);
+ assertNotNull("DirectBuffer", b);
+ b.free();
+ b = null;
+ System.gc();
+ // This should be enough for a gc
+ // from Pointer.finalize()
+ Thread.sleep(200);
+ }
+
public void testPointerGc()
throws Throwable
{
@@ -616,11 +630,11 @@
assertNotNull("Descriptor", d);
assertEquals("Type", DescriptorType.SOCKET, d.getType());
d.close();
- int r = test037(d, 1964, 0xcafebabe);
+ int r = test037(d, 1964, 0xcafebabe);
assertEquals("Fd", d.fd(), 1964);
assertEquals("Type", DescriptorType.FILE, d.getType());
- d.close();
- d = null;
+ d.close();
+ d = null;
System.gc();
// This should be enough for a gc
// from Pointer.finalize()