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 --- .../common/os/MachineDescriptionRuntime.java | 94 +++++++++++++++------- 1 file changed, 63 insertions(+), 31 deletions(-) (limited to 'src/java/jogamp/common/os/MachineDescriptionRuntime.java') diff --git a/src/java/jogamp/common/os/MachineDescriptionRuntime.java b/src/java/jogamp/common/os/MachineDescriptionRuntime.java index a5ecc1d..bf49105 100644 --- a/src/java/jogamp/common/os/MachineDescriptionRuntime.java +++ b/src/java/jogamp/common/os/MachineDescriptionRuntime.java @@ -28,64 +28,96 @@ package jogamp.common.os; -import com.jogamp.common.nio.Buffers; import com.jogamp.common.os.MachineDescription; import com.jogamp.common.os.NativeLibrary; import com.jogamp.common.os.Platform; - -import java.nio.ByteBuffer; -import java.nio.IntBuffer; -import java.nio.ShortBuffer; +import com.jogamp.common.os.MachineDescription.StaticConfig; /** * Runtime MachineDescription */ public class MachineDescriptionRuntime { - public static MachineDescription getMachineDescription(boolean is32BitByCPUArch) { - boolean libsLoaded = true; + static volatile boolean smdQueried = false; + static MachineDescription.StaticConfig smd = null; + + public static MachineDescription.StaticConfig getStatic() { + if(!smdQueried) { + synchronized(MachineDescription.class) { // volatile dbl-checked-locking OK + if(!smdQueried) { + smd = getStaticImpl(); + smdQueried=true; + } + } + } + return smd; + } + private static MachineDescription.StaticConfig getStaticImpl() { + if(Platform.isCPUArch32Bit()) { + if(Platform.getCPUFamily() == Platform.CPUFamily.ARM && Platform.isLittleEndian()) { + return StaticConfig.ARMle_EABI; + } else if(Platform.getOSType() == Platform.OSType.WINDOWS) { + return StaticConfig.X86_32_WINDOWS; + } + return StaticConfig.X86_32_UNIX; + } else { + if(Platform.getOSType() == Platform.OSType.WINDOWS) { + return StaticConfig.X86_64_WINDOWS; + } + return StaticConfig.X86_64_UNIX; + } + } + + static volatile boolean rmdQueried = false; + static MachineDescription rmd = null; + + public static MachineDescription getRuntime() { + if(!rmdQueried) { + synchronized(MachineDescription.class) { // volatile dbl-checked-locking OK + if(!rmdQueried) { + rmd = getRuntimeImpl(); + rmdQueried=true; + } + } + } + return rmd; + } + private static MachineDescription getRuntimeImpl() { try { NativeLibrary.ensureNativeLibLoaded(); } catch (UnsatisfiedLinkError err) { - libsLoaded = false; + return null; } - if(libsLoaded) { - int pointerSizeInBytes = getPointerSizeInBytesImpl(); - switch(pointerSizeInBytes) { - case 4: - case 8: - break; - default: - throw new RuntimeException("Unsupported pointer size "+pointerSizeInBytes+"bytes, please implement."); - } - - final long pageSizeL = getPageSizeInBytesImpl(); - if(Integer.MAX_VALUE < pageSizeL) { - throw new InternalError("PageSize exceeds integer value: " + pageSizeL); - } - - return getMachineDescriptionImpl(pointerSizeInBytes, (int) pageSizeL); - } else { - return MachineDescription.createStatic(is32BitByCPUArch); + int pointerSizeInBytes = getPointerSizeInBytesImpl(); + switch(pointerSizeInBytes) { + case 4: + case 8: + break; + default: + throw new RuntimeException("Unsupported pointer size "+pointerSizeInBytes+"bytes, please implement."); } - } - private static MachineDescription getMachineDescriptionImpl(int pointerSize, int pageSize) { + final long pageSizeL = getPageSizeInBytesImpl(); + if(Integer.MAX_VALUE < pageSizeL) { + throw new InternalError("PageSize exceeds integer value: " + pageSizeL); + } + // size: int, long, float, double, pointer, pageSize // alignment: int8, int16, int32, int64, int, long, float, double, pointer return new MachineDescription( - true /* runtime validated */, MachineDescription.queryIsLittleEndian(), + true /* runtime validated */, Platform.isLittleEndian(), getSizeOfIntImpl(), getSizeOfLongImpl(), - getSizeOfFloatImpl(), getSizeOfDoubleImpl(), getSizeOfLongDoubleImpl(), pointerSize, pageSize, + getSizeOfFloatImpl(), getSizeOfDoubleImpl(), getSizeOfLongDoubleImpl(), + pointerSizeInBytes, (int)pageSizeL, getAlignmentInt8Impl(), getAlignmentInt16Impl(), getAlignmentInt32Impl(), getAlignmentInt64Impl(), getAlignmentIntImpl(), getAlignmentLongImpl(), getAlignmentFloatImpl(), getAlignmentDoubleImpl(), getAlignmentLongDoubleImpl(), getAlignmentPointerImpl()); } - + private static native int getPointerSizeInBytesImpl(); private static native long getPageSizeInBytesImpl(); -- cgit v1.2.3