io.netty.util.internal.PlatformDependent Java Examples

The following examples show how to use io.netty.util.internal.PlatformDependent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ReferenceCountedOpenSslEngine.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
/**
 * Write encrypted data to the OpenSSL network BIO.将加密数据写入OpenSSL网络BIO。
 */
private ByteBuf writeEncryptedData(final ByteBuffer src, int len) {
    final int pos = src.position();
    if (src.isDirect()) {
        SSL.bioSetByteBuffer(networkBIO, bufferAddress(src) + pos, len, false);
    } else {
        final ByteBuf buf = alloc.directBuffer(len);
        try {
            final int limit = src.limit();
            src.limit(pos + len);
            buf.writeBytes(src);
            // Restore the original position and limit because we don't want to consume from `src`.
            src.position(pos);
            src.limit(limit);

            SSL.bioSetByteBuffer(networkBIO, memoryAddress(buf), len, false);
            return buf;
        } catch (Throwable cause) {
            buf.release();
            PlatformDependent.throwException(cause);
        }
    }
    return null;
}
 
Example #2
Source File: ResourceLeakDetectorFactory.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
private static Constructor<?> obsoleteCustomClassConstructor(String customLeakDetector) {
    try {
        final Class<?> detectorClass = Class.forName(customLeakDetector, true,
                PlatformDependent.getSystemClassLoader());

        if (ResourceLeakDetector.class.isAssignableFrom(detectorClass)) {
            return detectorClass.getConstructor(Class.class, int.class, long.class);
        } else {
            logger.error("Class {} does not inherit from ResourceLeakDetector.", customLeakDetector);
        }
    } catch (Throwable t) {
        logger.error("Could not load custom resource leak detector class provided: {}",
                customLeakDetector, t);
    }
    return null;
}
 
Example #3
Source File: ThreadLocalByteBufferPool.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Override
public void release(ByteBuffer buffer) {
   Objects.requireNonNull(buffer);
   boolean directBuffer = buffer.isDirect();
   if (directBuffer == direct && !buffer.isReadOnly()) {
      final ByteBuffer byteBuffer = bytesPool.get();
      if (byteBuffer != buffer) {
         //replace with the current pooled only if greater or null
         if (byteBuffer == null || buffer.capacity() > byteBuffer.capacity()) {
            if (byteBuffer != null) {
               //free the smaller one
               if (directBuffer) {
                  PlatformDependent.freeDirectBuffer(byteBuffer);
               }
            }
            bytesPool.set(buffer);
         } else {
            if (directBuffer) {
               PlatformDependent.freeDirectBuffer(buffer);
            }
         }
      }
   }
}
 
Example #4
Source File: AbstractSslEngineThroughputBenchmark.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Setup(Level.Iteration)
public final void setup() throws Exception {
    ByteBufAllocator allocator = new PooledByteBufAllocator(true);
    initEngines(allocator);
    initHandshakeBuffers();

    wrapDstBuffer = allocateBuffer(clientEngine.getSession().getPacketBufferSize() << 2);
    wrapSrcBuffer = allocateBuffer(messageSize);

    byte[] bytes = new byte[messageSize];
    PlatformDependent.threadLocalRandom().nextBytes(bytes);
    wrapSrcBuffer.put(bytes);
    wrapSrcBuffer.flip();

    // Complete the initial TLS handshake.
    if (!doHandshake()) {
        throw new IllegalStateException();
    }
    doSetup();
}
 
Example #5
Source File: UTF8Util.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
private static int unsafeOffHeapWriteUTF(final CharSequence str, final long addressBytes, final int index, final int length) {
   int charCount = index;
   for (int i = 0; i < length; i++) {
      char charAtPos = str.charAt(i);
      if (charAtPos <= 0x7f) {
         PlatformDependent.putByte(addressBytes + charCount++, (byte) charAtPos);
      } else if (charAtPos >= 0x800) {
         PlatformDependent.putByte(addressBytes + charCount++, (byte) (0xE0 | charAtPos >> 12 & 0x0F));
         PlatformDependent.putByte(addressBytes + charCount++, (byte) (0x80 | charAtPos >> 6 & 0x3F));
         PlatformDependent.putByte(addressBytes + charCount++, (byte) (0x80 | charAtPos >> 0 & 0x3F));
      } else {
         PlatformDependent.putByte(addressBytes + charCount++, (byte) (0xC0 | charAtPos >> 6 & 0x1F));
         PlatformDependent.putByte(addressBytes + charCount++, (byte) (0x80 | charAtPos >> 0 & 0x3F));
      }
   }

   final int writtenBytes = (charCount - index);
   return writtenBytes;
}
 
Example #6
Source File: SumAccumulatorsNoSpill.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
public void accumulate(final long memoryAddr, final int count) {
  final long maxAddr = memoryAddr + count * 4;
  final long incomingBit = getInput().getValidityBufferAddress();
  final long incomingValue =  getInput().getDataBufferAddress();
  final long[] bitAddresses = this.bitAddresses;
  final long[] valueAddresses = this.valueAddresses;

  int incomingIndex = 0;
  for(long ordinalAddr = memoryAddr; ordinalAddr < maxAddr; ordinalAddr += 4, incomingIndex++){
    final int bitVal = (PlatformDependent.getByte(incomingBit + ((incomingIndex >>> 3))) >>> (incomingIndex & 7)) & 1;
    final long newVal = PlatformDependent.getLong(incomingValue + (incomingIndex * WIDTH)) * bitVal;
    final int tableIndex = PlatformDependent.getInt(ordinalAddr);
    int chunkIndex = tableIndex >>> LBlockHashTableNoSpill.BITS_IN_CHUNK;
    int chunkOffset = tableIndex & LBlockHashTableNoSpill.CHUNK_OFFSET_MASK;
    final long sumAddr = valueAddresses[chunkIndex] + (chunkOffset) * 8;
    final long bitUpdateAddr = bitAddresses[chunkIndex] + ((chunkOffset >>> 5) * 4);
    final int bitUpdateVal = bitVal << (chunkOffset & 31);
    PlatformDependent.putLong(sumAddr, PlatformDependent.getLong(sumAddr) + newVal);
    PlatformDependent.putInt(bitUpdateAddr, PlatformDependent.getInt(bitUpdateAddr) | bitUpdateVal);
  }
}
 
Example #7
Source File: MultiDestCopier.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@Override
public void copy(long compoundAddr, final int srcStart, final int count) {
  copyWatch.start();

  final long[] dstAddrs = this.dstAddrs;

  long srcAddr = source.getDataBufferAddress() + srcStart * SIZE;

  final long max = compoundAddr + count * OFFSET_SIZE;
  for (; compoundAddr < max; compoundAddr +=OFFSET_SIZE, srcAddr += SIZE) {
    final int compoundIdx = PlatformDependent.getInt(compoundAddr);
    final int batchIdx = compoundIdx >>> 16;
    final int rowIdx = compoundIdx & 65535;

    final long dstAddr = dstAddrs[batchIdx] + rowIdx * SIZE;
    PlatformDependent.putLong(dstAddr, PlatformDependent.getLong(srcAddr));
    PlatformDependent.putLong(dstAddr + 8, PlatformDependent.getLong(srcAddr + 8));
  }

  copyWatch.stop();
}
 
Example #8
Source File: MaxAccumulatorsNoSpill.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
public void accumulate(final long memoryAddr, final int count) {
  final long maxMemAddr = memoryAddr + count * WIDTH_ORDINAL;
  FieldVector inputVector = getInput();
  final long incomingBit = inputVector.getValidityBufferAddress();
  final long incomingValue = inputVector.getDataBufferAddress();
  final long[] bitAddresses = this.bitAddresses;
  final long[] valueAddresses = this.valueAddresses;
  final int scale = ((DecimalVector)inputVector).getScale();

  int incomingIndex = 0;
  for(long ordinalAddr = memoryAddr; ordinalAddr < maxMemAddr; ordinalAddr += WIDTH_ORDINAL, incomingIndex++){
    java.math.BigDecimal newVal = DecimalAccumulatorUtilsNoSpill.getBigDecimal(incomingValue + (incomingIndex * WIDTH_INPUT), valBuf, scale);
    final int tableIndex = PlatformDependent.getInt(ordinalAddr);
    int chunkIndex = tableIndex >>> LBlockHashTableNoSpill.BITS_IN_CHUNK;
    int chunkOffset = tableIndex & LBlockHashTableNoSpill.CHUNK_OFFSET_MASK;
    final long maxAddr = valueAddresses[chunkIndex] + (chunkOffset) * WIDTH_ACCUMULATOR;
    final long bitUpdateAddr = bitAddresses[chunkIndex] + ((chunkOffset >>> 5) * 4);
    final int bitVal = (PlatformDependent.getByte(incomingBit + ((incomingIndex >>> 3))) >>> (incomingIndex & 7)) & 1;
    final int bitUpdateVal = bitVal << (chunkOffset & 31);
    PlatformDependent.putLong(maxAddr, Double.doubleToLongBits(max(Double.longBitsToDouble(PlatformDependent.getLong(maxAddr)), newVal.doubleValue(), bitVal)));
    PlatformDependent.putInt(bitUpdateAddr, PlatformDependent.getInt(bitUpdateAddr) | bitUpdateVal);
  }
}
 
Example #9
Source File: PoolArena.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Override
protected void memoryCopy(ByteBuffer src, int srcOffset, ByteBuffer dst, int dstOffset, int length) {
    if (length == 0) {
        return;
    }

    if (HAS_UNSAFE) {
        PlatformDependent.copyMemory(
                PlatformDependent.directBufferAddress(src) + srcOffset,
                PlatformDependent.directBufferAddress(dst) + dstOffset, length);
    } else {
        // We must duplicate the NIO buffers because they may be accessed by other Netty buffers.我们必须复制NIO缓冲区,因为它们可能被其他Netty缓冲区访问。
        src = src.duplicate();
        dst = dst.duplicate();
        src.position(srcOffset).limit(srcOffset + length);
        dst.position(dstOffset);
        dst.put(src);
    }
}
 
Example #10
Source File: ResourceLeakDetectorFactory.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
private static Constructor<?> customClassConstructor(String customLeakDetector) {
    try {
        final Class<?> detectorClass = Class.forName(customLeakDetector, true,
                PlatformDependent.getSystemClassLoader());

        if (ResourceLeakDetector.class.isAssignableFrom(detectorClass)) {
            return detectorClass.getConstructor(Class.class, int.class);
        } else {
            logger.error("Class {} does not inherit from ResourceLeakDetector.", customLeakDetector);
        }
    } catch (Throwable t) {
        logger.error("Could not load custom resource leak detector class provided: {}",
                customLeakDetector, t);
    }
    return null;
}
 
Example #11
Source File: UTF8Util.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
private static int unsafeOnHeapWriteUTF(final CharSequence str, final byte[] bytes, final int index, final int length) {
   int charCount = index;
   for (int i = 0; i < length; i++) {
      char charAtPos = str.charAt(i);
      if (charAtPos <= 0x7f) {
         PlatformDependent.putByte(bytes, charCount++, (byte) charAtPos);
      } else if (charAtPos >= 0x800) {
         PlatformDependent.putByte(bytes, charCount++, (byte) (0xE0 | charAtPos >> 12 & 0x0F));
         PlatformDependent.putByte(bytes, charCount++, (byte) (0x80 | charAtPos >> 6 & 0x3F));
         PlatformDependent.putByte(bytes, charCount++, (byte) (0x80 | charAtPos >> 0 & 0x3F));
      } else {
         PlatformDependent.putByte(bytes, charCount++, (byte) (0xC0 | charAtPos >> 6 & 0x1F));
         PlatformDependent.putByte(bytes, charCount++, (byte) (0x80 | charAtPos >> 0 & 0x3F));
      }
   }

   final int writtenBytes = (charCount - index);
   return writtenBytes;
}
 
Example #12
Source File: DefaultHttpHeaders.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Override
public void validateName(CharSequence name) {
    if (name == null || name.length() == 0) {
        throw new IllegalArgumentException("empty headers are not allowed [" + name + "]");
    }
    if (name instanceof AsciiString) {
        try {
            ((AsciiString) name).forEachByte(HEADER_NAME_VALIDATOR);
        } catch (Exception e) {
            PlatformDependent.throwException(e);
        }
    } else {
        // Go through each character in the name检查名称中的每个字符
        for (int index = 0; index < name.length(); ++index) {
            validateHeaderNameElement(name.charAt(index));
        }
    }
}
 
Example #13
Source File: VirtualClientConnection.java    From quarkus with Apache License 2.0 6 votes vote down vote up
private void runFinishPeerReadTask(final VirtualChannel peer) {
    // If the peer is writing, we must wait until after reads are completed for that peer before we can read. So
    // we keep track of the task, and coordinate later that our read can't happen until the peer is done.
    final Runnable finishPeerReadTask = new Runnable() {
        @Override
        public void run() {
            finishPeerRead0(peer);
        }
    };
    try {
        if (peer.writeInProgress) {
            peer.finishReadFuture = peer.eventLoop().submit(finishPeerReadTask);
        } else {
            peer.eventLoop().execute(finishPeerReadTask);
        }
    } catch (Throwable cause) {
        close();
        peer.close();
        PlatformDependent.throwException(cause);
    }
}
 
Example #14
Source File: ReferenceCountedOpenSslEngine.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Override
public final synchronized SSLParameters getSSLParameters() {
    SSLParameters sslParameters = super.getSSLParameters();

    int version = PlatformDependent.javaVersion();
    if (version >= 7) {
        sslParameters.setEndpointIdentificationAlgorithm(endPointIdentificationAlgorithm);
        Java7SslParametersUtils.setAlgorithmConstraints(sslParameters, algorithmConstraints);
        if (version >= 8) {
            if (sniHostNames != null) {
                Java8SslUtils.setSniHostNames(sslParameters, sniHostNames);
            }
            if (!isDestroyed()) {
                Java8SslUtils.setUseCipherSuitesOrder(
                        sslParameters, (SSL.getOptions(ssl) & SSL.SSL_OP_CIPHER_SERVER_PREFERENCE) != 0);
            }

            Java8SslUtils.setSNIMatchers(sslParameters, matchers);
        }
    }
    return sslParameters;
}
 
Example #15
Source File: ReadOnlyUnsafeDirectByteBuf.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
@Override
public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) {
    checkIndex(index, length);
    if (dst == null) {
        throw new NullPointerException("dst");
    }
    if (dstIndex < 0 || dstIndex > dst.capacity() - length) {
        throw new IndexOutOfBoundsException("dstIndex: " + dstIndex);
    }

    if (dst.hasMemoryAddress()) {
        PlatformDependent.copyMemory(addr(index), dst.memoryAddress() + dstIndex, length);
    } else if (dst.hasArray()) {
        PlatformDependent.copyMemory(addr(index), dst.array(), dst.arrayOffset() + dstIndex, length);
    } else {
        dst.setBytes(dstIndex, this, index, length);
    }
    return this;
}
 
Example #16
Source File: MaxAccumulators.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public void accumulate(final long memoryAddr, final int count,
                       final int bitsInChunk, final int chunkOffsetMask) {
  final long maxMemAddr = memoryAddr + count * PARTITIONINDEX_HTORDINAL_WIDTH;
  FieldVector inputVector = getInput();
  final long incomingBit = inputVector.getValidityBufferAddress();
  final long incomingValue = inputVector.getDataBufferAddress();
  final long[] bitAddresses = this.bitAddresses;
  final long[] valueAddresses = this.valueAddresses;
  final int maxValuesPerBatch = super.maxValuesPerBatch;

  for (long partitionAndOrdinalAddr = memoryAddr; partitionAndOrdinalAddr < maxMemAddr; partitionAndOrdinalAddr += PARTITIONINDEX_HTORDINAL_WIDTH) {
    /* get the hash table ordinal */
    final int tableIndex = PlatformDependent.getInt(partitionAndOrdinalAddr + HTORDINAL_OFFSET);
    /* get the index of data in input vector */
    final int incomingIndex = PlatformDependent.getInt(partitionAndOrdinalAddr + KEYINDEX_OFFSET);
    /* get the corresponding data from input vector -- source data for accumulation */
    final float newVal = Float.intBitsToFloat(PlatformDependent.getInt(incomingValue + (incomingIndex * WIDTH_INPUT)));
    final int bitVal = (PlatformDependent.getByte(incomingBit + ((incomingIndex >>> 3))) >>> (incomingIndex & 7)) & 1;
    /* get the hash table batch index */
    final int chunkIndex = tableIndex >>> bitsInChunk;
    final int chunkOffset = tableIndex & chunkOffsetMask;
    /* get the target addresses of accumulation vector */
    final long maxAddr = valueAddresses[chunkIndex] + (chunkOffset) * WIDTH_ACCUMULATOR;
    final long bitUpdateAddr = bitAddresses[chunkIndex] + ((chunkOffset >>> 5) * 4);
    final int bitUpdateVal = bitVal << (chunkOffset & 31);
    /* store the accumulated values(new max or existing) at the target location of accumulation vector */
    PlatformDependent.putInt(maxAddr, Float.floatToRawIntBits(max(Float.intBitsToFloat(PlatformDependent.getInt(maxAddr)), newVal, bitVal)));
    PlatformDependent.putInt(bitUpdateAddr, PlatformDependent.getInt(bitUpdateAddr) | bitUpdateVal);
  }
}
 
Example #17
Source File: MultiDestCopier.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public void copy(long compoundAddr, int srcStart, final int count) {
  copyWatch.start();

  final long[] dstAddrs = this.dstAddrs;

  // skip bytes, but make sure to account for the remaining bits too
  final long srcAddr;
  switch (bufferOrdinal) {
    case NULL_BUFFER_ORDINAL:
      srcAddr = source.getValidityBufferAddress();
      break;
    case VALUE_BUFFER_ORDINAL:
      srcAddr = source.getDataBufferAddress();
      break;
    default:
      throw new UnsupportedOperationException("unexpected buffer offset");
  }

  final long max = compoundAddr + count * OFFSET_SIZE;
  for(; compoundAddr < max; compoundAddr +=OFFSET_SIZE, srcStart++){
    final int compoundIdx = PlatformDependent.getInt(compoundAddr);
    final int batchIdx = compoundIdx >>> 16;
    final int rowIdx = compoundIdx & 65535;

    final int byteValue = PlatformDependent.getByte(srcAddr + (srcStart >>> 3));
    final int bitVal = ((byteValue >>> (srcStart & 7)) & 1) << (rowIdx & 7);
    final long dstAddr = dstAddrs[batchIdx] + (rowIdx >>> 3);
    PlatformDependent.putByte(dstAddr, (byte) (PlatformDependent.getByte(dstAddr) | bitVal));
  }

  copyWatch.stop();
}
 
Example #18
Source File: TestCopierRoundTrip.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Test
public void intAppend(){
  final int count = 1024;
  try(
    IntVector in = new IntVector("in", allocator);
    IntVector out = new IntVector("out", allocator);
  ){

    in.allocateNew(count);
    for(int i = 0; i < count; i++){
      if(i % 5 == 0){
        in.setSafe(i, i);
      }
    }
    in.setValueCount(count);

    List<FieldBufferCopier> copiers = FieldBufferCopier.getCopiers(ImmutableList.<FieldVector>of(in), ImmutableList.<FieldVector>of(out));
    try(
      final SelectionVector2 sv2 = new SelectionVector2(allocator);
    ){

      sv2.allocateNew(count / 2);
      // set alternate elements.
      int x = 0;
      for(long mem = sv2.memoryAddress(); mem < sv2.memoryAddress() + count; mem+=2){
        PlatformDependent.putShort(mem, (short) (char) x);
        x += 2;
      }
      sv2.setRecordCount(count/2);

      append(copiers, sv2);

      out.setValueCount(count / 2);
      for(int i =0; i < count / 2; i++){
        assertEquals(in.getObject(i * 2), out.getObject(i));
      }
    }
  }
}
 
Example #19
Source File: TestCopierRoundTrip.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Test
public void intRoundtrip(){
  final int count = 1024;
  try(
      IntVector in = new IntVector("in", allocator);
      IntVector out = new IntVector("out", allocator);
      ){

    in.allocateNew(count);

    for(int i = 0; i < count; i++){
      if(i % 5 == 0){
        in.setSafe(i, i);
      }
    }
    in.setValueCount(count);

    List<FieldBufferCopier> copiers = FieldBufferCopier.getCopiers(ImmutableList.<FieldVector>of(in), ImmutableList.<FieldVector>of(out));
    try(
        final SelectionVector2 sv2 = new SelectionVector2(allocator);
        ){

      sv2.allocateNew(count);
      // create full sv2.
      int x = 0;
      for(long mem = sv2.memoryAddress(); mem < sv2.memoryAddress() + count * 2; mem+=2){
        PlatformDependent.putShort(mem, (short) (char) x);
        x++;
      }
      sv2.setRecordCount(count);
      copy(copiers, sv2);

      out.setValueCount(count);
      for(int i =0; i < count; i++){
        assertEquals(in.getObject(i), out.getObject(i));
      }
    }
  }
}
 
Example #20
Source File: TestDnsServer.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public Set<ResourceRecord> getRecords(QuestionRecord questionRecord) {
    String name = questionRecord.getDomainName();
    if (domains.contains(name)) {
        ResourceRecordModifier rm = new ResourceRecordModifier();
        rm.setDnsClass(RecordClass.IN);
        rm.setDnsName(name);
        rm.setDnsTtl(100);
        rm.setDnsType(questionRecord.getRecordType());

        switch (questionRecord.getRecordType()) {
            case A:
                do {
                    rm.put(DnsAttribute.IP_ADDRESS, nextIp());
                } while (PlatformDependent.threadLocalRandom().nextBoolean());
                break;
            case AAAA:
                do {
                    rm.put(DnsAttribute.IP_ADDRESS, nextIp6());
                } while (PlatformDependent.threadLocalRandom().nextBoolean());
                break;
            case MX:
                int priority = 0;
                do {
                    rm.put(DnsAttribute.DOMAIN_NAME, nextDomain());
                    rm.put(DnsAttribute.MX_PREFERENCE, String.valueOf(++priority));
                } while (PlatformDependent.threadLocalRandom().nextBoolean());
                break;
            default:
                return null;
        }
        return Collections.singleton(rm.getEntry());
    }
    return null;
}
 
Example #21
Source File: AbstractByteBufAllocator.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Override
public ByteBuf ioBuffer() {
    if (PlatformDependent.hasUnsafe()) {
        return directBuffer(DEFAULT_INITIAL_CAPACITY);
    }
    return heapBuffer(DEFAULT_INITIAL_CAPACITY);
}
 
Example #22
Source File: NdvAccumulatorsNoSpill.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public void accumulate(final long memoryAddr, final int count) {
  final long maxAddr = memoryAddr + count * 4;
  final long incomingBit = getInput().getValidityBufferAddress();
  final long incomingValue =  getInput().getDataBufferAddress();

  int incomingIndex = 0;
  for(long ordinalAddr = memoryAddr; ordinalAddr < maxAddr; ordinalAddr += 4, incomingIndex++){
    final int bitVal = (PlatformDependent.getByte(incomingBit + ((incomingIndex >>> 3))) >>> (incomingIndex & 7)) & 1;

    //incoming record is null, skip it
    if (bitVal == 0) {
      continue;
    }

    final int newVal = PlatformDependent.getInt(incomingValue + (incomingIndex * WIDTH));

    //get the proper chunk from the ordinal
    final int tableIndex = PlatformDependent.getInt(ordinalAddr);
    int chunkIndex = tableIndex >>> LBlockHashTableNoSpill.BITS_IN_CHUNK;
    int chunkOffset = tableIndex & LBlockHashTableNoSpill.CHUNK_OFFSET_MASK;

    final HllAccumHolder ah =  this.accumulators[chunkIndex];
    final HllSketch sketch = ah.getAccums()[chunkOffset];
    sketch.update(newVal);
  }
}
 
Example #23
Source File: TestCopierRoundTrip.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Test
public void bigintRoundtrip(){
  final int count = 1024;
  try(
      BigIntVector in = new BigIntVector("in", allocator);
      BigIntVector out = new BigIntVector("out", allocator);
      ){

    in.allocateNew(count);

    for(int i = 0; i < count; i++){
      if(i % 5 == 0){
        in.setSafe(i, i);
      }
    }
    in.setValueCount(count);

    List<FieldBufferCopier> copiers = FieldBufferCopier.getCopiers(ImmutableList.<FieldVector>of(in), ImmutableList.<FieldVector>of(out));
    try(
        final SelectionVector2 sv2 = new SelectionVector2(allocator);
        ){

      sv2.allocateNew(count);
      // create full sv2.
      int x = 0;
      for(long mem = sv2.memoryAddress(); mem < sv2.memoryAddress() + count * 2; mem+=2){
        PlatformDependent.putShort(mem, (short) (char) x);
        x++;
      }
      sv2.setRecordCount(count);
      copy(copiers, sv2);

      out.setValueCount(count);
      for(int i =0; i < count; i++){
        assertEquals(in.getObject(i), out.getObject(i));
      }
    }
  }
}
 
Example #24
Source File: UnSafeTuple.java    From tajo with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] getBytes(int fieldId) {
  long pos = getFieldAddr(fieldId);
  int len = PlatformDependent.getInt(pos);
  pos += SizeOf.SIZE_OF_INT;

  byte [] bytes = new byte[len];
  PlatformDependent.copyMemory(pos, bytes, 0, len);
  return bytes;
}
 
Example #25
Source File: UnsafeByteBufUtil.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
static void setShortLE(long address, int value) {
    if (UNALIGNED) {
        PlatformDependent.putShort(
            address, BIG_ENDIAN_NATIVE_ORDER ? Short.reverseBytes((short) value) : (short) value);
    } else {
        PlatformDependent.putByte(address, (byte) value);
        PlatformDependent.putByte(address + 1, (byte) (value >>> 8));
    }
}
 
Example #26
Source File: OpenSslEngineTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test
public void testWrapBuffersNoWritePendingError() throws Exception {
    clientSslCtx = SslContextBuilder.forClient()
            .trustManager(InsecureTrustManagerFactory.INSTANCE)
            .sslProvider(sslClientProvider())
            .build();
    SelfSignedCertificate ssc = new SelfSignedCertificate();
    serverSslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey())
            .sslProvider(sslServerProvider())
            .build();
    SSLEngine clientEngine = null;
    SSLEngine serverEngine = null;
    try {
        clientEngine = clientSslCtx.newEngine(UnpooledByteBufAllocator.DEFAULT);
        serverEngine = serverSslCtx.newEngine(UnpooledByteBufAllocator.DEFAULT);
        handshake(clientEngine, serverEngine);

        ByteBuffer src = allocateBuffer(1024 * 10);
        byte[] data = new byte[src.capacity()];
        PlatformDependent.threadLocalRandom().nextBytes(data);
        src.put(data).flip();
        ByteBuffer dst = allocateBuffer(1);
        // Try to wrap multiple times so we are more likely to hit the issue.
        for (int i = 0; i < 100; i++) {
            src.position(0);
            dst.position(0);
            assertSame(SSLEngineResult.Status.BUFFER_OVERFLOW, clientEngine.wrap(src, dst).getStatus());
        }
    } finally {
        cleanupClientSslEngine(clientEngine);
        cleanupServerSslEngine(serverEngine);
    }
}
 
Example #27
Source File: UnSafeTuple.java    From tajo with Apache License 2.0 5 votes vote down vote up
public HeapTuple toHeapTuple() {
  HeapTuple heapTuple = new HeapTuple();
  byte [] bytes = new byte[getLength()];
  PlatformDependent.copyMemory(address(), bytes, 0, getLength());
  heapTuple.set(bytes, types);
  return heapTuple;
}
 
Example #28
Source File: NettyBufferPool.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void recycle(ByteBuffer byteBuffer) {
    ByteBuf byteBuf =
            allocator.recycleMaps.get(PlatformDependent.directBufferAddress(byteBuffer));

    if (byteBuf != null) {
        byteBuf.release();
        allocator.recycleMaps.remove(PlatformDependent.directBufferAddress(byteBuffer));
    }

}
 
Example #29
Source File: OffHeapRowWriter.java    From tajo with Apache License 2.0 5 votes vote down vote up
@Override
public void putByte(byte val) {
  ensureSize(SizeOf.SIZE_OF_BYTE);
  long addr = currentAddr();

  PlatformDependent.putByte(addr, val);
  putFieldHeader(addr, curOffset);
  forwardField(SizeOf.SIZE_OF_BYTE);
}
 
Example #30
Source File: UkcpClientUdpChannel.java    From kcp-netty with MIT License 5 votes vote down vote up
@Override
protected int doReadMessages(List<Object> buf) throws Exception {
    DatagramChannel ch = javaChannel();
    ChannelConfig config = config();
    RecvByteBufAllocator.Handle allocHandle = unsafe().recvBufAllocHandle();

    ByteBuf data = allocHandle.allocate(config.getAllocator());
    allocHandle.attemptedBytesRead(data.writableBytes());
    boolean free = true;
    try {
        ByteBuffer nioData = data.internalNioBuffer(data.writerIndex(), data.writableBytes());
        int pos = nioData.position();
        int read = ch.read(nioData);
        if (read <= 0) {
            return read;
        }

        allocHandle.lastBytesRead(nioData.position() - pos);
        buf.add(data.writerIndex(data.writerIndex() + allocHandle.lastBytesRead()));
        free = false;
        return 1;
    } catch (Throwable cause) {
        PlatformDependent.throwException(cause);
        return -1;
    } finally {
        if (free) {
            data.release();
        }
    }
}