use of sun.nio.ch.DirectBuffer in project mapdb by jankotek.
the class ByteBufferVol method unmap.
/**
* Hack to unmap MappedByteBuffer.
* Unmap is necessary on Windows, otherwise file is locked until JVM exits or BB is GCed.
* There is no public JVM API to unmap buffer, so this tries to use SUN proprietary API for unmap.
* Any error is silently ignored (for example SUN API does not exist on Android).
*/
protected static boolean unmap(MappedByteBuffer b) {
if (!unmapHackSupported) {
return false;
}
if (!(b instanceof DirectBuffer))
return false;
// need to dispose old direct buffer, see bug
// http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4724038
DirectBuffer bb = (DirectBuffer) b;
Cleaner c = bb.cleaner();
if (c != null) {
c.clean();
return true;
}
Object attachment = bb.attachment();
return attachment != null && attachment instanceof DirectBuffer && attachment != b && unmap((MappedByteBuffer) attachment);
}
use of sun.nio.ch.DirectBuffer in project Mycat-Server by MyCATApache.
the class TestDirectByteBufferPool method testAllocateSign.
@Test
public void testAllocateSign() {
int size = 256;
int pageSize = size * 4;
int allocTimes = 9;
DirectByteBufferPool pool = new DirectByteBufferPool(pageSize, (short) 256, (short) 2, 0);
long start = System.currentTimeMillis();
ByteBuffer byteBuffer = null;
List<ByteBuffer> buffs = new ArrayList<ByteBuffer>();
int i = 0;
for (; i < allocTimes; i++) {
byteBuffer = pool.allocate(size);
if (byteBuffer == null || !(byteBuffer instanceof DirectBuffer)) {
break;
}
buffs.add(byteBuffer);
}
for (ByteBuffer buff : buffs) {
pool.recycle(buff);
}
Assert.assertEquals("Should out of memory when i = " + 8, i, 8);
}
use of sun.nio.ch.DirectBuffer in project Mycat-Server by MyCATApache.
the class TestByteBufferArena method testAllocateWithDifferentAddress.
@Test
public void testAllocateWithDifferentAddress() {
int size = 256;
int pageSize = size * 4;
int allocTimes = 8;
ByteBufferArena byteBufferArena = new ByteBufferArena(256 * 4, 256, 2, 8);
Map<Long, ByteBuffer> buffs = new HashMap<Long, ByteBuffer>(8);
ByteBuffer byteBuffer = null;
DirectBuffer directBuffer = null;
ByteBuffer temp = null;
long address;
boolean failure = false;
for (int i = 0; i < allocTimes; i++) {
byteBuffer = byteBufferArena.allocate(size);
if (byteBuffer == null) {
Assert.fail("Should have enough memory");
}
directBuffer = (DirectBuffer) byteBuffer;
address = directBuffer.address();
System.out.println(address);
temp = buffs.get(address);
buffs.put(address, byteBuffer);
if (null != temp) {
failure = true;
break;
}
}
for (ByteBuffer buff : buffs.values()) {
byteBufferArena.recycle(buff);
}
if (failure == true) {
Assert.fail("Allocate with same address");
}
}
use of sun.nio.ch.DirectBuffer in project jdk8u_jdk by JetBrains.
the class SctpChannelImpl method receive.
private int receive(int fd, ByteBuffer dst, ResultContainer resultContainer, boolean peek) throws IOException {
int pos = dst.position();
int lim = dst.limit();
assert (pos <= lim);
int rem = (pos <= lim ? lim - pos : 0);
if (dst instanceof DirectBuffer && rem > 0)
return receiveIntoNativeBuffer(fd, resultContainer, dst, rem, pos, peek);
/* Substitute a native buffer */
int newSize = Math.max(rem, 1);
ByteBuffer bb = Util.getTemporaryDirectBuffer(newSize);
try {
int n = receiveIntoNativeBuffer(fd, resultContainer, bb, newSize, 0, peek);
bb.flip();
if (n > 0 && rem > 0)
dst.put(bb);
return n;
} finally {
Util.releaseTemporaryDirectBuffer(bb);
}
}
use of sun.nio.ch.DirectBuffer in project jdk8u_jdk by JetBrains.
the class SctpChannelImpl method sendFromNativeBuffer.
private int sendFromNativeBuffer(int fd, ByteBuffer bb, SocketAddress target, int streamNumber, boolean unordered, int ppid) throws IOException {
// no preferred address
InetAddress addr = null;
int port = 0;
if (target != null) {
InetSocketAddress isa = Net.checkAddress(target);
addr = isa.getAddress();
port = isa.getPort();
}
int pos = bb.position();
int lim = bb.limit();
assert (pos <= lim);
int rem = (pos <= lim ? lim - pos : 0);
int written = send0(fd, ((DirectBuffer) bb).address() + pos, rem, addr, port, -1, /*121*/
streamNumber, unordered, ppid);
if (written > 0)
bb.position(pos + written);
return written;
}
Aggregations