summaryrefslogtreecommitdiffstats
path: root/src/java/com/jogamp/common/nio/AbstractBuffer.java
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2011-04-27 19:47:43 +0200
committerSven Gothel <[email protected]>2011-04-27 19:47:43 +0200
commit8f8aa3f73e3c9804c4a86f5d4fdac257d50d831a (patch)
tree87497a9ffd60f4d3c2be2535312a72f84087a23d /src/java/com/jogamp/common/nio/AbstractBuffer.java
parenta07892f07f15c96ca248fc12133748be7058241f (diff)
NativeBuffer/PointerBuffer API/Impl Change (remove explicit backup array, alloc referenced data map if used only)
This patch doesn't impact GlueGen's code generation, but enhance and fix PointerBuffer usage only. remove explicit backup array As suggested by Michael Bien with a proposed patch, PointerBuffer's backup array is not only redundant in case it's not used, but also erroneous - due to possible sliced buffers. Removes the explicit backup array implementation leaving it up to the user, ie how PointerBuffer is created (alloc/allocDirect) and use the underlying nio's buffer backup array, if available. This also fixes the (never tested) case of indirect w/ backup array usage on 32bit platform size. In this case the array shall be of type int[], holding 32bit pointer - on 64bit long[]. Previous to this patch, it was always long[]. Added more thorough tests of PointerBuffer, notably indirect w/ backup array and native deep copy and filling of a pointer array. alloc referenced data map if used only As suggested by Michael Bien with a proposed patch, the allocation of the dataMap hash map is redundant in case it's not used. The hash map will be initialized lazy, if needed only.
Diffstat (limited to 'src/java/com/jogamp/common/nio/AbstractBuffer.java')
-rw-r--r--src/java/com/jogamp/common/nio/AbstractBuffer.java49
1 files changed, 34 insertions, 15 deletions
diff --git a/src/java/com/jogamp/common/nio/AbstractBuffer.java b/src/java/com/jogamp/common/nio/AbstractBuffer.java
index 870f3c0..58bab65 100644
--- a/src/java/com/jogamp/common/nio/AbstractBuffer.java
+++ b/src/java/com/jogamp/common/nio/AbstractBuffer.java
@@ -33,15 +33,17 @@ package com.jogamp.common.nio;
import com.jogamp.common.os.*;
+import java.nio.Buffer;
import java.nio.ByteBuffer;
/**
- * @author Michael Bien
* @author Sven Gothel
+ * @author Michael Bien
*/
public abstract class AbstractBuffer<B extends AbstractBuffer> implements NativeBuffer<B> {
- protected final ByteBuffer bb;
+ protected final int elementSize;
+ protected final Buffer buffer;
protected int capacity;
protected int position;
@@ -49,13 +51,22 @@ public abstract class AbstractBuffer<B extends AbstractBuffer> implements Native
NativeLibrary.ensureNativeLibLoaded();
}
- protected AbstractBuffer(ByteBuffer bb, int elementSize) {
- this.bb = bb;
+ /**
+ * @param buffer expected in target format
+ * @param elementSize the target element size in bytes
+ */
+ protected AbstractBuffer(Buffer buffer, int elementSize) {
+ this.elementSize = elementSize;
+ this.buffer = buffer;
- capacity = bb.capacity() / elementSize;
+ capacity = buffer.capacity() /* / elementSize */;
position = 0;
}
+ public final int elementSize() {
+ return elementSize;
+ }
+
public final int limit() {
return capacity;
}
@@ -90,25 +101,33 @@ public abstract class AbstractBuffer<B extends AbstractBuffer> implements Native
return (B) this;
}
- public boolean hasArray() {
- return false;
+ public final Buffer getBuffer() {
+ return buffer;
}
- public int arrayOffset() {
- return 0;
+ public final boolean isDirect() {
+ return buffer.isDirect();
}
-
- public final ByteBuffer getBuffer() {
- return bb;
+
+ public final boolean hasArray() {
+ return buffer.hasArray();
}
- public final boolean isDirect() {
- return bb.isDirect();
+ public final int arrayOffset() {
+ if(hasArray()) {
+ return buffer.arrayOffset();
+ } else {
+ return 0;
+ }
}
+ public Object array() throws UnsupportedOperationException {
+ return buffer.array();
+ }
+
@Override
public String toString() {
- return "AbstractBuffer[capacity "+capacity+", position "+position+", elementSize "+(bb.capacity()/capacity)+", ByteBuffer.capacity "+bb.capacity()+"]";
+ return "AbstractBuffer[direct "+isDirect()+", hasArray "+hasArray()+", capacity "+capacity+", position "+position+", elementSize "+elementSize+", buffer[capacity "+buffer.capacity()+", lim "+buffer.limit()+", pos "+buffer.position()+"]]";
}
}