From 7220416bcef3140883d3966d921442feae3107c4 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Tue, 30 Mar 2010 01:53:58 +0200 Subject: http://www.jogamp.org/bugzilla/show_bug.cgi?id=389 32bit/64bit values and arrays are misrepresented - PointerBuffer is used to map 64bit integer values, which is illegal due to the latest PointerBuffer changes, where the underlying ByteBuffer is either 32bit wide or 64bit wide (in respect to the pointer size). The PointerBuffer semantic itself is correct, but no more suitable to represent 64bit values and arrays. A Int64Buffer (LongBuffer does not exist in CDC/CVM patch) should be used instead. - Determine use of Int64Buffer and PointerBuffer Assuming the 1st step being solved, it has to determined for which cases gluegen shall map a type to a PointerBuffer. All pointer pointer types, regardless of a Opaque mapping, ie: +++ typedef struct __MYAPIConfig * MYAPIConfig; Opaque long MYAPIConfig void foo(MYAPIConfig * ptrarray); +++ The argument 'ptrarray' must be represented as a PointerBuffer otherwise data is misrepresented in case: - 32bit machines _and_ - array semantics - more than one value is being used Impl: java/com/sun/gluegen/JavaEmitter.java: - Checks ptr-ptr for Opaque values - Returns PointerBuffer mapping for ptr-ptr types - Allow PointerBuffer being mapped as String[] Very elaborated tests .. :) ++++++++++++ Misc Changes: - Added .put(Buffer src) for Int64Buffer/PointerBuffer --- .../com/jogamp/gluegen/runtime/Int64Buffer.java | 14 +++ .../com/jogamp/gluegen/runtime/PointerBuffer.java | 14 +++ .../com/sun/gluegen/CMethodBindingEmitter.java | 7 +- src/java/com/sun/gluegen/JavaEmitter.java | 62 +++++++++-- .../com/sun/gluegen/JavaMethodBindingEmitter.java | 12 ++- .../com/jogamp/gluegen/test/junit/BaseTest1.java | 115 ++++++++++++++++++++- .../com/jogamp/gluegen/test/junit/test1-common.cfg | 15 +++ src/junit/com/jogamp/gluegen/test/junit/test1.c | 55 +++++++++- src/junit/com/jogamp/gluegen/test/junit/test1.h | 19 +++- 9 files changed, 290 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/java/com/jogamp/gluegen/runtime/Int64Buffer.java b/src/java/com/jogamp/gluegen/runtime/Int64Buffer.java index 5f7cc33..98d0834 100644 --- a/src/java/com/jogamp/gluegen/runtime/Int64Buffer.java +++ b/src/java/com/jogamp/gluegen/runtime/Int64Buffer.java @@ -152,4 +152,18 @@ public abstract class Int64Buffer { public abstract Int64Buffer put(long value); + public Int64Buffer put(Int64Buffer src) { + if (remaining() < src.remaining()) { + throw new IndexOutOfBoundsException(); + } + while (src.hasRemaining()) { + put(src.get()); + } + return this; + } + + public String toString() { + return "Int64Buffer[capacity "+capacity+", position "+position+", elementSize "+elementSize()+", ByteBuffer.capacity "+bb.capacity()+"]"; + } + } diff --git a/src/java/com/jogamp/gluegen/runtime/PointerBuffer.java b/src/java/com/jogamp/gluegen/runtime/PointerBuffer.java index a89dace..0adbb36 100644 --- a/src/java/com/jogamp/gluegen/runtime/PointerBuffer.java +++ b/src/java/com/jogamp/gluegen/runtime/PointerBuffer.java @@ -156,4 +156,18 @@ public abstract class PointerBuffer { public abstract PointerBuffer put(long value); + public PointerBuffer put(PointerBuffer src) { + if (remaining() < src.remaining()) { + throw new IndexOutOfBoundsException(); + } + while (src.hasRemaining()) { + put(src.get()); + } + return this; + } + + public String toString() { + return "PointerBuffer[capacity "+capacity+", position "+position+", elementSize "+elementSize()+", ByteBuffer.capacity "+bb.capacity()+"]"; + } + } diff --git a/src/java/com/sun/gluegen/CMethodBindingEmitter.java b/src/java/com/sun/gluegen/CMethodBindingEmitter.java index c91e992..022df39 100644 --- a/src/java/com/sun/gluegen/CMethodBindingEmitter.java +++ b/src/java/com/sun/gluegen/CMethodBindingEmitter.java @@ -1009,7 +1009,12 @@ public class CMethodBindingEmitter extends FunctionEmitter System.err.println( "WARNING: No capacity specified for java.nio.Buffer return " + "value for function \"" + binding + "\";" + - " assuming size of equivalent C return type (sizeof(" + cReturnType.getName() + ")): " + binding); + " assuming size of equivalent C return type (sizeof(" + cReturnType.getName() + ")): " + binding); + /** + throw new RuntimeException( + "WARNING: No capacity specified for java.nio.Buffer return " + + "value for function \"" + binding + "\";" + + " C return type is " + cReturnType.getName() + ": " + binding); */ } writer.println(");"); } else if (javaReturnType.isString()) { diff --git a/src/java/com/sun/gluegen/JavaEmitter.java b/src/java/com/sun/gluegen/JavaEmitter.java index 0a92735..25aeb16 100644 --- a/src/java/com/sun/gluegen/JavaEmitter.java +++ b/src/java/com/sun/gluegen/JavaEmitter.java @@ -1215,13 +1215,40 @@ public class JavaEmitter implements GlueEmitter { (opt.getTargetType().getName().equals("JNIEnv"))) { return JavaType.createForJNIEnv(); } + Type t = cType; // Opaque specifications override automatic conversions - TypeInfo info = cfg.typeInfo(cType, typedefDictionary); + // in case the identity is being used .. not if ptr-ptr + TypeInfo info = cfg.typeInfo(t, typedefDictionary); if (info != null) { - return info.javaType(); + boolean isPointerPointer = false; + if (t.pointerDepth() > 0 || t.arrayDimension() > 0) { + Type targetType; // target type + if (t.isPointer()) { + // t is *, we need to get + targetType = t.asPointer().getTargetType(); + } else { + // t is [], we need to get + targetType = t.asArray().getElementType(); + } + if (t.pointerDepth() == 2 || t.arrayDimension() == 2) { + // Get the target type of the target type (targetType was computer earlier + // as to be a pointer to the target type, so now we need to get its + // target type) + if (targetType.isPointer()) { + isPointerPointer = true; + + // t is**, targetType is *, we need to get + Type bottomType = targetType.asPointer().getTargetType(); + System.out.println("INFO: Opaque Type: "+t+", targetType: "+targetType+", bottomType: "+bottomType+" is ptr-ptr"); + } + } + } + if(!isPointerPointer) { + return info.javaType(); + } } - Type t = cType; + if (t.isInt() || t.isEnum()) { switch ((int) t.getSize(curMachDesc)) { case 1: return javaType(Byte.TYPE); @@ -1302,11 +1329,16 @@ public class JavaEmitter implements GlueEmitter { if (targetType.isPointer()) { // t is**, targetType is *, we need to get bottomType = targetType.asPointer().getTargetType(); + return JavaType.forNIOPointerBufferClass(); } else { // t is[][], targetType is [], we need to get bottomType = targetType.asArray().getElementType(); + System.out.println("WARNING: typeToJavaType(ptr-ptr): "+t+", targetType: "+targetType+", bottomType: "+bottomType+" -> Unhandled!"); } + // Warning: The below code is not backed up by an implementation, + // the only working variant is a ptr-ptr type which results in a PointerBuffer. + // if (bottomType.isPrimitive()) { if (bottomType.isInt()) { switch ((int) bottomType.getSize(curMachDesc)) { @@ -1700,6 +1732,8 @@ public class JavaEmitter implements GlueEmitter { binding.renameMethodName(cfg.getJavaSymbolRename(sym.getName())); + // System.out.println("bindFunction(0) "+sym.getReturnType()); + if (cfg.returnsString(binding.getName())) { PointerType prt = sym.getReturnType().asPointer(); if (prt == null || @@ -1714,6 +1748,8 @@ public class JavaEmitter implements GlueEmitter { binding.setJavaReturnType(typeToJavaType(sym.getReturnType(), false, curMachDesc)); } + // System.out.println("bindFunction(1) "+binding.getJavaReturnType()); + // List of the indices of the arguments in this function that should be // converted from byte[] or short[] to String List stringArgIndices = cfg.stringArguments(binding.getName()); @@ -1721,22 +1757,23 @@ public class JavaEmitter implements GlueEmitter { for (int i = 0; i < sym.getNumArguments(); i++) { Type cArgType = sym.getArgumentType(i); JavaType mappedType = typeToJavaType(cArgType, true, curMachDesc); - //System.out.println("C arg type -> \"" + cArgType + "\"" ); - //System.out.println(" Java -> \"" + mappedType + "\"" ); + // System.out.println("C arg type -> \"" + cArgType + "\"" ); + // System.out.println(" Java -> \"" + mappedType + "\"" ); // Take into account any ArgumentIsString configuration directives that apply if (stringArgIndices != null && stringArgIndices.contains(i)) { - //System.out.println("Forcing conversion of " + binding.getName() + " arg #" + i + " from byte[] to String "); + // System.out.println("Forcing conversion of " + binding.getName() + " arg #" + i + " from byte[] to String "); if (mappedType.isCVoidPointerType() || mappedType.isCCharPointerType() || mappedType.isCShortPointerType() || + mappedType.isNIOPointerBuffer() || (mappedType.isArray() && (mappedType.getJavaClass() == ArrayTypes.byteBufferArrayClass) || (mappedType.getJavaClass() == ArrayTypes.shortBufferArrayClass))) { // convert mapped type from: // void*, byte[], and short[] to String // ByteBuffer[] and ShortBuffer[] to String[] - if (mappedType.isArray()) { + if (mappedType.isArray() || mappedType.isNIOPointerBuffer()) { mappedType = javaType(ArrayTypes.stringArrayClass); } else { mappedType = javaType(String.class); @@ -1753,8 +1790,9 @@ public class JavaEmitter implements GlueEmitter { //System.out.println("During binding of [" + sym + "], added mapping from C type: " + cArgType + " to Java type: " + mappedType); } - //System.err.println("---> " + binding); - //System.err.println(" ---> " + binding.getCSymbol()); + // System.out.println("---> " + binding); + // System.out.println(" ---> " + binding.getCSymbol()); + // System.out.println("bindFunction(3) "+binding); return binding; } @@ -1764,6 +1802,8 @@ public class JavaEmitter implements GlueEmitter { MethodBinding result = inputBinding; boolean arrayPossible = false; + // System.out.println("lowerMethodBindingPointerTypes(0): "+result); + for (int i = 0; i < inputBinding.getNumArguments(); i++) { JavaType t = inputBinding.getJavaArgumentType(i); if (t.isCPrimitivePointerType()) { @@ -1818,6 +1858,8 @@ public class JavaEmitter implements GlueEmitter { } } + // System.out.println("lowerMethodBindingPointerTypes(1): "+result); + // Always return primitive pointer types as NIO buffers JavaType t = result.getJavaReturnType(); if (t.isCPrimitivePointerType()) { @@ -1840,6 +1882,8 @@ public class JavaEmitter implements GlueEmitter { } } + // System.out.println("lowerMethodBindingPointerTypes(2): "+result); + if (canProduceArrayVariant != null) { canProduceArrayVariant[0] = arrayPossible; } diff --git a/src/java/com/sun/gluegen/JavaMethodBindingEmitter.java b/src/java/com/sun/gluegen/JavaMethodBindingEmitter.java index b5b5b9c..4153e32 100644 --- a/src/java/com/sun/gluegen/JavaMethodBindingEmitter.java +++ b/src/java/com/sun/gluegen/JavaMethodBindingEmitter.java @@ -730,11 +730,17 @@ public class JavaMethodBindingEmitter extends FunctionEmitter if (!returnType.isNIOByteBuffer()) { // See whether we have to expand pointers to longs if (getBinding().getCReturnType().pointerDepth() >= 2) { - if (!returnType.isNIOPointerBuffer()) { + if (returnType.isNIOPointerBuffer()) { + writer.println(" return PointerBuffer.wrap(_res);"); + } else if (returnType.isNIOInt64Buffer()) { + writer.println(" return Int64Buffer.wrap(_res);"); + } else { throw new RuntimeException("While emitting glue code for " + getName() + - ": can not legally make pointers opaque to anything but longs"); + ": can not legally make pointers opaque to anything but PointerBuffer or Int64Buffer/long"); } - writer.println(" return PointerBuffer.wrap(_res);"); + } else if (getBinding().getCReturnType().pointerDepth() == 1 && + returnType.isNIOInt64Buffer()) { + writer.println(" return Int64Buffer.wrap(_res);"); } else { String returnTypeName = returnType.getName().substring("java.nio.".length()); writer.println(" return _res.as" + returnTypeName + "();"); diff --git a/src/junit/com/jogamp/gluegen/test/junit/BaseTest1.java b/src/junit/com/jogamp/gluegen/test/junit/BaseTest1.java index c53d142..420dc2b 100644 --- a/src/junit/com/jogamp/gluegen/test/junit/BaseTest1.java +++ b/src/junit/com/jogamp/gluegen/test/junit/BaseTest1.java @@ -35,6 +35,7 @@ package com.jogamp.gluegen.test.junit; import com.jogamp.gluegen.runtime.Buffers; import com.jogamp.gluegen.runtime.PointerBuffer; import com.jogamp.gluegen.runtime.Int64Buffer; +import com.jogamp.gluegen.runtime.Platform; import java.nio.*; import java.io.File; import java.lang.reflect.InvocationTargetException; @@ -89,6 +90,7 @@ public class BaseTest1 { long context = 0; ByteBuffer bb=null; Int64Buffer lb=null; + PointerBuffer pb=null; IntBuffer ib=null; long[] larray = null; int larray_offset = 0; @@ -103,10 +105,16 @@ public class BaseTest1 { result = binding.arrayTestInt64(context, lb); result = binding.arrayTestInt64(context, larray, larray_offset); - result = binding.arrayTestFoo(context, lb); - result = binding.arrayTestFoo(context, larray, larray_offset); + result = binding.arrayTestFoo1(context, lb); + result = binding.arrayTestFoo1(context, larray, larray_offset); result = binding.arrayTestFooNioOnly(context, lb); + lb = binding.arrayTestFoo2(lb); + lb = binding.arrayTestFoo2(larray, larray_offset); + + pb = binding.arrayTestFoo3ArrayToPtrPtr(lb); + pb = binding.arrayTestFoo3PtrPtr(pb); + result = binding.bufferTest(bb); result = binding.bufferTestNioOnly(bb); @@ -128,6 +136,9 @@ public class BaseTest1 { i = binding.intArrayRead(ib, i); i = binding.intArrayRead(iarray, iarray_offset, i); + long cfg=0; + cfg = binding.typeTestAnonSingle(cfg); + pb = binding.typeTestAnonPointer(pb); } /** @@ -192,15 +203,84 @@ public class BaseTest1 { result = binding.arrayTestInt64(context, larray1, larray1_offset); Assert.assertTrue("Wrong result: "+result, 1+8000==result); - result = binding.arrayTestFoo(context, lb1); + result = binding.arrayTestFoo1(context, lb1); Assert.assertTrue("Wrong result: "+result, 1+8000==result); - result = binding.arrayTestFoo(context, larray1, larray1_offset); + result = binding.arrayTestFoo1(context, larray1, larray1_offset); Assert.assertTrue("Wrong result: "+result, 1+8000==result); result = binding.arrayTestFooNioOnly(context, lb1); Assert.assertTrue("Wrong result: "+result, 1+8000==result); + { + lb2.rewind(); + Int64Buffer lb3 = Int64Buffer.allocateDirect(BindingTest1.ARRAY_SIZE); + lb3.put(lb2); + lb3.rewind(); + lb2.rewind(); + + // System.out.println("lb3: "+lb3); + Assert.assertTrue("Wrong result: "+lb3.capacity(), BindingTest1.ARRAY_SIZE == lb3.capacity()); + Assert.assertTrue("Wrong result: "+lb3.remaining(), BindingTest1.ARRAY_SIZE == lb3.remaining()); + + Int64Buffer lbR = binding.arrayTestFoo2(lb3); + // System.out.println("lbR: "+lbR); + + Assert.assertNotNull(lbR); + Assert.assertTrue("Wrong result: "+lb3.capacity(), BindingTest1.ARRAY_SIZE == lb3.capacity()); + Assert.assertTrue("Wrong result: "+lb3.remaining(), BindingTest1.ARRAY_SIZE == lb3.remaining()); + Assert.assertTrue("Wrong result: "+lbR.capacity(), BindingTest1.ARRAY_SIZE == lbR.capacity()); + Assert.assertTrue("Wrong result: "+lbR.remaining(), BindingTest1.ARRAY_SIZE == lbR.remaining()); + int j=0; + for(j=0; j #include #include +#include foo nopTest() { return 42; @@ -31,7 +32,38 @@ int64_t arrayTestInt64(int64_t context, int64_t * array) { return r+context; } -foo arrayTestFoo(int64_t context, foo * array) { +foo * arrayTestFoo2( foo * array ) { + int i; + foo * result = calloc(ARRAY_SIZE, sizeof(foo)); + assert(NULL!=array); + for(i=0; i %d\n", i, (int)array[i], (int)result[i]); + } + return result; +} + +foo * * arrayTestFoo3ArrayToPtrPtr(foo * array) { + int j; + foo * * result = calloc(ARRAY_SIZE, sizeof(foo *)); + for(j=0; j