List of usage examples for io.netty.util.internal PlatformDependent hasUnsafe
public static boolean hasUnsafe()
From source file:org.apache.activemq.artemis.core.io.util.ThreadLocalByteBufferPoolTest.java
License:Apache License
@Test public void shouldNotPoolBufferOfDifferentType() { final int size = 32; final ByteBuffer buffer = isDirect ? ByteBuffer.allocate(size) : ByteBuffer.allocateDirect(size); try {/*from ww w. j a va 2 s . c o m*/ pool.release(buffer); Assert.assertNotSame(buffer, pool.borrow(size, zeroed)); } catch (Throwable t) { if (PlatformDependent.hasUnsafe()) { if (buffer.isDirect()) { PlatformDependent.freeDirectBuffer(buffer); } } } }
From source file:org.apache.activemq.artemis.utils.AbstractByteBufPool.java
License:Apache License
/** * Batch hash code implementation that works at its best if {@code bytes} * contains a {@link org.apache.activemq.artemis.api.core.SimpleString} encoded. *//* w ww. j a v a 2s .c om*/ private static int hashCode(final ByteBuf bytes, final int offset, final int length) { if (PlatformDependent.isUnaligned() && PlatformDependent.hasUnsafe()) { //if the platform allows it, the hash code could be computed without bounds checking if (bytes.hasArray()) { return onHeapHashCode(bytes.array(), bytes.arrayOffset() + offset, length); } else if (bytes.hasMemoryAddress()) { return offHeapHashCode(bytes.memoryAddress(), offset, length); } } return byteBufHashCode(bytes, offset, length); }
From source file:org.apache.activemq.artemis.utils.ByteUtilTest.java
License:Apache License
@Test public void shouldZeroesDirectByteBuffer() { final byte one = (byte) 1; final int capacity = 64; final int bytes = 32; final int offset = 1; final ByteBuffer buffer = ByteBuffer.allocateDirect(capacity); try {/*ww w.j av a2s .com*/ fill(buffer, 0, capacity, one); shouldZeroesByteBuffer(buffer, offset, bytes); } finally { if (PlatformDependent.hasUnsafe()) { PlatformDependent.freeDirectBuffer(buffer); } } }
From source file:org.apache.activemq.artemis.utils.ByteUtilTest.java
License:Apache License
@Test public void shouldZeroesLimitedDirectByteBuffer() { final byte one = (byte) 1; final int capacity = 64; final int bytes = 32; final int offset = 1; final ByteBuffer buffer = ByteBuffer.allocateDirect(capacity); try {/*from w w w . j a v a2 s. com*/ fill(buffer, 0, capacity, one); buffer.limit(0); shouldZeroesByteBuffer(buffer, offset, bytes); } finally { if (PlatformDependent.hasUnsafe()) { PlatformDependent.freeDirectBuffer(buffer); } } }
From source file:org.apache.activemq.artemis.utils.UTF8Util.java
License:Apache License
public static void saveUTF(final ByteBuf out, final String str) { if (str.length() > 0xffff) { throw ActiveMQUtilBundle.BUNDLE.stringTooLong(str.length()); }//ww w .j a v a2 s . c o m final int len = UTF8Util.calculateUTFSize(str); if (len > 0xffff) { throw ActiveMQUtilBundle.BUNDLE.stringTooLong(len); } out.writeShort((short) len); final int stringLength = str.length(); if (UTF8Util.isTrace) { // This message is too verbose for debug, that's why we are using trace here ActiveMQUtilLogger.LOGGER.trace("Saving string with utfSize=" + len + " stringSize=" + stringLength); } if (out.hasArray()) { out.ensureWritable(len); final byte[] bytes = out.array(); final int writerIndex = out.writerIndex(); final int index = out.arrayOffset() + writerIndex; if (PlatformDependent.hasUnsafe()) { unsafeOnHeapWriteUTF(str, bytes, index, stringLength); } else { writeUTF(str, bytes, index, stringLength); } out.writerIndex(writerIndex + len); } else { if (PlatformDependent.hasUnsafe() && out.hasMemoryAddress()) { out.ensureWritable(len); final long addressBytes = out.memoryAddress(); final int writerIndex = out.writerIndex(); unsafeOffHeapWriteUTF(str, addressBytes, writerIndex, stringLength); out.writerIndex(writerIndex + len); } else { final StringUtilBuffer buffer = UTF8Util.getThreadLocalBuffer(); final byte[] bytes = buffer.borrowByteBuffer(len); writeUTF(str, bytes, 0, stringLength); out.writeBytes(bytes, 0, len); } } }
From source file:org.apache.activemq.artemis.utils.UTF8Util.java
License:Apache License
public static String readUTF(final ActiveMQBuffer input) { StringUtilBuffer buffer = UTF8Util.getThreadLocalBuffer(); final int size = input.readUnsignedShort(); if (UTF8Util.isTrace) { // This message is too verbose for debug, that's why we are using trace here ActiveMQUtilLogger.LOGGER.trace("Reading string with utfSize=" + size); }/*from ww w. j a v a 2 s . c om*/ if (PlatformDependent.hasUnsafe() && input.byteBuf() != null && input.byteBuf().hasMemoryAddress()) { final ByteBuf byteBuf = input.byteBuf(); final long addressBytes = byteBuf.memoryAddress(); final int index = byteBuf.readerIndex(); byteBuf.skipBytes(size); final char[] chars = buffer.borrowCharBuffer(size); return unsafeOffHeapReadUTF(addressBytes, index, chars, size); } final byte[] bytes; final int index; if (input.byteBuf() != null && input.byteBuf().hasArray()) { final ByteBuf byteBuf = input.byteBuf(); bytes = byteBuf.array(); index = byteBuf.arrayOffset() + byteBuf.readerIndex(); byteBuf.skipBytes(size); } else { bytes = buffer.borrowByteBuffer(size); index = 0; input.readBytes(bytes, 0, size); } final char[] chars = buffer.borrowCharBuffer(size); if (PlatformDependent.hasUnsafe()) { return unsafeOnHeapReadUTF(bytes, index, chars, size); } else { return readUTF(bytes, index, chars, size); } }
From source file:org.apache.flink.runtime.io.network.netty.NettyBufferPool.java
License:Apache License
/** * Creates Netty's buffer pool with the specified number of direct arenas. * * @param numberOfArenas Number of arenas (recommended: 2 * number of task * slots)/*from www . j a va 2s. com*/ */ NettyBufferPool(int numberOfArenas) { checkArgument(numberOfArenas >= 1, "Number of arenas"); this.numberOfArenas = numberOfArenas; if (!PlatformDependent.hasUnsafe()) { LOG.warn("Using direct buffers, but sun.misc.Unsafe not available."); } // We strictly prefer direct buffers and disallow heap allocations. boolean preferDirect = true; // Arenas allocate chunks of pageSize << maxOrder bytes. With these // defaults, this results in chunks of 16 MB. int pageSize = 8192; int maxOrder = 11; this.chunkSize = pageSize << maxOrder; // Number of direct arenas. Each arena allocates a chunk of 16 MB, i.e. // we allocate numDirectArenas * 16 MB of direct memory. This can grow // to multiple chunks per arena during runtime, but this should only // happen with a large amount of connections per task manager. We // control the memory allocations with low/high watermarks when writing // to the TCP channels. Chunks are allocated lazily. int numDirectArenas = numberOfArenas; // No heap arenas, please. int numHeapArenas = 0; this.alloc = new PooledByteBufAllocator(preferDirect, numHeapArenas, numDirectArenas, pageSize, maxOrder); Object[] allocDirectArenas = null; try { Field directArenasField = alloc.getClass().getDeclaredField("directArenas"); directArenasField.setAccessible(true); allocDirectArenas = (Object[]) directArenasField.get(alloc); } catch (Exception ignored) { LOG.warn("Memory statistics not available"); } finally { this.directArenas = allocDirectArenas; } }
From source file:org.apache.tajo.util.NumberUtil.java
License:Apache License
/** * Parses the byte array argument as if it was a double value and returns the * result. Throws NumberFormatException if the byte buffer does not represent a * double value.// w ww.j ava2s. co m * * @return double, the value represented by the argument * @throws NumberFormatException if the argument could not be parsed as a double */ public static double parseDouble(ByteBuf bytes, int start, int length) { if (!PlatformDependent.hasUnsafe()) { return parseDouble(bytes.array(), start, length); } if (bytes == null) { throw new NumberFormatException("String is null"); } if (length == 0 || bytes.writerIndex() < start + length) { throw new NumberFormatException("Empty string or Invalid buffer!"); } long memoryAddress = bytes.memoryAddress(); /* * Strip off leading blanks */ int offset = start; int end = start + length; while (offset < end && PlatformDependent.getByte(memoryAddress + offset) == ' ') { offset++; } if (offset == end) { throw new NumberFormatException("blank byte array!"); } /* * check for a sign. */ boolean sign = false; if (PlatformDependent.getByte(memoryAddress + offset) == '-') { sign = true; offset++; } else if (PlatformDependent.getByte(memoryAddress + offset) == '+') { offset++; } if (offset == end) { throw new NumberFormatException("the byte array only has a sign!"); } /* * Count the number of digits in the mantissa (including the decimal * point), and also locate the decimal point. */ int mantSize = 0; /* Number of digits in mantissa. */ int decicalOffset = -1; /* Number of mantissa digits BEFORE decimal point. */ for (; offset < end; offset++) { if (!isDigit(PlatformDependent.getByte(memoryAddress + offset))) { if ((PlatformDependent.getByte(memoryAddress + offset) != '.') || (decicalOffset >= 0)) { break; } decicalOffset = mantSize; } mantSize++; } int exponentOffset = offset; /* Temporarily holds location of exponent in bytes. */ /* * Now suck up the digits in the mantissa. Use two integers to * collect 9 digits each (this is faster than using floating-point). * If the mantissa has more than 18 digits, ignore the extras, since * they can't affect the value anyway. */ offset -= mantSize; if (decicalOffset < 0) { decicalOffset = mantSize; } else { mantSize -= 1; /* One of the digits was the decimal point. */ } int fracExponent; /* Exponent that derives from the fractional * part. Under normal circumstatnces, it is * the negative of the number of digits in F. * However, if I is very long, the last digits * of I get dropped (otherwise a long I with a * large negative exponent could cause an * unnecessary overflow on I alone). In this * case, fracExp is incremented one for each * dropped digit. */ if (mantSize > 18) { fracExponent = decicalOffset - 18; mantSize = 18; } else { fracExponent = decicalOffset - mantSize; } if (mantSize == 0) { return 0.0; } int frac1 = 0; for (; mantSize > 9; mantSize--) { int b = PlatformDependent.getByte(memoryAddress + offset); offset++; if (b == '.') { b = PlatformDependent.getByte(memoryAddress + offset); offset++; } frac1 = 10 * frac1 + (b - '0'); } int frac2 = 0; for (; mantSize > 0; mantSize--) { int b = PlatformDependent.getByte(memoryAddress + offset); offset++; if (b == '.') { b = PlatformDependent.getByte(memoryAddress + offset); offset++; } frac2 = 10 * frac2 + (b - '0'); } double fraction = (1.0e9 * frac1) + frac2; /* * Skim off the exponent. */ int exponent = 0; /* Exponent read from "EX" field. */ offset = exponentOffset; boolean expSign = false; if (offset < end) { if ((PlatformDependent.getByte(memoryAddress + offset) != 'E') && (PlatformDependent.getByte(memoryAddress + offset) != 'e')) { throw new NumberFormatException(bytes.toString(start, length, Charset.defaultCharset())); } // (bytes[offset] == 'E') || (bytes[offset] == 'e') offset++; if (PlatformDependent.getByte(memoryAddress + offset) == '-') { expSign = true; offset++; } else if (PlatformDependent.getByte(memoryAddress + offset) == '+') { offset++; } for (; offset < end; offset++) { if (isDigit(PlatformDependent.getByte(memoryAddress + offset))) { exponent = exponent * 10 + (PlatformDependent.getByte(memoryAddress + offset) - '0'); } else { throw new NumberFormatException(bytes.toString(start, length, Charset.defaultCharset())); } } } exponent = expSign ? (fracExponent - exponent) : (fracExponent + exponent); /* * Generate a floating-point number that represents the exponent. * Do this by processing the exponent one bit at a time to combine * many powers of 2 of 10. Then combine the exponent with the * fraction. */ if (exponent < 0) { expSign = true; exponent = -exponent; } else { expSign = false; } if (exponent > maxExponent) { throw new NumberFormatException(bytes.toString(start, length, Charset.defaultCharset())); } double dblExp = 1.0; for (int i = 0; exponent != 0; exponent >>= 1, i++) { if ((exponent & 01) == 01) { dblExp *= powersOf10[i]; } } fraction = (expSign) ? (fraction / dblExp) : (fraction * dblExp); return sign ? (-fraction) : fraction; }
From source file:org.apache.tajo.util.NumberUtil.java
License:Apache License
/** * Parses the byte buffer argument as if it was an int value and returns the * result. Throws NumberFormatException if the byte array does not represent an * int quantity. The second argument specifies the radix to use when parsing * the value.//w w w . j a v a 2 s. c o m * * @param radix the base to use for conversion. * @return the value represented by the argument * @throws NumberFormatException if the argument could not be parsed as an int quantity. */ public static int parseInt(ByteBuf bytes, int start, int length, int radix) { if (!PlatformDependent.hasUnsafe()) { return parseInt(bytes.array(), start, length); } if (bytes == null) { throw new NumberFormatException("String is null"); } if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) { throw new NumberFormatException("Invalid radix: " + radix); } if (length == 0 || bytes.writerIndex() < start + length) { throw new NumberFormatException("Empty string or Invalid buffer!"); } long memoryAddress = bytes.memoryAddress(); int offset = start; boolean negative = PlatformDependent.getByte(memoryAddress + start) == '-'; if (negative || PlatformDependent.getByte(memoryAddress + start) == '+') { offset++; if (length == 1) { throw new NumberFormatException(bytes.toString(start, length, Charset.defaultCharset())); } } return parseIntInternal(bytes, memoryAddress, start, length, offset, radix, negative); }
From source file:org.apache.tajo.util.NumberUtil.java
License:Apache License
/** * Parses the byte buffer argument as if it was an long value and returns the * result. Throws NumberFormatException if the string does not represent an * long quantity. The second argument specifies the radix to use when parsing * the value.//from w w w . j a v a 2 s . co m * * @param bytes the string byte buffer * @param start * @param length a UTF-8 encoded string representation of a long quantity. * @param radix the base to use for conversion. * @return the value represented by the argument * @throws NumberFormatException if the argument could not be parsed as an long quantity. */ public static long parseLong(ByteBuf bytes, int start, int length, int radix) { if (!PlatformDependent.hasUnsafe()) { return parseInt(bytes.array(), start, length); } if (bytes == null) { throw new NumberFormatException("String is null"); } if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) { throw new NumberFormatException("Invalid radix: " + radix); } if (length == 0 || bytes.writerIndex() < start + length) { throw new NumberFormatException("Empty string or Invalid buffer!"); } long memoryAddress = bytes.memoryAddress(); int offset = start; boolean negative = PlatformDependent.getByte(memoryAddress + start) == '-'; if (negative || PlatformDependent.getByte(memoryAddress + start) == '+') { offset++; if (length == 1) { throw new NumberFormatException(bytes.toString(start, length, Charset.defaultCharset())); } } return parseLongInternal(bytes, memoryAddress, start, length, offset, radix, negative); }