static void copyFromArray(byte[] src, int offset, long dstAddr, int length) { while (length > 0) { long size = (length > UNSAFE_COPY_THRESHOLD) ? UNSAFE_COPY_THRESHOLD : length; copyFromByteArray(src, offset, (int)size, dstAddr); length -= size; offset += size; dstAddr += size; } }
static void copyToArray(long srcAddr, byte[] dst, int offset, int length) { while (length > 0) { long size = (length > UNSAFE_COPY_THRESHOLD) ? UNSAFE_COPY_THRESHOLD : length; copyToByteArray(srcAddr, (int)size, dst, offset); length -= size; srcAddr += size; offset += size; } }
public void mlock(long position, long length) { if (position < 0) throw new IndexOutOfBoundsException(); if (length < 0) throw new IndexOutOfBoundsException(); if (position+length > memory.length()) throw new IndexOutOfBoundsException(); NativeMemoryUtils.mlock(address+position, length); }
public void munlock(long position, long length) { if (position < 0) throw new IndexOutOfBoundsException(); if (length < 0) throw new IndexOutOfBoundsException(); if (position+length > memory.length()) throw new IndexOutOfBoundsException(); NativeMemoryUtils.munlock(address+position, length); }
static void mincore(long addr, long length, DirectMemory memory) { int err = mincore(addr, length, memory.getAddress()); if (err != 0) { switch (err) { case ENOMEM: throw new IllegalArgumentException("illegal arguments: address: "+addr+" length: "+length+" vec: "+memory.getAddress()); case EAGAIN: mincore(addr, length, memory); return; case EFAULT: throw new IllegalArgumentException("memory at "+memory.getAddress()+" is not valid"); case EINVAL: throw new IllegalArgumentException("address "+addr+" is not a multiple of the page size"); default: throw new IllegalArgumentException("unknown error"); } } }
@Override public void getBytes(long l, ByteBuffer dest) { final int length = dest.remaining(); checkBounds(l, length); if (dest.isDirect()) { NativeMemoryUtils.copyToDirectBuffer(address+l, dest, dest.position(), length); dest.position(dest.position()+length); } else if (dest.hasArray()) { final byte[] array = dest.array(); final int offset = dest.arrayOffset(); getBytes(l, array, offset+dest.position(), length); dest.position(dest.position()+length); } else { final byte[] copyBuffer = new byte[Math.min(length, 4096)]; long sourceAddr = l; while (dest.remaining() > 0) { final int copySize = Math.min(copyBuffer.length, dest.remaining()); getBytes(sourceAddr, copyBuffer, 0, copySize); dest.put(copyBuffer, 0, copySize); sourceAddr+=copySize; } } }
@Override public void putBytes(final long l, final byte[] bytes, final int start, final int length) { checkBounds(l, length); checkArrayBounds(bytes, start, length); NativeMemoryUtils.copyFromArray(bytes, start, address + l, length); }
@Override public void getBytes(final long l, final byte[] bytes, final int start, final int length) { checkBounds(l, length); checkArrayBounds(bytes, start, length); NativeMemoryUtils.copyToArray(address + l, bytes, start, length); }
@Override public void putBytes(long l, ByteBuffer source) { final int length = source.remaining(); checkBounds(l, length); if (source.isDirect()) { NativeMemoryUtils.copyFromDirectBuffer(source, source.position(), address+l, length); source.position(source.position()+length); } else if (source.hasArray()) { final byte[] array = source.array(); final int offset = source.arrayOffset(); putBytes(l, array, offset+source.position(), length); source.position(source.position()+length); } else { final byte[] copyBuffer = new byte[Math.min(length, 4096)]; long destAddr = l; while (source.remaining() > 0) { final int copySize = Math.min(copyBuffer.length, source.remaining()); source.get(copyBuffer, 0, copySize); putBytes(destAddr, copyBuffer, 0, copySize); destAddr+=copySize; } } }
public void mlock(long position, long length) { if (position < 0) throw new IndexOutOfBoundsException(); if (length < 0) throw new IndexOutOfBoundsException(); if (position+length > memory.length()) throw new IndexOutOfBoundsException(); NativeMemoryUtils.mlock(address+position, length); }
public void munlock(long position, long length) { if (position < 0) throw new IndexOutOfBoundsException(); if (length < 0) throw new IndexOutOfBoundsException(); if (position+length > memory.length()) throw new IndexOutOfBoundsException(); NativeMemoryUtils.munlock(address+position, length); }
public void mincore(long position, long length, DirectMemory direct) { if (position+length > memory().length()) { throw new IndexOutOfBoundsException(); } final long ap = address+position; final long a = ap/PAGE_SIZE*PAGE_SIZE; final long l = length+(ap-a); if ((l+PAGE_SIZE-1)/PAGE_SIZE > direct.length()) throw new IndexOutOfBoundsException(); NativeMemoryUtils.mincore(a, l, direct); }