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 2011/10/18 09:56:24 UTC
svn commit: r1185520 - in /commons/sandbox/runtime/trunk/src/main:
native/include/acr/ native/modules/openssl/ native/os/unix/
native/os/win32/ native/shared/ test/org/apache/commons/runtime/
Author: mturk
Date: Tue Oct 18 07:56:24 2011
New Revision: 1185520
URL: http://svn.apache.org/viewvc?rev=1185520&view=rev
Log:
Add fast NIO Buffer access using Unsafe and object field offsets
Modified:
commons/sandbox/runtime/trunk/src/main/native/include/acr/misc.h
commons/sandbox/runtime/trunk/src/main/native/modules/openssl/rand.c
commons/sandbox/runtime/trunk/src/main/native/os/unix/sockstream.c
commons/sandbox/runtime/trunk/src/main/native/os/win32/ipcsstream.c
commons/sandbox/runtime/trunk/src/main/native/shared/bzip2.c
commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c
commons/sandbox/runtime/trunk/src/main/native/shared/directbb.c
commons/sandbox/runtime/trunk/src/main/native/shared/memstream.c
commons/sandbox/runtime/trunk/src/main/native/shared/zlib.c
commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestDirectByteBuffer.java
Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr/misc.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr/misc.h?rev=1185520&r1=1185519&r2=1185520&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr/misc.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr/misc.h Tue Oct 18 07:56:24 2011
@@ -26,6 +26,8 @@ ACR_CLASS_CTOR(ArrayList);
ACR_CLASS_DTOR(ArrayList);
ACR_CLASS_CTOR(HashSet);
ACR_CLASS_DTOR(HashSet);
+ACR_CLASS_CTOR(Buffer);
+ACR_CLASS_DTOR(Buffer);
int AcrArrayListAdd(JNI_STDARGS, jobject e);
int AcrArrayListEnsureCapacity(JNI_STDARGS, jint size);
@@ -35,6 +37,9 @@ int AcrHashSetAdd(JNI_STDARGS, jobje
void AcrLibLockAcquire(void);
void AcrLibLockRelease(void);
+void *AcrGetDirectBuffer(JNI_STDARGS);
+void *AcrGetDirectBufferEx(JNI_STDARGS, jint *len);
+
#ifdef __cplusplus
}
#endif
Modified: commons/sandbox/runtime/trunk/src/main/native/modules/openssl/rand.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/modules/openssl/rand.c?rev=1185520&r1=1185519&r2=1185520&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/modules/openssl/rand.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/modules/openssl/rand.c Tue Oct 18 07:56:24 2011
@@ -168,7 +168,7 @@ ACR_SSL_EXPORT(void, SSLRandom, bytes0)(
ACR_SSL_EXPORT(void, SSLRandom, bytes1)(JNI_STDARGS, jobject bb,
jint off, jint len)
{
- unsigned char *sb = (*env)->GetDirectBufferAddress(env, bb);
+ unsigned char *sb = AcrGetDirectBuffer(env, bb);
if (sb != 0)
RAND_bytes(sb + off, len);
Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/sockstream.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/sockstream.c?rev=1185520&r1=1185519&r2=1185520&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/sockstream.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/sockstream.c Tue Oct 18 07:56:24 2011
@@ -19,6 +19,7 @@
#include "acr/clazz.h"
#include "acr/string.h"
#include "acr/memory.h"
+#include "acr/misc.h"
#include "acr/iofd.h"
#include "acr/netapi.h"
#include "acr/unsafe.h"
@@ -328,7 +329,7 @@ ACR_NET_EXPORT(jint, SocketStream, read3
}
if (ACR_HASFLAG(ss->fd, ACR_SO_RDEOF))
goto finally;
- bb = (char *)(*env)->GetDirectBufferAddress(env, buf);
+ bb = AcrGetDirectBuffer(env, buf);
if (bb == 0) {
rc = ACR_EINVAL;
goto finally;
@@ -504,7 +505,7 @@ ACR_NET_EXPORT(jint, SocketStream, write
rc = ACR_EBADF;
goto finally;
}
- bb = (char *)(*env)->GetDirectBufferAddress(env, buf);
+ bb = AcrGetDirectBuffer(env, buf);
if (bb == 0) {
rc = ACR_EINVAL;
goto finally;
@@ -652,6 +653,7 @@ ACR_NET_EXPORT(jlong, SocketStream, writ
}
for (i = 0; i < len; i++) {
+ jint bs = 0;
jobject bb = (*env)->GetObjectArrayElement(env, vec, (jsize)(i + off));
if (bb == 0) {
/* XXX: What's the correct error ?
@@ -659,9 +661,8 @@ ACR_NET_EXPORT(jlong, SocketStream, writ
rc = ACR_EINVAL;
goto finally;
}
- iov[i].iov_len = (size_t)(*env)->GetDirectBufferCapacity(env, bb);
- iov[i].iov_base = (*env)->GetDirectBufferAddress(env, bb);
-
+ iov[i].iov_base = AcrGetDirectBufferEx(env, bb, &bs);
+ iov[i].iov_len = bs;
(*env)->DeleteLocalRef(env, bb);
}
if (ACR_HASFLAG(ss->fd, ACR_SO_WPART)) {
Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/ipcsstream.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/ipcsstream.c?rev=1185520&r1=1185519&r2=1185520&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/ipcsstream.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/ipcsstream.c Tue Oct 18 07:56:24 2011
@@ -19,6 +19,7 @@
#include "acr/clazz.h"
#include "acr/string.h"
#include "acr/memory.h"
+#include "acr/misc.h"
#include "acr/iofd.h"
#include "acr/netapi.h"
#include "acr/unsafe.h"
@@ -261,7 +262,7 @@ ACR_NET_EXPORT(jint, IpcStream, read3)(J
}
if (ACR_HASFLAG(ss->sd, ACR_SO_RDEOF))
goto finally;
- bb = (char *)(*env)->GetDirectBufferAddress(env, buf);
+ bb = AcrGetDirectBuffer(env, buf);
if (bb == 0) {
rc = ACR_EINVAL;
goto finally;
@@ -399,7 +400,7 @@ ACR_NET_EXPORT(jint, IpcStream, write3)(
rc = ACR_EBADF;
goto finally;
}
- bb = (char *)(*env)->GetDirectBufferAddress(env, buf);
+ bb = AcrGetDirectBuffer(env, buf);
if (bb == 0) {
rc = ACR_EINVAL;
goto finally;
@@ -535,6 +536,7 @@ ACR_NET_EXPORT(jlong, IpcStream, write5)
iov = onstack;
}
for (i = 0; i < len; i++) {
+ jint bs = 0;
jobject bb = (*env)->GetObjectArrayElement(env, vec, (jsize)(i + off));
if (bb == 0) {
/* XXX: What's the correct error ?
@@ -542,8 +544,8 @@ ACR_NET_EXPORT(jlong, IpcStream, write5)
rc = ACR_EINVAL;
goto finally;
}
- iov[i].len = (u_long)(*env)->GetDirectBufferCapacity(env, bb);
- iov[i].buf = (char *)(*env)->GetDirectBufferAddress(env, bb);
+ iov[i].buf = AcrGetDirectBufferEx(env, bb, &bs);
+ iov[i].len = bs;
siz += iov[i].len;
(*env)->DeleteLocalRef(env, bb);
}
Modified: commons/sandbox/runtime/trunk/src/main/native/shared/bzip2.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/bzip2.c?rev=1185520&r1=1185519&r2=1185520&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/bzip2.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/bzip2.c Tue Oct 18 07:56:24 2011
@@ -18,6 +18,7 @@
#include "acr/jniapi.h"
#include "acr/memory.h"
#include "acr/string.h"
+#include "acr/misc.h"
#include "bzip2/bzlib.h"
@@ -110,11 +111,11 @@ ACR_BZIP2_EXPORT(jint, Bzip2, deflate1)(
int rc;
dstLen = dlen;
- if ((scp = (char *)(*env)->GetDirectBufferAddress(env, src)) == 0) {
+ if ((scp = AcrGetDirectBuffer(env, src)) == 0) {
ACR_THROW(ACR_EX_ENULL, 0);
return 0;
}
- if ((dcp = (char *)(*env)->GetDirectBufferAddress(env, dst)) == 0) {
+ if ((dcp = AcrGetDirectBuffer(env, dst)) == 0) {
ACR_THROW(ACR_EX_ENULL, 0);
return 0;
}
@@ -200,11 +201,11 @@ ACR_BZIP2_EXPORT(jint, Bzip2, inflate1)(
int rc;
dstLen = dlen;
- if ((scp = (char *)(*env)->GetDirectBufferAddress(env, src)) == 0) {
+ if ((scp = AcrGetDirectBuffer(env, src)) == 0) {
ACR_THROW(ACR_EX_ENULL, 0);
return 0;
}
- if ((dcp = (char *)(*env)->GetDirectBufferAddress(env, dst)) == 0) {
+ if ((dcp = AcrGetDirectBuffer(env, dst)) == 0) {
ACR_THROW(ACR_EX_ENULL, 0);
return 0;
}
@@ -314,7 +315,7 @@ ACR_BZIP2_EXPORT(jint, Bzip2Impl, setInp
acr_bzstream *s = J2P(stream, acr_bzstream *);
ACR_MFREE(s->next_array);
- s->next_data = (*env)->GetDirectBufferAddress(env, src);
+ s->next_data = AcrGetDirectBuffer(env, src);
if (s->next_data == 0)
return BZ_PARAM_ERROR;
s->bz.next_in = s->next_data + off;
@@ -490,7 +491,7 @@ ACR_BZIP2_EXPORT(jint, Bzip2Deflater, de
if (s->eos)
return -1;
- next_out = (*env)->GetDirectBufferAddress(env, buf);
+ next_out = AcrGetDirectBuffer(env, buf);
if (next_out == 0) {
ACR_THROW(ACR_EX_ENULL, 0);
return -1;
@@ -605,7 +606,7 @@ ACR_BZIP2_EXPORT(jint, Bzip2Inflater, in
if (s->eos)
return -1;
- next_out = (*env)->GetDirectBufferAddress(env, buf);
+ next_out = AcrGetDirectBuffer(env, buf);
if (next_out == 0) {
ACR_THROW(ACR_EX_ENULL, 0);
return -1;
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=1185520&r1=1185519&r2=1185520&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c Tue Oct 18 07:56:24 2011
@@ -231,6 +231,7 @@ AcrLoadRuntimeClasses(JNI_STDENV)
ACR_CLASS_LOAD(HeapPointer);
ACR_CLASS_LOAD(SlicePointer);
ACR_CLASS_LOAD(FileDescriptor);
+ ACR_CLASS_LOAD(Buffer);
ACR_CLASS_LOAD(Group);
ACR_CLASS_LOAD(User);
@@ -249,6 +250,7 @@ AcrUnloadRuntimeClasses(JNI_STDENV)
ACR_CLASS_UNLOAD(ConstPointer);
ACR_CLASS_UNLOAD(HeapPointer);
ACR_CLASS_UNLOAD(SlicePointer);
+ ACR_CLASS_UNLOAD(Buffer);
ACR_CLASS_UNLOAD(FileDescriptor);
ACR_CLASS_UNLOAD(Callback);
ACR_CLASS_UNLOAD(HashSet);
Modified: commons/sandbox/runtime/trunk/src/main/native/shared/directbb.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/directbb.c?rev=1185520&r1=1185519&r2=1185520&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/directbb.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/directbb.c Tue Oct 18 07:56:24 2011
@@ -21,6 +21,102 @@
#include "acr/memory.h"
#include "acr/clazz.h"
+#include "acr/misc.h"
+#include "acr/unsafe.h"
+
+J_DECLARE_CLAZZ = {
+ INVALID_FIELD_OFFSET,
+ 0,
+ 0,
+ 0,
+ "java/nio/Buffer"
+};
+
+J_DECLARE_F_ID(0000) = {
+ INVALID_FIELD_OFFSET,
+ INVALID_FIELD_OFFSET,
+ 0,
+ "capacity",
+ "I"
+};
+
+J_DECLARE_F_ID(0001) = {
+ INVALID_FIELD_OFFSET,
+ INVALID_FIELD_OFFSET,
+ 0,
+ "address",
+ "J"
+};
+
+ACR_CLASS_CTOR(Buffer)
+{
+ if (AcrLoadClass(env, &_clazzn, 0) == JNI_FALSE)
+ return JNI_FALSE;
+ J_LOAD_IFIELD(0000);
+ J_LOAD_IFIELD(0001);
+ UNSAFE_IFIELD(0000);
+ UNSAFE_IFIELD(0001);
+ _clazzn.u = 1;
+ return JNI_TRUE;
+}
+
+ACR_CLASS_DTOR(Buffer)
+{
+ AcrUnloadClass(env, &_clazzn);
+}
+
+void *AcrGetDirectBuffer(JNI_STDARGS)
+{
+ void *ptr = 0;
+
+ if (obj == 0)
+ return 0;
+ if (J4FLD_OFF(0001) != INVALID_FIELD_OFFSET) {
+ char *oa = *(char **)obj;
+ if (oa != 0) {
+ char *fa = (oa + J4FLD_PTR(0001));
+ ptr = J2P(*((jlong *)fa), void *);
+ }
+ }
+ else {
+ /* Use JNI call */
+ ptr = (*env)->GetDirectBufferAddress(env, obj);
+ }
+ return ptr;
+}
+
+void *AcrGetDirectBufferEx(JNI_STDARGS, jint *len)
+{
+ void *ptr = 0;
+
+ if (obj == 0)
+ return 0;
+ if (J4FLD_OFF(0001) != INVALID_FIELD_OFFSET) {
+ char *oa = *(char **)obj;
+ if (oa != 0) {
+ char *fa = (oa + J4FLD_PTR(0001));
+ ptr = J2P(*((jlong *)fa), void *);
+ }
+ }
+ else {
+ /* Use JNI call */
+ ptr = (*env)->GetDirectBufferAddress(env, obj);
+ }
+ if (ptr == 0 || len == 0)
+ return ptr;
+ if (J4FLD_OFF(0000) != INVALID_FIELD_OFFSET) {
+ char *oa = *(char **)obj;
+ if (oa != 0) {
+ char *ca = (oa + J4FLD_PTR(0000));
+ *len = *((jint *)ca);
+ }
+ }
+ else {
+ /* Use JNI call */
+ *len = (*env)->GetDirectBufferCapacity(env, obj);
+ }
+ return ptr;
+}
/**
* DirectByteBuffer utilities
@@ -73,7 +169,7 @@ ACR_JNI_EXPORT(void, DirectByteBuffer, f
{
void *mem;
- if (bb != 0 && (mem = (*env)->GetDirectBufferAddress(env, bb)) != 0) {
+ if ((mem = AcrGetDirectBuffer(env, bb)) != 0) {
/* This can cause core dump if address was
* allocated using different method.
* Make sure you call Native.enableExceptionHandler() to
@@ -88,7 +184,7 @@ ACR_JNI_EXPORT(void, DirectByteBuffer, f
ACR_JNI_EXPORT(jlong, DirectByteBuffer, addr0)(JNI_STDARGS, jobject bb)
{
- return P2J((*env)->GetDirectBufferAddress(env, bb));
+ return P2J(AcrGetDirectBuffer(env, bb));
}
ACR_JNI_EXPORT(void, DirectByteBuffer, set0)(JNI_STDARGS,
@@ -97,7 +193,7 @@ ACR_JNI_EXPORT(void, DirectByteBuffer, s
jint count)
{
void *m;
- if ((m = (*env)->GetDirectBufferAddress(env, bb))) {
+ if ((m = AcrGetDirectBuffer(env, bb)) != 0) {
memset(m, c, count);
}
else {
@@ -115,15 +211,32 @@ ACR_JNI_EXPORT(void, DirectByteBuffer, c
char *d;
char *s;
- s = (char *)(*env)->GetDirectBufferAddress(env, srcb);
+ s = (char *)AcrGetDirectBuffer(env, srcb);
if (s == 0 ) {
ACR_THROW(ACR_EX_ENULL, 0);
return;
}
- d = (char *)(*env)->GetDirectBufferAddress(env, dstb);
+ d = (char *)AcrGetDirectBuffer(env, dstb);
if (d == 0 ) {
ACR_THROW(ACR_EX_ENULL, 0);
return;
}
memcpy(d + (intptr_t)dsto, s + (intptr_t)srco, count);
}
+
+#if defined(ENABLE_TEST_PRIVATE)
+ACR_JNI_EXPORT(jboolean, TestDirectByteBuffer, test0)(JNI_STDARGS, jobject o, jlong adr, jint len)
+{
+ jint siz = 0;
+ void *ba;
+ void *oa = J2P(adr, void *);
+
+ ba = AcrGetDirectBufferEx(env, o, &siz);
+ if (siz == len && ba == oa)
+ return JNI_TRUE;
+ else
+ return JNI_FALSE;
+
+
+}
+#endif
\ No newline at end of file
Modified: commons/sandbox/runtime/trunk/src/main/native/shared/memstream.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/memstream.c?rev=1185520&r1=1185519&r2=1185520&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/memstream.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/memstream.c Tue Oct 18 07:56:24 2011
@@ -17,6 +17,7 @@
#include "acr/memory.h"
#include "acr/pointer.h"
#include "acr/clazz.h"
+#include "acr/misc.h"
#include "acr/sbuf.h"
ACR_IO_EXPORT(jlong, MemoryStream, alloc0)(JNI_STDARGS, jint len)
@@ -218,7 +219,7 @@ ACR_IO_EXPORT(jint, MemoryStream, read3)
char *bb = 0;
acr_sb_t *sb = J2P(sh, acr_sb_t *);
- bb = (char *)(*env)->GetDirectBufferAddress(env, buf);
+ bb = (char *)AcrGetDirectBuffer(env, buf);
if (bb != 0) {
if (pos + len > sb->s_len)
rd = sb->s_len - pos;
@@ -300,7 +301,7 @@ ACR_IO_EXPORT(jint, MemoryStream, write3
char *bb = 0;
acr_sb_t *sb = J2P(sh, acr_sb_t *);
- bb = (char *)(*env)->GetDirectBufferAddress(env, buf);
+ bb = (char *)AcrGetDirectBuffer(env, buf);
if (bb == 0)
rc = ACR_EINVAL;
else
@@ -368,8 +369,7 @@ ACR_IO_EXPORT(jint, MemoryStream, write5
rc = ACR_EINVAL;
break;
}
- blen = (int)(*env)->GetDirectBufferCapacity(env, ba);
- bb = (*env)->GetDirectBufferAddress(env, ba);
+ bb = AcrGetDirectBufferEx(env, ba, &blen);
rc = AcrSbCatb(sb, bb, blen);
(*env)->DeleteLocalRef(env, ba);
if (rc != 0)
Modified: commons/sandbox/runtime/trunk/src/main/native/shared/zlib.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/zlib.c?rev=1185520&r1=1185519&r2=1185520&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/zlib.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/zlib.c Tue Oct 18 07:56:24 2011
@@ -18,6 +18,7 @@
#include "acr/jniapi.h"
#include "acr/memory.h"
#include "acr/string.h"
+#include "acr/misc.h"
#include "zlib/zlib.h"
@@ -88,11 +89,11 @@ ACR_ZLIB_EXPORT(jlong, Zlib, deflate1)(J
int rc;
dstLen = dlen;
- if ((scp = (Bytef *)(*env)->GetDirectBufferAddress(env, src)) == 0) {
+ if ((scp = AcrGetDirectBuffer(env, src)) == 0) {
ACR_THROW(ACR_EX_ENULL, 0);
return 0;
}
- if ((dcp = (Bytef *)(*env)->GetDirectBufferAddress(env, dst)) == 0) {
+ if ((dcp = AcrGetDirectBuffer(env, dst)) == 0) {
ACR_THROW(ACR_EX_ENULL, 0);
return 0;
}
@@ -168,11 +169,11 @@ ACR_ZLIB_EXPORT(jint, Zlib, inflate1)(JN
int rc;
dstLen = dlen;
- if ((scp = (Bytef *)(*env)->GetDirectBufferAddress(env, src)) == 0) {
+ if ((scp = AcrGetDirectBuffer(env, src)) == 0) {
ACR_THROW(ACR_EX_ENULL, 0);
return 0;
}
- if ((dcp = (Bytef *)(*env)->GetDirectBufferAddress(env, dst)) == 0) {
+ if ((dcp = AcrGetDirectBuffer(env, dst)) == 0) {
ACR_THROW(ACR_EX_ENULL, 0);
return 0;
}
@@ -286,7 +287,7 @@ ACR_ZLIB_EXPORT(jint, ZlibImpl, setInput
acr_zstream *s = J2P(stream, acr_zstream *);
ACR_MFREE(s->next_array);
- s->next_data = (*env)->GetDirectBufferAddress(env, src);
+ s->next_data = AcrGetDirectBuffer(env, src);
if (s->next_data == 0)
return Z_BUF_ERROR;
s->next_buff = s->next_data + off;
@@ -495,7 +496,7 @@ ACR_ZLIB_EXPORT(jint, ZlibDeflater, defl
if (s->eos)
return -1;
- next_out = (*env)->GetDirectBufferAddress(env, buf);
+ next_out = AcrGetDirectBuffer(env, buf);
if (next_out == 0) {
ACR_THROW(ACR_EX_ENULL, 0);
return -1;
@@ -632,7 +633,7 @@ ACR_ZLIB_EXPORT(jint, ZlibInflater, infl
if (s->eos)
return -1;
- next_out = (*env)->GetDirectBufferAddress(env, buf);
+ next_out = AcrGetDirectBuffer(env, buf);
if (next_out == 0) {
ACR_THROW(ACR_EX_ENULL, 0);
return -1;
Modified: commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestDirectByteBuffer.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestDirectByteBuffer.java?rev=1185520&r1=1185519&r2=1185520&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestDirectByteBuffer.java (original)
+++ commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestDirectByteBuffer.java Tue Oct 18 07:56:24 2011
@@ -26,6 +26,8 @@ import java.nio.ByteBuffer;
public class TestDirectByteBuffer extends Assert
{
+ private static native boolean test0(ByteBuffer bb, long addr, int len);
+
public void mallocDirectByteBuffer()
throws Exception
{
@@ -114,4 +116,20 @@ public class TestDirectByteBuffer extend
DirectByteBuffer.free(db);
}
+ public void checkUnsafeAccess()
+ throws Exception
+ {
+
+ Pointer ptr = Memory.malloc(1000);
+ ByteBuffer bb = DirectByteBuffer.allocate(ptr);
+ assertTrue(bb.isDirect());
+ assertEquals(bb.capacity(), 1000);
+ // Check if Unsafe filed offset works
+ try {
+ assertTrue(test0(bb, ptr.address(), 1000));
+ } catch (UnsatisfiedLinkError x) {
+ // Not compiled in. Ignore
+ }
+ }
+
}