< prev index next >

src/java.base/share/classes/sun/nio/fs/NativeBuffers.java

Print this page

        

*** 23,41 **** * questions. */ package sun.nio.fs; import jdk.internal.misc.TerminatingThreadLocal; import jdk.internal.misc.Unsafe; /** * Factory for native buffers. */ class NativeBuffers { ! private NativeBuffers() { } private static final Unsafe unsafe = Unsafe.getUnsafe(); private static final int TEMP_BUF_POOL_SIZE = 3; private static ThreadLocal<NativeBuffer[]> threadLocal = new TerminatingThreadLocal<>() { --- 23,43 ---- * questions. */ package sun.nio.fs; + import jdk.internal.access.JavaLangAccess; + import jdk.internal.access.SharedSecrets; import jdk.internal.misc.TerminatingThreadLocal; import jdk.internal.misc.Unsafe; /** * Factory for native buffers. */ class NativeBuffers { ! private static JavaLangAccess JLA = SharedSecrets.getJavaLangAccess(); private static final Unsafe unsafe = Unsafe.getUnsafe(); private static final int TEMP_BUF_POOL_SIZE = 3; private static ThreadLocal<NativeBuffer[]> threadLocal = new TerminatingThreadLocal<>() {
*** 52,61 **** --- 54,65 ---- } } } }; + private NativeBuffers() { } + /** * Allocates a native buffer, of at least the given size, from the heap. */ static NativeBuffer allocNativeBuffer(int size) { // Make a new one of at least 2k
*** 67,77 **** * Returns a native buffer, of at least the given size, from the thread * local cache. */ static NativeBuffer getNativeBufferFromCache(int size) { // return from cache if possible ! NativeBuffer[] buffers = threadLocal.get(); if (buffers != null) { for (int i=0; i<TEMP_BUF_POOL_SIZE; i++) { NativeBuffer buffer = buffers[i]; if (buffer != null && buffer.size() >= size) { buffers[i] = null; --- 71,81 ---- * Returns a native buffer, of at least the given size, from the thread * local cache. */ static NativeBuffer getNativeBufferFromCache(int size) { // return from cache if possible ! NativeBuffer[] buffers = JLA.getCarrierThreadLocal(threadLocal); if (buffers != null) { for (int i=0; i<TEMP_BUF_POOL_SIZE; i++) { NativeBuffer buffer = buffers[i]; if (buffer != null && buffer.size() >= size) { buffers[i] = null;
*** 101,111 **** * Releases the given buffer. If there is space in the thread local cache * then the buffer goes into the cache; otherwise the memory is deallocated. */ static void releaseNativeBuffer(NativeBuffer buffer) { // create cache if it doesn't exist ! NativeBuffer[] buffers = threadLocal.get(); if (buffers == null) { buffers = new NativeBuffer[TEMP_BUF_POOL_SIZE]; buffers[0] = buffer; threadLocal.set(buffers); return; --- 105,115 ---- * Releases the given buffer. If there is space in the thread local cache * then the buffer goes into the cache; otherwise the memory is deallocated. */ static void releaseNativeBuffer(NativeBuffer buffer) { // create cache if it doesn't exist ! NativeBuffer[] buffers = JLA.getCarrierThreadLocal(threadLocal); if (buffers == null) { buffers = new NativeBuffer[TEMP_BUF_POOL_SIZE]; buffers[0] = buffer; threadLocal.set(buffers); return;
< prev index next >