IovArray should support when there is no unsafe present (#10814)
Motivation: In some enviroments sun.misc.Unsafe is not present. We should support these as well. Modifications: Fallback to JNI if we can't directly access the memoryAddress of the buffer. Result: Fixes https://github.com/netty/netty/issues/10813
This commit is contained in:
parent
2ff8973f69
commit
ba83a8840f
@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright 2020 The Netty Project
|
||||
*
|
||||
* The Netty Project licenses this file to you under the Apache License,
|
||||
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at:
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package io.netty.channel.epoll;
|
||||
|
||||
import io.netty.channel.unix.tests.IovArrayTest;
|
||||
import org.junit.Assume;
|
||||
import org.junit.BeforeClass;
|
||||
|
||||
public class EpollKQueueIovArrayTest extends IovArrayTest {
|
||||
|
||||
@BeforeClass
|
||||
public static void loadNative() {
|
||||
Assume.assumeTrue(Epoll.isAvailable());
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright 2020 The Netty Project
|
||||
*
|
||||
* The Netty Project licenses this file to you under the Apache License,
|
||||
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at:
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package io.netty.channel.kqueue;
|
||||
|
||||
import io.netty.channel.unix.tests.IovArrayTest;
|
||||
import org.junit.Assume;
|
||||
import org.junit.BeforeClass;
|
||||
|
||||
public class KQueueIovArrayTest extends IovArrayTest {
|
||||
|
||||
@BeforeClass
|
||||
public static void loadNative() {
|
||||
Assume.assumeTrue(KQueue.isAvailable());
|
||||
}
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright 2020 The Netty Project
|
||||
*
|
||||
* The Netty Project licenses this file to you under the Apache License,
|
||||
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at:
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package io.netty.channel.unix.tests;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import io.netty.buffer.UnpooledByteBufAllocator;
|
||||
import io.netty.buffer.UnpooledDirectByteBuf;
|
||||
import io.netty.channel.unix.IovArray;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public abstract class IovArrayTest {
|
||||
|
||||
@Test
|
||||
public void testNotFailsWihtoutMemoryAddress() {
|
||||
ByteBuf buffer = new NoMemoryAddressByteBuf(128);
|
||||
IovArray array = new IovArray(buffer);
|
||||
|
||||
ByteBuf buf = Unpooled.directBuffer().writeZero(8);
|
||||
ByteBuf buf2 = new NoMemoryAddressByteBuf(8).writeZero(8);
|
||||
assertTrue(array.add(buf, 0, buf.readableBytes()));
|
||||
assertTrue(array.add(buf, 0, buf2.readableBytes()));
|
||||
assertEquals(2, array.count());
|
||||
assertEquals(16, array.size());
|
||||
assertTrue(buf.release());
|
||||
assertTrue(buf2.release());
|
||||
array.release();
|
||||
assertEquals(0, buffer.refCnt());
|
||||
}
|
||||
|
||||
private static final class NoMemoryAddressByteBuf extends UnpooledDirectByteBuf {
|
||||
|
||||
NoMemoryAddressByteBuf(int capacity) {
|
||||
super(UnpooledByteBufAllocator.DEFAULT, capacity, Integer.MAX_VALUE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasMemoryAddress() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long memoryAddress() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
}
|
@ -62,6 +62,7 @@ public final class IovArray implements MessageProcessor {
|
||||
*/
|
||||
private static final int MAX_CAPACITY = IOV_MAX * IOV_SIZE;
|
||||
|
||||
private final long memoryAddress;
|
||||
private final ByteBuf memory;
|
||||
private int count;
|
||||
private long size;
|
||||
@ -77,6 +78,12 @@ public final class IovArray implements MessageProcessor {
|
||||
assert memory.readerIndex() == 0;
|
||||
this.memory = PlatformDependent.hasUnsafe() ? memory : memory.order(
|
||||
PlatformDependent.BIG_ENDIAN_NATIVE_ORDER ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN);
|
||||
if (memory.hasMemoryAddress()) {
|
||||
memoryAddress = memory.memoryAddress();
|
||||
} else {
|
||||
// Fallback to using JNI as we were not be able to access the address otherwise.
|
||||
memoryAddress = Buffer.memoryAddress(memory.internalNioBuffer(0, memory.capacity()));
|
||||
}
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
@ -97,7 +104,6 @@ public final class IovArray implements MessageProcessor {
|
||||
// No more room!
|
||||
return false;
|
||||
}
|
||||
long memoryAddress = memory.memoryAddress();
|
||||
if (buf.nioBufferCount() == 1) {
|
||||
if (len == 0) {
|
||||
return true;
|
||||
|
Loading…
Reference in New Issue
Block a user