From c91f003551542c2aab62dd8ef89a7894c7e50689 Mon Sep 17 00:00:00 2001 From: Kenneth Russel Date: Mon, 15 Jun 2009 22:42:48 +0000 Subject: Copied JOGL_2_SANDBOX r145 on to trunk; JOGL_2_SANDBOX branch is now closed git-svn-id: file:///usr/local/projects/SUN/JOGL/git-svn/../svn-server-sync/gluegen/trunk@147 a78bb65f-1512-4460-ba86-f6dc96a7bf27 --- .../runtime/BufferFactory.java.javame_cdc_fp | 230 +++++++++++ .../sun/gluegen/runtime/BufferFactory.java.javase | 299 +++++++++++++++ .../sun/gluegen/runtime/BufferFactoryInternal.java | 115 ++++++ src/java/com/sun/gluegen/runtime/CPU.java | 85 +++++ .../com/sun/gluegen/runtime/DynamicLinker.java | 50 +++ .../sun/gluegen/runtime/DynamicLookupHelper.java | 53 +++ .../gluegen/runtime/MacOSXDynamicLinkerImpl.java | 60 +++ .../com/sun/gluegen/runtime/NativeLibLoader.java | 105 +++++ .../com/sun/gluegen/runtime/NativeLibrary.java | 425 +++++++++++++++++++++ .../com/sun/gluegen/runtime/ProcAddressHelper.java | 125 ++++++ .../com/sun/gluegen/runtime/ProcAddressTable.java | 41 ++ .../runtime/StructAccessor.java.javame_cdc_fp | 135 +++++++ .../sun/gluegen/runtime/StructAccessor.java.javase | 191 +++++++++ .../sun/gluegen/runtime/UnixDynamicLinkerImpl.java | 66 ++++ .../gluegen/runtime/WindowsDynamicLinkerImpl.java | 49 +++ .../gluegen/runtime/opengl/GLExtensionNames.java | 187 +++++++++ .../runtime/opengl/GLProcAddressHelper.java | 110 ++++++ 17 files changed, 2326 insertions(+) create mode 100755 src/java/com/sun/gluegen/runtime/BufferFactory.java.javame_cdc_fp create mode 100755 src/java/com/sun/gluegen/runtime/BufferFactory.java.javase create mode 100755 src/java/com/sun/gluegen/runtime/BufferFactoryInternal.java create mode 100755 src/java/com/sun/gluegen/runtime/CPU.java create mode 100755 src/java/com/sun/gluegen/runtime/DynamicLinker.java create mode 100755 src/java/com/sun/gluegen/runtime/DynamicLookupHelper.java create mode 100755 src/java/com/sun/gluegen/runtime/MacOSXDynamicLinkerImpl.java create mode 100755 src/java/com/sun/gluegen/runtime/NativeLibLoader.java create mode 100755 src/java/com/sun/gluegen/runtime/NativeLibrary.java create mode 100644 src/java/com/sun/gluegen/runtime/ProcAddressHelper.java create mode 100644 src/java/com/sun/gluegen/runtime/ProcAddressTable.java create mode 100755 src/java/com/sun/gluegen/runtime/StructAccessor.java.javame_cdc_fp create mode 100755 src/java/com/sun/gluegen/runtime/StructAccessor.java.javase create mode 100755 src/java/com/sun/gluegen/runtime/UnixDynamicLinkerImpl.java create mode 100755 src/java/com/sun/gluegen/runtime/WindowsDynamicLinkerImpl.java create mode 100644 src/java/com/sun/gluegen/runtime/opengl/GLExtensionNames.java create mode 100644 src/java/com/sun/gluegen/runtime/opengl/GLProcAddressHelper.java (limited to 'src/java/com/sun/gluegen/runtime') diff --git a/src/java/com/sun/gluegen/runtime/BufferFactory.java.javame_cdc_fp b/src/java/com/sun/gluegen/runtime/BufferFactory.java.javame_cdc_fp new file mode 100755 index 0000000..622cc37 --- /dev/null +++ b/src/java/com/sun/gluegen/runtime/BufferFactory.java.javame_cdc_fp @@ -0,0 +1,230 @@ +/* + * 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.sun.gluegen.runtime; + +import java.nio.*; + +public class BufferFactory { + public static final int SIZEOF_BYTE = 1; + public static final int SIZEOF_SHORT = 2; + public static final int SIZEOF_INT = 4; + public static final int SIZEOF_FLOAT = 4; + + public static ByteBuffer newDirectByteBuffer(int size) { + ByteBuffer buf = ByteBuffer.allocateDirect(size); + return buf; + } + + /** Helper routine to set a ByteBuffer to the native byte order, if + that operation is supported by the underlying NIO + implementation. */ + public static ByteBuffer nativeOrder(ByteBuffer buf) { + return buf; + } + + /** Helper routine to tell whether a buffer is direct or not. Null + pointers are considered direct. isDirect() should really be + public in Buffer and not replicated in all subclasses. */ + public static boolean isDirect(Buffer buf) { + if (buf == null) { + return true; + } + if (buf instanceof ByteBuffer) { + return ((ByteBuffer) buf).isDirect(); + } else if (buf instanceof FloatBuffer) { + return ((FloatBuffer) buf).isDirect(); + } else if (buf instanceof ShortBuffer) { + return ((ShortBuffer) buf).isDirect(); + } else if (buf instanceof IntBuffer) { + return ((IntBuffer) buf).isDirect(); + } + throw new RuntimeException("Unexpected buffer type " + buf.getClass().getName()); + } + + + /** Helper routine to get the Buffer byte offset by taking into + account the Buffer position and the underlying type. This is + the total offset for Direct Buffers. */ + + public static int getDirectBufferByteOffset(Buffer buf) { + if(buf == null) { + return 0; + } + if(buf instanceof ByteBuffer) { + return (buf.position()); + } else if (buf instanceof FloatBuffer) { + return (buf.position() * SIZEOF_FLOAT); + } else if (buf instanceof IntBuffer) { + return (buf.position() * SIZEOF_INT); + } else if (buf instanceof ShortBuffer) { + return (buf.position() * SIZEOF_SHORT); + } + + throw new RuntimeException("Disallowed array backing store type in buffer " + + buf.getClass().getName()); + } + + + /** Helper routine to return the array backing store reference from + a Buffer object. */ + + public static Object getArray(Buffer buf) { + if (buf == null) { + return null; + } + if(buf instanceof ByteBuffer) { + return ((ByteBuffer) buf).array(); + } else if (buf instanceof FloatBuffer) { + return ((FloatBuffer) buf).array(); + } else if (buf instanceof IntBuffer) { + return ((IntBuffer) buf).array(); + } else if (buf instanceof ShortBuffer) { + return ((ShortBuffer) buf).array(); + } + + throw new RuntimeException("Disallowed array backing store type in buffer " + + buf.getClass().getName()); + } + + + /** Helper routine to get the full byte offset from the beginning of + the array that is the storage for the indirect Buffer + object. The array offset also includes the position offset + within the buffer, in addition to any array offset. */ + + public static int getIndirectBufferByteOffset(Buffer buf) { + if(buf == null) { + return 0; + } + int pos = buf.position(); + if(buf instanceof ByteBuffer) { + return (((ByteBuffer)buf).arrayOffset() + pos); + } else if(buf instanceof FloatBuffer) { + return (SIZEOF_FLOAT*(((FloatBuffer)buf).arrayOffset() + pos)); + } else if(buf instanceof IntBuffer) { + return (SIZEOF_INT*(((IntBuffer)buf).arrayOffset() + pos)); + } else if(buf instanceof ShortBuffer) { + return (SIZEOF_SHORT*(((ShortBuffer)buf).arrayOffset() + pos)); + } + + throw new RuntimeException("Unknown buffer type " + buf.getClass().getName()); + } + + public static void rangeCheck(byte[] array, int offset, int minElementsRemaining) { + if (array == null) { + return; + } + + if (array.length < offset + minElementsRemaining) { + throw new ArrayIndexOutOfBoundsException("Required " + minElementsRemaining + " elements in array, only had " + (array.length - offset)); + } + } + + public static void rangeCheck(char[] array, int offset, int minElementsRemaining) { + if (array == null) { + return; + } + + if (array.length < offset + minElementsRemaining) { + throw new ArrayIndexOutOfBoundsException("Required " + minElementsRemaining + " elements in array, only had " + (array.length - offset)); + } + } + + public static void rangeCheck(short[] array, int offset, int minElementsRemaining) { + if (array == null) { + return; + } + + if (array.length < offset + minElementsRemaining) { + throw new ArrayIndexOutOfBoundsException("Required " + minElementsRemaining + " elements in array, only had " + (array.length - offset)); + } + } + + public static void rangeCheck(int[] array, int offset, int minElementsRemaining) { + if (array == null) { + return; + } + + if (array.length < offset + minElementsRemaining) { + throw new ArrayIndexOutOfBoundsException("Required " + minElementsRemaining + " elements in array, only had " + (array.length - offset)); + } + } + + public static void rangeCheck(float[] array, int offset, int minElementsRemaining) { + if (array == null) { + return; + } + + if (array.length < offset + minElementsRemaining) { + throw new ArrayIndexOutOfBoundsException("Required " + minElementsRemaining + " elements in array, only had " + (array.length - offset)); + } + } + + public static void rangeCheck(Buffer buffer, int minElementsRemaining) { + if (buffer == null) { + return; + } + + if (buffer.remaining() < minElementsRemaining) { + throw new IndexOutOfBoundsException("Required " + minElementsRemaining + " remaining elements in buffer, only had " + buffer.remaining()); + } + } + + public static void rangeCheckBytes(Buffer buffer, int minBytesRemaining) { + if (buffer == null) { + return; + } + + int elementsRemaining = buffer.remaining(); + int bytesRemaining = 0; + if (buffer instanceof ByteBuffer) { + bytesRemaining = elementsRemaining; + } else if (buffer instanceof FloatBuffer) { + bytesRemaining = elementsRemaining * SIZEOF_FLOAT; + } else if (buffer instanceof IntBuffer) { + bytesRemaining = elementsRemaining * SIZEOF_INT; + } else if (buffer instanceof ShortBuffer) { + bytesRemaining = elementsRemaining * SIZEOF_SHORT; + } + if (bytesRemaining < minBytesRemaining) { + throw new IndexOutOfBoundsException("Required " + minBytesRemaining + " remaining bytes in buffer, only had " + bytesRemaining); + } + } +} diff --git a/src/java/com/sun/gluegen/runtime/BufferFactory.java.javase b/src/java/com/sun/gluegen/runtime/BufferFactory.java.javase new file mode 100755 index 0000000..3986bf6 --- /dev/null +++ b/src/java/com/sun/gluegen/runtime/BufferFactory.java.javase @@ -0,0 +1,299 @@ +/* + * 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.sun.gluegen.runtime; + +import java.nio.*; + +public class BufferFactory { + public static final int SIZEOF_BYTE = 1; + public static final int SIZEOF_SHORT = 2; + public static final int SIZEOF_CHAR = 2; + public static final int SIZEOF_INT = 4; + public static final int SIZEOF_FLOAT = 4; + public static final int SIZEOF_LONG = 8; + public static final int SIZEOF_DOUBLE = 8; + + public static ByteBuffer newDirectByteBuffer(int size) { + ByteBuffer buf = ByteBuffer.allocateDirect(size); + buf.order(ByteOrder.nativeOrder()); + return buf; + } + + /** Helper routine to set a ByteBuffer to the native byte order, if + that operation is supported by the underlying NIO + implementation. */ + public static ByteBuffer nativeOrder(ByteBuffer buf) { + return buf.order(ByteOrder.nativeOrder()); + } + + /** Helper routine to tell whether a buffer is direct or not. Null + pointers are considered direct. isDirect() should really be + public in Buffer and not replicated in all subclasses. */ + public static boolean isDirect(Buffer buf) { + if (buf == null) { + return true; + } + if (buf instanceof ByteBuffer) { + return ((ByteBuffer) buf).isDirect(); + } else if (buf instanceof FloatBuffer) { + return ((FloatBuffer) buf).isDirect(); + } else if (buf instanceof DoubleBuffer) { + return ((DoubleBuffer) buf).isDirect(); + } else if (buf instanceof CharBuffer) { + return ((CharBuffer) buf).isDirect(); + } else if (buf instanceof ShortBuffer) { + return ((ShortBuffer) buf).isDirect(); + } else if (buf instanceof IntBuffer) { + return ((IntBuffer) buf).isDirect(); + } else if (buf instanceof LongBuffer) { + return ((LongBuffer) buf).isDirect(); + } + throw new RuntimeException("Unexpected buffer type " + buf.getClass().getName()); + } + + + /** Helper routine to get the Buffer byte offset by taking into + account the Buffer position and the underlying type. This is + the total offset for Direct Buffers. */ + + public static int getDirectBufferByteOffset(Buffer buf) { + if(buf == null) { + return 0; + } + if(buf instanceof ByteBuffer) { + return (buf.position()); + } else if (buf instanceof FloatBuffer) { + return (buf.position() * SIZEOF_FLOAT); + } else if (buf instanceof IntBuffer) { + return (buf.position() * SIZEOF_INT); + } else if (buf instanceof ShortBuffer) { + return (buf.position() * SIZEOF_SHORT); + } else if (buf instanceof DoubleBuffer) { + return (buf.position() * SIZEOF_DOUBLE); + } else if (buf instanceof LongBuffer) { + return (buf.position() * SIZEOF_LONG); + } else if (buf instanceof CharBuffer) { + return (buf.position() * SIZEOF_CHAR); + } + + throw new RuntimeException("Disallowed array backing store type in buffer " + + buf.getClass().getName()); + } + + + /** Helper routine to return the array backing store reference from + a Buffer object. */ + + public static Object getArray(Buffer buf) { + if (buf == null) { + return null; + } + if(buf instanceof ByteBuffer) { + return ((ByteBuffer) buf).array(); + } else if (buf instanceof FloatBuffer) { + return ((FloatBuffer) buf).array(); + } else if (buf instanceof IntBuffer) { + return ((IntBuffer) buf).array(); + } else if (buf instanceof ShortBuffer) { + return ((ShortBuffer) buf).array(); + } else if (buf instanceof DoubleBuffer) { + return ((DoubleBuffer) buf).array(); + } else if (buf instanceof LongBuffer) { + return ((LongBuffer) buf).array(); + } else if (buf instanceof CharBuffer) { + return ((CharBuffer) buf).array(); + } + + throw new RuntimeException("Disallowed array backing store type in buffer " + + buf.getClass().getName()); + } + + + /** Helper routine to get the full byte offset from the beginning of + the array that is the storage for the indirect Buffer + object. The array offset also includes the position offset + within the buffer, in addition to any array offset. */ + + public static int getIndirectBufferByteOffset(Buffer buf) { + if(buf == null) { + return 0; + } + int pos = buf.position(); + if(buf instanceof ByteBuffer) { + return (((ByteBuffer)buf).arrayOffset() + pos); + } else if(buf instanceof FloatBuffer) { + return (SIZEOF_FLOAT*(((FloatBuffer)buf).arrayOffset() + pos)); + } else if(buf instanceof IntBuffer) { + return (SIZEOF_INT*(((IntBuffer)buf).arrayOffset() + pos)); + } else if(buf instanceof ShortBuffer) { + return (SIZEOF_SHORT*(((ShortBuffer)buf).arrayOffset() + pos)); + } else if(buf instanceof DoubleBuffer) { + return (SIZEOF_DOUBLE*(((DoubleBuffer)buf).arrayOffset() + pos)); + } else if(buf instanceof LongBuffer) { + return (SIZEOF_LONG*(((LongBuffer)buf).arrayOffset() + pos)); + } else if(buf instanceof CharBuffer) { + return (SIZEOF_CHAR*(((CharBuffer)buf).arrayOffset() + pos)); + } + + throw new RuntimeException("Unknown buffer type " + buf.getClass().getName()); + } + + public static void rangeCheck(byte[] array, int offset, int minElementsRemaining) { + if (array == null) { + return; + } + + if (array.length < offset + minElementsRemaining) { + throw new ArrayIndexOutOfBoundsException("Required " + minElementsRemaining + " elements in array, only had " + (array.length - offset)); + } + } + + public static void rangeCheck(char[] array, int offset, int minElementsRemaining) { + if (array == null) { + return; + } + + if (array.length < offset + minElementsRemaining) { + throw new ArrayIndexOutOfBoundsException("Required " + minElementsRemaining + " elements in array, only had " + (array.length - offset)); + } + } + + public static void rangeCheck(short[] array, int offset, int minElementsRemaining) { + if (array == null) { + return; + } + + if (array.length < offset + minElementsRemaining) { + throw new ArrayIndexOutOfBoundsException("Required " + minElementsRemaining + " elements in array, only had " + (array.length - offset)); + } + } + + public static void rangeCheck(int[] array, int offset, int minElementsRemaining) { + if (array == null) { + return; + } + + if (array.length < offset + minElementsRemaining) { + throw new ArrayIndexOutOfBoundsException("Required " + minElementsRemaining + " elements in array, only had " + (array.length - offset)); + } + } + + public static void rangeCheck(long[] array, int offset, int minElementsRemaining) { + if (array == null) { + return; + } + + if (array.length < offset + minElementsRemaining) { + throw new ArrayIndexOutOfBoundsException("Required " + minElementsRemaining + " elements in array, only had " + (array.length - offset)); + } + } + + public static void rangeCheck(float[] array, int offset, int minElementsRemaining) { + if (array == null) { + return; + } + + if (array.length < offset + minElementsRemaining) { + throw new ArrayIndexOutOfBoundsException("Required " + minElementsRemaining + " elements in array, only had " + (array.length - offset)); + } + } + + public static void rangeCheck(double[] array, int offset, int minElementsRemaining) { + if (array == null) { + return; + } + + if (array.length < offset + minElementsRemaining) { + throw new ArrayIndexOutOfBoundsException("Required " + minElementsRemaining + " elements in array, only had " + (array.length - offset)); + } + } + + public static void rangeCheck(Buffer buffer, int minElementsRemaining) { + if (buffer == null) { + return; + } + + if (buffer.remaining() < minElementsRemaining) { + throw new IndexOutOfBoundsException("Required " + minElementsRemaining + " remaining elements in buffer, only had " + buffer.remaining()); + } + } + + public static void rangeCheckBytes(Buffer buffer, int minBytesRemaining) { + if (buffer == null) { + return; + } + + int elementsRemaining = buffer.remaining(); + int bytesRemaining = 0; + if (buffer instanceof ByteBuffer) { + bytesRemaining = elementsRemaining; + } else if (buffer instanceof FloatBuffer) { + bytesRemaining = elementsRemaining * SIZEOF_FLOAT; + } else if (buffer instanceof IntBuffer) { + bytesRemaining = elementsRemaining * SIZEOF_INT; + } else if (buffer instanceof ShortBuffer) { + bytesRemaining = elementsRemaining * SIZEOF_SHORT; + } else if (buffer instanceof DoubleBuffer) { + bytesRemaining = elementsRemaining * SIZEOF_DOUBLE; + } else if (buffer instanceof LongBuffer) { + bytesRemaining = elementsRemaining * SIZEOF_LONG; + } else if (buffer instanceof CharBuffer) { + bytesRemaining = elementsRemaining * SIZEOF_CHAR; + } + if (bytesRemaining < minBytesRemaining) { + throw new IndexOutOfBoundsException("Required " + minBytesRemaining + " remaining bytes in buffer, only had " + bytesRemaining); + } + } + + public static LongBuffer asPointerBuffer(ByteBuffer src) { + if (CPU.is32Bit()) { + // Must convert each pointer from 32-bit to 64-bit + IntBuffer buf = src.asIntBuffer(); + int len = buf.capacity(); + LongBuffer res = LongBuffer.wrap(new long[len]); + for (int i = 0; i < len; i++) { + res.put(i, buf.get(i)); + } + return res; + } else { + return src.asLongBuffer(); + } + } +} diff --git a/src/java/com/sun/gluegen/runtime/BufferFactoryInternal.java b/src/java/com/sun/gluegen/runtime/BufferFactoryInternal.java new file mode 100755 index 0000000..7a0e6a8 --- /dev/null +++ b/src/java/com/sun/gluegen/runtime/BufferFactoryInternal.java @@ -0,0 +1,115 @@ +/* + + * Copyright (c) 2006 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.sun.gluegen.runtime; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.nio.*; +import sun.misc.Unsafe; + +public class BufferFactoryInternal { + private static final long addressFieldOffset; + private static final Constructor directByteBufferConstructor; + + static { + try { + Field f = Buffer.class.getDeclaredField("address"); + addressFieldOffset = UnsafeAccess.getUnsafe().objectFieldOffset(f); + + Class directByteBufferClass = Class.forName("java.nio.DirectByteBuffer"); + directByteBufferConstructor = directByteBufferClass.getDeclaredConstructor(new Class[] { Long.TYPE, Integer.TYPE }); + directByteBufferConstructor.setAccessible(true); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + public static long getDirectBufferAddress(Buffer buf) { + return ((buf == null) ? 0 : UnsafeAccess.getUnsafe().getLong(buf, addressFieldOffset)); + } + + public static ByteBuffer newDirectByteBuffer(long address, int capacity) { + try { + if (address == 0) { + return null; + } + return (ByteBuffer) directByteBufferConstructor.newInstance(new Object[] { new Long(address), new Integer(capacity) }); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + public static long newCString(String str) { + byte[] strBytes = str.getBytes(); + long strBlock = UnsafeAccess.getUnsafe().allocateMemory(strBytes.length+1); + for (int i = 0; i < strBytes.length; i++) { + UnsafeAccess.getUnsafe().putByte(strBlock+i, strBytes[i]); + } + UnsafeAccess.getUnsafe().putByte(strBlock+strBytes.length, (byte)0); // null termination + return strBlock; + } + + public static void freeCString(long cStr) { + UnsafeAccess.getUnsafe().freeMemory(cStr); + } + + public static String newJavaString(long cStr) { + if (cStr == 0) { + return null; + } + int numChars = 0; + while (UnsafeAccess.getUnsafe().getByte(cStr + numChars) != 0) { + ++numChars; + } + byte[] bytes = new byte[numChars]; + for (int i = 0; i < numChars; i++) { + bytes[i] = UnsafeAccess.getUnsafe().getByte(cStr + i); + } + return new String(bytes); + } + + public static int arrayBaseOffset(Object array) { + return UnsafeAccess.getUnsafe().arrayBaseOffset(array.getClass()); + } + public static int arrayIndexScale(Object array) { + return UnsafeAccess.getUnsafe().arrayIndexScale(array.getClass()); + } +} diff --git a/src/java/com/sun/gluegen/runtime/CPU.java b/src/java/com/sun/gluegen/runtime/CPU.java new file mode 100755 index 0000000..0456e9f --- /dev/null +++ b/src/java/com/sun/gluegen/runtime/CPU.java @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2005 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.sun.gluegen.runtime; + +/** Provides information to autogenerated struct accessors about what + kind of data model (32- or 64-bit) is being used by the currently + running process. */ + +public class CPU { + private static boolean is32Bit; + + static { + // We don't seem to need an AccessController.doPrivileged() block + // here as these system properties are visible even to unsigned + // applets + // Note: this code is replicated in StructLayout.java + String os = System.getProperty("os.name").toLowerCase(); + String cpu = System.getProperty("os.arch").toLowerCase(); + if ((os.startsWith("windows") && cpu.equals("x86")) || + (os.startsWith("windows") && cpu.equals("arm")) || + (os.startsWith("linux") && cpu.equals("i386")) || + (os.startsWith("linux") && cpu.equals("x86")) || + (os.startsWith("mac os") && cpu.equals("ppc")) || + (os.startsWith("mac os") && cpu.equals("i386")) || + (os.startsWith("darwin") && cpu.equals("ppc")) || + (os.startsWith("darwin") && cpu.equals("i386")) || + (os.startsWith("sunos") && cpu.equals("sparc")) || + (os.startsWith("sunos") && cpu.equals("x86")) || + (os.startsWith("freebsd") && cpu.equals("i386")) || + (os.startsWith("hp-ux") && cpu.equals("pa_risc2.0"))) { + is32Bit = true; + } else if ((os.startsWith("windows") && cpu.equals("amd64")) || + (os.startsWith("linux") && cpu.equals("amd64")) || + (os.startsWith("linux") && cpu.equals("x86_64")) || + (os.startsWith("linux") && cpu.equals("ia64")) || + (os.startsWith("mac os") && cpu.equals("x86_64")) || + (os.startsWith("darwin") && cpu.equals("x86_64")) || + (os.startsWith("sunos") && cpu.equals("sparcv9")) || + (os.startsWith("sunos") && cpu.equals("amd64"))) { + } else { + throw new RuntimeException("Please port CPU detection (32/64 bit) to your platform (" + os + "/" + cpu + ")"); + } + } + + public static boolean is32Bit() { + return is32Bit; + } +} diff --git a/src/java/com/sun/gluegen/runtime/DynamicLinker.java b/src/java/com/sun/gluegen/runtime/DynamicLinker.java new file mode 100755 index 0000000..871be04 --- /dev/null +++ b/src/java/com/sun/gluegen/runtime/DynamicLinker.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2006 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.sun.gluegen.runtime; + +/** Provides an abstract interface to the OS's low-level dynamic + linking functionality. */ + +interface DynamicLinker { + public long openLibraryGlobal(String pathname, boolean debug); + public long openLibraryLocal(String pathname, boolean debug); + public long lookupSymbol(long libraryHandle, String symbolName); + public void closeLibrary(long libraryHandle); +} diff --git a/src/java/com/sun/gluegen/runtime/DynamicLookupHelper.java b/src/java/com/sun/gluegen/runtime/DynamicLookupHelper.java new file mode 100755 index 0000000..ad3de7f --- /dev/null +++ b/src/java/com/sun/gluegen/runtime/DynamicLookupHelper.java @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2003-2005 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.sun.gluegen.runtime; + +/** Interface callers may use to use the ProcAddressHelper's {@link + ProcAddressHelper#resetProcAddressTable resetProcAddressTable} + helper method to install function pointers into a + ProcAddressTable. This must typically be written with native + code. */ + +public interface DynamicLookupHelper { + /** + * Try to fetch the function pointer for function 'funcName'. + */ + public long dynamicLookupFunction(String funcName); +} diff --git a/src/java/com/sun/gluegen/runtime/MacOSXDynamicLinkerImpl.java b/src/java/com/sun/gluegen/runtime/MacOSXDynamicLinkerImpl.java new file mode 100755 index 0000000..83ebd7a --- /dev/null +++ b/src/java/com/sun/gluegen/runtime/MacOSXDynamicLinkerImpl.java @@ -0,0 +1,60 @@ +/* !---- DO NOT EDIT: This file autogenerated by com\sun\gluegen\JavaEmitter.java on Mon Jul 31 16:27:00 PDT 2006 ----! */ + +package com.sun.gluegen.runtime; + +import com.sun.gluegen.runtime.*; + +public class MacOSXDynamicLinkerImpl implements DynamicLinker +{ + + public static final int RTLD_LAZY = 0x1; + public static final int RTLD_NOW = 0x2; + public static final int RTLD_LOCAL = 0x4; + public static final int RTLD_GLOBAL = 0x8; + + /** Interface to C language function:
int dlclose(void * __handle); */ + private static native int dlclose(long __handle); + + /** Interface to C language function:
char * dlerror(void); */ + private static native java.lang.String dlerror(); + + /** Interface to C language function:
void * dlopen(const char * __path, int __mode); */ + private static native long dlopen(java.lang.String __path, int __mode); + + /** Interface to C language function:
void * dlsym(void * __handle, const char * __symbol); */ + private static native long dlsym(long __handle, java.lang.String __symbol); + + + // --- Begin CustomJavaCode .cfg declarations + public long openLibraryLocal(String pathname, boolean debug) { + // Note we use RTLD_LOCAL visibility to _NOT_ allow this functionality to + // be used to pre-resolve dependent libraries of JNI code without + // requiring that all references to symbols in those libraries be + // looked up dynamically via the ProcAddressTable mechanism; in + // other words, one can actually link against the library instead of + // having to dlsym all entry points. System.loadLibrary() uses + // RTLD_LOCAL visibility so can't be used for this purpose. + return dlopen(pathname, RTLD_LAZY | RTLD_LOCAL); + } + + public long openLibraryGlobal(String pathname, boolean debug) { + // Note we use RTLD_GLOBAL visibility to allow this functionality to + // be used to pre-resolve dependent libraries of JNI code without + // requiring that all references to symbols in those libraries be + // looked up dynamically via the ProcAddressTable mechanism; in + // other words, one can actually link against the library instead of + // having to dlsym all entry points. System.loadLibrary() uses + // RTLD_LOCAL visibility so can't be used for this purpose. + return dlopen(pathname, RTLD_LAZY | RTLD_GLOBAL); + } + + public long lookupSymbol(long libraryHandle, String symbolName) { + return dlsym(libraryHandle, symbolName); + } + + public void closeLibrary(long libraryHandle) { + dlclose(libraryHandle); + } + // ---- End CustomJavaCode .cfg declarations + +} // end of class MacOSXDynamicLinkerImpl diff --git a/src/java/com/sun/gluegen/runtime/NativeLibLoader.java b/src/java/com/sun/gluegen/runtime/NativeLibLoader.java new file mode 100755 index 0000000..0e6e3da --- /dev/null +++ b/src/java/com/sun/gluegen/runtime/NativeLibLoader.java @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2006 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.sun.gluegen.runtime; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.security.*; + +/** Class providing control over whether GlueGen loads the native code + associated with the NativeLibrary implementation. Alternative app + launchers such as those running within applets may want to disable + this default loading behavior and load the native code via another + (manual) mechanism. */ +public class NativeLibLoader { + private static volatile boolean loadingEnabled = true; + private static volatile boolean didLoading; + + public static void disableLoading() { + loadingEnabled = false; + } + + public static void enableLoading() { + loadingEnabled = true; + } + + public static void loadGlueGenRT() { + if (!didLoading && loadingEnabled) { + synchronized (NativeLibLoader.class) { + if (!didLoading && loadingEnabled) { + didLoading = true; + AccessController.doPrivileged(new PrivilegedAction() { + public Object run() { + loadLibraryInternal("gluegen-rt"); + return null; + } + }); + } + } + } + } + + private static void loadLibraryInternal(String libraryName) { + String sunAppletLauncher = System.getProperty("sun.jnlp.applet.launcher"); + boolean usingJNLPAppletLauncher = Boolean.valueOf(sunAppletLauncher).booleanValue(); + + if (usingJNLPAppletLauncher) { + try { + Class jnlpAppletLauncherClass = Class.forName("org.jdesktop.applet.util.JNLPAppletLauncher"); + Method jnlpLoadLibraryMethod = jnlpAppletLauncherClass.getDeclaredMethod("loadLibrary", new Class[] { String.class }); + jnlpLoadLibraryMethod.invoke(null, new Object[] { libraryName }); + } catch (Exception e) { + Throwable t = e; + if (t instanceof InvocationTargetException) { + t = ((InvocationTargetException) t).getTargetException(); + } + if (t instanceof Error) + throw (Error) t; + if (t instanceof RuntimeException) { + throw (RuntimeException) t; + } + // Throw UnsatisfiedLinkError for best compatibility with System.loadLibrary() + throw (UnsatisfiedLinkError) new UnsatisfiedLinkError().initCause(e); + } + } else { + System.loadLibrary(libraryName); + } + } +} diff --git a/src/java/com/sun/gluegen/runtime/NativeLibrary.java b/src/java/com/sun/gluegen/runtime/NativeLibrary.java new file mode 100755 index 0000000..22063a5 --- /dev/null +++ b/src/java/com/sun/gluegen/runtime/NativeLibrary.java @@ -0,0 +1,425 @@ +/* + * Copyright (c) 2006 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.sun.gluegen.runtime; + +import java.io.*; +import java.lang.reflect.*; +import java.security.*; +import java.util.*; + +/** Provides low-level, relatively platform-independent access to + shared ("native") libraries. The core library routines + System.load() and System.loadLibrary() + in general provide suitable functionality for applications using + native code, but are not flexible enough to support certain kinds + of glue code generation and deployment strategies. This class + supports direct linking of native libraries to other shared + objects not necessarily installed on the system (in particular, + via the use of dlopen(RTLD_GLOBAL) on Unix platforms) as well as + manual lookup of function names to support e.g. GlueGen's + ProcAddressTable glue code generation style without additional + supporting code needed in the generated library. */ + +public class NativeLibrary { + private static final int WINDOWS = 1; + private static final int UNIX = 2; + private static final int MACOSX = 3; + private static boolean DEBUG; + 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("gluegen.debug.NativeLibrary") != null); + + return null; + } + }); + // Instantiate dynamic linker implementation + switch (platform) { + 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: + dynLink = new MacOSXDynamicLinkerImpl(); + prefixes = new String[] { "lib", "" }; + suffixes = new String[] { ".dylib", ".jnilib", "" }; + break; + default: + throw new InternalError("Platform not initialized properly"); + } + } + + // Platform-specific representation for the handle to the open + // library. This is an HMODULE on Windows and a void* (the result of + // a dlopen() call) on Unix and Mac OS X platforms. + private long libraryHandle; + + // May as well keep around the path to the library we opened + private String libraryPath; + + // Private constructor to prevent arbitrary instances from floating around + private NativeLibrary(long libraryHandle, String libraryPath) { + this.libraryHandle = libraryHandle; + this.libraryPath = libraryPath; + } + + /** Opens the given native library, assuming it has the same base + name on all platforms, looking first in the system's search + path, and in the context of the specified ClassLoader, which is + used to help find the library in the case of e.g. Java Web Start. */ + public static NativeLibrary open(String libName, ClassLoader loader) { + return open(libName, libName, libName, true, loader, true); + } + + /** Opens the given native library, assuming it has the same base + name on all platforms, looking first in the system's search + path, and in the context of the specified ClassLoader, which is + used to help find the library in the case of e.g. Java Web Start. */ + public static NativeLibrary open(String libName, ClassLoader loader, boolean global) { + return open(libName, libName, libName, true, loader, global); + } + + /** Opens the given native library, assuming it has the given base + names (no "lib" prefix or ".dll/.so/.dylib" suffix) on the + Windows, Unix and Mac OS X platforms, respectively, and in the + context of the specified ClassLoader, which is used to help find + the library in the case of e.g. Java Web Start. The + searchSystemPathFirst argument changes the behavior to first + search the default system path rather than searching it last. + Note that we do not currently handle DSO versioning on Unix. + Experience with JOAL and OpenAL has shown that it is extremely + problematic to rely on a specific .so version (for one thing, + ClassLoader.findLibrary on Unix doesn't work with files not + ending in .so, for example .so.0), and in general if this + dynamic loading facility is used correctly the version number + will be irrelevant. + */ + public static NativeLibrary open(String windowsLibName, + String unixLibName, + String macOSXLibName, + boolean searchSystemPathFirst, + ClassLoader loader) { + return open(windowsLibName, unixLibName, macOSXLibName, searchSystemPathFirst, loader, true); + } + + public static NativeLibrary open(String windowsLibName, + String unixLibName, + String macOSXLibName, + boolean searchSystemPathFirst, + ClassLoader loader, boolean global) { + List possiblePaths = enumerateLibraryPaths(windowsLibName, + unixLibName, + macOSXLibName, + searchSystemPathFirst, + loader); + // Iterate down these and see which one if any we can actually find. + for (Iterator iter = possiblePaths.iterator(); iter.hasNext(); ) { + String path = (String) iter.next(); + if (DEBUG) { + System.out.println("Trying to load " + path); + } + ensureNativeLibLoaded(); + long res; + if(global) { + res = dynLink.openLibraryGlobal(path, DEBUG); + } else { + res = dynLink.openLibraryLocal(path, DEBUG); + } + if (res != 0) { + if (DEBUG) { + System.out.println("Successfully loaded " + path + ": res = 0x" + Long.toHexString(res)); + } + return new NativeLibrary(res, path); + } + } + + if (DEBUG) { + System.out.println("Did not succeed in loading (" + windowsLibName + ", " + unixLibName + ", " + macOSXLibName + ")"); + } + + // For now, just return null to indicate the open operation didn't + // succeed (could also throw an exception if we could tell which + // of the openLibrary operations actually failed) + return null; + } + + /** Looks up the given function name in this native library. */ + public long lookupFunction(String functionName) { + if (libraryHandle == 0) + throw new RuntimeException("Library is not open"); + return dynLink.lookupSymbol(libraryHandle, functionName); + } + + /** Retrieves the low-level library handle from this NativeLibrary + object. On the Windows platform this is an HMODULE, and on Unix + and Mac OS X platforms the void* result of calling dlopen(). */ + public long getLibraryHandle() { + return libraryHandle; + } + + /** Retrieves the path under which this library was opened. */ + public String getLibraryPath() { + return libraryPath; + } + + /** Closes this native library. Further lookup operations are not + allowed after calling this method. */ + public void close() { + if (libraryHandle == 0) + throw new RuntimeException("Library already closed"); + long handle = libraryHandle; + libraryHandle = 0; + dynLink.closeLibrary(handle); + } + + /** Given the base library names (no prefixes/suffixes) for the + various platforms, enumerate the possible locations and names of + the indicated native library on the system. */ + private static List enumerateLibraryPaths(String windowsLibName, + String unixLibName, + String macOSXLibName, + boolean searchSystemPathFirst, + ClassLoader loader) { + List paths = new ArrayList(); + String libName = selectName(windowsLibName, unixLibName, macOSXLibName); + if (libName == null) + return paths; + + // Allow user's full path specification to override our building of paths + File file = new File(libName); + if (file.isAbsolute()) { + paths.add(libName); + return paths; + } + + String[] baseNames = buildNames(libName); + + if (searchSystemPathFirst) { + // Add just the library names to use the OS's search algorithm + for (int i = 0; i < baseNames.length; i++) { + paths.add(baseNames[i]); + } + } + + // The idea to ask the ClassLoader to find the library is borrowed + // from the LWJGL library + String clPath = getPathFromClassLoader(libName, loader); + if (DEBUG) { + System.out.println("Class loader path to " + libName + ": " + clPath); + } + if (clPath != null) { + paths.add(clPath); + } + + // Add entries from java.library.path + String javaLibraryPath = + (String) AccessController.doPrivileged(new PrivilegedAction() { + public Object run() { + return System.getProperty("java.library.path"); + } + }); + if (javaLibraryPath != null) { + StringTokenizer tokenizer = new StringTokenizer(javaLibraryPath, File.pathSeparator); + while (tokenizer.hasMoreTokens()) { + addPaths(tokenizer.nextToken(), baseNames, paths); + } + } + + // Add current working directory + String userDir = + (String) AccessController.doPrivileged(new PrivilegedAction() { + public Object run() { + return System.getProperty("user.dir"); + } + }); + addPaths(userDir, baseNames, paths); + + // Add probable Mac OS X-specific paths + if (platform == MACOSX) { + // Add historical location + addPaths("/Library/Frameworks/" + libName + ".Framework", baseNames, paths); + // Add current location + addPaths("/System/Library/Frameworks/" + libName + ".Framework", baseNames, paths); + } + + if (!searchSystemPathFirst) { + // Add just the library names to use the OS's search algorithm + for (int i = 0; i < baseNames.length; i++) { + paths.add(baseNames[i]); + } + } + + return paths; + } + + + private static String selectName(String windowsLibName, + String unixLibName, + String macOSXLibName) { + switch (platform) { + case WINDOWS: + return windowsLibName; + case UNIX: + return unixLibName; + case MACOSX: + return macOSXLibName; + default: + throw new InternalError(); + } + } + + private static String[] buildNames(String libName) { + // If the library name already has the prefix / suffix added + // (principally because we want to force a version number on Unix + // operating systems) then just return the library name. + if (libName.startsWith(prefixes[0])) { + if (libName.endsWith(suffixes[0])) { + return new String[] { libName }; + } + + int idx = libName.indexOf(suffixes[0]); + boolean ok = true; + if (idx >= 0) { + // Check to see if everything after it is a Unix version number + for (int i = idx + suffixes[0].length(); + i < libName.length(); + i++) { + char c = libName.charAt(i); + if (!(c == '.' || (c >= '0' && c <= '9'))) { + ok = false; + break; + } + } + if (ok) { + return new String[] { libName }; + } + } + } + + String[] res = new String[prefixes.length * suffixes.length]; + int idx = 0; + for (int i = 0; i < prefixes.length; i++) { + for (int j = 0; j < suffixes.length; j++) { + res[idx++] = prefixes[i] + libName + suffixes[j]; + } + } + return res; + } + + private static void addPaths(String path, String[] baseNames, List paths) { + for (int j = 0; j < baseNames.length; j++) { + paths.add(path + File.separator + baseNames[j]); + } + } + + private static boolean initializedFindLibraryMethod = false; + private static Method findLibraryMethod = null; + private static String getPathFromClassLoader(final String libName, final ClassLoader loader) { + if (loader == null) + return null; + if (!initializedFindLibraryMethod) { + AccessController.doPrivileged(new PrivilegedAction() { + public Object run() { + try { + findLibraryMethod = ClassLoader.class.getDeclaredMethod("findLibrary", + new Class[] { String.class }); + findLibraryMethod.setAccessible(true); + } catch (Exception e) { + // Fail silently disabling this functionality + } + initializedFindLibraryMethod = true; + return null; + } + }); + } + if (findLibraryMethod != null) { + try { + return (String) AccessController.doPrivileged(new PrivilegedAction() { + public Object run() { + try { + return findLibraryMethod.invoke(loader, new Object[] { libName }); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + }); + } catch (Exception e) { + if (DEBUG) { + e.printStackTrace(); + } + // Fail silently and continue with other search algorithms + } + } + return null; + } + + private static volatile boolean loadedDynLinkNativeLib; + private static void ensureNativeLibLoaded() { + if (!loadedDynLinkNativeLib) { + synchronized (NativeLibrary.class) { + if (!loadedDynLinkNativeLib) { + loadedDynLinkNativeLib = true; + NativeLibLoader.loadGlueGenRT(); + } + } + } + } +} diff --git a/src/java/com/sun/gluegen/runtime/ProcAddressHelper.java b/src/java/com/sun/gluegen/runtime/ProcAddressHelper.java new file mode 100644 index 0000000..f0dbe8b --- /dev/null +++ b/src/java/com/sun/gluegen/runtime/ProcAddressHelper.java @@ -0,0 +1,125 @@ +/* + * Copyright (c) 2003-2005 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.sun.gluegen.runtime; + +import java.security.*; + +// Debugging only +import java.io.*; + +/** Helper class containing constants and methods to assist with the + manipulation of auto-generated ProcAddressTables. */ + +public class ProcAddressHelper { + public static final String PROCADDRESS_VAR_PREFIX = "_addressof_"; + protected static boolean DEBUG; + protected static String DEBUG_PREFIX; + protected static int debugNum; + + static { + AccessController.doPrivileged(new PrivilegedAction() { + public Object run() { + DEBUG = (System.getProperty("gluegen.debug.ProcAddressHelper") != null); + if (DEBUG) { + DEBUG_PREFIX = System.getProperty("gluegen.debug.ProcAddressHelper.prefix"); + } + return null; + } + }); + } + + protected static PrintStream getDebugOutStream() { + PrintStream out = null; + if (DEBUG) { + if (DEBUG_PREFIX != null) { + try { + out = new PrintStream(new BufferedOutputStream(new FileOutputStream(DEBUG_PREFIX + File.separatorChar + + "procaddresshelper-" + (++debugNum) + ".txt"))); + } catch (IOException e) { + e.printStackTrace(); + out = System.err; + } + } else { + out = System.err; + } + } + return out; + } + + public static void resetProcAddressTable(Object table, + DynamicLookupHelper lookup) throws RuntimeException { + Class tableClass = table.getClass(); + java.lang.reflect.Field[] fields = tableClass.getFields(); + PrintStream dout = getDebugOutStream(); + + if (DEBUG) { + dout.println("ProcAddressHelper.resetProcAddressTable(" + table.getClass().getName() + ")"); + } + for (int i = 0; i < fields.length; ++i) { + String addressFieldName = fields[i].getName(); + if (!addressFieldName.startsWith(ProcAddressHelper.PROCADDRESS_VAR_PREFIX)) { + // not a proc address variable + continue; + } + int startOfMethodName = ProcAddressHelper.PROCADDRESS_VAR_PREFIX.length(); + String funcName = addressFieldName.substring(startOfMethodName); + try { + java.lang.reflect.Field addressField = fields[i]; + assert(addressField.getType() == Long.TYPE); + long newProcAddress = lookup.dynamicLookupFunction(funcName); + // set the current value of the proc address variable in the table object + addressField.setLong(table, newProcAddress); + if (DEBUG) { + dout.println(" " + addressField.getName() + " -> 0x" + Long.toHexString(newProcAddress)); + } + } catch (Exception e) { + throw new RuntimeException("Can not get proc address for method \"" + + funcName + "\": Couldn't set value of field \"" + addressFieldName + + "\" in class " + tableClass.getName(), e); + } + } + if (DEBUG) { + dout.flush(); + if (DEBUG_PREFIX != null) { + dout.close(); + } + } + } +} diff --git a/src/java/com/sun/gluegen/runtime/ProcAddressTable.java b/src/java/com/sun/gluegen/runtime/ProcAddressTable.java new file mode 100644 index 0000000..4271a8c --- /dev/null +++ b/src/java/com/sun/gluegen/runtime/ProcAddressTable.java @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2003-2005 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. + */ + +package com.sun.gluegen.runtime; + +public interface ProcAddressTable { + public long getAddressFor(String functionName); +} diff --git a/src/java/com/sun/gluegen/runtime/StructAccessor.java.javame_cdc_fp b/src/java/com/sun/gluegen/runtime/StructAccessor.java.javame_cdc_fp new file mode 100755 index 0000000..3e3f372 --- /dev/null +++ b/src/java/com/sun/gluegen/runtime/StructAccessor.java.javame_cdc_fp @@ -0,0 +1,135 @@ +/* + * 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.sun.gluegen.runtime; + +import java.nio.*; + +public class StructAccessor { + private ByteBuffer bb; + private FloatBuffer fb; + private IntBuffer ib; + private ShortBuffer sb; + + public StructAccessor(ByteBuffer bb) { + this.bb = bb; + } + + public ByteBuffer getBuffer() { + return bb; + } + + /** Return a slice of the current ByteBuffer starting at the + specified byte offset and extending the specified number of + bytes. Note that this method is not thread-safe with respect to + the other methods in this class. */ + public ByteBuffer slice(int byteOffset, int byteLength) { + bb.position(byteOffset); + bb.limit(byteOffset + byteLength); + ByteBuffer newBuf = bb.slice(); + bb.position(0); + bb.limit(bb.capacity()); + return newBuf; + } + + /** Retrieves the byte at the specified slot (byte offset). */ + public byte getByteAt(int slot) { + return bb.get(slot); + } + + /** Puts a byte at the specified slot (byte offset). */ + public void setByteAt(int slot, byte v) { + bb.put(slot, v); + } + + /** Retrieves the float at the specified slot (4-byte offset). */ + public float getFloatAt(int slot) { + return floatBuffer().get(slot); + } + + /** Puts a float at the specified slot (4-byte offset). */ + public void setFloatAt(int slot, float v) { + floatBuffer().put(slot, v); + } + + /** Retrieves the int at the specified slot (4-byte offset). */ + public int getIntAt(int slot) { + return intBuffer().get(slot); + } + + /** Puts a int at the specified slot (4-byte offset). */ + public void setIntAt(int slot, int v) { + intBuffer().put(slot, v); + } + + /** Retrieves the short at the specified slot (2-byte offset). */ + public short getShortAt(int slot) { + return shortBuffer().get(slot); + } + + /** Puts a short at the specified slot (2-byte offset). */ + public void setShortAt(int slot, short v) { + shortBuffer().put(slot, v); + } + + //---------------------------------------------------------------------- + // Internals only below this point + // + + private FloatBuffer floatBuffer() { + if (fb == null) { + fb = bb.asFloatBuffer(); + } + return fb; + } + + private IntBuffer intBuffer() { + if (ib == null) { + ib = bb.asIntBuffer(); + } + return ib; + } + + private ShortBuffer shortBuffer() { + if (sb == null) { + sb = bb.asShortBuffer(); + } + return sb; + } +} diff --git a/src/java/com/sun/gluegen/runtime/StructAccessor.java.javase b/src/java/com/sun/gluegen/runtime/StructAccessor.java.javase new file mode 100755 index 0000000..5a0fa5d --- /dev/null +++ b/src/java/com/sun/gluegen/runtime/StructAccessor.java.javase @@ -0,0 +1,191 @@ +/* + * 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.sun.gluegen.runtime; + +import java.nio.*; + +public class StructAccessor { + private ByteBuffer bb; + private CharBuffer cb; + private DoubleBuffer db; + private FloatBuffer fb; + private IntBuffer ib; + private LongBuffer lb; + private ShortBuffer sb; + + public StructAccessor(ByteBuffer bb) { + // Setting of byte order is concession to native code which needs + // to instantiate these + this.bb = bb.order(ByteOrder.nativeOrder()); + } + + public ByteBuffer getBuffer() { + return bb; + } + + /** Return a slice of the current ByteBuffer starting at the + specified byte offset and extending the specified number of + bytes. Note that this method is not thread-safe with respect to + the other methods in this class. */ + public ByteBuffer slice(int byteOffset, int byteLength) { + bb.position(byteOffset); + bb.limit(byteOffset + byteLength); + ByteBuffer newBuf = bb.slice(); + bb.position(0); + bb.limit(bb.capacity()); + return newBuf; + } + + /** Retrieves the byte at the specified slot (byte offset). */ + public byte getByteAt(int slot) { + return bb.get(slot); + } + + /** Puts a byte at the specified slot (byte offset). */ + public void setByteAt(int slot, byte v) { + bb.put(slot, v); + } + + /** Retrieves the char at the specified slot (2-byte offset). */ + public char getCharAt(int slot) { + return charBuffer().get(slot); + } + + /** Puts a char at the specified slot (2-byte offset). */ + public void setCharAt(int slot, char v) { + charBuffer().put(slot, v); + } + + /** Retrieves the double at the specified slot (8-byte offset). */ + public double getDoubleAt(int slot) { + return doubleBuffer().get(slot); + } + + /** Puts a double at the specified slot (8-byte offset). */ + public void setDoubleAt(int slot, double v) { + doubleBuffer().put(slot, v); + } + + /** Retrieves the float at the specified slot (4-byte offset). */ + public float getFloatAt(int slot) { + return floatBuffer().get(slot); + } + + /** Puts a float at the specified slot (4-byte offset). */ + public void setFloatAt(int slot, float v) { + floatBuffer().put(slot, v); + } + + /** Retrieves the int at the specified slot (4-byte offset). */ + public int getIntAt(int slot) { + return intBuffer().get(slot); + } + + /** Puts a int at the specified slot (4-byte offset). */ + public void setIntAt(int slot, int v) { + intBuffer().put(slot, v); + } + + /** Retrieves the long at the specified slot (8-byte offset). */ + public long getLongAt(int slot) { + return longBuffer().get(slot); + } + + /** Puts a long at the specified slot (8-byte offset). */ + public void setLongAt(int slot, long v) { + longBuffer().put(slot, v); + } + + /** Retrieves the short at the specified slot (2-byte offset). */ + public short getShortAt(int slot) { + return shortBuffer().get(slot); + } + + /** Puts a short at the specified slot (2-byte offset). */ + public void setShortAt(int slot, short v) { + shortBuffer().put(slot, v); + } + + //---------------------------------------------------------------------- + // Internals only below this point + // + + private CharBuffer charBuffer() { + if (cb == null) { + cb = bb.asCharBuffer(); + } + return cb; + } + + private DoubleBuffer doubleBuffer() { + if (db == null) { + db = bb.asDoubleBuffer(); + } + return db; + } + + private FloatBuffer floatBuffer() { + if (fb == null) { + fb = bb.asFloatBuffer(); + } + return fb; + } + + private IntBuffer intBuffer() { + if (ib == null) { + ib = bb.asIntBuffer(); + } + return ib; + } + + private LongBuffer longBuffer() { + if (lb == null) { + lb = bb.asLongBuffer(); + } + return lb; + } + + private ShortBuffer shortBuffer() { + if (sb == null) { + sb = bb.asShortBuffer(); + } + return sb; + } +} diff --git a/src/java/com/sun/gluegen/runtime/UnixDynamicLinkerImpl.java b/src/java/com/sun/gluegen/runtime/UnixDynamicLinkerImpl.java new file mode 100755 index 0000000..773d832 --- /dev/null +++ b/src/java/com/sun/gluegen/runtime/UnixDynamicLinkerImpl.java @@ -0,0 +1,66 @@ +/* !---- DO NOT EDIT: This file autogenerated by com\sun\gluegen\JavaEmitter.java on Mon Jul 31 16:26:59 PDT 2006 ----! */ + +package com.sun.gluegen.runtime; + +import com.sun.gluegen.runtime.*; + +public class UnixDynamicLinkerImpl implements DynamicLinker +{ + + public static final int RTLD_LAZY = 0x00001; + public static final int RTLD_NOW = 0x00002; + public static final int RTLD_NOLOAD = 0x00004; + public static final int RTLD_GLOBAL = 0x00100; + public static final int RTLD_LOCAL = 0x00000; + public static final int RTLD_PARENT = 0x00200; + public static final int RTLD_GROUP = 0x00400; + public static final int RTLD_WORLD = 0x00800; + public static final int RTLD_NODELETE = 0x01000; + public static final int RTLD_FIRST = 0x02000; + + /** Interface to C language function:
int dlclose(void * ); */ + private static native int dlclose(long arg0); + + /** Interface to C language function:
char * dlerror(void); */ + private static native java.lang.String dlerror(); + + /** Interface to C language function:
void * dlopen(const char * , int); */ + private static native long dlopen(java.lang.String arg0, int arg1); + + /** Interface to C language function:
void * dlsym(void * , const char * ); */ + private static native long dlsym(long arg0, java.lang.String arg1); + + + // --- Begin CustomJavaCode .cfg declarations + public long openLibraryLocal(String pathname, boolean debug) { + // Note we use RTLD_GLOBAL visibility to _NOT_ allow this functionality to + // be used to pre-resolve dependent libraries of JNI code without + // requiring that all references to symbols in those libraries be + // looked up dynamically via the ProcAddressTable mechanism; in + // other words, one can actually link against the library instead of + // having to dlsym all entry points. System.loadLibrary() uses + // RTLD_LOCAL visibility so can't be used for this purpose. + return dlopen(pathname, RTLD_LAZY | RTLD_LOCAL); + } + + public long openLibraryGlobal(String pathname, boolean debug) { + // Note we use RTLD_GLOBAL visibility to allow this functionality to + // be used to pre-resolve dependent libraries of JNI code without + // requiring that all references to symbols in those libraries be + // looked up dynamically via the ProcAddressTable mechanism; in + // other words, one can actually link against the library instead of + // having to dlsym all entry points. System.loadLibrary() uses + // RTLD_LOCAL visibility so can't be used for this purpose. + return dlopen(pathname, RTLD_LAZY | RTLD_GLOBAL); + } + + public long lookupSymbol(long libraryHandle, String symbolName) { + return dlsym(libraryHandle, symbolName); + } + + public void closeLibrary(long libraryHandle) { + dlclose(libraryHandle); + } + // ---- End CustomJavaCode .cfg declarations + +} // end of class UnixDynamicLinkerImpl diff --git a/src/java/com/sun/gluegen/runtime/WindowsDynamicLinkerImpl.java b/src/java/com/sun/gluegen/runtime/WindowsDynamicLinkerImpl.java new file mode 100755 index 0000000..325078b --- /dev/null +++ b/src/java/com/sun/gluegen/runtime/WindowsDynamicLinkerImpl.java @@ -0,0 +1,49 @@ +/* !---- DO NOT EDIT: This file autogenerated by com\sun\gluegen\JavaEmitter.java on Tue May 27 02:37:55 PDT 2008 ----! */ + +package com.sun.gluegen.runtime; + +import com.sun.gluegen.runtime.*; + +public class WindowsDynamicLinkerImpl implements DynamicLinker +{ + + + /** Interface to C language function:
BOOL FreeLibrary(HANDLE hLibModule); */ + private static native int FreeLibrary(long hLibModule); + + /** Interface to C language function:
DWORD GetLastError(void); */ + private static native int GetLastError(); + + /** Interface to C language function:
PROC GetProcAddressA(HANDLE hModule, LPCSTR lpProcName); */ + private static native long GetProcAddressA(long hModule, java.lang.String lpProcName); + + /** Interface to C language function:
HANDLE LoadLibraryW(LPCWSTR lpLibFileName); */ + private static native long LoadLibraryW(java.lang.String lpLibFileName); + + + // --- Begin CustomJavaCode .cfg declarations + public long openLibraryLocal(String libraryName, boolean debug) { + // How does that work under Windows ? + // Don't know .. so it's an alias for the time being + return openLibraryGlobal(libraryName, debug); + } + + public long openLibraryGlobal(String libraryName, boolean debug) { + long handle = LoadLibraryW(libraryName); + if(0==handle && debug) { + int err = GetLastError(); + System.err.println("LoadLibraryW \""+libraryName+"\" failed, error code: 0x"+Integer.toHexString(err)+", "+err); + } + return handle; + } + + public long lookupSymbol(long libraryHandle, String symbolName) { + return GetProcAddressA(libraryHandle, symbolName); + } + + public void closeLibrary(long libraryHandle) { + FreeLibrary(libraryHandle); + } + // ---- End CustomJavaCode .cfg declarations + +} // end of class WindowsDynamicLinkerImpl diff --git a/src/java/com/sun/gluegen/runtime/opengl/GLExtensionNames.java b/src/java/com/sun/gluegen/runtime/opengl/GLExtensionNames.java new file mode 100644 index 0000000..3c6f600 --- /dev/null +++ b/src/java/com/sun/gluegen/runtime/opengl/GLExtensionNames.java @@ -0,0 +1,187 @@ +/* + * Copyright (c) 2003-2005 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. + * + */ + +package com.sun.gluegen.runtime.opengl; + +import java.util.*; +import com.sun.gluegen.runtime.*; + +public class GLExtensionNames { + //GL_XYZ : GL_XYZ, GL_XYZ_GL2, GL_XYZ_ARB, GL_XYZ_OES, GL_XYZ_OML + //GL_XYZ : GL_XYZ, GL_GL2_XYZ, GL_ARB_XYZ, GL_OES_XYZ, GL_OML_XYZ + // + // Pass-1 Unify ARB extensions with the same value + // Pass-2 Unify vendor extensions, + // if exist as an ARB extension with the same value. + // Pass-3 Emit + + public static final String[] extensionsARB = { "ARB", "GL2", "OES", "KHR", "OML" }; + public static final String[] extensionsVEN = { "3DFX", + "AMD", + "APPLE", + "ATI", + "EXT", + "HP", + "IBM", + "MESA", + "MESAX", + "NV", + "SGI", + "SGIS", + "SGIX", + "SUN", + "WIN" + }; + + + public static final boolean isGLFunction(String str) { + return str.startsWith("gl") || /* str.startsWith("glu") || str.startsWith("glX") || */ + str.startsWith("egl") || str.startsWith("wgl") || str.startsWith("agl") || + str.startsWith("cgl") ; + } + + public static final boolean isGLEnumeration(String str) { + return str.startsWith("GL_") || str.startsWith("GLU_") || str.startsWith("GLX_") || + str.startsWith("EGL_") || str.startsWith("WGL_") || str.startsWith("AGL_") || + str.startsWith("CGL_") ; + } + + public static final int getExtensionIdx(String[] extensions, String str, boolean isGLFunc) { + if(isGLFunc) { + for(int i = extensions.length - 1 ; i>=0 ; i--) { + if( str.endsWith(extensions[i]) ) { + return i; + } + } + } else { + for(int i = extensions.length - 1 ; i>=0 ; i--) { + if( str.endsWith("_"+extensions[i]) ) { + return i; + } + } + } + return -1; + } + + public static final boolean isExtension(String[] extensions, String str, boolean isGLFunc) { + return getExtensionIdx(extensions, str, isGLFunc)>=0; + } + + public static final String getExtensionSuffix(String str, boolean isGLFunc) { + int idx = getExtensionIdx(extensionsARB, str, isGLFunc); + if(idx>=0) { + return extensionsARB[idx]; + } + idx = getExtensionIdx(extensionsVEN, str, isGLFunc); + if(idx>=0) { + return extensionsVEN[idx]; + } + return null; + } + + public static final String normalize(String[] extensions, String str, boolean isGLFunc) { + boolean touched = false; + for(int i = extensions.length - 1 ; !touched && i>=0 ; i--) { + if(isGLFunc) { + if(str.endsWith(extensions[i])) { + // functions + str = str.substring(0, str.length()-extensions[i].length()); + touched=true; + } + } else { + if(str.endsWith("_"+extensions[i])) { + // enums + str = str.substring(0, str.length()-1-extensions[i].length()); + touched=true; + } + } + } + return str; + } + public static final String normalizeARB(String str, boolean isGLFunc) { + return normalize(extensionsARB, str, isGLFunc); + } + public static final boolean isExtensionARB(String str, boolean isGLFunc) { + return isExtension(extensionsARB, str, isGLFunc); + } + public static final String normalizeVEN(String str, boolean isGLFunc) { + return normalize(extensionsVEN, str, isGLFunc); + } + public static final boolean isExtensionVEN(String str, boolean isGLFunc) { + return isExtension(extensionsVEN, str, isGLFunc); + } + public static final String normalize(String str, boolean isGLFunc) { + if (isExtensionARB(str, isGLFunc)) { + return normalizeARB(str, isGLFunc); + } + if (isExtensionVEN(str, isGLFunc)) { + return normalizeVEN(str, isGLFunc); + } + return str; + } + public static final boolean isExtension(String str, boolean isGLFunc) { + return isExtension(extensionsARB, str, isGLFunc) || + isExtension(extensionsVEN, str, isGLFunc); + } + + public static final int getFuncNamePermutationNumber(String name) { + if(isExtensionARB(name, true) || isExtensionVEN(name, true)) { + // no name permutation, if it's already a known extension + return 1; + } + return 1 + extensionsARB.length + extensionsVEN.length; + } + + public static final String getFuncNamePermutation(String name, int i) { + // identity + if(i==0) { + return name; + } + if(0>i || i>=(1+extensionsARB.length + extensionsVEN.length)) { + throw new RuntimeException("Index out of range [0.."+(1+extensionsARB.length+extensionsVEN.length-1)+"]: "+i); + } + // ARB + i-=1; + if(i 0x" + Long.toHexString(newProcAddress)); + } + } catch (Exception e) { + throw new RuntimeException("Can not set proc address field for method \"" + + funcNameBase + "\": Couldn't set field \"" + addressFieldName + + "\" in class " + tableClass.getName(), e); + } + } + if (DEBUG) { + dout.flush(); + if (DEBUG_PREFIX != null) { + dout.close(); + } + } + } +} -- cgit v1.2.3