32package com.jogamp.common.nio;
34import java.nio.Buffer;
35import java.nio.ByteBuffer;
36import java.nio.IntBuffer;
37import java.nio.LongBuffer;
39import com.jogamp.common.os.Platform;
40import com.jogamp.common.util.LongObjectHashMap;
69 PointerBuffer(
final LongBuffer b) {
73 private final void validateDataMap() {
75 dataMap =
new LongObjectHashMap();
105 public static PointerBuffer wrap(
final ByteBuffer src,
final int srcByteOffset,
final int elemCount) {
107 final int oldLimit = src.limit();
108 src.position(srcByteOffset);
110 final ByteBuffer ref = src.slice().order(src.order());
111 src.position(oldPos);
118 throw new NullPointerException(
"aptr is null");
122 throw new InternalError(
"Couldn't dereference aptr 0x"+Long.toHexString(aptr)+
", size "+elemCount+
" * "+
POINTER_SIZE);
151 throw new IndexOutOfBoundsException(
"remaining[this "+
remaining()+
" < src "+src.
remaining()+
"], this "+
this+
", src "+src);
160 final long addr = src.
get();
163 final Buffer bb = (Buffer) src.
dataMap.
get(addr);
179 public final long get(
final int idx) {
180 if (0 > idx || idx >=
limit()) {
181 throw new IndexOutOfBoundsException(
"idx "+idx+
" not within [0.."+
limit()+
"), "+
this);
184 return ((IntBuffer)
buffer).get(idx) & 0x00000000FFFFFFFFL;
186 return ((LongBuffer)
buffer).get(idx);
190 public final long get() {
197 public final PointerBuffer get(
final int srcElemPos,
final long[] dest,
final int destElemPos,
final int elemCount) {
198 if (0 > srcElemPos || srcElemPos + elemCount >
limit() || 0 > elemCount ||
199 0 > destElemPos || destElemPos + elemCount > dest.length )
201 throw new IndexOutOfBoundsException(
"destElemPos "+destElemPos+
", srcElemPos "+srcElemPos+
", elemCount "+elemCount+
202 ", srcLimit "+
limit()+
", destLimit "+dest.length+
", "+
this);
205 final IntBuffer src = (IntBuffer)
buffer;
206 for(
int i=0; i<elemCount; ++i) {
207 dest[destElemPos+i] = src.get(srcElemPos+i) & 0x00000000FFFFFFFFL;
210 final LongBuffer src = (LongBuffer)
buffer;
211 final int oldSrcLim = src.limit();
212 final int oldSrcPos = src.position();
213 src.position( srcElemPos ).limit( srcElemPos + elemCount );
214 src.get(dest, destElemPos, elemCount);
215 src.limit(oldSrcLim).position(oldSrcPos);
224 public final PointerBuffer get(
final long[] dest,
final int destElemPos,
final int elemCount) {
225 get(
position, dest, destElemPos, elemCount);
232 if (0 > idx || idx >=
limit()) {
233 throw new IndexOutOfBoundsException(
"idx "+idx+
" not within [0.."+
limit()+
"), "+
this);
236 ((IntBuffer)
buffer).put(idx, (
int) v);
238 ((LongBuffer)
buffer).put(idx, v);
250 public final PointerBuffer put(
final long[] src,
final int srcElemPos,
final int destElemPos,
final int elemCount) {
251 if (0 > destElemPos || destElemPos + elemCount >
limit() || 0 > elemCount ||
252 0 > srcElemPos || srcElemPos + elemCount > src.length )
254 throw new IndexOutOfBoundsException(
"srcElemPos "+srcElemPos+
", destElemPos "+destElemPos+
", elemCount "+elemCount+
255 ", destLimit "+
limit()+
", srcLimit "+src.length+
", "+
this);
258 final IntBuffer dest = (IntBuffer)
buffer;
259 for(
int i=0; i<elemCount; ++i) {
260 dest.put(destElemPos+i, (
int) src[srcElemPos+i]);
263 final LongBuffer dest = (LongBuffer)
buffer;
264 final int oldDestLim = dest.limit();
265 final int oldDestPos = dest.position();
266 dest.position( destElemPos ).limit( destElemPos + elemCount );
267 dest.put(src, srcElemPos, elemCount);
268 dest.limit(oldDestLim).position(oldDestPos);
275 public final PointerBuffer put(
final long[] src,
final int srcElemPos,
final int elemCount) {
289 throw new IllegalArgumentException(
"Buffer is null");
292 throw new IllegalArgumentException(
"Buffer is not direct");
294 final long mask =
Platform.
is32Bit() ? 0x00000000FFFFFFFFL : 0xFFFFFFFFFFFFFFFFL ;
295 final long bbAddr =
Buffers.getDirectBufferAddressImpl(bb) & mask;
297 throw new RuntimeException(
"Couldn't determine native address of given Buffer: "+bb);
316 final long addr =
get(index);
final boolean hasRemaining()
static final int POINTER_SIZE
Platform dependent pointer size in bytes, i.e.
Utility methods allowing easy java.nio.Buffer manipulations.
static ByteBuffer newDirectByteBuffer(final int numElements)
Allocates a new direct ByteBuffer with the specified number of elements.
static boolean isDirect(final Object buf)
Helper routine to tell whether a buffer is direct or not.
Hardware independent container holding an array of native pointer, while its getDirectBufferAddress()...
final PointerBuffer put(final long[] src, final int srcElemPos, final int elemCount)
Relative bulk put method.
final Buffer getReferencedBuffer(final int index)
static PointerBuffer allocateDirect(final int size)
Returns a direct PointerBuffer in native order, w/o backup array.
static PointerBuffer wrap(final ByteBuffer src, final int srcByteOffset, final int elemCount)
Wraps given ByteBuffer src @ srcByteOffset to contain elemCount pointers.
LongObjectHashMap dataMap
final PointerBuffer put(final int idx, final long v)
Absolute put method.
static PointerBuffer allocate(final int size)
Returns a non direct PointerBuffer, having a backup array.
static PointerBuffer derefPointer(final ByteBuffer ptrSrc, final int ptrSrcByteOffset, final int elemCount)
static PointerBuffer derefPointer(final long aptr, final int elemCount)
static PointerBuffer wrap(final ByteBuffer src)
Wraps given ByteBuffer src up to it ByteBuffer#capacity()/POINTER_SIZE pointers.
final PointerBuffer referenceBuffer(final int index, final Buffer bb)
Put the address of the given direct Buffer at the given position of this pointer array.
final PointerBuffer put(final PointerBuffer src)
final PointerBuffer put(final long value)
Relative put method.
final PointerBuffer referenceBuffer(final Buffer bb)
Put the address of the given direct Buffer at the end of this pointer array.
final PointerBuffer duplicate()
final PointerBuffer put(final long[] src, final int srcElemPos, final int destElemPos, final int elemCount)
Absolute put method.
final Buffer getReferencedBuffer()
final long get(final int idx)
Absolute get method.
Fast HashMap for primitive data.
Object clone()
Disclaimer: If the value type doesn't implement clone(), only the reference is copied.
Object get(final long key)
Returns the value to which the specified key is mapped, or getKeyNotFoundValue if this map contains n...
Object put(final long key, final Object value)
Maps the key to the specified value.
Object remove(final long key)
Removes the key-value mapping from this map.
Object setKeyNotFoundValue(final Object newKeyNotFoundValue)
Sets the new key not found value.