Java Code Examples for io.netty.util.internal.PlatformDependent#hasUnsafe()
The following examples show how to use
io.netty.util.internal.PlatformDependent#hasUnsafe() .
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: ByteUtilTest.java From activemq-artemis with Apache License 2.0 | 6 votes |
@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 { fill(buffer, 0, capacity, one); shouldZeroesByteBuffer(buffer, offset, bytes); } finally { if (PlatformDependent.hasUnsafe()) { PlatformDependent.freeDirectBuffer(buffer); } } }
Example 2
Source File: PooledByteBufAllocator.java From netty-4.1.22 with Apache License 2.0 | 6 votes |
@Override protected ByteBuf newDirectBuffer(int initialCapacity, int maxCapacity) { PoolThreadCache cache = threadCache.get(); PoolArena<ByteBuffer> directArena = cache.directArena; final ByteBuf buf; if (directArena != null) { buf = directArena.allocate(cache, initialCapacity, maxCapacity); } else { buf = PlatformDependent.hasUnsafe() ? UnsafeByteBufUtil.newUnsafeDirectByteBuf(this, initialCapacity, maxCapacity) : new UnpooledDirectByteBuf(this, initialCapacity, maxCapacity); } return toLeakAwareBuffer(buf); }
Example 3
Source File: ByteUtil.java From activemq-artemis with Apache License 2.0 | 6 votes |
private static void uncheckedZeros(final ByteBuffer buffer, int offset, int bytes) { if (buffer.isReadOnly()) { throw new ReadOnlyBufferException(); } final byte zero = (byte) 0; if (buffer.isDirect() && PlatformDependent.hasUnsafe()) { PlatformDependent.setMemory(PlatformDependent.directBufferAddress(buffer) + offset, bytes, zero); } else if (buffer.hasArray()) { //SIMD OPTIMIZATION final int arrayOffset = buffer.arrayOffset(); final int start = arrayOffset + offset; Arrays.fill(buffer.array(), start, start + bytes, zero); } else { //slow path for (int i = 0; i < bytes; i++) { buffer.put(i + offset, zero); } } }
Example 4
Source File: ByteUtil.java From activemq-artemis with Apache License 2.0 | 6 votes |
public static boolean equals(final byte[] left, final byte[] right, final int rightOffset, final int rightLength) { if (left == right) return true; if (left == null || right == null) return false; if (left.length != rightLength) return false; if (PlatformDependent.isUnaligned() && PlatformDependent.hasUnsafe()) { return equalsUnsafe(left, right, rightOffset, rightLength); } else { return equalsSafe(left, right, rightOffset, rightLength); } }
Example 5
Source File: PooledByteBufAllocator.java From netty4.0.27Learn with Apache License 2.0 | 6 votes |
@Override protected ByteBuf newDirectBuffer(int initialCapacity, int maxCapacity) { PoolThreadCache cache = threadCache.get(); PoolArena<ByteBuffer> directArena = cache.directArena; ByteBuf buf; if (directArena != null) { buf = directArena.allocate(cache, initialCapacity, maxCapacity); } else { if (PlatformDependent.hasUnsafe()) { buf = new UnpooledUnsafeDirectByteBuf(this, initialCapacity, maxCapacity); } else { buf = new UnpooledDirectByteBuf(this, initialCapacity, maxCapacity); } } return toLeakAwareBuffer(buf); }
Example 6
Source File: ByteUtil.java From activemq-artemis with Apache License 2.0 | 6 votes |
/** * Returns {@code true} if the {@link SimpleString} encoded content into {@code bytes} is equals to {@code s}, * {@code false} otherwise. * <p> * It assumes that the {@code bytes} content is read using {@link SimpleString#readSimpleString(ByteBuf, int)} ie starting right after the * length field. */ public static boolean equals(final byte[] bytes, final ByteBuf byteBuf, final int offset, final int length) { if (bytes.length != length) return false; if (PlatformDependent.isUnaligned() && PlatformDependent.hasUnsafe()) { if ((offset + length) > byteBuf.writerIndex()) { throw new IndexOutOfBoundsException(); } if (byteBuf.hasArray()) { return equals(bytes, byteBuf.array(), byteBuf.arrayOffset() + offset, length); } else if (byteBuf.hasMemoryAddress()) { return equalsOffHeap(bytes, byteBuf.memoryAddress(), offset, length); } } return equalsOnHeap(bytes, byteBuf, offset, length); }
Example 7
Source File: AbstractEpollStreamChannel.java From netty-4.1.22 with Apache License 2.0 | 6 votes |
/** * Attempt to write multiple {@link ByteBuf} objects. * @param in the collection which contains objects to write. * @return The value that should be decremented from the write quantum which starts at * {@link ChannelConfig#getWriteSpinCount()}. The typical use cases are as follows: * <ul> * <li>0 - if no write was attempted. This is appropriate if an empty {@link ByteBuf} (or other empty content) * is encountered</li> * <li>1 - if a single call to write data was made to the OS</li> * <li>{@link ChannelUtils#WRITE_STATUS_SNDBUF_FULL} - if an attempt to write data was made to the OS, but * no data was accepted</li> * </ul> * @throws Exception If an I/O error occurs. */ private int doWriteMultiple(ChannelOutboundBuffer in) throws Exception { final long maxBytesPerGatheringWrite = config().getMaxBytesPerGatheringWrite(); if (PlatformDependent.hasUnsafe()) { IovArray array = ((EpollEventLoop) eventLoop()).cleanArray(); array.maxBytes(maxBytesPerGatheringWrite); in.forEachFlushedMessage(array); if (array.count() >= 1) { // TODO: Handle the case where cnt == 1 specially. return writeBytesMultiple(in, array); } } else { ByteBuffer[] buffers = in.nioBuffers(); int cnt = in.nioBufferCount(); if (cnt >= 1) { // TODO: Handle the case where cnt == 1 specially. return writeBytesMultiple(in, buffers, cnt, in.nioBufferSize(), maxBytesPerGatheringWrite); } } // cnt == 0, which means the outbound buffer contained empty buffers only. in.removeBytes(0); return 0; }
Example 8
Source File: AbstractKQueueStreamChannel.java From netty-4.1.22 with Apache License 2.0 | 6 votes |
/** * Attempt to write multiple {@link ByteBuf} objects. * @param in the collection which contains objects to write. * @return The value that should be decremented from the write quantum which starts at * {@link ChannelConfig#getWriteSpinCount()}. The typical use cases are as follows: * <ul> * <li>0 - if no write was attempted. This is appropriate if an empty {@link ByteBuf} (or other empty content) * is encountered</li> * <li>1 - if a single call to write data was made to the OS</li> * <li>{@link ChannelUtils#WRITE_STATUS_SNDBUF_FULL} - if an attempt to write data was made to the OS, but no * data was accepted</li> * </ul> * @throws Exception If an I/O error occurs. */ private int doWriteMultiple(ChannelOutboundBuffer in) throws Exception { final long maxBytesPerGatheringWrite = config().getMaxBytesPerGatheringWrite(); if (PlatformDependent.hasUnsafe()) { IovArray array = ((KQueueEventLoop) eventLoop()).cleanArray(); array.maxBytes(maxBytesPerGatheringWrite); in.forEachFlushedMessage(array); if (array.count() >= 1) { // TODO: Handle the case where cnt == 1 specially. return writeBytesMultiple(in, array); } } else { ByteBuffer[] buffers = in.nioBuffers(); int cnt = in.nioBufferCount(); if (cnt >= 1) { // TODO: Handle the case where cnt == 1 specially. return writeBytesMultiple(in, buffers, cnt, in.nioBufferSize(), maxBytesPerGatheringWrite); } } // cnt == 0, which means the outbound buffer contained empty buffers only. in.removeBytes(0); return 0; }
Example 9
Source File: ByteUtilTest.java From activemq-artemis with Apache License 2.0 | 6 votes |
@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 { fill(buffer, 0, capacity, one); buffer.limit(0); shouldZeroesByteBuffer(buffer, offset, bytes); } finally { if (PlatformDependent.hasUnsafe()) { PlatformDependent.freeDirectBuffer(buffer); } } }
Example 10
Source File: AbstractByteBufPool.java From activemq-artemis with Apache License 2.0 | 5 votes |
/** * Batch hash code implementation that works at its best if {@code bytes} * contains a {@link org.apache.activemq.artemis.api.core.SimpleString} encoded. */ 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); }
Example 11
Source File: UnitializedArrayBenchmark.java From netty-4.1.22 with Apache License 2.0 | 5 votes |
@Setup(Level.Trial) public void setupTrial() { if (PlatformDependent.javaVersion() < 9) { throw new IllegalStateException("Needs Java9"); } if (!PlatformDependent.hasUnsafe()) { throw new IllegalStateException("Needs Unsafe"); } }
Example 12
Source File: MyCatMemoryAllocator.java From Mycat2 with GNU General Public License v3.0 | 5 votes |
/** * numberOfArenas 设置为处理器cores*2 * @param numberOfArenas */ public MyCatMemoryAllocator(int numberOfArenas){ this.numberOfArenas = numberOfArenas; if (!PlatformDependent.hasUnsafe()) { LOGGER.warn("Using direct memory, but sun.misc.Unsafe not available."); } boolean preferDirect = true; this.pageSize = 8192*2; int maxOrder = 11; this.chunkSize = pageSize << maxOrder; int numDirectArenas = numberOfArenas; int numHeapArenas = 0; /** for 4.1.x*/ this.alloc = new PooledByteBufAllocator( preferDirect, numHeapArenas, numDirectArenas, pageSize, maxOrder, 512, 256, 64, true); /**for 5.0.x this.alloc = new PooledByteBufAllocator(preferDirect);**/ }
Example 13
Source File: XnioByteBufAllocator.java From netty-xnio-transport with Apache License 2.0 | 5 votes |
@Override protected ByteBuf newDirectBuffer(int initialCapacity, int maxCapacity) { if (PlatformDependent.hasUnsafe()) { return new XnioUnsafeDirectByteBuf(this, initialCapacity, maxCapacity); } return new XnioDirectByteBuf(this, initialCapacity, maxCapacity); }
Example 14
Source File: UnpooledByteBufAllocator.java From netty-4.1.22 with Apache License 2.0 | 5 votes |
@Override protected ByteBuf newDirectBuffer(int initialCapacity, int maxCapacity) { final ByteBuf buf; if (PlatformDependent.hasUnsafe()) { buf = noCleaner ? new InstrumentedUnpooledUnsafeNoCleanerDirectByteBuf(this, initialCapacity, maxCapacity) : new InstrumentedUnpooledUnsafeDirectByteBuf(this, initialCapacity, maxCapacity); } else { buf = new InstrumentedUnpooledDirectByteBuf(this, initialCapacity, maxCapacity); } return disableLeakDetector ? buf : toLeakAwareBuffer(buf); }
Example 15
Source File: AbstractByteBufAllocator.java From netty4.0.27Learn with Apache License 2.0 | 5 votes |
@Override public ByteBuf ioBuffer(int initialCapacity) { if (PlatformDependent.hasUnsafe()) { return directBuffer(initialCapacity); } return heapBuffer(initialCapacity); }
Example 16
Source File: AbstractByteBufAllocator.java From netty4.0.27Learn with Apache License 2.0 | 5 votes |
@Override public ByteBuf ioBuffer() { if (PlatformDependent.hasUnsafe()) { return directBuffer(DEFAULT_INITIAL_CAPACITY); } return heapBuffer(DEFAULT_INITIAL_CAPACITY); }
Example 17
Source File: Unpooled.java From netty-4.1.22 with Apache License 2.0 | 5 votes |
/** * Creates a new buffer which wraps the specified NIO buffer's current * slice. A modification on the specified buffer's content will be * visible to the returned buffer.创建一个新的缓冲区,用于包装指定的NIO缓冲区的当前片。对指定缓冲区内容的修改将对返回的缓冲区可见。 */ public static ByteBuf wrappedBuffer(ByteBuffer buffer) { if (!buffer.hasRemaining()) { return EMPTY_BUFFER; } if (!buffer.isDirect() && buffer.hasArray()) { return wrappedBuffer( buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining()).order(buffer.order()); } else if (PlatformDependent.hasUnsafe()) { if (buffer.isReadOnly()) { if (buffer.isDirect()) { return new ReadOnlyUnsafeDirectByteBuf(ALLOC, buffer); } else { return new ReadOnlyByteBufferBuf(ALLOC, buffer); } } else { return new UnpooledUnsafeDirectByteBuf(ALLOC, buffer, buffer.remaining()); } } else { if (buffer.isReadOnly()) { return new ReadOnlyByteBufferBuf(ALLOC, buffer); } else { return new UnpooledDirectByteBuf(ALLOC, buffer, buffer.remaining()); } } }
Example 18
Source File: UnpooledByteBufAllocator.java From netty4.0.27Learn with Apache License 2.0 | 5 votes |
@Override protected ByteBuf newDirectBuffer(int initialCapacity, int maxCapacity) { ByteBuf buf; if (PlatformDependent.hasUnsafe()) { buf = new UnpooledUnsafeDirectByteBuf(this, initialCapacity, maxCapacity); } else { buf = new UnpooledDirectByteBuf(this, initialCapacity, maxCapacity); } return toLeakAwareBuffer(buf); }
Example 19
Source File: AbstractByteBufAllocator.java From netty-4.1.22 with Apache License 2.0 | 5 votes |
@Override public ByteBuf ioBuffer(int initialCapacity) { if (PlatformDependent.hasUnsafe()) { return directBuffer(initialCapacity); } return heapBuffer(initialCapacity); }
Example 20
Source File: Unpooled.java From netty4.0.27Learn with Apache License 2.0 | 5 votes |
/** * Creates a new buffer which wraps the specified NIO buffer's current * slice. A modification on the specified buffer's content will be * visible to the returned buffer. */ public static ByteBuf wrappedBuffer(ByteBuffer buffer) { if (!buffer.hasRemaining()) { return EMPTY_BUFFER; } if (buffer.hasArray()) { return wrappedBuffer( buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining()).order(buffer.order()); } else if (PlatformDependent.hasUnsafe()) { if (buffer.isReadOnly()) { if (buffer.isDirect()) { return new ReadOnlyUnsafeDirectByteBuf(ALLOC, buffer); } else { return new ReadOnlyByteBufferBuf(ALLOC, buffer); } } else { return new UnpooledUnsafeDirectByteBuf(ALLOC, buffer, buffer.remaining()); } } else { if (buffer.isReadOnly()) { return new ReadOnlyByteBufferBuf(ALLOC, buffer); } else { return new UnpooledDirectByteBuf(ALLOC, buffer, buffer.remaining()); } } }