From 8f8aa3f73e3c9804c4a86f5d4fdac257d50d831a Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Wed, 27 Apr 2011 19:47:43 +0200 Subject: NativeBuffer/PointerBuffer API/Impl Change (remove explicit backup array, alloc referenced data map if used only) This patch doesn't impact GlueGen's code generation, but enhance and fix PointerBuffer usage only. remove explicit backup array As suggested by Michael Bien with a proposed patch, PointerBuffer's backup array is not only redundant in case it's not used, but also erroneous - due to possible sliced buffers. Removes the explicit backup array implementation leaving it up to the user, ie how PointerBuffer is created (alloc/allocDirect) and use the underlying nio's buffer backup array, if available. This also fixes the (never tested) case of indirect w/ backup array usage on 32bit platform size. In this case the array shall be of type int[], holding 32bit pointer - on 64bit long[]. Previous to this patch, it was always long[]. Added more thorough tests of PointerBuffer, notably indirect w/ backup array and native deep copy and filling of a pointer array. alloc referenced data map if used only As suggested by Michael Bien with a proposed patch, the allocation of the dataMap hash map is redundant in case it's not used. The hash map will be initialized lazy, if needed only. --- .../gluegen/test/junit/generation/BaseClass.java | 176 +++++++++++++++++---- .../test/junit/generation/Test1p1JavaEmitter.java | 35 +++- .../generation/Test1p2ProcAddressEmitter.java | 44 ++++-- .../jogamp/gluegen/test/junit/generation/test1.c | 25 +++ .../jogamp/gluegen/test/junit/generation/test1.h | 6 + 5 files changed, 235 insertions(+), 51 deletions(-) (limited to 'src/junit/com/jogamp/gluegen/test') diff --git a/src/junit/com/jogamp/gluegen/test/junit/generation/BaseClass.java b/src/junit/com/jogamp/gluegen/test/junit/generation/BaseClass.java index 30ef6fd..c57a288 100644 --- a/src/junit/com/jogamp/gluegen/test/junit/generation/BaseClass.java +++ b/src/junit/com/jogamp/gluegen/test/junit/generation/BaseClass.java @@ -32,6 +32,7 @@ import com.jogamp.common.nio.Buffers; import com.jogamp.common.nio.PointerBuffer; import com.jogamp.common.os.Platform; import java.nio.ByteBuffer; +import java.nio.ByteOrder; import java.nio.IntBuffer; import java.nio.LongBuffer; @@ -70,7 +71,6 @@ public class BaseClass { */ public void chapter__TestCoverageSignature(Bindingtest1 binding) throws Exception { int i; - long result; long context = 0; ByteBuffer bb=null; LongBuffer lb=null; @@ -82,7 +82,8 @@ public class BaseClass { String[] strings = null; int[] iarray = null; int iarray_offset = 0; - long l = 0; + long result = 0; + long l = result; result = binding.arrayTestInt32(context, ib); result = binding.arrayTestInt32(context, iarray, iarray_offset); @@ -137,34 +138,90 @@ public class BaseClass { l = binding.typeTestUIntPtrT(l, l); } + ByteBuffer newByteBuffer(int size, boolean direct) { + if(direct) { + final ByteBuffer bb = Buffers.newDirectByteBuffer(size); + Assert.assertTrue(bb.isDirect()); + return bb; + } else { + final ByteBuffer bb = ByteBuffer.wrap(new byte[size]); + Assert.assertTrue(bb.hasArray()); + Assert.assertTrue(!bb.isDirect()); + bb.order(ByteOrder.nativeOrder()); + Assert.assertTrue(bb.hasArray()); + Assert.assertTrue(!bb.isDirect()); + return bb; + } + } + + IntBuffer newIntBuffer(int size, boolean direct) { + if(direct) { + final IntBuffer ib = Buffers.newDirectIntBuffer(size); + Assert.assertTrue(ib.isDirect()); + return ib; + } else { + final IntBuffer ib = IntBuffer.wrap(new int[size]); + Assert.assertTrue(ib.hasArray()); + Assert.assertTrue(!ib.isDirect()); + return ib; + } + } + + LongBuffer newLongBuffer(int size, boolean direct) { + if(direct) { + final LongBuffer lb = Buffers.newDirectLongBuffer(size); + Assert.assertTrue(lb.isDirect()); + return lb; + } else { + final LongBuffer lb = LongBuffer.wrap(new long[size]); + Assert.assertTrue(!lb.isDirect()); + Assert.assertTrue(lb.hasArray()); + return lb; + } + } + + PointerBuffer newPointerBuffer(int size, boolean direct) { + if(direct) { + final PointerBuffer pb = PointerBuffer.allocateDirect(size); + Assert.assertTrue(pb.isDirect()); + Assert.assertTrue(pb.getBuffer().isDirect()); + return pb; + } else { + final PointerBuffer pb = PointerBuffer.allocate(size); + Assert.assertTrue(pb.hasArray()); + Assert.assertTrue(!pb.isDirect()); + return pb; + } + } + /** * Verifies if all methods / signatures are properly generated, * can be invoked and functions. * This is a compilation (coverage) and runtime time (semantic) test. * This covers indirect primitive arrays and direct NIO buffers. */ - public void chapter03TestCoverageFunctionalityDirectNIOAndPrimitiveArray(Bindingtest1 binding) throws Exception { + public void chapter03TestCoverageFunctionalityNIOAndPrimitiveArray(Bindingtest1 binding, boolean direct) throws Exception { int i; long result; - long context = 1; - LongBuffer lb = Buffers.newDirectLongBuffer(1); + final long context = 1; + LongBuffer lb = newLongBuffer(1, direct); lb.put(0, 10); - ByteBuffer bb2 = Buffers.newDirectByteBuffer(Buffers.SIZEOF_LONG); + ByteBuffer bb2 = newByteBuffer(Buffers.SIZEOF_LONG, direct); LongBuffer bb2L = bb2.asLongBuffer(); bb2L.put(0, 100); - IntBuffer ib1 = Buffers.newDirectByteBuffer(Buffers.SIZEOF_INT * Bindingtest1.ARRAY_SIZE).asIntBuffer(); + IntBuffer ib1 = newIntBuffer(Bindingtest1.ARRAY_SIZE, direct); for(i=0; i