private static Object getViewed(ByteBuffer buffer) { return ((sun.nio.ch.DirectBuffer) buffer).attachment(); // java<7: viewedBuffer(); }
FloatBuffer floatBuffer = ByteBuffer.allocateDirect (...).asFloatBuffer(); ByteBuffer byteBuffer = (ByteBuffer) ((sun.nio.ch.DirectBuffer)floatBuffer).attachment();
public void recycle(ByteBuffer theBuf) { if (!(theBuf instanceof DirectBuffer)) { theBuf.clear(); return; } final long size = theBuf.capacity(); boolean recycled = false; DirectBuffer thisNavBuf = (DirectBuffer) theBuf; int chunkCount = theBuf.capacity() / chunkSize; DirectBuffer parentBuf = (DirectBuffer) thisNavBuf.attachment(); int startChunk = (int) ((thisNavBuf.address() - parentBuf.address()) / this.chunkSize); for (ByteBufferPage allPage : allPages) { if ((recycled = allPage.recycleBuffer((ByteBuffer) parentBuf, startChunk, chunkCount))) { break; } } final long threadId = Thread.currentThread().getId(); if (memoryUsage.containsKey(threadId)) { memoryUsage.put(threadId, memoryUsage.get(threadId) - size); } if (!recycled) { LOGGER.info("warning ,not recycled buffer " + theBuf); } }
private static int getOffset(ByteBuffer slice) { final sun.nio.ch.DirectBuffer _slice = (sun.nio.ch.DirectBuffer) slice; final sun.nio.ch.DirectBuffer parent = (sun.nio.ch.DirectBuffer) _slice.attachment(); // java<7: viewedBuffer(); return (int) (_slice.address() - parent.address()); }
@SuppressWarnings("restriction") @Override public void recycle(ByteBuffer theBuf) { if(theBuf !=null && (!(theBuf instanceof sun.nio.ch.DirectBuffer) )){ theBuf.clear(); return; } final long size = theBuf.capacity(); boolean recycled = false; sun.nio.ch.DirectBuffer thisNavBuf = (sun.nio.ch.DirectBuffer) theBuf; int chunkCount = theBuf.capacity() / chunkSize; sun.nio.ch.DirectBuffer parentBuf = (sun.nio.ch.DirectBuffer) thisNavBuf.attachment(); int startChunk = (int) ((thisNavBuf.address() - parentBuf.address()) / chunkSize); for (int i = 0; i < allPages.length; i++) { if ((recycled = allPages[i].recycleBuffer((ByteBuffer) parentBuf, startChunk,chunkCount) == true)) { break; } } final long threadId = Thread.currentThread().getId(); if (memoryUsage.containsKey(threadId)) { memoryUsage.put(threadId, memoryUsage.get(threadId) - size); } if (recycled == false) { LOGGER.warn("warning , not recycled buffer " + theBuf); } sharedOptsCount++; }
/** * 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); }