From f733203dfbd034a6b1aa3eb2cd616437c982c435 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Sun, 17 Jul 2011 16:34:39 +0200 Subject: GlueGen proper size / alignment of primitive and compound types usage [1/2] - Preparation. Currently GlueGen fails for type long (size) and some alignments (see package.html). - The size and alignment values shall be queried at runtime. - Compound alignment needs to follow the described natural alignment (also @runtime). - - Build - add Linux Arm7 (EABI) - junit test - added compound/struct tests, pointing out the shortcomings of current impl. - package.html - Added alignment documentation - remove intptr.cfg - add GluGen types int8_t, int16_t, uint8_t, uint16_t - move MachineDescription* into runtime - Platform - has runtime MachineDescription - moved size, .. to MachineDescription - use enums for OSType, CPUArch and CPUType defined by os.name/os.arch, triggering exception if os/arch is not supported. This avoids Java String comparison and conscious os/arch detection. - MachineDescription: - compile time instances MachineDescription32Bits, MachineDescription64Bits - runtime queried instance MachineDescriptionRuntime - correct size, alignment, page size, .. --- .../com/jogamp/common/os/MachineDescription.java | 223 ++++++++++++++++++ src/java/com/jogamp/common/os/NativeLibrary.java | 64 +++--- src/java/com/jogamp/common/os/Platform.java | 256 ++++++++++++--------- src/java/com/jogamp/common/util/IOUtil.java | 18 +- src/java/com/jogamp/common/util/VersionUtil.java | 5 +- .../com/jogamp/gluegen/CMethodBindingEmitter.java | 1 + src/java/com/jogamp/gluegen/DebugEmitter.java | 1 + src/java/com/jogamp/gluegen/GlueEmitter.java | 2 + src/java/com/jogamp/gluegen/GlueGen.java | 5 + src/java/com/jogamp/gluegen/JavaEmitter.java | 2 + src/java/com/jogamp/gluegen/StructLayout.java | 53 +++-- src/java/com/jogamp/gluegen/cgram/GnuCParser.g | 4 + src/java/com/jogamp/gluegen/cgram/GnuCTreeParser.g | 4 + src/java/com/jogamp/gluegen/cgram/HeaderParser.g | 12 +- src/java/com/jogamp/gluegen/cgram/StdCParser.g | 4 + src/java/com/jogamp/gluegen/cgram/types/Field.java | 2 + .../gluegen/cgram/types/MachineDescription.java | 82 ------- .../cgram/types/MachineDescription32Bit.java | 46 ---- .../cgram/types/MachineDescription64Bit.java | 46 ---- .../com/jogamp/gluegen/cgram/types/SizeThunk.java | 20 ++ src/java/com/jogamp/gluegen/cgram/types/Type.java | 2 + src/java/com/jogamp/gluegen/package.html | 90 ++++++-- .../jogamp/common/os/MachineDescription32Bit.java | 61 +++++ .../jogamp/common/os/MachineDescription64Bit.java | 61 +++++ .../jogamp/common/nio/TestPointerBufferEndian.java | 10 +- .../common/nio/TestStructAccessorEndian.java | 9 +- src/junit/com/jogamp/common/util/TestIOUtil01.java | 4 +- .../com/jogamp/common/util/TestPlatform01.java | 16 +- .../gluegen/test/junit/generation/BaseClass.java | 75 ++++++ .../test/junit/generation/BuildEnvironment.java | 4 +- .../test/junit/generation/Test1p1JavaEmitter.java | 8 + .../generation/Test1p2ProcAddressEmitter.java | 8 + .../gluegen/test/junit/generation/test1-common.cfg | 20 ++ .../test/junit/generation/test1-gluegen.cfg | 1 + .../jogamp/gluegen/test/junit/generation/test1.c | 81 +++++++ .../jogamp/gluegen/test/junit/generation/test1.h | 62 +++++ .../test/junit/generation/test1p2-gluegen.cfg | 1 - src/native/common/MachineDescriptionRuntime.c | 174 ++++++++++++++ src/native/common/Platform.c | 29 --- 39 files changed, 1140 insertions(+), 426 deletions(-) create mode 100644 src/java/com/jogamp/common/os/MachineDescription.java delete mode 100644 src/java/com/jogamp/gluegen/cgram/types/MachineDescription.java delete mode 100644 src/java/com/jogamp/gluegen/cgram/types/MachineDescription32Bit.java delete mode 100644 src/java/com/jogamp/gluegen/cgram/types/MachineDescription64Bit.java create mode 100644 src/java/jogamp/common/os/MachineDescription32Bit.java create mode 100644 src/java/jogamp/common/os/MachineDescription64Bit.java create mode 100644 src/native/common/MachineDescriptionRuntime.c delete mode 100644 src/native/common/Platform.c (limited to 'src') diff --git a/src/java/com/jogamp/common/os/MachineDescription.java b/src/java/com/jogamp/common/os/MachineDescription.java new file mode 100644 index 0000000..a407187 --- /dev/null +++ b/src/java/com/jogamp/common/os/MachineDescription.java @@ -0,0 +1,223 @@ +/* + * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved. + * Copyright (c) 2010 JogAmp Community. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * - Redistribution of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistribution in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * Neither the name of Sun Microsystems, Inc. or the names of + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * This software is provided "AS IS," without a warranty of any kind. ALL + * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, + * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN + * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR + * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR + * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR + * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR + * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE + * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, + * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF + * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + * + * You acknowledge that this software is not designed or intended for use + * in the design, construction, operation or maintenance of any nuclear + * facility. + * + * Sun gratefully acknowledges that this software was originally authored + * and developed by Kenneth Bradley Russell and Christopher John Kline. + */ + +package com.jogamp.common.os; + +import com.jogamp.common.util.VersionUtil; + +/** + * For alignment and size see {@link com.jogamp.gluegen} + */ +public class MachineDescription { + final private boolean runtimeValidated; + + final private boolean littleEndian; + + final private int int8SizeInBytes = 1; + final private int int16SizeInBytes = 2; + final private int int32SizeInBytes = 4; + final private int int64SizeInBytes = 8; + + final private int charSizeInBytes; + final private int shortSizeInBytes; + final private int intSizeInBytes; + final private int longSizeInBytes; + final private int floatSizeInBytes; + final private int doubleSizeInBytes; + final private int pointerSizeInBytes; + final private int pageSizeInBytes; + final private boolean is32Bit; + + final private int int8AlignmentInBytes; + final private int int16AlignmentInBytes; + final private int int32AlignmentInBytes; + final private int int64AlignmentInBytes; + final private int charAlignmentInBytes; + final private int shortAlignmentInBytes; + final private int intAlignmentInBytes; + final private int longAlignmentInBytes; + final private int floatAlignmentInBytes; + final private int doubleAlignmentInBytes; + final private int pointerAlignmentInBytes; + + public MachineDescription(boolean runtimeValidated, + + boolean littleEndian, + + int charSizeInBytes, + int shortSizeInBytes, + int intSizeInBytes, + int longSizeInBytes, + int floatSizeInBytes, + int doubleSizeInBytes, + int pointerSizeInBytes, + int pageSizeInBytes, + + int int8AlignmentInBytes, + int int16AlignmentInBytes, + int int32AlignmentInBytes, + int int64AlignmentInBytes, + int charAlignmentInBytes, + int shortAlignmentInBytes, + int intAlignmentInBytes, + int longAlignmentInBytes, + int floatAlignmentInBytes, + int doubleAlignmentInBytes, + int pointerAlignmentInBytes) { + this.runtimeValidated = runtimeValidated; + + this.littleEndian = littleEndian; + + this.charSizeInBytes = charSizeInBytes; + this.shortSizeInBytes = shortSizeInBytes; + this.intSizeInBytes = intSizeInBytes; + this.longSizeInBytes = longSizeInBytes; + this.floatSizeInBytes = floatSizeInBytes; + this.doubleSizeInBytes = doubleSizeInBytes; + this.pointerSizeInBytes = pointerSizeInBytes; + this.pageSizeInBytes = pageSizeInBytes; + this.is32Bit = 4 == pointerSizeInBytes; + + this.int8AlignmentInBytes = int8AlignmentInBytes; + this.int16AlignmentInBytes = int16AlignmentInBytes; + this.int32AlignmentInBytes = int32AlignmentInBytes; + this.int64AlignmentInBytes = int64AlignmentInBytes; + this.charAlignmentInBytes = charAlignmentInBytes; + this.shortAlignmentInBytes = shortAlignmentInBytes; + this.intAlignmentInBytes = intAlignmentInBytes; + this.longAlignmentInBytes = longAlignmentInBytes; + this.floatAlignmentInBytes = floatAlignmentInBytes; + this.doubleAlignmentInBytes = doubleAlignmentInBytes; + this.pointerAlignmentInBytes = pointerAlignmentInBytes; + } + + /** + * @return true if all values are validated at runtime, otherwise false (i.e. for static compilation w/ preset values) + */ + public final boolean isRuntimeValidated() { + return runtimeValidated; + } + + /** + * Returns true only if this system uses little endian byte ordering. + */ + public final boolean isLittleEndian() { + return littleEndian; + } + + /** + * Returns true if this JVM/ARCH is 32bit. + */ + public final boolean is32Bit() { + return is32Bit; + } + + /** + * Returns true if this JVM/ARCH is 64bit. + */ + public final boolean is64Bit() { + return !is32Bit; + } + + public final int charSizeInBytes() { return charSizeInBytes; } + public final int shortSizeInBytes() { return shortSizeInBytes; } + public final int intSizeInBytes() { return intSizeInBytes; } + public final int longSizeInBytes() { return longSizeInBytes; } + public final int int8SizeInBytes() { return int8SizeInBytes; } + public final int int16SizeInBytes() { return int16SizeInBytes; } + public final int int32SizeInBytes() { return int32SizeInBytes; } + public final int int64SizeInBytes() { return int64SizeInBytes; } + public final int floatSizeInBytes() { return floatSizeInBytes; } + public final int doubleSizeInBytes() { return doubleSizeInBytes; } + public final int pointerSizeInBytes() { return pointerSizeInBytes; } + public final int pageSizeInBytes() { return pageSizeInBytes; } + + public final int charAlignmentInBytes() { return charAlignmentInBytes; } + public final int shortAlignmentInBytes() { return shortAlignmentInBytes; } + public final int intAlignmentInBytes() { return intAlignmentInBytes; } + public final int longAlignmentInBytes() { return longAlignmentInBytes; } + public final int int8AlignmentInBytes() { return int8AlignmentInBytes; } + public final int int16AlignmentInBytes() { return int16AlignmentInBytes; } + public final int int32AlignmentInBytes() { return int32AlignmentInBytes; } + public final int int64AlignmentInBytes() { return int64AlignmentInBytes; } + public final int floatAlignmentInBytes() { return floatAlignmentInBytes; } + public final int doubleAlignmentInBytes() { return doubleAlignmentInBytes; } + public final int pointerAlignmentInBytes() { return pointerAlignmentInBytes; } + + /** + * @return number of pages required for size in bytes + */ + public int pageCount(int size) { + return ( size + ( pageSizeInBytes - 1) ) / pageSizeInBytes ; // integer arithmetic + } + + /** + * @return page aligned size in bytes + */ + public int pageAlignedSize(int size) { + return pageCount(size) * pageSizeInBytes; + } + + public StringBuilder toString(StringBuilder sb) { + if(null==sb) { + sb = new StringBuilder(); + } + sb.append("MachineDescription: runtimeValidated ").append(isRuntimeValidated()).append(", littleEndian ").append(isLittleEndian()).append(", 32Bit ").append(is32Bit()).append(", primitive size / alignment:").append(Platform.getNewline()); + sb.append(" char ").append(charSizeInBytes) .append(" / ").append(charAlignmentInBytes); + sb.append(", short ").append(shortSizeInBytes) .append(" / ").append(shortAlignmentInBytes); + sb.append(", int ").append(intSizeInBytes) .append(" / ").append(intAlignmentInBytes); + sb.append(", long ").append(longSizeInBytes) .append(" / ").append(longAlignmentInBytes).append(Platform.getNewline()); + sb.append(" int8 ").append(int8SizeInBytes) .append(" / ").append(int8AlignmentInBytes); + sb.append(", int16 ").append(int16SizeInBytes) .append(" / ").append(int16AlignmentInBytes); + sb.append(", int32 ").append(int32SizeInBytes) .append(" / ").append(int32AlignmentInBytes); + sb.append(", int64 ").append(int64SizeInBytes) .append(" / ").append(int64AlignmentInBytes).append(Platform.getNewline()); + sb.append(" float ").append(floatSizeInBytes) .append(" / ").append(floatAlignmentInBytes); + sb.append(", double ").append(doubleSizeInBytes) .append(" / ").append(doubleAlignmentInBytes); + sb.append(", pointer ").append(pointerSizeInBytes).append(" / ").append(pointerAlignmentInBytes); + sb.append(", page ").append(pageSizeInBytes); + return sb; + } + + @Override + public String toString() { + return toString(null).toString(); + } + +} diff --git a/src/java/com/jogamp/common/os/NativeLibrary.java b/src/java/com/jogamp/common/os/NativeLibrary.java index 558aa3e..30075fd 100755 --- a/src/java/com/jogamp/common/os/NativeLibrary.java +++ b/src/java/com/jogamp/common/os/NativeLibrary.java @@ -59,54 +59,39 @@ import java.util.*; supporting code needed in the generated library. */ public class NativeLibrary implements DynamicLookupHelper { - private static final int WINDOWS = 1; - private static final int UNIX = 2; - private static final int MACOSX = 3; protected static boolean DEBUG; protected static boolean DEBUG_LOOKUP; - private static int platform; private static DynamicLinker dynLink; private static String[] prefixes; private static String[] suffixes; static { - // Determine platform we're running on - AccessController.doPrivileged(new PrivilegedAction() { - public Object run() { - String osName = System.getProperty("os.name").toLowerCase(); - if (osName.startsWith("wind")) { - platform = WINDOWS; - } else if (osName.startsWith("mac os x")) { - platform = MACOSX; - } else { - platform = UNIX; - } - - DEBUG = (System.getProperty("jogamp.debug.NativeLibrary") != null); - DEBUG_LOOKUP = (System.getProperty("jogamp.debug.NativeLibrary.Lookup") != null); - - return null; - } - }); // Instantiate dynamic linker implementation - switch (platform) { + switch (Platform.OS_TYPE) { case WINDOWS: dynLink = new WindowsDynamicLinkerImpl(); prefixes = new String[] { "" }; suffixes = new String[] { ".dll" }; break; - case UNIX: - dynLink = new UnixDynamicLinkerImpl(); - prefixes = new String[] { "lib" }; - suffixes = new String[] { ".so" }; - break; - case MACOSX: + + case MACOS: dynLink = new MacOSXDynamicLinkerImpl(); prefixes = new String[] { "lib", "" }; suffixes = new String[] { ".dylib", ".jnilib", "" }; break; + + /* + case FREEBSD: + case DALVIK: + case SUNOS: + case HPUX: + case OPENKODE: + case LINUX: */ default: - throw new InternalError("Platform not initialized properly"); + dynLink = new UnixDynamicLinkerImpl(); + prefixes = new String[] { "lib" }; + suffixes = new String[] { ".so" }; + break; } } @@ -306,7 +291,7 @@ public class NativeLibrary implements DynamicLookupHelper { addPaths(userDir, baseNames, paths); // Add probable Mac OS X-specific paths - if (platform == MACOSX) { + if (Platform.OS_TYPE == Platform.OSType.MACOS) { // Add historical location addPaths("/Library/Frameworks/" + libName + ".Framework", baseNames, paths); // Add current location @@ -325,15 +310,22 @@ public class NativeLibrary implements DynamicLookupHelper { private static String selectName(String windowsLibName, String unixLibName, String macOSXLibName) { - switch (platform) { + switch (Platform.OS_TYPE) { case WINDOWS: return windowsLibName; - case UNIX: - return unixLibName; - case MACOSX: + + case MACOS: return macOSXLibName; + + /* + case FREEBSD: + case DALVIK: + case SUNOS: + case HPUX: + case OPENKODE: + case LINUX: */ default: - throw new InternalError(); + return unixLibName; } } diff --git a/src/java/com/jogamp/common/os/Platform.java b/src/java/com/jogamp/common/os/Platform.java index 0775f37..680d5be 100644 --- a/src/java/com/jogamp/common/os/Platform.java +++ b/src/java/com/jogamp/common/os/Platform.java @@ -26,18 +26,13 @@ * or implied, of JogAmp Community. */ -/* - * Created on Sunday, March 28 2010 14:43 - */ package com.jogamp.common.os; -import com.jogamp.common.nio.Buffers; -import java.nio.ByteBuffer; -import java.nio.IntBuffer; -import java.nio.ShortBuffer; import java.security.AccessController; import java.security.PrivilegedAction; +import jogamp.common.os.MachineDescriptionRuntime; + /** * Utility class for querying platform specific properties. * @author Michael Bien @@ -46,97 +41,157 @@ import java.security.PrivilegedAction; public class Platform { public static final boolean JAVA_SE; - public static final boolean LITTLE_ENDIAN; public static final String OS; + public static final String OS_lower; public static final String OS_VERSION; public static final String ARCH; + public static final String ARCH_lower; public static final String JAVA_VENDOR; public static final String JAVA_VENDOR_URL; public static final String JAVA_VERSION; public static final String NEWLINE; + public enum OSType { + LINUX(0), FREEBSD(1), DALVIK(2), MACOS(3), SUNOS(4), HPUX(5), WINDOWS(6), OPENKODE(7); + + public final int id; + + OSType(int id){ + this.id = id; + } + } + public static final OSType OS_TYPE; + + public enum CPUType { + X86(0), IA(1), ARM(2), SPARC(3), PA_RISC(4), PPC(5); + + public final int id; + + CPUType(int id){ + this.id = id; + } + } + public static final CPUType CPU_TYPE; + + public enum CPUArch { + X86_32(0), X86_64(1), IA64(2), ARM_32(3), SPARC_32(4), SPARCV9_64(5), PA_RISC2_0(6), PPC(7); + + public final int id; + + CPUArch(int id){ + this.id = id; + } + } + public static final CPUArch CPU_ARCH; + private static final boolean is32Bit; - private static final int pointerSizeInBits; - private static final int pageSize; + private static final MachineDescription machineDescription; + static { // We don't seem to need an AccessController.doPrivileged() block // here as these system properties are visible even to unsigned // applets OS = System.getProperty("os.name"); + OS_lower = OS.toLowerCase(); OS_VERSION = System.getProperty("os.version"); ARCH = System.getProperty("os.arch"); + ARCH_lower = ARCH.toLowerCase(); JAVA_VENDOR = System.getProperty("java.vendor"); JAVA_VENDOR_URL = System.getProperty("java.vendor.url"); JAVA_VERSION = System.getProperty("java.version"); NEWLINE = System.getProperty("line.separator"); JAVA_SE = initIsJavaSE(); - LITTLE_ENDIAN = initByteOrder(); - boolean libsLoaded = true; - try{ - NativeLibrary.ensureNativeLibLoaded(); - }catch (UnsatisfiedLinkError err){ - libsLoaded = false; - } + if( ARCH_lower.equals("x86") || + ARCH_lower.equals("i386") || + ARCH_lower.equals("i486") || + ARCH_lower.equals("i586") || + ARCH_lower.equals("i686") ) { + CPU_ARCH = CPUArch.X86_32; + CPU_TYPE = CPUType.X86; + } else if( ARCH_lower.equals("x86_64") || + ARCH_lower.equals("amd64") ) { + CPU_ARCH = CPUArch.X86_64; + CPU_TYPE = CPUType.X86; + } else if( ARCH_lower.equals("ia64") ) { + CPU_ARCH = CPUArch.IA64; + CPU_TYPE = CPUType.IA; + } else if( ARCH_lower.equals("arm") ) { + CPU_ARCH = CPUArch.ARM_32; + CPU_TYPE = CPUType.ARM; + } else if( ARCH_lower.equals("sparc") ) { + CPU_ARCH = CPUArch.SPARC_32; + CPU_TYPE = CPUType.SPARC; + } else if( ARCH_lower.equals("sparcv9") ) { + CPU_ARCH = CPUArch.SPARCV9_64; + CPU_TYPE = CPUType.SPARC; + } else if( ARCH_lower.equals("pa_risc2.0") ) { + CPU_ARCH = CPUArch.PA_RISC2_0; + CPU_TYPE = CPUType.PA_RISC; + } else if( ARCH_lower.equals("ppc") ) { + CPU_ARCH = CPUArch.PPC; + CPU_TYPE = CPUType.PPC; + } else { + throw new RuntimeException("Please port CPU detection to your platform (" + OS_lower + "/" + ARCH_lower + ")"); + } + OS_TYPE = getOSTypeImpl(); - if(libsLoaded) { - pointerSizeInBits = getPointerSizeInBitsImpl(); - final long pageSizeL = getPageSizeImpl(); - if(Integer.MAX_VALUE < pageSizeL) { - throw new InternalError("PageSize exceeds integer value: " + pageSizeL); - } - pageSize = (int) pageSizeL ; - }else{ - pointerSizeInBits = -1; - pageSize = -1; - } - - is32Bit = initArch(); - + machineDescription = MachineDescriptionRuntime.getMachineDescription(getIs32BitByCPUArchImpl()); + is32Bit = machineDescription.is32Bit(); } private Platform() {} - private static boolean initArch() throws RuntimeException { - if ( 32 == pointerSizeInBits || 64 == pointerSizeInBits ) { - return 32 == pointerSizeInBits; - }else { - String os_lc = OS.toLowerCase(); - String arch_lc = ARCH.toLowerCase(); - - if ((os_lc.startsWith("windows") && arch_lc.equals("x86")) || - (os_lc.startsWith("windows") && arch_lc.equals("arm")) || - (os_lc.startsWith("linux") && arch_lc.equals("i386")) || - (os_lc.startsWith("linux") && arch_lc.equals("x86")) || - (os_lc.startsWith("mac os") && arch_lc.equals("ppc")) || - (os_lc.startsWith("mac os") && arch_lc.equals("i386")) || - (os_lc.startsWith("darwin") && arch_lc.equals("ppc")) || - (os_lc.startsWith("darwin") && arch_lc.equals("i386")) || - (os_lc.startsWith("sunos") && arch_lc.equals("sparc")) || - (os_lc.startsWith("sunos") && arch_lc.equals("x86")) || - (os_lc.startsWith("freebsd") && arch_lc.equals("i386")) || - (os_lc.startsWith("hp-ux") && arch_lc.equals("pa_risc2.0"))) { + private static boolean getIs32BitByCPUArchImpl() throws RuntimeException { + switch( CPU_ARCH ) { + case X86_32: + case ARM_32: + case SPARC_32: + case PPC: return true; - } else if ((os_lc.startsWith("windows") && arch_lc.equals("amd64")) || - (os_lc.startsWith("linux") && arch_lc.equals("amd64")) || - (os_lc.startsWith("linux") && arch_lc.equals("x86_64")) || - (os_lc.startsWith("linux") && arch_lc.equals("ia64")) || - (os_lc.startsWith("mac os") && arch_lc.equals("x86_64")) || - (os_lc.startsWith("darwin") && arch_lc.equals("x86_64")) || - (os_lc.startsWith("sunos") && arch_lc.equals("sparcv9")) || - (os_lc.startsWith("sunos") && arch_lc.equals("amd64"))) { + case X86_64: + case IA64: + case SPARCV9_64: + case PA_RISC2_0: return false; - }else{ - throw new RuntimeException("Please port CPU detection (32/64 bit) to your platform (" + os_lc + "/" + arch_lc + ")"); - } + default: + throw new RuntimeException("Please port CPU detection (32/64 bit) to your platform (" + Platform.OS_lower + "/" + Platform.ARCH_lower + "("+Platform.CPU_ARCH+"))"); } } - + + private static OSType getOSTypeImpl() throws RuntimeException { + if ( OS_lower.startsWith("linux") ) { + return OSType.LINUX; + } + if ( OS_lower.startsWith("freebsd") ) { + return OSType.FREEBSD; + } + if ( OS_lower.startsWith("dalvik") ) { + return OSType.DALVIK; + } + if ( OS_lower.startsWith("mac os x") || + OS_lower.startsWith("darwin") ) { + return OSType.MACOS; + } + if ( OS_lower.startsWith("sunos") ) { + return OSType.SUNOS; + } + if ( OS_lower.startsWith("hp-ux") ) { + return OSType.HPUX; + } + if ( OS_lower.startsWith("windows") ) { + return OSType.WINDOWS; + } + if ( OS_lower.startsWith("kd") ) { + return OSType.OPENKODE; + } + throw new RuntimeException("Please port OS detection to your platform (" + OS_lower + "/" + ARCH_lower + ")"); + } + private static boolean initIsJavaSE() { - // the fast path, check property Java SE instead of traversing through the ClassLoader String java_runtime_name = (String) AccessController.doPrivileged(new PrivilegedAction() { public Object run() { @@ -159,18 +214,6 @@ public class Platform { return false; } - private static boolean initByteOrder() { - ByteBuffer tst_b = Buffers.newDirectByteBuffer(Buffers.SIZEOF_INT); // 32bit in native order - IntBuffer tst_i = tst_b.asIntBuffer(); - ShortBuffer tst_s = tst_b.asShortBuffer(); - tst_i.put(0, 0x0A0B0C0D); - return 0x0C0D == tst_s.get(0); - } - - private static native int getPointerSizeInBitsImpl(); - private static native long getPageSizeImpl(); - - /** * Returns true only if this program is running on the Java Standard Edition. */ @@ -178,13 +221,6 @@ public class Platform { return JAVA_SE; } - /** - * Returns true only if this system uses little endian byte ordering. - */ - public static boolean isLittleEndian() { - return LITTLE_ENDIAN; - } - /** * Returns the OS name. */ @@ -207,6 +243,27 @@ public class Platform { return ARCH; } + /** + * Returns the OS type. + */ + public static OSType getOSType() { + return OS_TYPE; + } + + /** + * Returns the CPU type. + */ + public static CPUType getCPUType() { + return CPU_TYPE; + } + + /** + * Returns the CPU architecture. + */ + public static CPUArch getCPUArch() { + return CPU_ARCH; + } + /** * Returns the JAVA. */ @@ -236,37 +293,28 @@ public class Platform { } /** - * Returns true if this JVM is a 32bit JVM. + * Returns true if this JVM/ARCH is 32bit. + *

Shortcut to {@link #getMachineDescription()}.{@link MachineDescription#is32Bit() is32Bit()}

*/ public static boolean is32Bit() { - return is32Bit; + // return Platform.machineDescription.is32Bit(); + return Platform.is32Bit; // used very often } /** - * Returns true if this JVM is a 64bit JVM. + * Returns true if this JVM/ARCH is 64bit. + *

Shortcut to {@link #getMachineDescription()}.{@link MachineDescription#is32Bit() is64Bit()}

*/ public static boolean is64Bit() { - return !is32Bit; - } - - public static int getPointerSizeInBits() { - return pointerSizeInBits; + // return Platform.machineDescription.is64Bit(); + return !Platform.is32Bit; // used very often } - public static int getPointerSizeInBytes() { - return pointerSizeInBits/8; - } - - public static int getPageSize() { - return pageSize; - } - - public static int getPageNumber(int size) { - return ( size + ( pageSize - 1) ) / pageSize ; // integer arithmetic + /** + * Returns the MachineDescription of the running machine. + */ + public static MachineDescription getMachineDescription() { + return machineDescription; } - - public static int getPageAlignedSize(int size) { - return getPageNumber(size) * pageSize; - } } diff --git a/src/java/com/jogamp/common/util/IOUtil.java b/src/java/com/jogamp/common/util/IOUtil.java index b2d9ed4..5b00fb4 100644 --- a/src/java/com/jogamp/common/util/IOUtil.java +++ b/src/java/com/jogamp/common/util/IOUtil.java @@ -42,6 +42,7 @@ import java.net.URLConnection; import java.nio.ByteBuffer; import com.jogamp.common.nio.Buffers; +import com.jogamp.common.os.MachineDescription; import com.jogamp.common.os.Platform; public class IOUtil { @@ -81,7 +82,7 @@ public class IOUtil { * number of bytes written is returned. */ public static int copyStream2Stream(InputStream in, OutputStream out, int totalNumBytes) throws IOException { - final byte[] buf = new byte[Platform.getPageSize()]; + final byte[] buf = new byte[Platform.getMachineDescription().pageSizeInBytes()]; int numBytes = 0; while (true) { int count; @@ -137,16 +138,16 @@ public class IOUtil { if( !(stream instanceof BufferedInputStream) ) { stream = new BufferedInputStream(stream); } - int totalRead = 0; int avail = stream.available(); - ByteBuffer data = Buffers.newDirectByteBuffer( Platform.getPageAlignedSize(avail) ); - byte[] chunk = new byte[Platform.getPageSize()]; - int chunk2Read = Math.min(Platform.getPageSize(), avail); + final MachineDescription machine = Platform.getMachineDescription(); + ByteBuffer data = Buffers.newDirectByteBuffer( machine.pageAlignedSize(avail) ); + byte[] chunk = new byte[machine.pageSizeInBytes()]; + int chunk2Read = Math.min(machine.pageSizeInBytes(), avail); int numRead = 0; do { if (avail > data.remaining()) { - final ByteBuffer newData = Buffers.newDirectByteBuffer( - Platform.getPageAlignedSize(data.position() + avail) ); + final ByteBuffer newData = Buffers.newDirectByteBuffer( + machine.pageAlignedSize(data.position() + avail) ); newData.put(data); data = newData; } @@ -154,10 +155,9 @@ public class IOUtil { numRead = stream.read(chunk, 0, chunk2Read); if (numRead >= 0) { data.put(chunk, 0, numRead); - totalRead += numRead; } avail = stream.available(); - chunk2Read = Math.min(Platform.getPageSize(), avail); + chunk2Read = Math.min(machine.pageSizeInBytes(), avail); } while (avail > 0 && numRead >= 0); data.flip(); diff --git a/src/java/com/jogamp/common/util/VersionUtil.java b/src/java/com/jogamp/common/util/VersionUtil.java index 632e127..1589755 100644 --- a/src/java/com/jogamp/common/util/VersionUtil.java +++ b/src/java/com/jogamp/common/util/VersionUtil.java @@ -57,10 +57,7 @@ public class VersionUtil { sb.append(Platform.getArch()).append(" (arch) ").append(Runtime.getRuntime().availableProcessors()).append(" cores"); sb.append(Platform.getNewline()); - // arch - sb.append("Platform: littleEndian ").append(Platform.isLittleEndian()).append(", 32Bit "); - sb.append(Platform.is32Bit()).append(", a-ptr bit-size ").append(Platform.getPointerSizeInBits()); - sb.append(Platform.getNewline()); + Platform.getMachineDescription().toString(sb).append(Platform.getNewline()); // JVM/JRE sb.append("Platform: Java ").append(Platform.getJavaVersion()).append(", ").append(System.getProperty("java.vm.name")).append(", "); diff --git a/src/java/com/jogamp/gluegen/CMethodBindingEmitter.java b/src/java/com/jogamp/gluegen/CMethodBindingEmitter.java index ba2eb17..d47cded 100644 --- a/src/java/com/jogamp/gluegen/CMethodBindingEmitter.java +++ b/src/java/com/jogamp/gluegen/CMethodBindingEmitter.java @@ -43,6 +43,7 @@ import java.util.*; import java.io.*; import java.text.MessageFormat; +import com.jogamp.common.os.MachineDescription; import com.jogamp.gluegen.cgram.types.*; import java.util.logging.Logger; diff --git a/src/java/com/jogamp/gluegen/DebugEmitter.java b/src/java/com/jogamp/gluegen/DebugEmitter.java index 22cc0c5..fc536c8 100644 --- a/src/java/com/jogamp/gluegen/DebugEmitter.java +++ b/src/java/com/jogamp/gluegen/DebugEmitter.java @@ -41,6 +41,7 @@ package com.jogamp.gluegen; import java.util.*; +import com.jogamp.common.os.MachineDescription; import com.jogamp.gluegen.cgram.types.*; /** Debug emitter which prints the parsing results to standard output. */ diff --git a/src/java/com/jogamp/gluegen/GlueEmitter.java b/src/java/com/jogamp/gluegen/GlueEmitter.java index 5f627f9..ed06b19 100644 --- a/src/java/com/jogamp/gluegen/GlueEmitter.java +++ b/src/java/com/jogamp/gluegen/GlueEmitter.java @@ -40,6 +40,8 @@ package com.jogamp.gluegen; import java.util.*; + +import com.jogamp.common.os.MachineDescription; import com.jogamp.gluegen.cgram.types.*; /** Specifies the interface by which GlueGen requests glue code to be diff --git a/src/java/com/jogamp/gluegen/GlueGen.java b/src/java/com/jogamp/gluegen/GlueGen.java index 286aac4..c898058 100644 --- a/src/java/com/jogamp/gluegen/GlueGen.java +++ b/src/java/com/jogamp/gluegen/GlueGen.java @@ -40,9 +40,14 @@ package com.jogamp.gluegen; import com.jogamp.common.GlueGenVersion; +import com.jogamp.common.os.MachineDescription; + import java.io.*; import java.util.*; +import jogamp.common.os.MachineDescription32Bit; +import jogamp.common.os.MachineDescription64Bit; + import antlr.*; import com.jogamp.gluegen.cgram.*; import com.jogamp.gluegen.cgram.types.*; diff --git a/src/java/com/jogamp/gluegen/JavaEmitter.java b/src/java/com/jogamp/gluegen/JavaEmitter.java index 745176a..d92e589 100644 --- a/src/java/com/jogamp/gluegen/JavaEmitter.java +++ b/src/java/com/jogamp/gluegen/JavaEmitter.java @@ -42,6 +42,8 @@ package com.jogamp.gluegen; import com.jogamp.common.nio.Buffers; import com.jogamp.common.os.DynamicLookupHelper; +import com.jogamp.common.os.MachineDescription; + import java.io.*; import java.util.*; import java.text.MessageFormat; diff --git a/src/java/com/jogamp/gluegen/StructLayout.java b/src/java/com/jogamp/gluegen/StructLayout.java index ea8768f..392e1b1 100644 --- a/src/java/com/jogamp/gluegen/StructLayout.java +++ b/src/java/com/jogamp/gluegen/StructLayout.java @@ -1,5 +1,6 @@ /* * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved. + * Copyright (c) 2010 JogAmp Community. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are @@ -39,6 +40,7 @@ package com.jogamp.gluegen; +import com.jogamp.common.os.Platform; import com.jogamp.gluegen.cgram.types.*; /** Encapsulates algorithm for laying out data structures. Note that @@ -119,36 +121,39 @@ public class StructLayout { + /** + *

See alignment in {@link com.jogamp.common.os.MachineDescription}.

+ * + *

The code is currently used at compile time {@link JavaEmitter#layoutStruct(CompoundType t)} once, + * and code for structs is emitted for generic 32bit and 64bit only {@link JavaEmitter#emitStruct(CompoundType structType, String alternateName)}.

+ */ public static StructLayout createForCurrentPlatform() { - // Note: this code is replicated in (from?) Platform.java - String os = System.getProperty("os.name").toLowerCase(); - String cpu = System.getProperty("os.arch").toLowerCase(); - if ((os.startsWith("windows") && cpu.equals("x86"))) { - // It appears that Windows uses a packing alignment of 4 bytes in 32-bit mode + final Platform.OSType osType = Platform.getOSType(); + final Platform.CPUArch cpuArch = Platform.getCPUArch(); + + if( ( Platform.OSType.WINDOWS == osType && Platform.CPUArch.X86_32 == cpuArch ) || // It appears that Windows uses a packing alignment of 4 bytes in 32-bit mode + ( Platform.CPUArch.ARM_32 == cpuArch ) + ) { return new StructLayout(0, 4); - } else if ((os.startsWith("windows") && cpu.equals("amd64")) || - (os.startsWith("linux") && cpu.equals("i386")) || - (os.startsWith("linux") && cpu.equals("x86")) || - (os.startsWith("linux") && cpu.equals("amd64")) || - (os.startsWith("linux") && cpu.equals("x86_64")) || - (os.startsWith("linux") && cpu.equals("ia64")) || - (os.startsWith("sunos") && cpu.equals("sparc")) || - (os.startsWith("sunos") && cpu.equals("sparcv9")) || - (os.startsWith("sunos") && cpu.equals("x86")) || - (os.startsWith("sunos") && cpu.equals("amd64")) || - (os.startsWith("mac os") && cpu.equals("ppc")) || - (os.startsWith("mac os") && cpu.equals("i386")) || - (os.startsWith("mac os") && cpu.equals("x86_64")) || - (os.startsWith("freebsd") && cpu.equals("i386")) || - (os.startsWith("freebsd") && cpu.equals("amd64")) || - (os.startsWith("hp-ux") && cpu.equals("pa_risc2.0")) + } else if ((Platform.OSType.WINDOWS == osType && Platform.CPUArch.X86_64 == cpuArch) || + (Platform.OSType.LINUX == osType && Platform.CPUArch.X86_32 == cpuArch) || + (Platform.OSType.LINUX == osType && Platform.CPUArch.X86_64 == cpuArch) || + (Platform.OSType.LINUX == osType && Platform.CPUArch.IA64 == cpuArch) || + (Platform.OSType.SUNOS == osType && Platform.CPUArch.SPARC_32 == cpuArch) || + (Platform.OSType.SUNOS == osType && Platform.CPUArch.SPARCV9_64 == cpuArch) || + (Platform.OSType.SUNOS == osType && Platform.CPUArch.X86_32 == cpuArch) || + (Platform.OSType.SUNOS == osType && Platform.CPUArch.X86_64 == cpuArch) || + (Platform.OSType.MACOS == osType && Platform.CPUArch.PPC == cpuArch) || + (Platform.OSType.MACOS == osType && Platform.CPUArch.X86_32 == cpuArch) || + (Platform.OSType.MACOS == osType && Platform.CPUArch.X86_64 == cpuArch) || + (Platform.OSType.FREEBSD == osType && Platform.CPUArch.X86_32 == cpuArch) || + (Platform.OSType.FREEBSD == osType && Platform.CPUArch.X86_64 == cpuArch) || + (Platform.OSType.HPUX == osType && Platform.CPUArch.PA_RISC2_0 == cpuArch) ) { - // FIXME: make struct alignment configurable? May need to change - // packing rules on a per-type basis? return new StructLayout(0, 8); } else { // FIXME: add more ports - throw new RuntimeException("Please port StructLayout to your OS (" + os + ") and CPU (" + cpu + ")"); + throw new RuntimeException("Please port StructLayout to your OS (" + osType + ") and CPU (" + cpuArch + ")"); } } } diff --git a/src/java/com/jogamp/gluegen/cgram/GnuCParser.g b/src/java/com/jogamp/gluegen/cgram/GnuCParser.g index 58d3171..e8ca8c5 100644 --- a/src/java/com/jogamp/gluegen/cgram/GnuCParser.g +++ b/src/java/com/jogamp/gluegen/cgram/GnuCParser.g @@ -313,6 +313,10 @@ typeSpecifier [int specCount] returns [int retSpecCount] | "double" | "signed" | "unsigned" + | "int8_t" + | "uint8_t" + | "int16_t" + | "uint16_t" | "__int32" | "int32_t" | "wchar_t" diff --git a/src/java/com/jogamp/gluegen/cgram/GnuCTreeParser.g b/src/java/com/jogamp/gluegen/cgram/GnuCTreeParser.g index 1beeb7d..dbe2f98 100644 --- a/src/java/com/jogamp/gluegen/cgram/GnuCTreeParser.g +++ b/src/java/com/jogamp/gluegen/cgram/GnuCTreeParser.g @@ -183,6 +183,10 @@ typeSpecifier | "double" | "signed" | "unsigned" + | "int8_t" + | "uint8_t" + | "int16_t" + | "uint16_t" | "__int32" | "int32_t" | "wchar_t" diff --git a/src/java/com/jogamp/gluegen/cgram/HeaderParser.g b/src/java/com/jogamp/gluegen/cgram/HeaderParser.g index a0d2a83..79f966f 100644 --- a/src/java/com/jogamp/gluegen/cgram/HeaderParser.g +++ b/src/java/com/jogamp/gluegen/cgram/HeaderParser.g @@ -504,11 +504,15 @@ typeSpecifier[int attributes] returns [Type t] { | "long" { t = new IntType("long" , SizeThunk.LONG, unsigned, cvAttrs); } | "float" { t = new FloatType("float", SizeThunk.FLOAT, cvAttrs); } | "double" { t = new DoubleType("double", SizeThunk.DOUBLE, cvAttrs); } - | "__int32" { t = new IntType("__int32", SizeThunk.INT, unsigned, cvAttrs); } - | "int32_t" { t = new IntType("int32_t", SizeThunk.INT, false, cvAttrs); /* TS: always signed */ } - | "wchar_t" { t = new IntType("wchar_t", SizeThunk.INT, false, cvAttrs); /* TS: always signed */ } - | "uint32_t" { t = new IntType("uint32_t", SizeThunk.INT, true, cvAttrs, true); /* TS: always unsigned */ } + | "__int32" { t = new IntType("__int32", SizeThunk.INT32, unsigned, cvAttrs); } | "__int64" { t = new IntType("__int64", SizeThunk.INT64, unsigned, cvAttrs); } + | "int8_t" { t = new IntType("int8_t", SizeThunk.INT8, false, cvAttrs); /* TS: always signed */ } + | "uint8_t" { t = new IntType("uint8_t", SizeThunk.INT8, true, cvAttrs); /* TS: always unsigned */ } + | "int16_t" { t = new IntType("int16_t", SizeThunk.INT16, false, cvAttrs); /* TS: always signed */ } + | "uint16_t" { t = new IntType("uint16_t", SizeThunk.INT16, true, cvAttrs); /* TS: always unsigned */ } + | "int32_t" { t = new IntType("int32_t", SizeThunk.INT32, false, cvAttrs); /* TS: always signed */ } + | "wchar_t" { t = new IntType("wchar_t", SizeThunk.INT32, false, cvAttrs); /* TS: always signed */ } + | "uint32_t" { t = new IntType("uint32_t", SizeThunk.INT32, true, cvAttrs, true); /* TS: always unsigned */ } | "int64_t" { t = new IntType("int64_t", SizeThunk.INT64, false, cvAttrs); /* TS: always signed */ } | "uint64_t" { t = new IntType("uint64_t", SizeThunk.INT64, true, cvAttrs, true); /* TS: always unsigned */ } | "ptrdiff_t" { t = new IntType("ptrdiff_t", SizeThunk.POINTER, false, cvAttrs); /* TS: always signed */ } diff --git a/src/java/com/jogamp/gluegen/cgram/StdCParser.g b/src/java/com/jogamp/gluegen/cgram/StdCParser.g index 231e371..7b34656 100644 --- a/src/java/com/jogamp/gluegen/cgram/StdCParser.g +++ b/src/java/com/jogamp/gluegen/cgram/StdCParser.g @@ -265,6 +265,10 @@ typeSpecifier [int specCount] returns [int retSpecCount] | "double" | "signed" | "unsigned" + | "int8_t" + | "uint8_t" + | "int16_t" + | "uint16_t" | "__int32" | "int32_t" | "wchar_t" diff --git a/src/java/com/jogamp/gluegen/cgram/types/Field.java b/src/java/com/jogamp/gluegen/cgram/types/Field.java index 07d90ea..2479e3d 100644 --- a/src/java/com/jogamp/gluegen/cgram/types/Field.java +++ b/src/java/com/jogamp/gluegen/cgram/types/Field.java @@ -39,6 +39,8 @@ package com.jogamp.gluegen.cgram.types; +import com.jogamp.common.os.MachineDescription; + /** Represents a field in a struct or union. */ public class Field { diff --git a/src/java/com/jogamp/gluegen/cgram/types/MachineDescription.java b/src/java/com/jogamp/gluegen/cgram/types/MachineDescription.java deleted file mode 100644 index d2598e0..0000000 --- a/src/java/com/jogamp/gluegen/cgram/types/MachineDescription.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * - Redistribution of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistribution in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * Neither the name of Sun Microsystems, Inc. or the names of - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * This software is provided "AS IS," without a warranty of any kind. ALL - * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, - * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A - * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN - * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR - * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR - * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR - * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR - * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE - * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, - * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF - * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. - * - * You acknowledge that this software is not designed or intended for use - * in the design, construction, operation or maintenance of any nuclear - * facility. - * - * Sun gratefully acknowledges that this software was originally authored - * and developed by Kenneth Bradley Russell and Christopher John Kline. - */ - -package com.jogamp.gluegen.cgram.types; - -public class MachineDescription { - private int charSizeInBytes; - private int shortSizeInBytes; - private int intSizeInBytes; - private int longSizeInBytes; - private int int64SizeInBytes; - private int floatSizeInBytes; - private int doubleSizeInBytes; - private int pointerSizeInBytes; - private int structAlignmentInBytes; - - public MachineDescription(int charSizeInBytes, - int shortSizeInBytes, - int intSizeInBytes, - int longSizeInBytes, - int int64SizeInBytes, - int floatSizeInBytes, - int doubleSizeInBytes, - int pointerSizeInBytes, - int structAlignmentInBytes) { - this.charSizeInBytes = charSizeInBytes; - this.shortSizeInBytes = shortSizeInBytes; - this.intSizeInBytes = intSizeInBytes; - this.longSizeInBytes = longSizeInBytes; - this.int64SizeInBytes = int64SizeInBytes; - this.floatSizeInBytes = floatSizeInBytes; - this.doubleSizeInBytes = doubleSizeInBytes; - this.pointerSizeInBytes = pointerSizeInBytes; - this.structAlignmentInBytes = structAlignmentInBytes; - } - - public int charSizeInBytes() { return charSizeInBytes; } - public int shortSizeInBytes() { return shortSizeInBytes; } - public int intSizeInBytes() { return intSizeInBytes; } - public int longSizeInBytes() { return longSizeInBytes; } - public int int64SizeInBytes() { return int64SizeInBytes; } - public int floatSizeInBytes() { return floatSizeInBytes; } - public int doubleSizeInBytes() { return doubleSizeInBytes; } - public int pointerSizeInBytes() { return pointerSizeInBytes; } - public int structAlignmentInBytes() { return structAlignmentInBytes; } -} diff --git a/src/java/com/jogamp/gluegen/cgram/types/MachineDescription32Bit.java b/src/java/com/jogamp/gluegen/cgram/types/MachineDescription32Bit.java deleted file mode 100644 index 6bbb801..0000000 --- a/src/java/com/jogamp/gluegen/cgram/types/MachineDescription32Bit.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * - Redistribution of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistribution in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * Neither the name of Sun Microsystems, Inc. or the names of - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * This software is provided "AS IS," without a warranty of any kind. ALL - * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, - * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A - * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN - * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR - * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR - * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR - * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR - * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE - * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, - * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF - * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. - * - * You acknowledge that this software is not designed or intended for use - * in the design, construction, operation or maintenance of any nuclear - * facility. - * - * Sun gratefully acknowledges that this software was originally authored - * and developed by Kenneth Bradley Russell and Christopher John Kline. - */ - -package com.jogamp.gluegen.cgram.types; - -public class MachineDescription32Bit extends MachineDescription { - public MachineDescription32Bit() { - super(1, 2, 4, 4, 8, 4, 8, 4, 8); - } -} diff --git a/src/java/com/jogamp/gluegen/cgram/types/MachineDescription64Bit.java b/src/java/com/jogamp/gluegen/cgram/types/MachineDescription64Bit.java deleted file mode 100644 index 38328e4..0000000 --- a/src/java/com/jogamp/gluegen/cgram/types/MachineDescription64Bit.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * - Redistribution of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistribution in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * Neither the name of Sun Microsystems, Inc. or the names of - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * This software is provided "AS IS," without a warranty of any kind. ALL - * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, - * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A - * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN - * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR - * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR - * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR - * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR - * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE - * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, - * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF - * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. - * - * You acknowledge that this software is not designed or intended for use - * in the design, construction, operation or maintenance of any nuclear - * facility. - * - * Sun gratefully acknowledges that this software was originally authored - * and developed by Kenneth Bradley Russell and Christopher John Kline. - */ - -package com.jogamp.gluegen.cgram.types; - -public class MachineDescription64Bit extends MachineDescription { - public MachineDescription64Bit() { - super(1, 2, 4, 8, 8, 4, 8, 8, 16); - } -} diff --git a/src/java/com/jogamp/gluegen/cgram/types/SizeThunk.java b/src/java/com/jogamp/gluegen/cgram/types/SizeThunk.java index 40ddd57..1584a13 100755 --- a/src/java/com/jogamp/gluegen/cgram/types/SizeThunk.java +++ b/src/java/com/jogamp/gluegen/cgram/types/SizeThunk.java @@ -40,6 +40,8 @@ package com.jogamp.gluegen.cgram.types; +import com.jogamp.common.os.MachineDescription; + /** Provides a level of indirection between the definition of a type's size and the absolute value of this size. Necessary when generating glue code for two different CPU architectures (e.g., @@ -84,6 +86,24 @@ public abstract class SizeThunk implements Cloneable { } }; + public static final SizeThunk INT8 = new SizeThunk() { + public long compute(MachineDescription machDesc) { + return machDesc.int8SizeInBytes(); + } + }; + + public static final SizeThunk INT16 = new SizeThunk() { + public long compute(MachineDescription machDesc) { + return machDesc.int16SizeInBytes(); + } + }; + + public static final SizeThunk INT32 = new SizeThunk() { + public long compute(MachineDescription machDesc) { + return machDesc.int32SizeInBytes(); + } + }; + public static final SizeThunk INT64 = new SizeThunk() { public long compute(MachineDescription machDesc) { return machDesc.int64SizeInBytes(); diff --git a/src/java/com/jogamp/gluegen/cgram/types/Type.java b/src/java/com/jogamp/gluegen/cgram/types/Type.java index c58bfae..95afc2c 100644 --- a/src/java/com/jogamp/gluegen/cgram/types/Type.java +++ b/src/java/com/jogamp/gluegen/cgram/types/Type.java @@ -42,6 +42,8 @@ package com.jogamp.gluegen.cgram.types; import java.util.List; +import com.jogamp.common.os.MachineDescription; + /** Models a C type. Primitive types include int, float, and double. All types have an associated name. Structs and unions are modeled as "compound" types -- composed of fields of primitive or diff --git a/src/java/com/jogamp/gluegen/package.html b/src/java/com/jogamp/gluegen/package.html index 2862fe6..f8a2e6d 100644 --- a/src/java/com/jogamp/gluegen/package.html +++ b/src/java/com/jogamp/gluegen/package.html @@ -9,32 +9,80 @@ Gluegen has build-in types (terminal symbols) for:

- - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + +
type java bits native-x32 bitsnative-x64 bitstype signed origin -
void 0 0 0 void void ANSI-C
char 8 8 8 integersigned or unsignedANSI-C
short 16 16 16 integersigned or unsignedANSI-C
int 32 32 32 integersigned or unsignedANSI-C
long 64 32 321 integersigned or unsignedWindows
long 64 32 64 integersigned or unsignedUnix
float 32 32 32 float signed ANSI-C
double 64 64 64 double signed ANSI-C
__int32 32 32 32 integersigned or unsignedwindows
int32_t 32 32 32 integersigned stdint.h
wchar_t 32 32 32 integersigned stddef.h
uint32_t 32 32 32 integerunsigned stdint.h
__int64 64 64 64 integersigned or unsignedwindows
int64_t 64 64 64 integersigned stdint.h
uint64_t 64 64 64 integerunsigned stdint.h
ptrdiff_t 64 32 64 integersigned stddef.h
intptr_t 64 32 64 integersigned stdint.h
size_t 64 32 64 integerunsigned stddef.h
uintptr_t 64 32 64 integerunsigned stdint.h
type java bits native bits type signed origin
x32 x64
void 0 0 0 void void ANSI-C
char 8 8 8 integerany ANSI-C
short 16 16 16 integerany ANSI-C
int 32 32 32 integerany ANSI-C
long 64 32 321 integerany ANSI-C - Windows
long 64 32 64 integerany ANSI-C - Unix
float 32 32 32 float signed ANSI-C
double 64 64 64 double signed ANSI-C
__int32 32 32 32 integerany windows
__int64 64 64 64 integerany windows
int8_t 8 8 8 integersigned stdint.h
uint8_t 8 8 8 integerunsigned stdint.h
int16_t 16 16 16 integersigned stdint.h
uint16_t 16 16 16 integerunsigned stdint.h
int32_t 32 32 32 integersigned stdint.h
uint32_t 32 32 32 integerunsigned stdint.h
int64_t 64 64 64 integersigned stdint.h
uint64_t 64 64 64 integerunsigned stdint.h
intptr_t 64 32 64 integersigned stdint.h
uintptr_t 64 32 64 integerunsigned stdint.h
ptrdiff_t 64 32 64 integersigned stddef.h
size_t 64 32 64 integerunsigned stddef.h
wchar_t 32 32 32 integersigned stddef.h

Warning: Try to avoid unspecified bit sized types, especially long, since it differs on Unix and Windows!
Note 1: Type long will result in broken code on Windows, since we don't differentiate the OS and it's bit size is ambiguous.

-

+ +

GlueGen Internal Alignment for Compound Data

+ In general, depending on CPU and it's configuration (OS), alignment is set up + for each type (char, short, int, long, ..),
+ where structures are aligned naturally, i.e. their inner components are aligned.
+ See:
+ + +
Type Size & Alignment for x86, x86_64, armv7l-32bit-eabi and Window(mingw/mingw64)
+ Runtime query is implemented as follows: +
+   typedef struct {
+     _TYPE_ s0;    // ensures start address alignment
+     char   fill;  // nibble one byte
+                   // padding to align s1 
+     _TYPE_ s1;    // 
+   } type_t;
+  
+     padding = sizeof(type_t) - 2 * sizeof(_TYPE_) - sizeof(char);
+   alignment = sizeof(type_t) - 2 * sizeof(_TYPE_) ;
+  
+ + + + + + + + + + + + +
type 32 bits64 bits
sizealignmentsizealignment
char 1 1 1 1
short 2 2 2 2
int 4 4 4 4
float 4 4 4 4
long 4 4 8,4 8,4
pointer 4 4 8 8
long long 8 4,8+ 8 8
double 8 4,8+ 8 8
long double12,8+ 4,8+ 16 16

+ Linux, Darwin
+ +armv7l-32bit-eabi (linux)
+ Windows
+

+

GlueGen Platform Header Files

GlueGen provides convenient platform headers,
which can be included in your C header files for native compilation and GlueGen code generation.
diff --git a/src/java/jogamp/common/os/MachineDescription32Bit.java b/src/java/jogamp/common/os/MachineDescription32Bit.java new file mode 100644 index 0000000..ec2bb4c --- /dev/null +++ b/src/java/jogamp/common/os/MachineDescription32Bit.java @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved. + * Copyright (c) 2010 JogAmp Community. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * - Redistribution of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistribution in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * Neither the name of Sun Microsystems, Inc. or the names of + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * This software is provided "AS IS," without a warranty of any kind. ALL + * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, + * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN + * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR + * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR + * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR + * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR + * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE + * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, + * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF + * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + * + * You acknowledge that this software is not designed or intended for use + * in the design, construction, operation or maintenance of any nuclear + * facility. + * + * Sun gratefully acknowledges that this software was originally authored + * and developed by Kenneth Bradley Russell and Christopher John Kline. + */ + +package jogamp.common.os; + +import com.jogamp.common.os.MachineDescription; + +/** + * Compile time machine description. + *

littleEndian is guessed, true

+ *

pageSize is guessed, 4096

+ *

alignments are wrong, just set to sizeof

+ * For alignment and size see {@link com.jogamp.gluegen} + */ +public class MachineDescription32Bit extends MachineDescription { + public MachineDescription32Bit() { + // size: char, short, int, long, float, double, pointer, pageSize + // alignment: int8, int16, int32, int64, char, short, int, long, float, double, pointer + super( false /* runtime validated */, true /* little endian */, + /* size */ 1, 2, 4, 4, 4, 8, 4, 4096, + /*align */ 1, 2, 4, 8, 1, 2, 4, 4, 4, 8, 4); + + } +} diff --git a/src/java/jogamp/common/os/MachineDescription64Bit.java b/src/java/jogamp/common/os/MachineDescription64Bit.java new file mode 100644 index 0000000..254ba30 --- /dev/null +++ b/src/java/jogamp/common/os/MachineDescription64Bit.java @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved. + * Copyright (c) 2010 JogAmp Community. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * - Redistribution of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistribution in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * Neither the name of Sun Microsystems, Inc. or the names of + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * This software is provided "AS IS," without a warranty of any kind. ALL + * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, + * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN + * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR + * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR + * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR + * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR + * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE + * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, + * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF + * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + * + * You acknowledge that this software is not designed or intended for use + * in the design, construction, operation or maintenance of any nuclear + * facility. + * + * Sun gratefully acknowledges that this software was originally authored + * and developed by Kenneth Bradley Russell and Christopher John Kline. + */ + +package jogamp.common.os; + +import com.jogamp.common.os.MachineDescription; + +/** + * Compile time machine description. + *

littleEndian is guessed, true

+ *

sizeof(long) is wrong for Windows (64bit only)

+ *

pageSize is guessed, 4096

+ *

alignments are wrong, just set to sizeof

+ * For alignment and size see {@link com.jogamp.gluegen} + */ +public class MachineDescription64Bit extends MachineDescription { + public MachineDescription64Bit() { + // size: char, short, int, long, float, double, pointer, pageSize + // alignment: int8, int16, int32, int64, char, short, int, long, float, double, pointer + super( false /* runtime validated */, true /* little endian */, + /* size */ 1, 2, 4, 8 /* on win, long is 4 !!! */, 4, 8, 8, 4096, + /*align */ 1, 2, 4, 8, 1, 2, 4, 8, 4, 8, 8); + } +} diff --git a/src/junit/com/jogamp/common/nio/TestPointerBufferEndian.java b/src/junit/com/jogamp/common/nio/TestPointerBufferEndian.java index da232a2..e587acf 100644 --- a/src/junit/com/jogamp/common/nio/TestPointerBufferEndian.java +++ b/src/junit/com/jogamp/common/nio/TestPointerBufferEndian.java @@ -11,13 +11,11 @@ import static java.lang.System.*; public class TestPointerBufferEndian { protected void testImpl (boolean direct) { - int bitsPtr = Platform.getPointerSizeInBits(); + final MachineDescription machine = Platform.getMachineDescription(); + int bitsPtr = machine.pointerSizeInBytes() * 8; String bitsProp = System.getProperty("sun.arch.data.model"); - String os = System.getProperty("os.name"); - String cpu = System.getProperty("os.arch"); - out.println("OS: <"+os+"> CPU: <"+cpu+"> Bits: <"+bitsPtr+"/"+bitsProp+">"); - out.println("CPU is: "+ (Platform.is32Bit()?"32":"64") + " bit"); - out.println("Buffer is in: "+ (Platform.isLittleEndian()?"little":"big") + " endian"); + out.println("OS: <"+Platform.OS+"> CPU: <"+Platform.ARCH+"> Bits: <"+bitsPtr+"/"+bitsProp+">"); + out.println(machine.toString()); long[] valuesSource = { 0x0123456789ABCDEFL, 0x8877665544332211L, 0xAFFEDEADBEEFAFFEL }; long[] values32Bit = { 0x0000000089ABCDEFL, 0x0000000044332211L, 0x00000000BEEFAFFEL }; diff --git a/src/junit/com/jogamp/common/nio/TestStructAccessorEndian.java b/src/junit/com/jogamp/common/nio/TestStructAccessorEndian.java index 09781ec..8a20272 100644 --- a/src/junit/com/jogamp/common/nio/TestStructAccessorEndian.java +++ b/src/junit/com/jogamp/common/nio/TestStructAccessorEndian.java @@ -14,13 +14,12 @@ public class TestStructAccessorEndian { @Test public void testStructAccessorEndian1 () { - int bitsPtr = Platform.getPointerSizeInBits(); + final MachineDescription machine = Platform.getMachineDescription(); + int bitsPtr = machine.pointerSizeInBytes() * 8; String bitsProp = System.getProperty("sun.arch.data.model"); - String os = System.getProperty("os.name"); - String cpu = System.getProperty("os.arch"); - out.println("OS: <"+os+"> CPU: <"+cpu+"> Bits: <"+bitsPtr+"/"+bitsProp+">"); + out.println("OS: <"+Platform.OS+"> CPU: <"+Platform.ARCH+"> Bits: <"+bitsPtr+"/"+bitsProp+">"); out.println("CPU is: "+ (Platform.is32Bit()?"32":"64") + " bit"); - out.println("Buffer is in: "+ (Platform.isLittleEndian()?"little":"big") + " endian"); + out.println(machine.toString()); long[] valuesSource = { 0x0123456789ABCDEFL, 0x8877665544332211L, 0xAFFEDEADBEEFAFFEL }; ByteBuffer tst = Buffers.newDirectByteBuffer(Buffers.SIZEOF_LONG * valuesSource.length); diff --git a/src/junit/com/jogamp/common/util/TestIOUtil01.java b/src/junit/com/jogamp/common/util/TestIOUtil01.java index ad95213..fd95652 100644 --- a/src/junit/com/jogamp/common/util/TestIOUtil01.java +++ b/src/junit/com/jogamp/common/util/TestIOUtil01.java @@ -44,11 +44,13 @@ import org.junit.BeforeClass; import org.junit.AfterClass; import org.junit.Test; +import com.jogamp.common.os.MachineDescription; import com.jogamp.common.os.Platform; public class TestIOUtil01 { - static final int tsz = Platform.getPageSize() + Platform.getPageSize() / 2 ; + static final MachineDescription machine = Platform.getMachineDescription(); + static final int tsz = machine.pageSizeInBytes() + machine.pageSizeInBytes() / 2 ; static final byte[] orig = new byte[tsz]; static final String tfilename = "./test.bin" ; diff --git a/src/junit/com/jogamp/common/util/TestPlatform01.java b/src/junit/com/jogamp/common/util/TestPlatform01.java index 544418a..1266c45 100644 --- a/src/junit/com/jogamp/common/util/TestPlatform01.java +++ b/src/junit/com/jogamp/common/util/TestPlatform01.java @@ -31,34 +31,36 @@ package com.jogamp.common.util; import org.junit.Assert; import org.junit.Test; +import com.jogamp.common.os.MachineDescription; import com.jogamp.common.os.Platform; public class TestPlatform01 { @Test public void testPageSize01() { - final int ps = Platform.getPageSize(); + final MachineDescription machine = Platform.getMachineDescription(); + final int ps = machine.pageSizeInBytes(); System.err.println("PageSize: "+ps); Assert.assertTrue("PageSize is 0", 0 < ps ); - final int ps_pages = Platform.getPageNumber(ps); + final int ps_pages = machine.pageCount(ps); Assert.assertTrue("PageNumber of PageSize is not 1, but "+ps_pages, 1 == ps_pages); final int sz0 = ps - 10; - final int sz0_pages = Platform.getPageNumber(sz0); + final int sz0_pages = machine.pageCount(sz0); Assert.assertTrue("PageNumber of PageSize-10 is not 1, but "+sz0_pages, 1 == sz0_pages); final int sz1 = ps + 10; - final int sz1_pages = Platform.getPageNumber(sz1); + final int sz1_pages = machine.pageCount(sz1); Assert.assertTrue("PageNumber of PageSize+10 is not 2, but "+sz1_pages, 2 == sz1_pages); - final int ps_psa = Platform.getPageAlignedSize(ps); + final int ps_psa = machine.pageAlignedSize(ps); Assert.assertTrue("PageAlignedSize of PageSize is not PageSize, but "+ps_psa, ps == ps_psa); - final int sz0_psa = Platform.getPageAlignedSize(sz0); + final int sz0_psa = machine.pageAlignedSize(sz0); Assert.assertTrue("PageAlignedSize of PageSize-10 is not PageSize, but "+sz0_psa, ps == sz0_psa); - final int sz1_psa = Platform.getPageAlignedSize(sz1); + final int sz1_psa = machine.pageAlignedSize(sz1); Assert.assertTrue("PageAlignedSize of PageSize+10 is not 2*PageSize, but "+sz1_psa, ps*2 == sz1_psa); } 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 c57a288..df3df83 100644 --- a/src/junit/com/jogamp/gluegen/test/junit/generation/BaseClass.java +++ b/src/junit/com/jogamp/gluegen/test/junit/generation/BaseClass.java @@ -532,5 +532,80 @@ public class BaseClass { i = binding.intArrayRead(iarray, 0, 3); Assert.assertTrue("Wrong result: "+i, 6==i); } + + void assertAPTR(final long expected, final long actual) { + System.err.println("0x"+Long.toHexString(expected)+" == 0x"+Long.toHexString(actual)); + if (Platform.is32Bit()) { + int exp32; + int act32; + // if(Platform.isLittleEndian()) { + exp32 = (int) ( expected ) ; + act32 = (int) ( actual ) ; + /* } else { + exp32 = (int) ( expected >> 32 ) ; + act32 = (int) ( actual >> 32 ) ; + } */ + System.err.println("0x"+Integer.toHexString(exp32)+" == 0x"+Integer.toHexString(act32)); + Assert.assertEquals(exp32, act32); + } else { + Assert.assertEquals(expected, actual); + } + } + + 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()); + + binding.destroyComplicatedSuperSet(cs); + + /********************************************************************************/ + + TK_Surface surface = binding.createSurface(); + + assertAPTR(0x123456789abcdef0L, surface.getCtx()); + + TK_Engine engine = surface.getEngine(); + assertAPTR(0x123456789abcdef0L, engine.getCtx()); + Assert.assertEquals(0x0111, engine.render(0x0100, 0x0010, 0x0001)); + + TK_Dimension dimension = surface.getBounds(); + Assert.assertEquals(0x11111111, dimension.getX()); + Assert.assertEquals(0x22222222, dimension.getY()); + Assert.assertEquals(0x33333333, dimension.getWidth()); + Assert.assertEquals(0x44444444, dimension.getHeight()); + + Assert.assertEquals(2, surface.getClipSize()); + + for(int i=0; iclips[idx] ) ; +} + +static int32_t _TK_render (int x, int y, int ps) { + return x + y + ps ; +} + +MYAPI TK_Surface * MYAPIENTRY createSurface() { + TK_Surface * s = calloc(1, sizeof(TK_Surface)); + + s->getClip = _TK_getClip; + + s->ctx = (void *) 0x123456789abcdef0UL; + //s->engine = (TK_Engine *) calloc(1, sizeof(TK_Engine)); + //s->engine->ctx = (void *) 0x123456789abcdef0UL; + //s->engine->render = _TK_render; + s->engine.ctx = (void *) 0x123456789abcdef0UL; + s->engine.render = _TK_render; + + s->bounds.x = 0x11111111U; + s->bounds.y = 0x22222222U; + s->bounds.width = 0x33333333U; + s->bounds.height = 0x44444444U; + + s->clipSize = 2; + s->clips = (TK_Dimension *) calloc(2, sizeof(TK_Dimension)); + s->clips[0].x = 0x55555555U; + s->clips[0].y = 0x66666666U; + s->clips[0].width = 0x77777777U; + s->clips[0].height = 0x88888888U; + s->clips[1].x = 0x99999999U; + s->clips[1].y = 0xaaaaaaaaU; + s->clips[1].width = 0xbbbbbbbbU; + s->clips[1].height = 0xccccccccU; + + return s; +} + +MYAPI void MYAPIENTRY destroySurface(TK_Surface * surface) { + free(surface->clips); + // free(surface->engine); + free(surface); +} + +MYAPI TK_ComplicatedSuperSet * MYAPIENTRY createComplicatedSuperSet() { + TK_ComplicatedSuperSet * s = calloc(1, sizeof(TK_ComplicatedSuperSet)); + + 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.1415926535897932384626433832795L; + s->sub1.bits4 = 0xA4U; + 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.bits4 = 0xB4U; + s->bits3 = 0xC0U; + + fprintf(stderr, "TK_ComplicatedSubSet: sizeof(): %ld\n", (long) sizeof(TK_ComplicatedSubSet)); + fprintf(stderr, "TK_ComplicatedSubSet: bits2-s offset: %ld\n", (long) ((void *)(&s->sub1.bits2) - (void *)(&s->sub1)) ); + fprintf(stderr, "TK_ComplicatedSubSet: bits3-s offset: %ld\n", (long) ((void *)(&s->sub1.bits3) - (void *)(&s->sub1)) ); + fprintf(stderr, "TK_ComplicatedSubSet: bits4-s offset: %ld\n", (long) ((void *)(&s->sub1.bits4) - (void *)(&s->sub1)) ); + + fprintf(stderr, "TK_ComplicatedSuperSet: sizeof(): %ld\n", (long) sizeof(TK_ComplicatedSuperSet)); + fprintf(stderr, "TK_ComplicatedSuperSet: bits2-s offset: %ld\n", (long) ((void *)(&s->bits2) - (void *)(s)) ); + fprintf(stderr, "TK_ComplicatedSuperSet: bits3-s offset: %ld\n", (long) ((void *)(&s->bits3) - (void *)(s)) ); + + return s; +} + +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 5b1c241..041a8d2 100644 --- a/src/junit/com/jogamp/gluegen/test/junit/generation/test1.h +++ b/src/junit/com/jogamp/gluegen/test/junit/generation/test1.h @@ -125,3 +125,65 @@ MYAPI uintptr_t MYAPIENTRY typeTestUIntPtrT(const uintptr_t ptr1, uintptr_t ptr2 #warning "Hello Native Compiler" #endif +typedef struct { + int32_t x; + int32_t y; + int32_t width; + int32_t height; +} TK_Dimension; + +typedef struct _TK_Context * TK_Context; // anonymous + +typedef struct { + TK_Context ctx; + int32_t (MYAPIENTRY *render) (int x, int y, int ps); +} TK_Engine; + +typedef struct tk_Surface { + TK_Context ctx; + // const TK_Engine * engine; + TK_Engine engine; + TK_Dimension bounds; + int32_t clipSize; + TK_Dimension * clips; + TK_Dimension * (MYAPIENTRY *getClip) (struct tk_Surface * ds, int idx); +} TK_Surface; + +typedef struct { + int8_t bits1; // +1 + // +3 (p64) + int32_t id; // +4 + int8_t bits2; // +1 + // +7 (p64) + int64_t long0; // +8 + int8_t bits3; // +1 + // +7 (p64) + double real0; // +8 + int8_t bits4; // +1 + // +7 (p64) (for next struct ..) + + // 24 net + + // 48 gross 64bit/linux +} TK_ComplicatedSubSet; + +typedef struct { + int8_t bits1; // + 1 + // + 7 (p64) + TK_ComplicatedSubSet sub1; // +48 (64bit) + int8_t bits2; // + 1 + // + 7 (p64) + TK_ComplicatedSubSet sub2; // +48 (64bit) + int8_t bits3; // + 1 + // + 7 (p64) + + // 51 net + + // 120 gross 64bit/linux +} TK_ComplicatedSuperSet; + +MYAPI TK_Surface * MYAPIENTRY createSurface(); +MYAPI void MYAPIENTRY destroySurface(TK_Surface * surface); + +MYAPI TK_ComplicatedSuperSet * MYAPIENTRY createComplicatedSuperSet(); +MYAPI void MYAPIENTRY destroyComplicatedSuperSet(TK_ComplicatedSuperSet * s); diff --git a/src/junit/com/jogamp/gluegen/test/junit/generation/test1p2-gluegen.cfg b/src/junit/com/jogamp/gluegen/test/junit/generation/test1p2-gluegen.cfg index 649499d..708bd26 100644 --- a/src/junit/com/jogamp/gluegen/test/junit/generation/test1p2-gluegen.cfg +++ b/src/junit/com/jogamp/gluegen/test/junit/generation/test1p2-gluegen.cfg @@ -22,7 +22,6 @@ ForceProcAddressGen __ALL__ LocalProcAddressCallingConvention __ALL__ MYAPIENTRY Include test1-common.cfg -Include ../../../../../../../../make/config/intptr.cfg Import com.jogamp.gluegen.test.junit.generation.Bindingtest1 Import com.jogamp.gluegen.test.junit.generation.Bindingtest1p2 diff --git a/src/native/common/MachineDescriptionRuntime.c b/src/native/common/MachineDescriptionRuntime.c new file mode 100644 index 0000000..4cd6088 --- /dev/null +++ b/src/native/common/MachineDescriptionRuntime.c @@ -0,0 +1,174 @@ + +#include + +#include + +#include "jogamp_common_os_MachineDescriptionRuntime.h" + +#if defined(_WIN32) + #include +#else /* assume POSIX sysconf() availability */ + #include +#endif + +#include + +JNIEXPORT jint JNICALL +Java_jogamp_common_os_MachineDescriptionRuntime_getPointerSizeInBytesImpl(JNIEnv *env, jclass _unused) { + return sizeof(void *); +} + +JNIEXPORT jlong JNICALL +Java_jogamp_common_os_MachineDescriptionRuntime_getPageSizeInBytesImpl(JNIEnv *env, jclass _unused) { +#if defined(_WIN32) + SYSTEM_INFO si; + GetSystemInfo(&si); + return (jlong) si.dwPageSize; +#else + return (jlong) sysconf(_SC_PAGESIZE); +#endif +} + +typedef struct { + int8_t c1; + int8_t v; +} struct_alignment_int8; + +typedef struct { + int8_t c1; + int16_t v; +} struct_alignment_int16; + +typedef struct { + int8_t c1; + int32_t v; +} struct_alignment_int32; + +typedef struct { + int8_t c1; + int64_t v; +} struct_alignment_int64; + +typedef struct { + int8_t c1; + char v; +} struct_alignment_char; + +typedef struct { + int8_t c1; + short v; +} struct_alignment_short; + +typedef struct { + int8_t c1; + int v; +} struct_alignment_int; + +typedef struct { + int8_t c1; + long v; +} struct_alignment_long; + +typedef struct { + int8_t c1; + void * v; +} struct_alignment_pointer; + +typedef struct { + int8_t c1; + float v; +} struct_alignment_float; + +typedef struct { + int8_t c1; + double v; +} struct_alignment_double; + +// size_t padding(size_t totalsize, size_t typesize) { return totalsize - typesize - sizeof(char); } +static size_t alignment(size_t totalsize, size_t typesize) { return totalsize - typesize; } + +JNIEXPORT jint JNICALL +Java_jogamp_common_os_MachineDescriptionRuntime_getAlignmentInt8Impl(JNIEnv *env, jclass _unused) { + return alignment(sizeof( struct_alignment_int8 ), sizeof(int8_t)); +} + +JNIEXPORT jint JNICALL +Java_jogamp_common_os_MachineDescriptionRuntime_getAlignmentInt16Impl(JNIEnv *env, jclass _unused) { + return alignment(sizeof( struct_alignment_int16 ), sizeof(int16_t)); +} + +JNIEXPORT jint JNICALL +Java_jogamp_common_os_MachineDescriptionRuntime_getAlignmentInt32Impl(JNIEnv *env, jclass _unused) { + return alignment(sizeof( struct_alignment_int32 ), sizeof(int32_t)); +} + +JNIEXPORT jint JNICALL +Java_jogamp_common_os_MachineDescriptionRuntime_getAlignmentInt64Impl(JNIEnv *env, jclass _unused) { + return alignment(sizeof( struct_alignment_int64 ), sizeof(int64_t)); +} + +JNIEXPORT jint JNICALL +Java_jogamp_common_os_MachineDescriptionRuntime_getAlignmentCharImpl(JNIEnv *env, jclass _unused) { + return alignment(sizeof( struct_alignment_char ), sizeof(char)); +} + +JNIEXPORT jint JNICALL +Java_jogamp_common_os_MachineDescriptionRuntime_getAlignmentShortImpl(JNIEnv *env, jclass _unused) { + return alignment(sizeof( struct_alignment_short ), sizeof(short)); +} + +JNIEXPORT jint JNICALL +Java_jogamp_common_os_MachineDescriptionRuntime_getAlignmentIntImpl(JNIEnv *env, jclass _unused) { + return alignment(sizeof( struct_alignment_int ), sizeof(int)); +} + +JNIEXPORT jint JNICALL +Java_jogamp_common_os_MachineDescriptionRuntime_getAlignmentLongImpl(JNIEnv *env, jclass _unused) { + return alignment(sizeof( struct_alignment_long ), sizeof(long)); +} + +JNIEXPORT jint JNICALL +Java_jogamp_common_os_MachineDescriptionRuntime_getAlignmentPointerImpl(JNIEnv *env, jclass _unused) { + return alignment(sizeof( struct_alignment_pointer ), sizeof(void *)); +} + +JNIEXPORT jint JNICALL +Java_jogamp_common_os_MachineDescriptionRuntime_getAlignmentFloatImpl(JNIEnv *env, jclass _unused) { + return alignment(sizeof( struct_alignment_float ), sizeof(float)); +} + +JNIEXPORT jint JNICALL +Java_jogamp_common_os_MachineDescriptionRuntime_getAlignmentDoubleImpl(JNIEnv *env, jclass _unused) { + return alignment(sizeof( struct_alignment_double ), sizeof(double)); +} + +JNIEXPORT jint JNICALL +Java_jogamp_common_os_MachineDescriptionRuntime_getSizeOfCharImpl(JNIEnv *env, jclass _unused) { + return sizeof(char); +} + +JNIEXPORT jint JNICALL +Java_jogamp_common_os_MachineDescriptionRuntime_getSizeOfShortImpl(JNIEnv *env, jclass _unused) { + return sizeof(short); +} + +JNIEXPORT jint JNICALL +Java_jogamp_common_os_MachineDescriptionRuntime_getSizeOfIntImpl(JNIEnv *env, jclass _unused) { + return sizeof(int); +} + +JNIEXPORT jint JNICALL +Java_jogamp_common_os_MachineDescriptionRuntime_getSizeOfLongImpl(JNIEnv *env, jclass _unused) { + return sizeof(long); +} + +JNIEXPORT jint JNICALL +Java_jogamp_common_os_MachineDescriptionRuntime_getSizeOfFloatImpl(JNIEnv *env, jclass _unused) { + return sizeof(float); +} + +JNIEXPORT jint JNICALL +Java_jogamp_common_os_MachineDescriptionRuntime_getSizeOfDoubleImpl(JNIEnv *env, jclass _unused) { + return sizeof(double); +} + diff --git a/src/native/common/Platform.c b/src/native/common/Platform.c deleted file mode 100644 index c385e12..0000000 --- a/src/native/common/Platform.c +++ /dev/null @@ -1,29 +0,0 @@ - -#include - -#include - -#include "com_jogamp_common_os_Platform.h" - -#if defined(_WIN32) - #include -#else /* assume POSIX sysconf() availability */ - #include -#endif - -JNIEXPORT jint JNICALL -Java_com_jogamp_common_os_Platform_getPointerSizeInBitsImpl(JNIEnv *env, jclass _unused) { - return sizeof(void *) * 8; -} - -JNIEXPORT jlong JNICALL -Java_com_jogamp_common_os_Platform_getPageSizeImpl(JNIEnv *env, jclass _unused) { -#if defined(_WIN32) - SYSTEM_INFO si; - GetSystemInfo(&si); - return (jlong) si.dwPageSize; -#else - return (jlong) sysconf(_SC_PAGESIZE); -#endif -} - -- cgit v1.2.3