aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsg215889 <[email protected]>2009-07-13 02:54:29 -0700
committersg215889 <[email protected]>2009-07-13 02:54:29 -0700
commit35cad1e103bb30737b4feacf554219b795358eec (patch)
tree5dabd55a164f675d882bfea482fbd568f3c473dd
parent558389f22d7a257e422ae50385364dcc74c4f106 (diff)
Add missing changes ..
-rwxr-xr-xsrc/java/com/sun/gluegen/runtime/BufferFactory.java.javame_cdc_fp105
-rwxr-xr-xsrc/java/com/sun/gluegen/runtime/BufferFactory.java.javase151
-rwxr-xr-xsrc/java/com/sun/gluegen/runtime/PointerBuffer.java.javame_cdc_fp124
-rwxr-xr-xsrc/java/com/sun/gluegen/runtime/PointerBuffer.java.javase112
-rwxr-xr-xsrc/java/com/sun/gluegen/runtime/StructAccessor.java.javame_cdc_fp27
5 files changed, 397 insertions, 122 deletions
diff --git a/src/java/com/sun/gluegen/runtime/BufferFactory.java.javame_cdc_fp b/src/java/com/sun/gluegen/runtime/BufferFactory.java.javame_cdc_fp
index 622cc37..5f32b0e 100755
--- a/src/java/com/sun/gluegen/runtime/BufferFactory.java.javame_cdc_fp
+++ b/src/java/com/sun/gluegen/runtime/BufferFactory.java.javame_cdc_fp
@@ -44,25 +44,43 @@ import java.nio.*;
public class BufferFactory {
public static final int SIZEOF_BYTE = 1;
public static final int SIZEOF_SHORT = 2;
+ public static final int SIZEOF_CHAR = 2;
public static final int SIZEOF_INT = 4;
public static final int SIZEOF_FLOAT = 4;
+ public static final int SIZEOF_LONG = 8;
+ public static final int SIZEOF_DOUBLE = 8;
+ private static final boolean isLittleEndian;
+
+ static {
+ ByteBuffer tst_b = BufferFactory.newDirectByteBuffer(BufferFactory.SIZEOF_INT); // 32bit in native order
+ IntBuffer tst_i = tst_b.asIntBuffer();
+ ShortBuffer tst_s = tst_b.asShortBuffer();
+ tst_i.put(0, 0x0A0B0C0D);
+ isLittleEndian = 0x0C0D == tst_s.get(0) ;
+ }
+
+ public static boolean isLittleEndian() {
+ return isLittleEndian;
+ }
+
+ /** Helper routine to create a direct ByteBuffer with native order */
public static ByteBuffer newDirectByteBuffer(int size) {
- ByteBuffer buf = ByteBuffer.allocateDirect(size);
- return buf;
+ return nativeOrder(ByteBuffer.allocateDirect(size));
}
/** 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) {
+ // 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;
}
/** Helper routine to tell whether a buffer is direct or not. Null
pointers are considered direct. isDirect() should really be
public in Buffer and not replicated in all subclasses. */
- public static boolean isDirect(Buffer buf) {
+ public static boolean isDirect(Object buf) {
if (buf == null) {
return true;
}
@@ -74,6 +92,8 @@ public class BufferFactory {
return ((ShortBuffer) buf).isDirect();
} else if (buf instanceof IntBuffer) {
return ((IntBuffer) buf).isDirect();
+ } else if (buf instanceof PointerBuffer) {
+ return ((PointerBuffer) buf).isDirect();
}
throw new RuntimeException("Unexpected buffer type " + buf.getClass().getName());
}
@@ -83,18 +103,23 @@ public class BufferFactory {
account the Buffer position and the underlying type. This is
the total offset for Direct Buffers. */
- public static int getDirectBufferByteOffset(Buffer buf) {
+ public static int getDirectBufferByteOffset(Object buf) {
if(buf == null) {
return 0;
}
- if(buf instanceof ByteBuffer) {
- return (buf.position());
- } else if (buf instanceof FloatBuffer) {
- return (buf.position() * SIZEOF_FLOAT);
- } else if (buf instanceof IntBuffer) {
- return (buf.position() * SIZEOF_INT);
- } else if (buf instanceof ShortBuffer) {
- return (buf.position() * SIZEOF_SHORT);
+ 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 (buf instanceof PointerBuffer) {
+ return (((PointerBuffer)buf).position() * SIZEOF_LONG);
}
throw new RuntimeException("Disallowed array backing store type in buffer "
@@ -105,7 +130,7 @@ public class BufferFactory {
/** Helper routine to return the array backing store reference from
a Buffer object. */
- public static Object getArray(Buffer buf) {
+ public static Object getArray(Object buf) {
if (buf == null) {
return null;
}
@@ -117,6 +142,10 @@ public class BufferFactory {
return ((IntBuffer) buf).array();
} else if (buf instanceof ShortBuffer) {
return ((ShortBuffer) buf).array();
+ } else if (buf instanceof ShortBuffer) {
+ return ((ShortBuffer) buf).array();
+ } else if (buf instanceof PointerBuffer) {
+ return ((PointerBuffer) buf).array();
}
throw new RuntimeException("Disallowed array backing store type in buffer "
@@ -129,20 +158,24 @@ public class BufferFactory {
object. The array offset also includes the position offset
within the buffer, in addition to any array offset. */
- public static int getIndirectBufferByteOffset(Buffer buf) {
+ public static int getIndirectBufferByteOffset(Object buf) {
if(buf == null) {
return 0;
}
- int pos = buf.position();
- if(buf instanceof ByteBuffer) {
- return (((ByteBuffer)buf).arrayOffset() + pos);
- } else if(buf instanceof FloatBuffer) {
- return (SIZEOF_FLOAT*(((FloatBuffer)buf).arrayOffset() + pos));
- } else if(buf instanceof IntBuffer) {
- return (SIZEOF_INT*(((IntBuffer)buf).arrayOffset() + pos));
- } else if(buf instanceof ShortBuffer) {
- return (SIZEOF_SHORT*(((ShortBuffer)buf).arrayOffset() + pos));
- }
+ 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(buf instanceof PointerBuffer) {
+ return (SIZEOF_LONG*(((PointerBuffer)buf).arrayOffset() + ((PointerBuffer)buf).position()));
+ }
throw new RuntimeException("Unknown buffer type " + buf.getClass().getName());
}
@@ -207,21 +240,25 @@ public class BufferFactory {
}
}
- public static void rangeCheckBytes(Buffer buffer, int minBytesRemaining) {
+ public static void rangeCheckBytes(Object buffer, int minBytesRemaining) {
if (buffer == null) {
return;
}
- int elementsRemaining = buffer.remaining();
int bytesRemaining = 0;
- if (buffer instanceof ByteBuffer) {
- bytesRemaining = elementsRemaining;
- } else if (buffer instanceof FloatBuffer) {
- bytesRemaining = elementsRemaining * SIZEOF_FLOAT;
- } else if (buffer instanceof IntBuffer) {
- bytesRemaining = elementsRemaining * SIZEOF_INT;
- } else if (buffer instanceof ShortBuffer) {
- bytesRemaining = elementsRemaining * SIZEOF_SHORT;
+ if (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 (buffer instanceof PointerBuffer) {
+ bytesRemaining = ((PointerBuffer)buffer).remaining() * SIZEOF_LONG;
}
if (bytesRemaining < minBytesRemaining) {
throw new IndexOutOfBoundsException("Required " + minBytesRemaining + " remaining bytes in buffer, only had " + bytesRemaining);
diff --git a/src/java/com/sun/gluegen/runtime/BufferFactory.java.javase b/src/java/com/sun/gluegen/runtime/BufferFactory.java.javase
index 3986bf6..472733f 100755
--- a/src/java/com/sun/gluegen/runtime/BufferFactory.java.javase
+++ b/src/java/com/sun/gluegen/runtime/BufferFactory.java.javase
@@ -50,10 +50,23 @@ public class BufferFactory {
public static final int SIZEOF_LONG = 8;
public static final int SIZEOF_DOUBLE = 8;
+ private static final boolean isLittleEndian;
+
+ static {
+ ByteBuffer tst_b = BufferFactory.newDirectByteBuffer(BufferFactory.SIZEOF_INT); // 32bit in native order
+ IntBuffer tst_i = tst_b.asIntBuffer();
+ ShortBuffer tst_s = tst_b.asShortBuffer();
+ tst_i.put(0, 0x0A0B0C0D);
+ isLittleEndian = 0x0C0D == tst_s.get(0) ;
+ }
+
+ public static boolean isLittleEndian() {
+ return isLittleEndian;
+ }
+
+ /** Helper routine to create a direct ByteBuffer with native order */
public static ByteBuffer newDirectByteBuffer(int size) {
- ByteBuffer buf = ByteBuffer.allocateDirect(size);
- buf.order(ByteOrder.nativeOrder());
- return buf;
+ return nativeOrder(ByteBuffer.allocateDirect(size));
}
/** Helper routine to set a ByteBuffer to the native byte order, if
@@ -66,7 +79,7 @@ public class BufferFactory {
/** Helper routine to tell whether a buffer is direct or not. Null
pointers are considered direct. isDirect() should really be
public in Buffer and not replicated in all subclasses. */
- public static boolean isDirect(Buffer buf) {
+ public static boolean isDirect(Object buf) {
if (buf == null) {
return true;
}
@@ -84,6 +97,8 @@ public class BufferFactory {
return ((IntBuffer) buf).isDirect();
} else if (buf instanceof LongBuffer) {
return ((LongBuffer) buf).isDirect();
+ } else if (buf instanceof PointerBuffer) {
+ return ((PointerBuffer) buf).isDirect();
}
throw new RuntimeException("Unexpected buffer type " + buf.getClass().getName());
}
@@ -93,24 +108,29 @@ public class BufferFactory {
account the Buffer position and the underlying type. This is
the total offset for Direct Buffers. */
- public static int getDirectBufferByteOffset(Buffer buf) {
+ public static int getDirectBufferByteOffset(Object buf) {
if(buf == null) {
return 0;
}
- if(buf instanceof ByteBuffer) {
- return (buf.position());
- } else if (buf instanceof FloatBuffer) {
- return (buf.position() * SIZEOF_FLOAT);
- } else if (buf instanceof IntBuffer) {
- return (buf.position() * SIZEOF_INT);
- } else if (buf instanceof ShortBuffer) {
- return (buf.position() * SIZEOF_SHORT);
- } else if (buf instanceof DoubleBuffer) {
- return (buf.position() * SIZEOF_DOUBLE);
- } else if (buf instanceof LongBuffer) {
- return (buf.position() * SIZEOF_LONG);
- } else if (buf instanceof CharBuffer) {
- return (buf.position() * SIZEOF_CHAR);
+ 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 (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 PointerBuffer) {
+ return (((PointerBuffer)buf).position() * SIZEOF_LONG);
}
throw new RuntimeException("Disallowed array backing store type in buffer "
@@ -121,7 +141,7 @@ public class BufferFactory {
/** Helper routine to return the array backing store reference from
a Buffer object. */
- public static Object getArray(Buffer buf) {
+ public static Object getArray(Object buf) {
if (buf == null) {
return null;
}
@@ -139,6 +159,8 @@ public class BufferFactory {
return ((LongBuffer) buf).array();
} else if (buf instanceof CharBuffer) {
return ((CharBuffer) buf).array();
+ } else if (buf instanceof PointerBuffer) {
+ return ((PointerBuffer) buf).array();
}
throw new RuntimeException("Disallowed array backing store type in buffer "
@@ -151,26 +173,30 @@ public class BufferFactory {
object. The array offset also includes the position offset
within the buffer, in addition to any array offset. */
- public static int getIndirectBufferByteOffset(Buffer buf) {
+ public static int getIndirectBufferByteOffset(Object buf) {
if(buf == null) {
return 0;
}
- int pos = buf.position();
- if(buf instanceof ByteBuffer) {
- return (((ByteBuffer)buf).arrayOffset() + pos);
- } else if(buf instanceof FloatBuffer) {
- return (SIZEOF_FLOAT*(((FloatBuffer)buf).arrayOffset() + pos));
- } else if(buf instanceof IntBuffer) {
- return (SIZEOF_INT*(((IntBuffer)buf).arrayOffset() + pos));
- } else if(buf instanceof ShortBuffer) {
- return (SIZEOF_SHORT*(((ShortBuffer)buf).arrayOffset() + pos));
- } else if(buf instanceof DoubleBuffer) {
- return (SIZEOF_DOUBLE*(((DoubleBuffer)buf).arrayOffset() + pos));
- } else if(buf instanceof LongBuffer) {
- return (SIZEOF_LONG*(((LongBuffer)buf).arrayOffset() + pos));
- } else if(buf instanceof CharBuffer) {
- return (SIZEOF_CHAR*(((CharBuffer)buf).arrayOffset() + pos));
- }
+ 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(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 PointerBuffer) {
+ return (SIZEOF_LONG*(((PointerBuffer)buf).arrayOffset() + ((PointerBuffer)buf).position()));
+ }
throw new RuntimeException("Unknown buffer type " + buf.getClass().getName());
}
@@ -255,45 +281,34 @@ public class BufferFactory {
}
}
- public static void rangeCheckBytes(Buffer buffer, int minBytesRemaining) {
+ public static void rangeCheckBytes(Object buffer, int minBytesRemaining) {
if (buffer == null) {
return;
}
- int elementsRemaining = buffer.remaining();
int bytesRemaining = 0;
- if (buffer instanceof ByteBuffer) {
- bytesRemaining = elementsRemaining;
- } else if (buffer instanceof FloatBuffer) {
- bytesRemaining = elementsRemaining * SIZEOF_FLOAT;
- } else if (buffer instanceof IntBuffer) {
- bytesRemaining = elementsRemaining * SIZEOF_INT;
- } else if (buffer instanceof ShortBuffer) {
- bytesRemaining = elementsRemaining * SIZEOF_SHORT;
- } else if (buffer instanceof DoubleBuffer) {
- bytesRemaining = elementsRemaining * SIZEOF_DOUBLE;
- } else if (buffer instanceof LongBuffer) {
- bytesRemaining = elementsRemaining * SIZEOF_LONG;
- } else if (buffer instanceof CharBuffer) {
- bytesRemaining = elementsRemaining * SIZEOF_CHAR;
+ if (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 (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 PointerBuffer) {
+ bytesRemaining = ((PointerBuffer)buffer).remaining() * SIZEOF_LONG;
}
if (bytesRemaining < minBytesRemaining) {
throw new IndexOutOfBoundsException("Required " + minBytesRemaining + " remaining bytes in buffer, only had " + bytesRemaining);
}
}
-
- public static LongBuffer asPointerBuffer(ByteBuffer src) {
- if (CPU.is32Bit()) {
- // Must convert each pointer from 32-bit to 64-bit
- IntBuffer buf = src.asIntBuffer();
- int len = buf.capacity();
- LongBuffer res = LongBuffer.wrap(new long[len]);
- for (int i = 0; i < len; i++) {
- res.put(i, buf.get(i));
- }
- return res;
- } else {
- return src.asLongBuffer();
- }
- }
}
diff --git a/src/java/com/sun/gluegen/runtime/PointerBuffer.java.javame_cdc_fp b/src/java/com/sun/gluegen/runtime/PointerBuffer.java.javame_cdc_fp
index b2d216e..14a3d34 100755
--- a/src/java/com/sun/gluegen/runtime/PointerBuffer.java.javame_cdc_fp
+++ b/src/java/com/sun/gluegen/runtime/PointerBuffer.java.javame_cdc_fp
@@ -41,38 +41,136 @@ import java.nio.*;
public class PointerBuffer {
private ByteBuffer bb;
private IntBuffer ib;
+ private int capacity, position;
+ private long[] backup;
- public PointerBuffer(ByteBuffer bb) {
+ private PointerBuffer(ByteBuffer bb) {
this.bb = bb;
this.ib = bb.asIntBuffer();
+ capacity = bb.capacity()/BufferFactory.SIZEOF_LONG;
+ position=0;
+ backup = new long[capacity];
+ }
+
+ public final int limit() {
+ return capacity;
+ }
+ public final int capacity() {
+ return capacity;
+ }
+
+ public final int position() {
+ return position;
+ }
+
+ public final int remaining() {
+ return capacity - position;
+ }
+
+ public final boolean hasRemaining() {
+ return position < capacity;
+ }
+
+ public final void rewind() {
+ position=0;
+ }
+
+ int arrayOffset() { return 0; }
+
+ boolean hasArray() { return true; }
+
+ public long[] array() {
+ return backup;
+ }
+
+ public static PointerBuffer allocate(int size) {
+ return new PointerBuffer(ByteBuffer.wrap(new byte[BufferFactory.SIZEOF_LONG * size]));
+ }
+
+ public static PointerBuffer allocateDirect(int size) {
+ return new PointerBuffer(BufferFactory.newDirectByteBuffer(size));
+ }
+
+ public static PointerBuffer wrap(ByteBuffer src) {
+ PointerBuffer res = new PointerBuffer(src);
+ res.updateBackup();
+ return res;
+ }
+
+ /**
+ * Wraps pointer arrays created by native code.
+ * Note: In case this is not a 64bit system, each pointer is being converted. */
+ public static PointerBuffer wrapNative2Java(ByteBuffer src, boolean keepDirect) {
+ PointerBuffer res;
+ if (CPU.is32Bit()) {
+ // Must convert each pointer from 32-bit to 64-bit
+ IntBuffer buf = src.asIntBuffer();
+ int len = buf.capacity();
+ res = (src.isDirect() && keepDirect) ? PointerBuffer.allocateDirect(len) : PointerBuffer.allocate(len);
+ for (int i = 0; i < len; i++) {
+ res.put(i, buf.get(i));
+ }
+ } else {
+ res = new PointerBuffer(src);
+ res.updateBackup();
+ }
+ return res;
}
public ByteBuffer getBuffer() {
return bb;
}
- /** Retrieves the long at the specified slot (8-byte offset). */
- public long get(int slot) {
- slot = slot << 1 ; // 8-byte to 4-byte offset
- long lo = 0x00000000FFFFFFFFL & ( (long) ib.get(slot) );
- long hi = 0x00000000FFFFFFFFL & ( (long) ib.get(slot+1) );
+ public boolean isDirect() {
+ return bb.isDirect();
+ }
+
+ public 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) ib.get(idx) );
+ long hi = 0x00000000FFFFFFFFL & ( (long) ib.get(idx+1) );
if(BufferFactory.isLittleEndian()) {
return hi << 32 | lo ;
}
return lo << 32 | hi ;
}
- /** Puts a long at the specified slot (8-byte offset). */
- public void put(int slot, long v) {
- slot = slot << 1 ; // 8-byte to 4-byte offset
+ public long get() {
+ long r = get(position+1);
+ position++;
+ return r;
+ }
+
+ public PointerBuffer 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(BufferFactory.isLittleEndian()) {
- ib.put(slot, lo);
- ib.put(slot+1, hi);
+ ib.put(idx, lo);
+ ib.put(idx+1, hi);
} else {
- ib.put(slot, hi);
- ib.put(slot+1, lo);
+ ib.put(idx, hi);
+ ib.put(idx+1, lo);
+ }
+ return this;
+ }
+
+ public PointerBuffer put(long v) {
+ put(position+1, v);
+ position++;
+ return this;
+ }
+
+ private void updateBackup() {
+ for (int i = 0; i < capacity; i++) {
+ backup[i] = get(i);
}
}
}
diff --git a/src/java/com/sun/gluegen/runtime/PointerBuffer.java.javase b/src/java/com/sun/gluegen/runtime/PointerBuffer.java.javase
index 921d186..88531e8 100755
--- a/src/java/com/sun/gluegen/runtime/PointerBuffer.java.javase
+++ b/src/java/com/sun/gluegen/runtime/PointerBuffer.java.javase
@@ -41,23 +41,121 @@ import java.nio.*;
public class PointerBuffer {
private ByteBuffer bb;
private LongBuffer lb;
+ private int capacity, position;
+ private long[] backup;
- public PointerBuffer(ByteBuffer bb) {
+ private PointerBuffer(ByteBuffer bb) {
this.bb = bb;
this.lb = bb.asLongBuffer();
+ capacity = bb.capacity()/BufferFactory.SIZEOF_LONG;
+ position=0;
+ backup = new long[capacity];
+ }
+
+ public final int limit() {
+ return capacity;
+ }
+ public final int capacity() {
+ return capacity;
+ }
+
+ public final int position() {
+ return position;
+ }
+
+ public final int remaining() {
+ return capacity - position;
+ }
+
+ public final boolean hasRemaining() {
+ return position < capacity;
+ }
+
+ public final void rewind() {
+ position=0;
+ }
+
+ int arrayOffset() { return 0; }
+
+ boolean hasArray() { return true; }
+
+ public long[] array() {
+ return backup;
+ }
+
+ public static PointerBuffer allocate(int size) {
+ return new PointerBuffer(ByteBuffer.wrap(new byte[BufferFactory.SIZEOF_LONG * size]));
+ }
+
+ public static PointerBuffer allocateDirect(int size) {
+ return new PointerBuffer(BufferFactory.newDirectByteBuffer(size));
+ }
+
+ public static PointerBuffer wrap(ByteBuffer src) {
+ PointerBuffer res = new PointerBuffer(src);
+ res.updateBackup();
+ return res;
+ }
+
+ /**
+ * Wraps pointer arrays created by native code.
+ * Note: In case this is not a 64bit system, each pointer is being converted. */
+ public static PointerBuffer wrapNative2Java(ByteBuffer src, boolean keepDirect) {
+ PointerBuffer res;
+ if (CPU.is32Bit()) {
+ // Must convert each pointer from 32-bit to 64-bit
+ IntBuffer buf = src.asIntBuffer();
+ int len = buf.capacity();
+ res = (src.isDirect() && keepDirect) ? PointerBuffer.allocateDirect(len) : PointerBuffer.allocate(len);
+ for (int i = 0; i < len; i++) {
+ res.put(i, buf.get(i));
+ }
+ } else {
+ res = new PointerBuffer(src);
+ res.updateBackup();
+ }
+ return res;
}
public ByteBuffer getBuffer() {
return bb;
}
- /** Retrieves the long at the specified slot (8-byte offset). */
- public long get(int slot) {
- return lb.get(slot);
+ public boolean isDirect() {
+ return bb.isDirect();
+ }
+
+ public long get(int idx) {
+ if(0>idx || idx>=capacity) {
+ throw new IndexOutOfBoundsException();
+ }
+ return lb.get(idx);
+ }
+
+ public long get() {
+ long r = get(position+1);
+ position++;
+ return r;
+ }
+
+ public PointerBuffer put(int idx, long v) {
+ if(0>idx || idx>=capacity) {
+ throw new IndexOutOfBoundsException();
+ }
+ backup[idx] = v;
+ lb.put(idx, v);
+ return this;
+ }
+
+ public PointerBuffer put(long v) {
+ put(position+1, v);
+ position++;
+ return this;
}
- /** Puts a long at the specified slot (8-byte offset). */
- public void put(int slot, long v) {
- lb.put(slot, v);
+ private void updateBackup() {
+ for (int i = 0; i < capacity; i++) {
+ backup[i] = get(i);
+ }
}
}
diff --git a/src/java/com/sun/gluegen/runtime/StructAccessor.java.javame_cdc_fp b/src/java/com/sun/gluegen/runtime/StructAccessor.java.javame_cdc_fp
index 3e3f372..d670bcc 100755
--- a/src/java/com/sun/gluegen/runtime/StructAccessor.java.javame_cdc_fp
+++ b/src/java/com/sun/gluegen/runtime/StructAccessor.java.javame_cdc_fp
@@ -98,6 +98,33 @@ public class StructAccessor {
intBuffer().put(slot, v);
}
+ /** Retrieves the long at the specified slot (8-byte offset). */
+ public long getLongAt(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(BufferFactory.isLittleEndian()) {
+ return hi << 32 | lo ;
+ }
+ return lo << 32 | hi ;
+ }
+
+ /** Puts a long at the specified slot (8-byte offset). */
+ public void setLongAt(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(BufferFactory.isLittleEndian()) {
+ intBuffer.put(slot, lo);
+ intBuffer.put(slot+1, hi);
+ } else {
+ intBuffer.put(slot, hi);
+ intBuffer.put(slot+1, lo);
+ }
+ }
+
/** Retrieves the short at the specified slot (2-byte offset). */
public short getShortAt(int slot) {
return shortBuffer().get(slot);