List of usage examples for io.netty.util.internal PlatformDependent directBufferAddress
public static long directBufferAddress(ByteBuffer buffer)
From source file:org.apache.activemq.artemis.core.io.mapped.BytesUtils.java
License:Apache License
public static void zerosDirect(final ByteBuffer buffer) { //DANGEROUS!! erases bound-checking using directly addresses -> safe only if it use counted loops int remaining = buffer.capacity(); long address = PlatformDependent.directBufferAddress(buffer); while (remaining >= 8) { PlatformDependent.putLong(address, 0L); address += 8;//from ww w. j a v a 2 s . c o m remaining -= 8; } while (remaining > 0) { PlatformDependent.putByte(address, (byte) 0); address++; remaining--; } }
From source file:org.apache.activemq.artemis.core.io.mapped.MappedFile.java
License:Apache License
private MappedFile(FileChannel channel, MappedByteBuffer byteBuffer, int position, int length) throws IOException { this.channel = channel; this.buffer = byteBuffer; this.position = position; this.length = length; this.byteBufWrapper = Unpooled.wrappedBuffer(buffer); this.channelBufferWrapper = new ChannelBufferWrapper(this.byteBufWrapper, false); this.address = PlatformDependent.directBufferAddress(buffer); }
From source file:org.apache.activemq.artemis.core.io.mapped.MappedFile.java
License:Apache License
/** * Reads a sequence of bytes from this file into the given buffer. * <p>//from www .j a v a 2s. c o m * <p> Bytes are read starting at this file's current position, and * then the position is updated with the number of bytes actually read. */ public int read(ByteBuffer dst, int dstStart, int dstLength) throws IOException { final int remaining = this.length - this.position; final int read = Math.min(remaining, dstLength); final long srcAddress = this.address + this.position; if (dst.isDirect()) { final long dstAddress = PlatformDependent.directBufferAddress(dst) + dstStart; PlatformDependent.copyMemory(srcAddress, dstAddress, read); } else { final byte[] dstArray = dst.array(); PlatformDependent.copyMemory(srcAddress, dstArray, dstStart, read); } this.position += read; return read; }
From source file:org.apache.activemq.artemis.core.io.mapped.MappedFile.java
License:Apache License
/** * Writes a sequence of bytes to this file from the given buffer. * <p>/*from www . j a va2 s .c om*/ * <p> Bytes are written starting at this file's current position, */ public void write(ByteBuffer src, int srcStart, int srcLength) throws IOException { final int nextPosition = this.position + srcLength; checkCapacity(nextPosition); final long destAddress = this.address + this.position; if (src.isDirect()) { final long srcAddress = PlatformDependent.directBufferAddress(src) + srcStart; PlatformDependent.copyMemory(srcAddress, destAddress, srcLength); } else { final byte[] srcArray = src.array(); PlatformDependent.copyMemory(srcArray, srcStart, destAddress, srcLength); } rawMovePositionAndLength(nextPosition); }
From source file:org.apache.activemq.artemis.core.io.mapped.MappedSequentialFileFactory.java
License:Apache License
@Override public ByteBuffer newBuffer(final int size) { if (!this.bufferPooling) { return allocateDirectBuffer(size); } else {// www.jav a 2 s.c o m final int requiredCapacity = (int) BytesUtils.align(size, Env.osPageSize()); ByteBuffer byteBuffer = bytesPool.get(); if (byteBuffer == null || requiredCapacity > byteBuffer.capacity()) { //do not free the old one (if any) until the new one will be released into the pool! byteBuffer = ByteBuffer.allocateDirect(requiredCapacity); } else { bytesPool.set(null); PlatformDependent.setMemory(PlatformDependent.directBufferAddress(byteBuffer), size, (byte) 0); byteBuffer.clear(); } byteBuffer.limit(size); return byteBuffer; } }
From source file:org.apache.activemq.artemis.core.io.nio.NIOSequentialFileFactory.java
License:Apache License
@Override public ByteBuffer newBuffer(final int size) { if (!this.bufferPooling) { return allocateDirectBuffer(size); } else {/*from w w w . j a v a2 s .c om*/ final int requiredCapacity = align(size, DEFAULT_CAPACITY_ALIGNMENT); ByteBuffer byteBuffer = bytesPool.get(); if (byteBuffer == null || requiredCapacity > byteBuffer.capacity()) { //do not free the old one (if any) until the new one will be released into the pool! byteBuffer = ByteBuffer.allocateDirect(requiredCapacity); } else { bytesPool.set(null); PlatformDependent.setMemory(PlatformDependent.directBufferAddress(byteBuffer), size, (byte) 0); byteBuffer.clear(); } byteBuffer.limit(size); return byteBuffer; } }
From source file:org.apache.activemq.artemis.core.io.nio.NIOSequentialFileFactory.java
License:Apache License
@Override public void clearBuffer(final ByteBuffer buffer) { if (buffer.isDirect()) { PlatformDependent.setMemory(PlatformDependent.directBufferAddress(buffer), buffer.limit(), (byte) 0); } else {//from ww w .j a va 2 s. c o m Arrays.fill(buffer.array(), buffer.arrayOffset(), buffer.limit(), (byte) 0); } }
From source file:org.apache.activemq.artemis.tests.extras.benchmarks.journal.gcfree.AddJournalRecordEncoder.java
License:Apache License
public AddJournalRecordEncoder record(final ByteBuffer recordBytes, final int recordOffset, final int recordLength) { this.bytes.putInt(this.limit, recordLength); final long dstAddr = PlatformDependent.directBufferAddress(bytes) + this.limit + 4; final long srcAddr = PlatformDependent.directBufferAddress(recordBytes) + recordOffset; PlatformDependent.copyMemory(srcAddr, dstAddr, recordLength); this.limit += (4 + recordLength); return this; }
From source file:org.apache.tajo.tuple.memory.UnSafeTuple.java
License:Apache License
public void writeTo(ByteBuffer bb) { if (bb.remaining() < getLength()) { throw new IndexOutOfBoundsException( "remaining length: " + bb.remaining() + ", tuple length: " + getLength()); }// w ww. j a va 2s .c o m if (getLength() > 0) { if (bb.isDirect()) { PlatformDependent.copyMemory(address(), PlatformDependent.directBufferAddress(bb) + bb.position(), getLength()); bb.position(bb.position() + getLength()); } else { PlatformDependent.copyMemory(address(), bb.array(), bb.arrayOffset() + bb.position(), getLength()); bb.position(bb.position() + getLength()); } } }