Search in sources :

Example 1 with DirectBuffer

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);
}
Also used : DirectBuffer(sun.nio.ch.DirectBuffer) MappedByteBuffer(java.nio.MappedByteBuffer) Cleaner(sun.misc.Cleaner)

Example 2 with DirectBuffer

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);
}
Also used : DirectBuffer(sun.nio.ch.DirectBuffer) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 3 with DirectBuffer

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");
    }
}
Also used : DirectBuffer(sun.nio.ch.DirectBuffer) HashMap(java.util.HashMap) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 4 with DirectBuffer

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);
    }
}
Also used : DirectBuffer(sun.nio.ch.DirectBuffer) ByteBuffer(java.nio.ByteBuffer)

Example 5 with DirectBuffer

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;
}
Also used : DirectBuffer(sun.nio.ch.DirectBuffer) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress)

Aggregations

DirectBuffer (sun.nio.ch.DirectBuffer)14 ByteBuffer (java.nio.ByteBuffer)10 Test (org.junit.Test)4 InetSocketAddress (java.net.InetSocketAddress)3 InetAddress (java.net.InetAddress)2 MappedByteBuffer (java.nio.MappedByteBuffer)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Cleaner (sun.misc.Cleaner)2 RandomAccessFile (java.io.RandomAccessFile)1 SocketAddress (java.net.SocketAddress)1 FileChannel (java.nio.channels.FileChannel)1 Histogram (org.HdrHistogram.Histogram)1