From dc4b259b6651bdc0cec0895bc74e26e504870c8e Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Thu, 21 Jul 2011 11:06:52 +0200 Subject: GlueGen proper size / alignment of primitive and compound types usage [2/2] - Fin MachineDesction == MD MD.StaticConfig: - enum for all supported static configs (ID -> MD) - verified at runtime: test runtime queried-MD versus static-MD, hard fail if not compatible (size/alignment) SizeThunk primitive sizes: - Add notion of fixed native size (eg. int64_t) and otherwise (eg. long) java struct 'wrappers' code generation: - single class using size/offset arrays of all MachineDescription configurations - at runtime the array idx is queried in static block - type aligment for not fixed-native-size types (SizeThunk, undef long/int) via StructAccessor junit test: - add float test - fix native code - add java (create, write) -> native (verify) test works (tested) on: linux 32/64 and windows 32/64 --- .../gluegen/test/junit/generation/BaseClass.java | 108 ++++++++++++++++----- .../gluegen/test/junit/generation/test1-common.cfg | 1 + .../jogamp/gluegen/test/junit/generation/test1.c | 89 ++++++++++++++++- .../jogamp/gluegen/test/junit/generation/test1.h | 23 +++-- 4 files changed, 188 insertions(+), 33 deletions(-) (limited to 'src/junit/com/jogamp/gluegen') 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 df3df83..e73aca6 100644 --- a/src/junit/com/jogamp/gluegen/test/junit/generation/BaseClass.java +++ b/src/junit/com/jogamp/gluegen/test/junit/generation/BaseClass.java @@ -30,12 +30,15 @@ package com.jogamp.gluegen.test.junit.generation; import com.jogamp.common.nio.Buffers; import com.jogamp.common.nio.PointerBuffer; +import com.jogamp.common.os.MachineDescription; import com.jogamp.common.os.Platform; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.IntBuffer; import java.nio.LongBuffer; +import jogamp.common.os.MachineDescriptionRuntime; + import org.junit.Assert; @@ -553,33 +556,90 @@ public class BaseClass { } public void chapter09TestCompoundAndAlignment(Bindingtest1 binding) throws Exception { - TK_ComplicatedSuperSet cs = binding.createComplicatedSuperSet(); - Assert.assertEquals((byte)0xA0, cs.getBits1()); - - TK_ComplicatedSubSet sub1 = cs.getSub1(); - Assert.assertEquals((byte)0xA1, sub1.getBits1()); - Assert.assertEquals(0x12345678, sub1.getId()); - Assert.assertEquals((byte)0xA2, sub1.getBits2()); - Assert.assertEquals(0x123456789abcdef0L, sub1.getLong0()); - Assert.assertEquals((byte)0xA3, sub1.getBits3()); - Assert.assertEquals(3.1415926535897932384626433832795, sub1.getReal0(), 0.0); - Assert.assertEquals((byte)0xA4, sub1.getBits4()); - - Assert.assertEquals((byte)0xB0, cs.getBits2()); - - TK_ComplicatedSubSet sub2 = cs.getSub2(); - Assert.assertEquals((byte)0xB1, sub2.getBits1()); - Assert.assertEquals(0x12345678, sub2.getId()); - Assert.assertEquals((byte)0xB2, sub2.getBits2()); - Assert.assertEquals(0x123456789abcdef0L, sub2.getLong0()); - Assert.assertEquals((byte)0xB3, sub2.getBits3()); - Assert.assertEquals(3.1415926535897932384626433832795, sub2.getReal0(), 0.0); - Assert.assertEquals((byte)0xB4, sub2.getBits4()); - Assert.assertEquals((byte)0xC0, cs.getBits3()); + MachineDescription.StaticConfig smd = MachineDescriptionRuntime.getStatic(); + MachineDescription md = MachineDescriptionRuntime.getRuntime(); - binding.destroyComplicatedSuperSet(cs); + System.err.println("static md: "+smd); + System.err.println("runtime md: "+md); + System.err.println("compatible static/runtime: "+md.compatible(smd.md)); + { + TK_ComplicatedSuperSet cs = binding.createComplicatedSuperSet(); + Assert.assertEquals((byte)0xA0, cs.getBits1()); + + TK_ComplicatedSubSet sub1 = cs.getSub1(); + Assert.assertEquals((byte)0xA1, sub1.getBits1()); + Assert.assertEquals(0x12345678, sub1.getId()); + Assert.assertEquals((byte)0xA2, sub1.getBits2()); + Assert.assertEquals(0x123456789abcdef0L, sub1.getLong0()); + Assert.assertEquals((byte)0xA3, sub1.getBits3()); + Assert.assertEquals(3.1415926535897932384626433832795, sub1.getReal0(), 0.0); + Assert.assertEquals((byte)0xA4, sub1.getBits4()); + Assert.assertEquals(256.12345f, sub1.getReal1(), 0.0); + Assert.assertEquals((byte)0xA5, sub1.getBits5()); + Assert.assertEquals((long)0xdeadbeefL, sub1.getLongX()); + Assert.assertEquals((byte)0xA6, sub1.getBits6()); + + Assert.assertEquals((byte)0xB0, cs.getBits2()); + + TK_ComplicatedSubSet sub2 = cs.getSub2(); + Assert.assertEquals((byte)0xB1, sub2.getBits1()); + Assert.assertEquals(0x12345678, sub2.getId()); + Assert.assertEquals((byte)0xB2, sub2.getBits2()); + Assert.assertEquals(0x123456789abcdef0L, sub2.getLong0()); + Assert.assertEquals((byte)0xB3, sub2.getBits3()); + Assert.assertEquals(3.1415926535897932384626433832795, sub2.getReal0(), 0.0); + Assert.assertEquals((byte)0xB4, sub2.getBits4()); + Assert.assertEquals(256.12345f, sub2.getReal1(), 0.0); + Assert.assertEquals((byte)0xB5, sub2.getBits5()); + Assert.assertEquals((long)0xdeadbeefL, sub2.getLongX()); + Assert.assertEquals((byte)0xB6, sub2.getBits6()); + + Assert.assertEquals((byte)0xC0, cs.getBits3()); + + binding.destroyComplicatedSuperSet(cs); + } + + /********************************************************************************/ + + { + TK_ComplicatedSuperSet cs = TK_ComplicatedSuperSet.create(); + cs.setBits1((byte)0xA0); + + TK_ComplicatedSubSet sub1 = cs.getSub1(); + sub1.setBits1((byte)0xA1); + sub1.setId(0x12345678); + sub1.setBits2((byte)0xA2); + sub1.setLong0(0x123456789abcdef0L); + sub1.setBits3((byte)0xA3); + sub1.setReal0(3.1415926535897932384626433832795); + sub1.setBits4((byte)0xA4); + sub1.setReal1(256.12345f); + sub1.setBits5((byte)0xA5); + sub1.setLongX((long)0xdeadbeefL); + sub1.setBits6((byte)0xA6); + + cs.setBits2((byte)0xB0); + + TK_ComplicatedSubSet sub2 = cs.getSub2(); + sub2.setBits1((byte)0xB1); + sub2.setId(0x12345678); + sub2.setBits2((byte)0xB2); + sub2.setLong0(0x123456789abcdef0L); + sub2.setBits3((byte)0xB3); + sub2.setReal0(3.1415926535897932384626433832795); + sub2.setBits4((byte)0xB4); + sub2.setReal1(256.12345f); + sub2.setBits5((byte)0xB5); + sub2.setLongX((long)0xdeadbeefL); + sub2.setBits6((byte)0xB6); + + cs.setBits3((byte)0xC0); + + Assert.assertTrue(binding.hasInitValues(cs)); + } + /********************************************************************************/ TK_Surface surface = binding.createSurface(); diff --git a/src/junit/com/jogamp/gluegen/test/junit/generation/test1-common.cfg b/src/junit/com/jogamp/gluegen/test/junit/generation/test1-common.cfg index c6e5415..17890eb 100644 --- a/src/junit/com/jogamp/gluegen/test/junit/generation/test1-common.cfg +++ b/src/junit/com/jogamp/gluegen/test/junit/generation/test1-common.cfg @@ -22,6 +22,7 @@ ReturnValueCapacity typeTestAnonPointer ARRAY_SIZE * sizeof(MYAPIConfig) # # typedef struct __MYAPIConfig * MYAPIConfig; Opaque long MYAPIConfig +Opaque boolean Bool CustomCCode #include "test1.h" diff --git a/src/junit/com/jogamp/gluegen/test/junit/generation/test1.c b/src/junit/com/jogamp/gluegen/test/junit/generation/test1.c index 6d3117a..2d7aa71 100644 --- a/src/junit/com/jogamp/gluegen/test/junit/generation/test1.c +++ b/src/junit/com/jogamp/gluegen/test/junit/generation/test1.c @@ -6,6 +6,8 @@ #include #include +#define DEBUG 1 + MYAPI foo MYAPIENTRY nopTest() { return 42; } @@ -279,6 +281,40 @@ MYAPI void MYAPIENTRY destroySurface(TK_Surface * surface) { free(surface); } +static void dumpTK_ComplicatedSuperSet(TK_ComplicatedSuperSet * s) { + fprintf(stderr, "TK_ComplicatedSuperSet [\n"); + fprintf(stderr, " cs.b1 0x%X\n", s->bits1); + + fprintf(stderr, " cs.sub1.b1 0x%X\n", s->sub1.bits1); + fprintf(stderr, " cs.sub1.id 0x%X\n", s->sub1.id); + fprintf(stderr, " cs.sub1.b2 0x%X\n", s->sub1.bits2); + fprintf(stderr, " cs.sub1.long0 0x%lX\n", s->sub1.long0); + fprintf(stderr, " cs.sub1.b3 0x%X\n", s->sub1.bits3); + fprintf(stderr, " cs.sub1.real0 %G %A\n", s->sub1.real0, s->sub1.real0); + fprintf(stderr, " cs.sub1.b4 0x%X\n", s->sub1.bits4); + fprintf(stderr, " cs.sub1.real1 %G %A\n", (double)s->sub1.real1, (double)s->sub1.real1); + fprintf(stderr, " cs.sub1.b5 0x%X\n", s->sub1.bits5); + fprintf(stderr, " cs.sub1.longX 0x%lX\n", (int64_t)s->sub1.longX); + fprintf(stderr, " cs.sub1.b6 0x%X\n", s->sub1.bits6); + + fprintf(stderr, " cs.b2 0x%X\n", s->bits2); + + fprintf(stderr, " cs.sub2.b1 0x%X\n", s->sub2.bits1); + fprintf(stderr, " cs.sub2.id 0x%X\n", s->sub2.id); + fprintf(stderr, " cs.sub2.b2 0x%X\n", s->sub2.bits2); + fprintf(stderr, " cs.sub2.long0 0x%lX\n", s->sub2.long0); + fprintf(stderr, " cs.sub2.b3 0x%X\n", s->sub2.bits3); + fprintf(stderr, " cs.sub2.real0 %G %A\n", s->sub2.real0, s->sub2.real0); + fprintf(stderr, " cs.sub2.b4 0x%X\n", s->sub2.bits4); + fprintf(stderr, " cs.sub2.real1 %G %A\n", (double)s->sub2.real1, (double)s->sub2.real1); + fprintf(stderr, " cs.sub2.b5 0x%X\n", s->sub2.bits5); + fprintf(stderr, " cs.sub2.longX 0x%lX\n", (int64_t)s->sub2.longX); + fprintf(stderr, " cs.sub2.b6 0x%X\n", s->sub2.bits6); + + fprintf(stderr, " cs.b3 0x%X\n", s->bits3); + fprintf(stderr, "]\n\n"); +} + MYAPI TK_ComplicatedSuperSet * MYAPIENTRY createComplicatedSuperSet() { TK_ComplicatedSuperSet * s = calloc(1, sizeof(TK_ComplicatedSuperSet)); @@ -288,16 +324,24 @@ MYAPI TK_ComplicatedSuperSet * MYAPIENTRY createComplicatedSuperSet() { s->sub1.bits2 = 0xA2U; s->sub1.long0 = 0x123456789abcdef0UL; s->sub1.bits3 = 0xA3U; - s->sub1.real0 = 3.1415926535897932384626433832795L; + s->sub1.real0 = 3.1415926535897932384626433832795; s->sub1.bits4 = 0xA4U; + s->sub1.real1 = 256.12345f; + s->sub1.bits5 = 0xA5U; + s->sub1.longX = (long) 0xdeadbeefU; + s->sub1.bits6 = 0xA6U; s->bits2 = 0xB0U; s->sub2.bits1 = 0xB1U; s->sub2.id = 0x12345678U; s->sub2.bits2 = 0xB2U; s->sub2.long0 = 0x123456789abcdef0UL; s->sub2.bits3 = 0xB3U; - s->sub2.real0 = 3.1415926535897932384626433832795L; + s->sub2.real0 = 3.1415926535897932384626433832795; s->sub2.bits4 = 0xB4U; + s->sub2.real1 = 256.12345f; + s->sub2.bits5 = 0xB5U; + s->sub2.longX = (long) 0xdeadbeefU; + s->sub2.bits6 = 0xB6U; s->bits3 = 0xC0U; fprintf(stderr, "TK_ComplicatedSubSet: sizeof(): %ld\n", (long) sizeof(TK_ComplicatedSubSet)); @@ -308,6 +352,10 @@ MYAPI TK_ComplicatedSuperSet * MYAPIENTRY createComplicatedSuperSet() { fprintf(stderr, "TK_ComplicatedSubSet: bits3-s offset: %ld\n", (long) ((void *)(&s->sub1.bits3) - (void *)(&s->sub1)) ); fprintf(stderr, "TK_ComplicatedSubSet: real0-s offset: %ld\n", (long) ((void *)(&s->sub1.real0) - (void *)(&s->sub1)) ); fprintf(stderr, "TK_ComplicatedSubSet: bits4-s offset: %ld\n", (long) ((void *)(&s->sub1.bits4) - (void *)(&s->sub1)) ); + fprintf(stderr, "TK_ComplicatedSubSet: real1-s offset: %ld\n", (long) ((void *)(&s->sub1.real1) - (void *)(&s->sub1)) ); + fprintf(stderr, "TK_ComplicatedSubSet: bits5-s offset: %ld\n", (long) ((void *)(&s->sub1.bits5) - (void *)(&s->sub1)) ); + fprintf(stderr, "TK_ComplicatedSubSet: longX-s offset: %ld\n", (long) ((void *)(&s->sub1.longX) - (void *)(&s->sub1)) ); + fprintf(stderr, "TK_ComplicatedSubSet: bits6-s offset: %ld\n", (long) ((void *)(&s->sub1.bits6) - (void *)(&s->sub1)) ); fprintf(stderr, "TK_ComplicatedSuperSet: sizeof(): %ld\n", (long) sizeof(TK_ComplicatedSuperSet)); fprintf(stderr, "TK_ComplicatedSuperSet: bits1-s offset: %ld\n", (long) ((void *)(&s->bits1) - (void *)(s)) ); @@ -316,9 +364,46 @@ MYAPI TK_ComplicatedSuperSet * MYAPIENTRY createComplicatedSuperSet() { fprintf(stderr, "TK_ComplicatedSuperSet: sub2-s offset: %ld\n", (long) ((void *)(&s->sub2) - (void *)(s)) ); fprintf(stderr, "TK_ComplicatedSuperSet: bits3-s offset: %ld\n", (long) ((void *)(&s->bits3) - (void *)(s)) ); + #ifdef DEBUG + fprintf(stderr, "createComplicatedSuperSet:\n"); + dumpTK_ComplicatedSuperSet(s); + #endif return s; } +MYAPI Bool MYAPIENTRY hasInitValues(TK_ComplicatedSuperSet * s) { + Bool b = s->bits1 == 0xA0U && + s->sub1.bits1 == 0xA1U && + s->sub1.id == 0x12345678U && + s->sub1.bits2 == 0xA2U && + s->sub1.long0 == 0x123456789abcdef0UL && + s->sub1.bits3 == 0xA3U && + s->sub1.real0 == 3.1415926535897932384626433832795 && + s->sub1.bits4 == 0xA4U && + s->sub1.real1 == 256.12345f && + s->sub1.bits5 == 0xA5U && + s->sub1.longX == (long) 0xdeadbeefU && + s->sub1.bits6 == 0xA6U && + s->bits2 == 0xB0U && + s->sub2.bits1 == 0xB1U && + s->sub2.id == 0x12345678U && + s->sub2.bits2 == 0xB2U && + s->sub2.long0 == 0x123456789abcdef0UL && + s->sub2.bits3 == 0xB3U && + s->sub2.real0 == 3.1415926535897932384626433832795 && + s->sub2.bits4 == 0xB4U && + s->sub2.real1 == 256.12345f && + s->sub2.bits5 == 0xB5U && + s->sub2.longX == (long) 0xdeadbeefU && + s->sub2.bits6 == 0xB6U && + s->bits3 == 0xC0U ; + #ifdef DEBUG + fprintf(stderr, "hasInitValues res %d:\n", b); + dumpTK_ComplicatedSuperSet(s); + #endif + return b; +} + MYAPI void MYAPIENTRY destroyComplicatedSuperSet(TK_ComplicatedSuperSet * s) { free(s); } diff --git a/src/junit/com/jogamp/gluegen/test/junit/generation/test1.h b/src/junit/com/jogamp/gluegen/test/junit/generation/test1.h index 041a8d2..3e20307 100644 --- a/src/junit/com/jogamp/gluegen/test/junit/generation/test1.h +++ b/src/junit/com/jogamp/gluegen/test/junit/generation/test1.h @@ -26,6 +26,7 @@ #include #include +typedef int Bool; typedef uint64_t foo; /** Returns 42 */ @@ -150,16 +151,23 @@ typedef struct tk_Surface { } TK_Surface; typedef struct { - int8_t bits1; // +1 + uint8_t bits1; // +1 // +3 (p64) int32_t id; // +4 - int8_t bits2; // +1 + uint8_t bits2; // +1 // +7 (p64) int64_t long0; // +8 - int8_t bits3; // +1 + uint8_t bits3; // +1 // +7 (p64) double real0; // +8 - int8_t bits4; // +1 + uint8_t bits4; // +1 + // +3 (p64) + float real1; // +4 + uint8_t bits5; // +1 + // +7 (p64) / +3 (windows andx 32bit) + long longX; // +8 / +4 (windows andx 32bit) + uint8_t bits6; // +1 + // +7 (p64) (for next struct ..) // 24 net @@ -168,13 +176,13 @@ typedef struct { } TK_ComplicatedSubSet; typedef struct { - int8_t bits1; // + 1 + uint8_t bits1; // + 1 // + 7 (p64) TK_ComplicatedSubSet sub1; // +48 (64bit) - int8_t bits2; // + 1 + uint8_t bits2; // + 1 // + 7 (p64) TK_ComplicatedSubSet sub2; // +48 (64bit) - int8_t bits3; // + 1 + uint8_t bits3; // + 1 // + 7 (p64) // 51 net @@ -186,4 +194,5 @@ MYAPI TK_Surface * MYAPIENTRY createSurface(); MYAPI void MYAPIENTRY destroySurface(TK_Surface * surface); MYAPI TK_ComplicatedSuperSet * MYAPIENTRY createComplicatedSuperSet(); +MYAPI Bool MYAPIENTRY hasInitValues(TK_ComplicatedSuperSet * s); MYAPI void MYAPIENTRY destroyComplicatedSuperSet(TK_ComplicatedSuperSet * s); -- cgit v1.2.3