Example usage for io.netty.util.internal PlatformDependent setMemory

List of usage examples for io.netty.util.internal PlatformDependent setMemory

Introduction

In this page you can find the example usage for io.netty.util.internal PlatformDependent setMemory.

Prototype

public static void setMemory(long address, long bytes, byte value) 

Source Link

Usage

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  ww  w. j  a v  a  2s.  c  om*/
 * <p> Bytes are written starting at this file's current position,
 */
public void zeros(int position, final int count) throws IOException {
    checkCapacity(position + count);
    //zeroes memory in reverse direction in OS_PAGE_SIZE batches
    //to gain sympathy by the page cache LRU policy
    final long start = this.address + position;
    final long end = start + count;
    int toZeros = count;
    final long lastGap = (int) (end & (OS_PAGE_SIZE - 1));
    final long lastStartPage = end - lastGap;
    long lastZeroed = end;
    if (start <= lastStartPage) {
        if (lastGap > 0) {
            PlatformDependent.setMemory(lastStartPage, lastGap, (byte) 0);
            lastZeroed = lastStartPage;
            toZeros -= lastGap;
        }
    }
    //any that will enter has lastZeroed OS page aligned
    while (toZeros >= OS_PAGE_SIZE) {
        assert BytesUtils.isAligned(lastZeroed, OS_PAGE_SIZE);/**/
        final long startPage = lastZeroed - OS_PAGE_SIZE;
        PlatformDependent.setMemory(startPage, OS_PAGE_SIZE, (byte) 0);
        lastZeroed = startPage;
        toZeros -= OS_PAGE_SIZE;
    }
    //there is anything left in the first OS page?
    if (toZeros > 0) {
        PlatformDependent.setMemory(start, toZeros, (byte) 0);
    }
    //do not move this.position: only this.length can be changed
    position += count;
    if (position > this.length) {
        this.length = position;
    }
}

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 {/*from ww  w  . ja v  a  2s.  c om*/
        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 .java 2s .c o m
        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 {//  ww  w. ja  v a  2 s .  c o m
        Arrays.fill(buffer.array(), buffer.arrayOffset(), buffer.limit(), (byte) 0);
    }
}

From source file:org.apache.arrow.vector.DecimalVector.java

License:Apache License

/**
 * Set the decimal element at given index to the provided array of bytes.
 * Decimal is now implemented as Little Endian. This API allows the user
 * to pass a decimal value in the form of byte array in BE byte order.
 *
 * <p>Consumers of Arrow code can use this API instead of first swapping
 * the source bytes (doing a write and read) and then finally writing to
 * ArrowBuf of decimal vector.//  w  w w.  ja  va2  s  . co  m
 *
 * <p>This method takes care of adding the necessary padding if the length
 * of byte array is less then 16 (length of decimal type).
 *
 * @param index position of element
 * @param value array of bytes containing decimal in big endian byte order.
 */
public void setBigEndian(int index, byte[] value) {
    BitVectorHelper.setValidityBitToOne(validityBuffer, index);
    final int length = value.length;

    // do the bound check.
    valueBuffer.checkBytes(index * TYPE_WIDTH, (index + 1) * TYPE_WIDTH);

    long outAddress = valueBuffer.memoryAddress() + index * TYPE_WIDTH;
    // swap bytes to convert BE to LE
    for (int byteIdx = 0; byteIdx < length; ++byteIdx) {
        PlatformDependent.putByte(outAddress + byteIdx, value[length - 1 - byteIdx]);
    }

    if (length == TYPE_WIDTH) {
        return;
    }

    if (length == 0) {
        PlatformDependent.setMemory(outAddress, DecimalVector.TYPE_WIDTH, (byte) 0);
    } else if (length < TYPE_WIDTH) {
        // sign extend
        final byte pad = (byte) (value[0] < 0 ? 0xFF : 0x00);
        PlatformDependent.setMemory(outAddress + length, DecimalVector.TYPE_WIDTH - length, pad);
    } else {
        throw new IllegalArgumentException(
                "Invalid decimal value length. Valid length in [1 - 16], got " + length);
    }
}

From source file:org.apache.arrow.vector.DecimalVector.java

License:Apache License

/**
 * Sets the element at given index using the buffer whose size maybe <= 16 bytes.
 * @param index index to write the decimal to
 * @param start start of value in the buffer
 * @param buffer contains the decimal in little endian bytes
 * @param length length of the value in the buffer
 *//*from   ww  w  .  j a va  2  s.  c o m*/
public void setSafe(int index, int start, ArrowBuf buffer, int length) {
    handleSafe(index);
    BitVectorHelper.setValidityBitToOne(validityBuffer, index);

    // do the bound checks.
    buffer.checkBytes(start, start + length);
    valueBuffer.checkBytes(index * TYPE_WIDTH, (index + 1) * TYPE_WIDTH);

    long inAddress = buffer.memoryAddress() + start;
    long outAddress = valueBuffer.memoryAddress() + index * TYPE_WIDTH;
    PlatformDependent.copyMemory(inAddress, outAddress, length);
    // sign extend
    if (length < 16) {
        byte msb = PlatformDependent.getByte(inAddress + length - 1);
        final byte pad = (byte) (msb < 0 ? 0xFF : 0x00);
        PlatformDependent.setMemory(outAddress + length, DecimalVector.TYPE_WIDTH - length, pad);
    }
}

From source file:org.apache.arrow.vector.DecimalVector.java

License:Apache License

/**
 * Sets the element at given index using the buffer whose size maybe <= 16 bytes.
 * @param index index to write the decimal to
 * @param start start of value in the buffer
 * @param buffer contains the decimal in big endian bytes
 * @param length length of the value in the buffer
 *///from  w  w  w  .j  a v a2  s  .  c om
public void setBigEndianSafe(int index, int start, ArrowBuf buffer, int length) {
    handleSafe(index);
    BitVectorHelper.setValidityBitToOne(validityBuffer, index);

    // do the bound checks.
    buffer.checkBytes(start, start + length);
    valueBuffer.checkBytes(index * TYPE_WIDTH, (index + 1) * TYPE_WIDTH);

    // not using buffer.getByte() to avoid boundary checks for every byte.
    long inAddress = buffer.memoryAddress() + start;
    long outAddress = valueBuffer.memoryAddress() + index * TYPE_WIDTH;
    // swap bytes to convert BE to LE
    for (int byteIdx = 0; byteIdx < length; ++byteIdx) {
        byte val = PlatformDependent.getByte((inAddress + length - 1) - byteIdx);
        PlatformDependent.putByte(outAddress + byteIdx, val);
    }
    // sign extend
    if (length < 16) {
        byte msb = PlatformDependent.getByte(inAddress);
        final byte pad = (byte) (msb < 0 ? 0xFF : 0x00);
        PlatformDependent.setMemory(outAddress + length, DecimalVector.TYPE_WIDTH - length, pad);
    }
}

From source file:org.apache.arrow.vector.TestBitVectorHelper.java

License:Apache License

@Test
public void testAllBitsSet() {
    final int bufferLength = 32 * 1024;
    try (RootAllocator allocator = new RootAllocator(bufferLength);
            ArrowBuf validityBuffer = allocator.buffer(bufferLength)) {

        PlatformDependent.setMemory(validityBuffer.memoryAddress(), bufferLength, (byte) -1);
        int bitLength = 1024;
        assertTrue(BitVectorHelper.checkAllBitsEqualTo(validityBuffer, bitLength, true));

        bitLength = 1028;//ww  w  .  j a  va  2 s.  co m
        assertTrue(BitVectorHelper.checkAllBitsEqualTo(validityBuffer, bitLength, true));

        PlatformDependent.setMemory(validityBuffer.memoryAddress(), bufferLength, (byte) -1);
        bitLength = 1025;
        BitVectorHelper.setValidityBit(validityBuffer, 12, 0);
        assertFalse(BitVectorHelper.checkAllBitsEqualTo(validityBuffer, bitLength, true));

        PlatformDependent.setMemory(validityBuffer.memoryAddress(), bufferLength, (byte) -1);
        bitLength = 1025;
        BitVectorHelper.setValidityBit(validityBuffer, 1024, 0);
        assertFalse(BitVectorHelper.checkAllBitsEqualTo(validityBuffer, bitLength, true));

        PlatformDependent.setMemory(validityBuffer.memoryAddress(), bufferLength, (byte) -1);
        bitLength = 1026;
        BitVectorHelper.setValidityBit(validityBuffer, 1024, 0);
        assertFalse(BitVectorHelper.checkAllBitsEqualTo(validityBuffer, bitLength, true));

        PlatformDependent.setMemory(validityBuffer.memoryAddress(), bufferLength, (byte) -1);
        bitLength = 1027;
        BitVectorHelper.setValidityBit(validityBuffer, 1025, 0);
        assertFalse(BitVectorHelper.checkAllBitsEqualTo(validityBuffer, bitLength, true));

        PlatformDependent.setMemory(validityBuffer.memoryAddress(), bufferLength, (byte) -1);
        bitLength = 1031;
        BitVectorHelper.setValidityBit(validityBuffer, 1029, 0);
        BitVectorHelper.setValidityBit(validityBuffer, 1030, 0);
        assertFalse(BitVectorHelper.checkAllBitsEqualTo(validityBuffer, bitLength, true));
    }
}