src/java.base/share/classes/sun/nio/fs/NativeBuffers.java
changeset 50719 106dc156ce6b
parent 47216 71c04702a3d5
equal deleted inserted replaced
50715:46492a773912 50719:106dc156ce6b
    23  * questions.
    23  * questions.
    24  */
    24  */
    25 
    25 
    26 package sun.nio.fs;
    26 package sun.nio.fs;
    27 
    27 
       
    28 import jdk.internal.misc.TerminatingThreadLocal;
    28 import jdk.internal.misc.Unsafe;
    29 import jdk.internal.misc.Unsafe;
    29 
    30 
    30 /**
    31 /**
    31  * Factory for native buffers.
    32  * Factory for native buffers.
    32  */
    33  */
    35     private NativeBuffers() { }
    36     private NativeBuffers() { }
    36 
    37 
    37     private static final Unsafe unsafe = Unsafe.getUnsafe();
    38     private static final Unsafe unsafe = Unsafe.getUnsafe();
    38 
    39 
    39     private static final int TEMP_BUF_POOL_SIZE = 3;
    40     private static final int TEMP_BUF_POOL_SIZE = 3;
    40     private static ThreadLocal<NativeBuffer[]> threadLocal =
    41     private static ThreadLocal<NativeBuffer[]> threadLocal = new TerminatingThreadLocal<>() {
    41         new ThreadLocal<NativeBuffer[]>();
    42         @Override
       
    43         protected void threadTerminated(NativeBuffer[] buffers) {
       
    44             // threadLocal may be initialized but with initialValue of null
       
    45             if (buffers != null) {
       
    46                 for (int i = 0; i < TEMP_BUF_POOL_SIZE; i++) {
       
    47                     NativeBuffer buffer = buffers[i];
       
    48                     if (buffer != null) {
       
    49                         buffer.free();
       
    50                         buffers[i] = null;
       
    51                     }
       
    52                 }
       
    53             }
       
    54         }
       
    55     };
    42 
    56 
    43     /**
    57     /**
    44      * Allocates a native buffer, of at least the given size, from the heap.
    58      * Allocates a native buffer, of at least the given size, from the heap.
    45      */
    59      */
    46     static NativeBuffer allocNativeBuffer(int size) {
    60     static NativeBuffer allocNativeBuffer(int size) {