diff options
Diffstat (limited to 'src/java/com/jogamp/common')
18 files changed, 2779 insertions, 0 deletions
diff --git a/src/java/com/jogamp/common/nio/AbstractBuffer.java b/src/java/com/jogamp/common/nio/AbstractBuffer.java new file mode 100644 index 0000000..d76574b --- /dev/null +++ b/src/java/com/jogamp/common/nio/AbstractBuffer.java @@ -0,0 +1,114 @@ +/* + * Copyright (c) 2010, Sven Gothel + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions 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 Sven Gothel nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL Sven Gothel BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * Created on Saturday, March 27 2010 11:55 + */ +package com.jogamp.common.nio; + +import com.jogamp.common.os.*; + +import java.nio.ByteBuffer; +import java.nio.Buffer; +import java.util.HashMap; + +/** + * @author Michael Bien + * @author Sven Gothel + */ +public abstract class AbstractBuffer implements NativeBuffer { + + protected final ByteBuffer bb; + protected int capacity; + protected int position; + + static { + NativeLibrary.ensureNativeLibLoaded(); + } + + protected AbstractBuffer(ByteBuffer bb, int elementSize) { + this.bb = bb; + + capacity = bb.capacity() / elementSize; + position = 0; + } + + public final int limit() { + return capacity; + } + + public final int capacity() { + return capacity; + } + + public final int position() { + return position; + } + + public final NativeBuffer position(int newPos) { + if (0 > newPos || newPos >= capacity) { + throw new IndexOutOfBoundsException("Sorry to interrupt, but the position "+newPos+" was out of bounds. " + + "My capacity is "+capacity()+"."); + } + position = newPos; + return this; + } + + public final int remaining() { + return capacity - position; + } + + public final boolean hasRemaining() { + return position < capacity; + } + + public final NativeBuffer rewind() { + position = 0; + return this; + } + + public boolean hasArray() { + return false; + } + + public int arrayOffset() { + return 0; + } + + public final ByteBuffer getBuffer() { + return bb; + } + + public final boolean isDirect() { + return bb.isDirect(); + } + + public String toString() { + return "AbstractBuffer[capacity "+capacity+", position "+position+", elementSize "+(bb.capacity()/capacity)+", ByteBuffer.capacity "+bb.capacity()+"]"; + } + +} diff --git a/src/java/com/jogamp/common/nio/AbstractLongBuffer.java b/src/java/com/jogamp/common/nio/AbstractLongBuffer.java new file mode 100644 index 0000000..ad90ffc --- /dev/null +++ b/src/java/com/jogamp/common/nio/AbstractLongBuffer.java @@ -0,0 +1,144 @@ +/* + * Copyright (c) 2010, Michael Bien + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions 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 Michael Bien nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL Michael Bien BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * Created on Saturday, March 27 2010 11:55 + */ +package com.jogamp.common.nio; + +import com.jogamp.common.os.*; +import java.nio.ByteBuffer; +import java.nio.Buffer; +import java.util.HashMap; + +/** + * Hardware independent container for native pointer arrays. + * + * The native values (NIO direct ByteBuffer) might be 32bit or 64bit wide, + * depending of the CPU pointer width. + * + * @author Michael Bien + * @author Sven Gothel + */ +public abstract class AbstractLongBuffer extends AbstractBuffer { + + protected long[] backup; + + protected HashMap/*<aptr, buffer>*/ dataMap = new HashMap(); + + static { + NativeLibrary.ensureNativeLibLoaded(); + } + + protected AbstractLongBuffer(ByteBuffer bb, int elementSize) { + super(bb, elementSize); + + backup = new long[capacity]; + } + + final void updateBackup() { + for (int i = 0; i < capacity; i++) { + backup[i] = get(i); + } + } + + public final boolean hasArray() { + return true; + } + + public final long[] array() { + return backup; + } + + /** Absolute get method. Get the pointer value at the given index */ + public abstract long get(int idx); + + /** Relative get method. Get the pointer value at the current position and increment the position by one. */ + public final long get() { + long r = get(position); + position++; + return r; + } + + /** + * Relative bulk get method. Copy the pointer values <code> [ position .. position+length [</code> + * to the destination array <code> [ dest[offset] .. dest[offset+length] [ </code> + * and increment the position by <code>length</code>. */ + public final AbstractLongBuffer get(long[] dest, int offset, int length) { + if (dest.length<offset+length) { + throw new IndexOutOfBoundsException(); + } + if (remaining() < length) { + throw new IndexOutOfBoundsException(); + } + while(length>0) { + dest[offset++] = get(position++); + length--; + } + return this; + } + + /** Absolute put method. Put the pointer value at the given index */ + public abstract AbstractLongBuffer put(int index, long value); + + /** Relative put method. Put the pointer value at the current position and increment the position by one. */ + public final AbstractLongBuffer put(long value) { + put(position, value); + position++; + return this; + } + + /** + * Relative bulk put method. Put the pointer values <code> [ src[offset] .. src[offset+length] [</code> + * at the current position and increment the position by <code>length</code>. */ + public final AbstractLongBuffer put(long[] src, int offset, int length) { + if (src.length<offset+length) { + throw new IndexOutOfBoundsException(); + } + if (remaining() < length) { + throw new IndexOutOfBoundsException(); + } + while(length>0) { + put(position++, src[offset++]); + length--; + } + return this; + } + + /** + * Relative bulk get method. Copy the source values <code> src[position .. capacity] [</code> + * to this buffer and increment the position by <code>capacity-position</code>. */ + public AbstractLongBuffer put(AbstractLongBuffer src) { + if (remaining() < src.remaining()) { + throw new IndexOutOfBoundsException(); + } + while (src.hasRemaining()) { + put(src.get()); + } + return this; + } +} diff --git a/src/java/com/jogamp/common/nio/Buffers.java b/src/java/com/jogamp/common/nio/Buffers.java new file mode 100755 index 0000000..e3bea17 --- /dev/null +++ b/src/java/com/jogamp/common/nio/Buffers.java @@ -0,0 +1,707 @@ +/* + * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * - Redistribution of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistribution in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * Neither the name of Sun Microsystems, Inc. or the names of + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * This software is provided "AS IS," without a warranty of any kind. ALL + * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, + * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN + * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR + * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR + * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR + * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR + * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE + * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, + * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF + * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + * + * You acknowledge that this software is not designed or intended for use + * in the design, construction, operation or maintenance of any nuclear + * facility. + * + * Sun gratefully acknowledges that this software was originally authored + * and developed by Kenneth Bradley Russell and Christopher John Kline. + */ +package com.jogamp.common.nio; + +import com.jogamp.common.os.Platform; +import java.nio.*; + +/** + * @author Kenneth Russel + * @author Sven Gothel + * @author Michael Bien + */ +public class Buffers { + + 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; + + protected Buffers() {} + + /** + * Allocates a new direct ByteBuffer with the specified number of + * elements. The returned buffer will have its byte order set to + * the host platform's native byte order. + */ + public static ByteBuffer newDirectByteBuffer(int numElements) { + return nativeOrder(ByteBuffer.allocateDirect(numElements)); + } + + public static ByteBuffer newDirectByteBuffer(byte[] values, int offset, int lenght) { + return (ByteBuffer)newDirectByteBuffer(lenght).put(values, offset, lenght).rewind(); + } + + public static ByteBuffer newDirectByteBuffer(byte[] values, int offset) { + return newDirectByteBuffer(values, offset, values.length-offset); + } + + public static ByteBuffer newDirectByteBuffer(byte[] values) { + return newDirectByteBuffer(values, 0); + } + + /** + * Allocates a new direct DoubleBuffer with the specified number of + * elements. The returned buffer will have its byte order set to + * the host platform's native byte order. + */ + public static DoubleBuffer newDirectDoubleBuffer(int numElements) { + return newDirectByteBuffer(numElements * SIZEOF_DOUBLE).asDoubleBuffer(); + } + + public static DoubleBuffer newDirectDoubleBuffer(double[] values, int offset, int lenght) { + return (DoubleBuffer)newDirectDoubleBuffer(lenght).put(values, offset, lenght).rewind(); + } + + public static DoubleBuffer newDirectDoubleBuffer(double[] values, int offset) { + return newDirectDoubleBuffer(values, offset, values.length - offset); + } + + public static DoubleBuffer newDirectDoubleBuffer(double[] values) { + return newDirectDoubleBuffer(values, 0); + } + + /** + * Allocates a new direct FloatBuffer with the specified number of + * elements. The returned buffer will have its byte order set to + * the host platform's native byte order. + */ + public static FloatBuffer newDirectFloatBuffer(int numElements) { + return newDirectByteBuffer(numElements * SIZEOF_FLOAT).asFloatBuffer(); + } + + public static FloatBuffer newDirectFloatBuffer(float[] values, int offset, int lenght) { + return (FloatBuffer)newDirectFloatBuffer(lenght).put(values, offset, lenght).rewind(); + } + + public static FloatBuffer newDirectFloatBuffer(float[] values, int offset) { + return newDirectFloatBuffer(values, offset, values.length - offset); + } + + public static FloatBuffer newDirectFloatBuffer(float[] values) { + return newDirectFloatBuffer(values, 0); + } + + /** + * Allocates a new direct IntBuffer with the specified number of + * elements. The returned buffer will have its byte order set to + * the host platform's native byte order. + */ + public static IntBuffer newDirectIntBuffer(int numElements) { + return newDirectByteBuffer(numElements * SIZEOF_INT).asIntBuffer(); + } + + public static IntBuffer newDirectIntBuffer(int[] values, int offset, int lenght) { + return (IntBuffer)newDirectIntBuffer(lenght).put(values, offset, lenght).rewind(); + } + + public static IntBuffer newDirectIntBuffer(int[] values, int offset) { + return newDirectIntBuffer(values, offset, values.length - offset); + } + + public static IntBuffer newDirectIntBuffer(int[] values) { + return newDirectIntBuffer(values, 0); + } + + /** + * Allocates a new direct LongBuffer with the specified number of + * elements. The returned buffer will have its byte order set to + * the host platform's native byte order. + */ + public static LongBuffer newDirectLongBuffer(int numElements) { + return newDirectByteBuffer(numElements * SIZEOF_LONG).asLongBuffer(); + } + + public static LongBuffer newDirectLongBuffer(long[] values, int offset, int lenght) { + return (LongBuffer)newDirectLongBuffer(lenght).put(values, offset, lenght).rewind(); + } + + public static LongBuffer newDirectLongBuffer(long[] values, int offset) { + return newDirectLongBuffer(values, offset, values.length - offset); + } + + public static LongBuffer newDirectLongBuffer(long[] values) { + return newDirectLongBuffer(values, 0); + } + + /** + * Allocates a new direct ShortBuffer with the specified number of + * elements. The returned buffer will have its byte order set to + * the host platform's native byte order. + */ + public static ShortBuffer newDirectShortBuffer(int numElements) { + return newDirectByteBuffer(numElements * SIZEOF_SHORT).asShortBuffer(); + } + + public static ShortBuffer newDirectShortBuffer(short[] values, int offset, int lenght) { + return (ShortBuffer)newDirectShortBuffer(lenght).put(values, offset, lenght).rewind(); + } + + public static ShortBuffer newDirectShortBuffer(short[] values, int offset) { + return newDirectShortBuffer(values, offset, values.length - offset); + } + + public static ShortBuffer newDirectShortBuffer(short[] values) { + return newDirectShortBuffer(values, 0); + } + + /** + * Allocates a new direct CharBuffer with the specified number of + * elements. The returned buffer will have its byte order set to + * the host platform's native byte order. + */ + public static CharBuffer newDirectCharBuffer(int numElements) { + return newDirectByteBuffer(numElements * SIZEOF_SHORT).asCharBuffer(); + } + + public static CharBuffer newDirectCharBuffer(char[] values, int offset, int lenght) { + return (CharBuffer)newDirectCharBuffer(lenght).put(values, offset, lenght).rewind(); + } + + public static CharBuffer newDirectCharBuffer(char[] values, int offset) { + return newDirectCharBuffer(values, offset, values.length - offset); + } + + public static CharBuffer newDirectCharBuffer(char[] values) { + return newDirectCharBuffer(values, 0); + } + + /** + * 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) { + if (Platform.isJavaSE()) { + return buf.order(ByteOrder.nativeOrder()); + } else { + // JSR 239 does not support the ByteOrder class or the order methods. + // The initial order of a byte buffer is the platform byte order. + return buf; + } + } + + /** + * Returns the size of a single element of this buffer in bytes. + */ + public static final int sizeOfBufferElem(Buffer buffer) { + if (buffer == null) { + return 0; + } + if (buffer instanceof ByteBuffer) { + return SIZEOF_BYTE; + } else if (buffer instanceof IntBuffer) { + return SIZEOF_INT; + } else if (buffer instanceof ShortBuffer) { + return SIZEOF_SHORT; + } else if (buffer instanceof FloatBuffer) { + return SIZEOF_FLOAT; + } else if (Platform.isJavaSE()) { + if (buffer instanceof DoubleBuffer) { + return SIZEOF_DOUBLE; + } else if (buffer instanceof LongBuffer) { + return SIZEOF_LONG; + } else if (buffer instanceof CharBuffer) { + return SIZEOF_CHAR; + } + } + throw new RuntimeException("Unexpected buffer type " + buffer.getClass().getName()); + } + + /** + * Helper routine to tell whether a buffer is direct or not. Null + * pointers are considered NOT direct. isDirect() should really be + * public in Buffer and not replicated in all subclasses. + */ + public static boolean isDirect(Object 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 IntBuffer) { + return ((IntBuffer) buf).isDirect(); + } else if (buf instanceof ShortBuffer) { + return ((ShortBuffer) buf).isDirect(); + } else if (buf instanceof Int64Buffer) { + return ((Int64Buffer) buf).isDirect(); + } else if (buf instanceof PointerBuffer) { + return ((PointerBuffer) buf).isDirect(); + } else if (Platform.isJavaSE()) { + if (buf instanceof DoubleBuffer) { + return ((DoubleBuffer) buf).isDirect(); + } else if (buf instanceof LongBuffer) { + return ((LongBuffer) buf).isDirect(); + }else if (buf instanceof CharBuffer) { + return ((CharBuffer) 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(Object buf) { + if (buf == null) { + return 0; + } + if (buf instanceof Buffer) { + int pos = ((Buffer) buf).position(); + if (buf instanceof ByteBuffer) { + return pos; + } else if (buf instanceof FloatBuffer) { + return pos * SIZEOF_FLOAT; + } else if (buf instanceof IntBuffer) { + return pos * SIZEOF_INT; + } else if (buf instanceof ShortBuffer) { + return pos * SIZEOF_SHORT; + }else if(Platform.isJavaSE()) { + if (buf instanceof DoubleBuffer) { + return pos * SIZEOF_DOUBLE; + } else if (buf instanceof LongBuffer) { + return pos * SIZEOF_LONG; + } else if (buf instanceof CharBuffer) { + return pos * SIZEOF_CHAR; + } + } + } else if (buf instanceof Int64Buffer) { + Int64Buffer int64Buffer = (Int64Buffer) buf; + return int64Buffer.position() * Int64Buffer.elementSize(); + } else if (buf instanceof PointerBuffer) { + PointerBuffer pointerBuffer = (PointerBuffer) buf; + return pointerBuffer.position() * PointerBuffer.elementSize(); + } + + 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(Object 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 Int64Buffer) { + return ((Int64Buffer) buf).array(); + } else if (buf instanceof PointerBuffer) { + return ((PointerBuffer) buf).array(); + }else if(Platform.isJavaSE()) { + 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(Object buf) { + if (buf == null) { + return 0; + } + if (buf instanceof Buffer) { + int pos = ((Buffer) 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(Platform.isJavaSE()) { + 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)); + } + } + } else if (buf instanceof Int64Buffer) { + Int64Buffer int64Buffer = (Int64Buffer) buf; + return Int64Buffer.elementSize() * (int64Buffer.arrayOffset() + int64Buffer.position()); + } else if (buf instanceof PointerBuffer) { + PointerBuffer pointerBuffer = (PointerBuffer) buf; + return PointerBuffer.elementSize() * (pointerBuffer.arrayOffset() + pointerBuffer.position()); + } + + throw new RuntimeException("Unknown buffer type " + buf.getClass().getName()); + } + + + //---------------------------------------------------------------------- + // Copy routines (type-to-type) + // + /** + * Copies the <i>remaining</i> elements (as defined by + * <code>limit() - position()</code>) in the passed ByteBuffer into + * a newly-allocated direct ByteBuffer. The returned buffer will + * have its byte order set to the host platform's native byte + * order. The position of the newly-allocated buffer will be zero, + * and the position of the passed buffer is unchanged (though its + * mark is changed). + */ + public static ByteBuffer copyByteBuffer(ByteBuffer orig) { + ByteBuffer dest = newDirectByteBuffer(orig.remaining()); + dest.put(orig); + dest.rewind(); + return dest; + } + + /** + * Copies the <i>remaining</i> elements (as defined by + * <code>limit() - position()</code>) in the passed FloatBuffer + * into a newly-allocated direct FloatBuffer. The returned buffer + * will have its byte order set to the host platform's native byte + * order. The position of the newly-allocated buffer will be zero, + * and the position of the passed buffer is unchanged (though its + * mark is changed). + */ + public static FloatBuffer copyFloatBuffer(FloatBuffer orig) { + return copyFloatBufferAsByteBuffer(orig).asFloatBuffer(); + } + + /** + * Copies the <i>remaining</i> elements (as defined by + * <code>limit() - position()</code>) in the passed IntBuffer + * into a newly-allocated direct IntBuffer. The returned buffer + * will have its byte order set to the host platform's native byte + * order. The position of the newly-allocated buffer will be zero, + * and the position of the passed buffer is unchanged (though its + * mark is changed). + */ + public static IntBuffer copyIntBuffer(IntBuffer orig) { + return copyIntBufferAsByteBuffer(orig).asIntBuffer(); + } + + /** + * Copies the <i>remaining</i> elements (as defined by + * <code>limit() - position()</code>) in the passed ShortBuffer + * into a newly-allocated direct ShortBuffer. The returned buffer + * will have its byte order set to the host platform's native byte + * order. The position of the newly-allocated buffer will be zero, + * and the position of the passed buffer is unchanged (though its + * mark is changed). + */ + public static ShortBuffer copyShortBuffer(ShortBuffer orig) { + return copyShortBufferAsByteBuffer(orig).asShortBuffer(); + } + + //---------------------------------------------------------------------- + // Copy routines (type-to-ByteBuffer) + // + /** + * Copies the <i>remaining</i> elements (as defined by + * <code>limit() - position()</code>) in the passed FloatBuffer + * into a newly-allocated direct ByteBuffer. The returned buffer + * will have its byte order set to the host platform's native byte + * order. The position of the newly-allocated buffer will be zero, + * and the position of the passed buffer is unchanged (though its + * mark is changed). + */ + public static ByteBuffer copyFloatBufferAsByteBuffer(FloatBuffer orig) { + ByteBuffer dest = newDirectByteBuffer(orig.remaining() * SIZEOF_FLOAT); + dest.asFloatBuffer().put(orig); + dest.rewind(); + return dest; + } + + /** + * Copies the <i>remaining</i> elements (as defined by + * <code>limit() - position()</code>) in the passed IntBuffer into + * a newly-allocated direct ByteBuffer. The returned buffer will + * have its byte order set to the host platform's native byte + * order. The position of the newly-allocated buffer will be zero, + * and the position of the passed buffer is unchanged (though its + * mark is changed). + */ + public static ByteBuffer copyIntBufferAsByteBuffer(IntBuffer orig) { + ByteBuffer dest = newDirectByteBuffer(orig.remaining() * SIZEOF_INT); + dest.asIntBuffer().put(orig); + dest.rewind(); + return dest; + } + + /** + * Copies the <i>remaining</i> elements (as defined by + * <code>limit() - position()</code>) in the passed ShortBuffer + * into a newly-allocated direct ByteBuffer. The returned buffer + * will have its byte order set to the host platform's native byte + * order. The position of the newly-allocated buffer will be zero, + * and the position of the passed buffer is unchanged (though its + * mark is changed). + */ + public static ByteBuffer copyShortBufferAsByteBuffer(ShortBuffer orig) { + ByteBuffer dest = newDirectByteBuffer(orig.remaining() * SIZEOF_SHORT); + dest.asShortBuffer().put(orig); + dest.rewind(); + return dest; + } + + //---------------------------------------------------------------------- + // Conversion routines + // + public final static FloatBuffer getFloatBuffer(DoubleBuffer source) { + source.rewind(); + FloatBuffer dest = newDirectFloatBuffer(source.limit()); + while (source.hasRemaining()) { + dest.put((float) source.get()); + } + return dest; + } + + //---------------------------------------------------------------------- + // Convenient put methods with generic target Buffer + // + public static Buffer put(Buffer dest, Buffer src) { + if ((dest instanceof ByteBuffer) && (src instanceof ByteBuffer)) { + return ((ByteBuffer) dest).put((ByteBuffer) src); + } else if ((dest instanceof ShortBuffer) && (src instanceof ShortBuffer)) { + return ((ShortBuffer) dest).put((ShortBuffer) src); + } else if ((dest instanceof IntBuffer) && (src instanceof IntBuffer)) { + return ((IntBuffer) dest).put((IntBuffer) src); + } else if ((dest instanceof FloatBuffer) && (src instanceof FloatBuffer)) { + return ((FloatBuffer) dest).put((FloatBuffer) src); + } else if (Platform.isJavaSE()) { + if ((dest instanceof LongBuffer) && (src instanceof LongBuffer)) { + return ((LongBuffer) dest).put((LongBuffer) src); + } else if ((dest instanceof DoubleBuffer) && (src instanceof DoubleBuffer)) { + return ((DoubleBuffer) dest).put((DoubleBuffer) src); + } else if ((dest instanceof CharBuffer) && (src instanceof CharBuffer)) { + return ((CharBuffer) dest).put((CharBuffer) src); + } + } + throw new RuntimeException("Incompatible Buffer classes: dest = " + dest.getClass().getName() + ", src = " + src.getClass().getName()); + } + + public static Buffer putb(Buffer dest, byte v) { + if (dest instanceof ByteBuffer) { + return ((ByteBuffer) dest).put(v); + } else if (dest instanceof ShortBuffer) { + return ((ShortBuffer) dest).put((short) v); + } else if (dest instanceof IntBuffer) { + return ((IntBuffer) dest).put((int) v); + } else { + throw new RuntimeException("Byte doesn't match Buffer Class: " + dest); + } + } + + public static Buffer puts(Buffer dest, short v) { + if (dest instanceof ShortBuffer) { + return ((ShortBuffer) dest).put(v); + } else if (dest instanceof IntBuffer) { + return ((IntBuffer) dest).put((int) v); + } else { + throw new RuntimeException("Short doesn't match Buffer Class: " + dest); + } + } + + public static void puti(Buffer dest, int v) { + if (dest instanceof IntBuffer) { + ((IntBuffer) dest).put(v); + } else { + throw new RuntimeException("Integer doesn't match Buffer Class: " + dest); + } + } + + public static void putf(Buffer dest, float v) { + if (dest instanceof FloatBuffer) { + ((FloatBuffer) dest).put(v); +/* TODO FixedPoint required + } else if (dest instanceof IntBuffer) { + ((IntBuffer) dest).put(FixedPoint.toFixed(v)); +*/ + } else { + throw new RuntimeException("Float doesn't match Buffer Class: " + dest); + } + } + public static void putd(Buffer dest, double v) { + if (dest instanceof FloatBuffer) { + ((FloatBuffer) dest).put((float) v); + } else { + throw new RuntimeException("Double doesn't match Buffer Class: " + dest); + } + } + + 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(Object buffer, int minBytesRemaining) { + if (buffer == null) { + return; + } + + int bytesRemaining = 0; + if (buffer instanceof Buffer) { + int elementsRemaining = ((Buffer) buffer).remaining(); + 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(Platform.isJavaSE()) { + 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; + } + } + } else if (buffer instanceof Int64Buffer) { + Int64Buffer int64Buffer = (Int64Buffer) buffer; + bytesRemaining = int64Buffer.remaining() * Int64Buffer.elementSize(); + } else if (buffer instanceof PointerBuffer) { + PointerBuffer pointerBuffer = (PointerBuffer) buffer; + bytesRemaining = pointerBuffer.remaining() * PointerBuffer.elementSize(); + } + if (bytesRemaining < minBytesRemaining) { + throw new IndexOutOfBoundsException("Required " + minBytesRemaining + " remaining bytes in buffer, only had " + bytesRemaining); + } + } + +} diff --git a/src/java/com/jogamp/common/nio/Int64Buffer.java b/src/java/com/jogamp/common/nio/Int64Buffer.java new file mode 100644 index 0000000..6f0b7a3 --- /dev/null +++ b/src/java/com/jogamp/common/nio/Int64Buffer.java @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2010, Michael Bien + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions 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 Michael Bien nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL Michael Bien BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package com.jogamp.common.nio; + +import com.jogamp.common.os.*; +import java.nio.ByteBuffer; + +/** + * Hardware independent container for native int64_t arrays. + * + * The native values (NIO direct ByteBuffer) are always 64bit wide. + * + * @author Michael Bien + * @author Sven Gothel + */ +public abstract class Int64Buffer extends AbstractLongBuffer { + + protected Int64Buffer(ByteBuffer bb) { + super(bb, elementSize()); + } + + public static Int64Buffer allocate(int size) { + if (Platform.isJavaSE()) { + return new Int64BufferSE(ByteBuffer.wrap(new byte[elementSize() * size])); + } else { + return new Int64BufferME_CDC_FP(ByteBuffer.wrap(new byte[elementSize() * size])); + } + } + + public static Int64Buffer allocateDirect(int size) { + if (Platform.isJavaSE()) { + return new Int64BufferSE(Buffers.newDirectByteBuffer(elementSize() * size)); + } else { + return new Int64BufferME_CDC_FP(Buffers.newDirectByteBuffer(elementSize() * size)); + } + } + + public static Int64Buffer wrap(ByteBuffer src) { + Int64Buffer res; + if (Platform.isJavaSE()) { + res = new Int64BufferSE(src); + } else { + res = new Int64BufferME_CDC_FP(src); + } + res.updateBackup(); + return res; + + } + + public static int elementSize() { + return Buffers.SIZEOF_LONG; + } + + public String toString() { + return "Int64Buffer:"+super.toString(); + } + +} diff --git a/src/java/com/jogamp/common/nio/Int64BufferME_CDC_FP.java b/src/java/com/jogamp/common/nio/Int64BufferME_CDC_FP.java new file mode 100755 index 0000000..9621677 --- /dev/null +++ b/src/java/com/jogamp/common/nio/Int64BufferME_CDC_FP.java @@ -0,0 +1,89 @@ +/* + * 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. + */ +package com.jogamp.common.nio; + +import com.jogamp.common.os.Platform; +import java.nio.*; + +/** + * @author Sven Gothel + * @author Michael Bien + */ +final class Int64BufferME_CDC_FP extends Int64Buffer { + + private IntBuffer pb; + + Int64BufferME_CDC_FP(ByteBuffer bb) { + super(bb); + this.pb = bb.asIntBuffer(); + + capacity = bb.capacity() / elementSize(); + + position = 0; + backup = new long[capacity]; + } + + public final long get(int idx) { + if (0 > idx || idx >= capacity) { + throw new IndexOutOfBoundsException(); + } + idx = idx << 1; // 8-byte to 4-byte offset + long lo = 0x00000000FFFFFFFFL & ((long) pb.get(idx)); + long hi = 0x00000000FFFFFFFFL & ((long) pb.get(idx + 1)); + if (Platform.isLittleEndian()) { + return hi << 32 | lo; + } + return lo << 32 | hi; + } + + public final AbstractLongBuffer put(int idx, long v) { + if (0 > idx || idx >= capacity) { + throw new IndexOutOfBoundsException(); + } + backup[idx] = v; + idx = idx << 1; // 8-byte to 4-byte offset + int lo = (int) ((v) & 0x00000000FFFFFFFFL); + int hi = (int) ((v >> 32) & 0x00000000FFFFFFFFL); + if (Platform.isLittleEndian()) { + pb.put(idx, lo); + pb.put(idx + 1, hi); + } else { + pb.put(idx, hi); + pb.put(idx + 1, lo); + } + return this; + } +} diff --git a/src/java/com/jogamp/common/nio/Int64BufferSE.java b/src/java/com/jogamp/common/nio/Int64BufferSE.java new file mode 100755 index 0000000..fc262fd --- /dev/null +++ b/src/java/com/jogamp/common/nio/Int64BufferSE.java @@ -0,0 +1,69 @@ +/* + * 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. + */ +package com.jogamp.common.nio; + +import java.nio.*; + +/** + * @author Sven Gothel + * @author Michael Bien + */ +final class Int64BufferSE extends Int64Buffer { + + private LongBuffer pb; + + Int64BufferSE(ByteBuffer bb) { + super(bb); + + this.pb = bb.asLongBuffer(); + } + + public final long get(int idx) { + if (0 > idx || idx >= capacity) { + throw new IndexOutOfBoundsException(); + } + return pb.get(idx); + } + + public final AbstractLongBuffer put(int idx, long v) { + if (0 > idx || idx >= capacity) { + throw new IndexOutOfBoundsException(); + } + backup[idx] = v; + pb.put(idx, v); + return this; + } +} diff --git a/src/java/com/jogamp/common/nio/NativeBuffer.java b/src/java/com/jogamp/common/nio/NativeBuffer.java new file mode 100644 index 0000000..99a5cbc --- /dev/null +++ b/src/java/com/jogamp/common/nio/NativeBuffer.java @@ -0,0 +1,54 @@ +/* + * Created on Tuesday, March 30 2010 18:22 + */ +package com.jogamp.common.nio; + +import java.nio.ByteBuffer; + +/** + * Hardware independent container for various kinds of buffers. + * + * @author Michael Bien + * @author Sven Gothel + */ +public interface NativeBuffer/*<B extends NativeBuffer>*/ { + + public int limit(); + + public int capacity(); + + public int position(); + + public NativeBuffer position(int newPos); + + public int remaining(); + + public boolean hasRemaining(); + + public NativeBuffer rewind(); + + public boolean hasArray(); + + public int arrayOffset(); + + public ByteBuffer getBuffer(); + + public boolean isDirect(); + +/* + public long[] array(); + + public B rewind(); + + public B put(int index, long value); + + public B put(long value); + + public B put(B src); + + public long get(); + + public long get(int idx); +*/ + +} diff --git a/src/java/com/jogamp/common/nio/PointerBuffer.java b/src/java/com/jogamp/common/nio/PointerBuffer.java new file mode 100644 index 0000000..26d2e7b --- /dev/null +++ b/src/java/com/jogamp/common/nio/PointerBuffer.java @@ -0,0 +1,156 @@ +/* + * Copyright (c) 2010, Michael Bien + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions 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 Michael Bien nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL Michael Bien BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * Created on Saturday, March 27 2010 11:55 + */ +package com.jogamp.common.nio; + +import com.jogamp.common.os.*; +import java.nio.ByteBuffer; +import java.nio.Buffer; +import java.util.HashMap; + +/** + * Hardware independent container for native pointer arrays. + * + * The native values (NIO direct ByteBuffer) might be 32bit or 64bit wide, + * depending of the CPU pointer width. + * + * @author Michael Bien + * @author Sven Gothel + */ +public abstract class PointerBuffer extends AbstractLongBuffer { + + protected HashMap/*<aptr, buffer>*/ dataMap = new HashMap(); + + static { + NativeLibrary.ensureNativeLibLoaded(); + } + + protected PointerBuffer(ByteBuffer bb) { + super(bb, elementSize()); + } + + public static PointerBuffer allocate(int size) { + if (Platform.isJavaSE()) { + return new PointerBufferSE(ByteBuffer.wrap(new byte[elementSize() * size])); + } else { + return new PointerBufferME_CDC_FP(ByteBuffer.wrap(new byte[elementSize() * size])); + } + } + + public static PointerBuffer allocateDirect(int size) { + if (Platform.isJavaSE()) { + return new PointerBufferSE(Buffers.newDirectByteBuffer(elementSize() * size)); + } else { + return new PointerBufferME_CDC_FP(Buffers.newDirectByteBuffer(elementSize() * size)); + } + } + + public static PointerBuffer wrap(ByteBuffer src) { + PointerBuffer res; + if (Platform.isJavaSE()) { + res = new PointerBufferSE(src); + } else { + res = new PointerBufferME_CDC_FP(src); + } + res.updateBackup(); + return res; + + } + + public static int elementSize() { + return Platform.is32Bit() ? Buffers.SIZEOF_INT : Buffers.SIZEOF_LONG; + } + + public final PointerBuffer put(PointerBuffer src) { + if (remaining() < src.remaining()) { + throw new IndexOutOfBoundsException(); + } + long addr; + while (src.hasRemaining()) { + addr = src.get(); + put(addr); + Long addrL = new Long(addr); + Buffer bb = (Buffer) dataMap.get(addrL); + if(null!=bb) { + dataMap.put(addrL, bb); + } else { + dataMap.remove(addrL); + } + } + return this; + } + + /** Put the address of the given direct Buffer at the given position + of this pointer array. + Adding a reference of the given direct Buffer to this object. */ + public final PointerBuffer referenceBuffer(int index, Buffer bb) { + if(null==bb) { + throw new RuntimeException("Buffer is null"); + } + if(!bb.isDirect()) { + throw new RuntimeException("Buffer is not direct"); + } + long bbAddr = getDirectBufferAddressImpl(bb); + if(0==bbAddr) { + throw new RuntimeException("Couldn't determine native address of given Buffer: "+bb); + } + + put(index, bbAddr); + dataMap.put(new Long(bbAddr), bb); + return this; + } + + /** Put the address of the given direct Buffer at the end + of this pointer array. + Adding a reference of the given direct Buffer to this object. */ + public final PointerBuffer referenceBuffer(Buffer bb) { + referenceBuffer(position, bb); + position++; + return this; + } + + public final Buffer getReferencedBuffer(int index) { + long addr = get(index); + return (Buffer) dataMap.get(new Long(addr)); + } + + public final Buffer getReferencedBuffer() { + Buffer bb = getReferencedBuffer(position); + position++; + return bb; + } + + private native long getDirectBufferAddressImpl(Object directBuffer); + + public String toString() { + return "PointerBuffer:"+super.toString(); + } + +} diff --git a/src/java/com/jogamp/common/nio/PointerBufferME_CDC_FP.java b/src/java/com/jogamp/common/nio/PointerBufferME_CDC_FP.java new file mode 100755 index 0000000..6e2c7d9 --- /dev/null +++ b/src/java/com/jogamp/common/nio/PointerBufferME_CDC_FP.java @@ -0,0 +1,92 @@ +/* + * 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. + */ +package com.jogamp.common.nio; + +import com.jogamp.common.os.Platform; +import java.nio.*; + +/** + * @author Sven Gothel + * @author Michael Bien + */ +final class PointerBufferME_CDC_FP extends PointerBuffer { + + private IntBuffer pb; + + PointerBufferME_CDC_FP(ByteBuffer bb) { + super(bb); + this.pb = bb.asIntBuffer(); + } + + public final long get(int idx) { + if (0 > idx || idx >= capacity) { + throw new IndexOutOfBoundsException(); + } + if (Platform.is32Bit()) { + return pb.get(idx); + } else { + idx = idx << 1; // 8-byte to 4-byte offset + long lo = 0x00000000FFFFFFFFL & ((long) pb.get(idx)); + long hi = 0x00000000FFFFFFFFL & ((long) pb.get(idx + 1)); + if (Platform.isLittleEndian()) { + return hi << 32 | lo; + } + return lo << 32 | hi; + } + } + + public final AbstractLongBuffer put(int idx, long v) { + if (0 > idx || idx >= capacity) { + throw new IndexOutOfBoundsException(); + } + backup[idx] = v; + if (Platform.is32Bit()) { + pb.put(idx, (int) v); + } else { + idx = idx << 1; // 8-byte to 4-byte offset + int lo = (int) ((v) & 0x00000000FFFFFFFFL); + int hi = (int) ((v >> 32) & 0x00000000FFFFFFFFL); + if (Platform.isLittleEndian()) { + pb.put(idx, lo); + pb.put(idx + 1, hi); + } else { + pb.put(idx, hi); + pb.put(idx + 1, lo); + } + } + return this; + } +} diff --git a/src/java/com/jogamp/common/nio/PointerBufferSE.java b/src/java/com/jogamp/common/nio/PointerBufferSE.java new file mode 100755 index 0000000..11dc629 --- /dev/null +++ b/src/java/com/jogamp/common/nio/PointerBufferSE.java @@ -0,0 +1,83 @@ +/* + * 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. + */ +package com.jogamp.common.nio; + +import com.jogamp.common.os.Platform; +import java.nio.*; + +/** + * @author Sven Gothel + * @author Michael Bien + */ +final class PointerBufferSE extends PointerBuffer { + + private Buffer pb; + + PointerBufferSE(ByteBuffer bb) { + super(bb); + + if (Platform.is32Bit()) { + this.pb = bb.asIntBuffer(); + } else { + this.pb = bb.asLongBuffer(); + } + } + + public final long get(int idx) { + if (0 > idx || idx >= capacity) { + throw new IndexOutOfBoundsException(); + } + if (Platform.is32Bit()) { + return ((IntBuffer) pb).get(idx); + } else { + return ((LongBuffer) pb).get(idx); + } + } + + public final AbstractLongBuffer put(int idx, long v) { + if (0 > idx || idx >= capacity) { + throw new IndexOutOfBoundsException(); + } + backup[idx] = v; + if (Platform.is32Bit()) { + ((IntBuffer) pb).put(idx, (int) v); + } else { + ((LongBuffer) pb).put(idx, v); + } + return this; + } + +} diff --git a/src/java/com/jogamp/common/nio/StructAccessor.java b/src/java/com/jogamp/common/nio/StructAccessor.java new file mode 100644 index 0000000..117e8de --- /dev/null +++ b/src/java/com/jogamp/common/nio/StructAccessor.java @@ -0,0 +1,327 @@ +/* + * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * - Redistribution of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistribution in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * Neither the name of Sun Microsystems, Inc. or the names of + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * This software is provided "AS IS," without a warranty of any kind. ALL + * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, + * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN + * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR + * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR + * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR + * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR + * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE + * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, + * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF + * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + * + * You acknowledge that this software is not designed or intended for use + * in the design, construction, operation or maintenance of any nuclear + * facility. + * + * Sun gratefully acknowledges that this software was originally authored + * and developed by Kenneth Bradley Russell and Christopher John Kline. + */ +package com.jogamp.common.nio; + +import com.jogamp.common.os.Platform; +import java.nio.*; + +/** + * @author Kenneth Russel + * @author Michael Bien + */ +public class StructAccessor { + + private ByteBuffer bb; + private FloatBuffer fb; + private IntBuffer ib; + private ShortBuffer sb; + + //Java SE only + private CharBuffer cb; + private DoubleBuffer db; + private LongBuffer lb; + + public StructAccessor(ByteBuffer bb) { + // Setting of byte order is concession to native code which needs + // to instantiate these + if(Platform.isJavaSE()) { + this.bb = bb.order(ByteOrder.nativeOrder()); + }else{ + // JSR 239 does not support the ByteOrder class or the order methods. + // The initial order of a byte buffer is the platform byte order. + this.bb = bb; + } + } + + public ByteBuffer getBuffer() { + return bb; + } + + /** + * Returns 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 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); + } + + public void setBytesAt(int slot, byte[] v) { + for (int i = 0; i < v.length; i++) { + bb.put(slot++, v[i]); + } + } + + public byte[] getBytesAt(int slot, byte[] v) { + for (int i = 0; i < v.length; i++) { + v[i] = bb.get(slot++); + } + return v; + } + + public void setCharsAt(int slot, char[] v) { + for (int i = 0; i < v.length; i++) { + charBuffer().put(slot++, v[i]); + } + } + + public char[] getCharsAt(int slot, char[] v) { + for (int i = 0; i < v.length; i++) { + v[i] = charBuffer().get(slot++); + } + return v; + } + + public void setIntsAt(int slot, int[] v) { + for (int i = 0; i < v.length; i++) { + intBuffer().put(slot++, v[i]); + } + } + + public int[] getIntsAt(int slot, int[] v) { + for (int i = 0; i < v.length; i++) { + v[i] = intBuffer().get(slot++); + } + return v; + } + + public void setFloatsAt(int slot, float[] v) { + for (int i = 0; i < v.length; i++) { + floatBuffer().put(slot++, v[i]); + } + } + + public float[] getFloatsAt(int slot, float[] v) { + for (int i = 0; i < v.length; i++) { + v[i] = floatBuffer().get(slot++); + } + return v; + } + + /** + * Puts a double at the specified slot (8-byte offset). + * May throw an {@link UnsupportedOperationException} + */ + public void setDoublesAt(int slot, double[] v) { + for (int i = 0; i < v.length; i++) { + doubleBuffer().put(slot++, v[i]); + } + } + + /** + * Retrieves the long at the specified slot (8-byte offset). + * May throw an {@link UnsupportedOperationException} + */ + public double[] getDoublesAt(int slot, double[] v) { + for (int i = 0; i < v.length; i++) { + v[i] = doubleBuffer().get(slot++); + } + return v; + } + + /** + * Retrieves the long at the specified slot (8-byte offset). + */ + public long getLongAt(int slot) { + if(Platform.isJavaSE()){ + return longBuffer().get(slot); + }else{ + return getLongCDCAt(slot); + } + } + + /** + * Puts a long at the specified slot (8-byte offset). + */ + public void setLongAt(int slot, long v) { + if(Platform.isJavaSE()){ + longBuffer().put(slot, v); + }else{ + setLongCDCAt(slot, v); + } + } + + //---------------------------------------------------------------------- + // Internals only below this point + // + + private final long getLongCDCAt(int slot) { + slot = slot << 1; // 8-byte to 4-byte offset + IntBuffer intBuffer = intBuffer(); + long lo = 0x00000000FFFFFFFFL & ((long) intBuffer.get(slot)); + long hi = 0x00000000FFFFFFFFL & ((long) intBuffer.get(slot + 1)); + if (Platform.isLittleEndian()) { + return hi << 32 | lo; + } + return lo << 32 | hi; + } + + private final void setLongCDCAt(int slot, long v) { + slot = slot << 1; // 8-byte to 4-byte offset + IntBuffer intBuffer = intBuffer(); + int lo = (int) ((v) & 0x00000000FFFFFFFFL); + int hi = (int) ((v >> 32) & 0x00000000FFFFFFFFL); + if (Platform.isLittleEndian()) { + intBuffer.put(slot, lo); + intBuffer.put(slot + 1, hi); + } else { + intBuffer.put(slot, hi); + intBuffer.put(slot + 1, lo); + } + } + + private final FloatBuffer floatBuffer() { + if (fb == null) { + fb = bb.asFloatBuffer(); + } + return fb; + } + + private final IntBuffer intBuffer() { + if (ib == null) { + ib = bb.asIntBuffer(); + } + return ib; + } + + private final ShortBuffer shortBuffer() { + if (sb == null) { + sb = bb.asShortBuffer(); + } + return sb; + } + + // - - Java SE only - - + + private final LongBuffer longBuffer() { + checkSE(); + if (lb == null) { + lb = bb.asLongBuffer(); + } + return lb; + } + + private final DoubleBuffer doubleBuffer() { + checkSE(); + if (db == null) { + db = bb.asDoubleBuffer(); + } + return db; + } + + private final CharBuffer charBuffer() { + checkSE(); + if (cb == null) { + cb = bb.asCharBuffer(); + } + return cb; + } + + private static void checkSE() { + if (!Platform.isJavaSE()) { + throw new UnsupportedOperationException("I am affraid, this Operation is not supportet on this platform."); + } + } +} diff --git a/src/java/com/jogamp/common/os/DynamicLinker.java b/src/java/com/jogamp/common/os/DynamicLinker.java new file mode 100755 index 0000000..ef5d5dc --- /dev/null +++ b/src/java/com/jogamp/common/os/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.jogamp.common.os; + +/** 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/jogamp/common/os/DynamicLookupHelper.java b/src/java/com/jogamp/common/os/DynamicLookupHelper.java new file mode 100755 index 0000000..a4f74a4 --- /dev/null +++ b/src/java/com/jogamp/common/os/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.jogamp.common.os; + +/** 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/jogamp/common/os/MacOSXDynamicLinkerImpl.java b/src/java/com/jogamp/common/os/MacOSXDynamicLinkerImpl.java new file mode 100755 index 0000000..531bc5c --- /dev/null +++ b/src/java/com/jogamp/common/os/MacOSXDynamicLinkerImpl.java @@ -0,0 +1,58 @@ +/* !---- DO NOT EDIT: This file autogenerated by com\sun\gluegen\JavaEmitter.java on Mon Jul 31 16:27:00 PDT 2006 ----! */ + +package com.jogamp.common.os; + + +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: <br> <code> int dlclose(void * __handle); </code> */ + private static native int dlclose(long __handle); + + /** Interface to C language function: <br> <code> char * dlerror(void); </code> */ + private static native java.lang.String dlerror(); + + /** Interface to C language function: <br> <code> void * dlopen(const char * __path, int __mode); </code> */ + private static native long dlopen(java.lang.String __path, int __mode); + + /** Interface to C language function: <br> <code> void * dlsym(void * __handle, const char * __symbol); </code> */ + 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/jogamp/common/os/NativeLibrary.java b/src/java/com/jogamp/common/os/NativeLibrary.java new file mode 100755 index 0000000..2de8bc9 --- /dev/null +++ b/src/java/com/jogamp/common/os/NativeLibrary.java @@ -0,0 +1,426 @@ +/* + * 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.jogamp.common.os; + +import com.jogamp.gluegen.runtime.NativeLibLoader; +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 + <code>System.load()</code> and <code>System.loadLibrary()</code> + 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 implements DynamicLookupHelper { + 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 dynamicLookupFunction(String funcName) { + if (libraryHandle == 0) + throw new RuntimeException("Library is not open"); + return dynLink.lookupSymbol(libraryHandle, funcName); + } + + /** 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; + public static void ensureNativeLibLoaded() { + if (!loadedDynLinkNativeLib) { + synchronized (NativeLibrary.class) { + if (!loadedDynLinkNativeLib) { + loadedDynLinkNativeLib = true; + NativeLibLoader.loadGlueGenRT(); + } + } + } + } +} diff --git a/src/java/com/jogamp/common/os/Platform.java b/src/java/com/jogamp/common/os/Platform.java new file mode 100644 index 0000000..169af13 --- /dev/null +++ b/src/java/com/jogamp/common/os/Platform.java @@ -0,0 +1,163 @@ +/* + * Copyright (c) 2010, Michael Bien, Sven Gothel + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions 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 Michael Bien nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL Michael Bien BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * Created on Sunday, March 28 2010 14:43 + */ +package com.jogamp.common.os; + +import com.jogamp.common.nio.Buffers; +import java.nio.ByteBuffer; +import java.nio.IntBuffer; +import java.nio.ShortBuffer; + +/** + * Utility class for querying platform specific properties. + * @author Michael Bien, Sven Gothel + */ +public class Platform { + + public static final boolean JAVA_SE; + public static final boolean LITTLE_ENDIAN; + + private final static boolean is32Bit; + private final static int pointerSizeInBits; + private final static String os, arch; + + static { + NativeLibrary.ensureNativeLibLoaded(); + + // We don't seem to need an AccessController.doPrivileged() block + // here as these system properties are visible even to unsigned + // applets + os = System.getProperty("os.name"); + arch = System.getProperty("os.arch"); + + pointerSizeInBits = getPointerSizeInBitsImpl(); + + // Try to use Sun's sun.arch.data.model first .. + if ( 32 == pointerSizeInBits || 64 == pointerSizeInBits ) { + is32Bit = ( 32 == pointerSizeInBits ); + }else { + String os_lc = os.toLowerCase(); + String arch_lc = arch.toLowerCase(); + + if ((os_lc.startsWith("windows") && arch_lc.equals("x86")) || + (os_lc.startsWith("windows") && arch_lc.equals("arm")) || + (os_lc.startsWith("linux") && arch_lc.equals("i386")) || + (os_lc.startsWith("linux") && arch_lc.equals("x86")) || + (os_lc.startsWith("mac os_lc") && arch_lc.equals("ppc")) || + (os_lc.startsWith("mac os_lc") && arch_lc.equals("i386")) || + (os_lc.startsWith("darwin") && arch_lc.equals("ppc")) || + (os_lc.startsWith("darwin") && arch_lc.equals("i386")) || + (os_lc.startsWith("sunos_lc") && arch_lc.equals("sparc")) || + (os_lc.startsWith("sunos_lc") && arch_lc.equals("x86")) || + (os_lc.startsWith("freebsd") && arch_lc.equals("i386")) || + (os_lc.startsWith("hp-ux") && arch_lc.equals("pa_risc2.0"))) { + is32Bit = true; + } else if ((os_lc.startsWith("windows") && arch_lc.equals("amd64")) || + (os_lc.startsWith("linux") && arch_lc.equals("amd64")) || + (os_lc.startsWith("linux") && arch_lc.equals("x86_64")) || + (os_lc.startsWith("linux") && arch_lc.equals("ia64")) || + (os_lc.startsWith("mac os_lc") && arch_lc.equals("x86_64")) || + (os_lc.startsWith("darwin") && arch_lc.equals("x86_64")) || + (os_lc.startsWith("sunos_lc") && arch_lc.equals("sparcv9")) || + (os_lc.startsWith("sunos_lc") && arch_lc.equals("amd64"))) { + is32Bit = false; + }else{ + throw new RuntimeException("Please port CPU detection (32/64 bit) to your platform (" + os_lc + "/" + arch_lc + ")"); + } + } + + // fast path + boolean se = System.getProperty("java.runtime.name").indexOf("Java SE") != -1; + + if(!se) { + try{ + Class.forName("java.nio.LongBuffer"); + Class.forName("java.nio.DoubleBuffer"); + se = true; + }catch(ClassNotFoundException ex) { + se = false; + } + } + JAVA_SE = se; + + // byte order + ByteBuffer tst_b = Buffers.newDirectByteBuffer(Buffers.SIZEOF_INT); // 32bit in native order + IntBuffer tst_i = tst_b.asIntBuffer(); + ShortBuffer tst_s = tst_b.asShortBuffer(); + tst_i.put(0, 0x0A0B0C0D); + LITTLE_ENDIAN = 0x0C0D == tst_s.get(0); + } + + private Platform() {} + + private static native int getPointerSizeInBitsImpl(); + + /** + * Returns true only if this program is running on the Java Standard Edition. + */ + public static boolean isJavaSE() { + return JAVA_SE; + } + + /** + * Returns true only if this system uses little endian byte ordering. + */ + public static boolean isLittleEndian() { + return LITTLE_ENDIAN; + } + + /** + * Returns the OS name. + */ + public static String getOS() { + return os; + } + + /** + * Returns the CPU architecture String. + */ + public static String getArch() { + return arch; + } + + /** + * Returns true if this JVM is a 32bit JVM. + */ + public static boolean is32Bit() { + return is32Bit; + } + + public static int getPointerSizeInBits() { + return pointerSizeInBits; + } + +} + + diff --git a/src/java/com/jogamp/common/os/UnixDynamicLinkerImpl.java b/src/java/com/jogamp/common/os/UnixDynamicLinkerImpl.java new file mode 100755 index 0000000..02bc828 --- /dev/null +++ b/src/java/com/jogamp/common/os/UnixDynamicLinkerImpl.java @@ -0,0 +1,64 @@ +/* !---- DO NOT EDIT: This file autogenerated by com\sun\gluegen\JavaEmitter.java on Mon Jul 31 16:26:59 PDT 2006 ----! */ + +package com.jogamp.common.os; + + +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: <br> <code> int dlclose(void * ); </code> */ + private static native int dlclose(long arg0); + + /** Interface to C language function: <br> <code> char * dlerror(void); </code> */ + private static native java.lang.String dlerror(); + + /** Interface to C language function: <br> <code> void * dlopen(const char * , int); </code> */ + private static native long dlopen(java.lang.String arg0, int arg1); + + /** Interface to C language function: <br> <code> void * dlsym(void * , const char * ); </code> */ + 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/jogamp/common/os/WindowsDynamicLinkerImpl.java b/src/java/com/jogamp/common/os/WindowsDynamicLinkerImpl.java new file mode 100755 index 0000000..f5a3312 --- /dev/null +++ b/src/java/com/jogamp/common/os/WindowsDynamicLinkerImpl.java @@ -0,0 +1,47 @@ +/* !---- DO NOT EDIT: This file autogenerated by com\sun\gluegen\JavaEmitter.java on Tue May 27 02:37:55 PDT 2008 ----! */ + +package com.jogamp.common.os; + + +public class WindowsDynamicLinkerImpl implements DynamicLinker { + + + /** Interface to C language function: <br> <code> BOOL FreeLibrary(HANDLE hLibModule); </code> */ + private static native int FreeLibrary(long hLibModule); + + /** Interface to C language function: <br> <code> DWORD GetLastError(void); </code> */ + private static native int GetLastError(); + + /** Interface to C language function: <br> <code> PROC GetProcAddressA(HANDLE hModule, LPCSTR lpProcName); </code> */ + private static native long GetProcAddressA(long hModule, java.lang.String lpProcName); + + /** Interface to C language function: <br> <code> HANDLE LoadLibraryW(LPCWSTR lpLibFileName); </code> */ + 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 |