< prev index next >

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

Print this page




   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.nio.fs;
  27 


  28 import jdk.internal.misc.TerminatingThreadLocal;
  29 import jdk.internal.misc.Unsafe;
  30 
  31 /**
  32  * Factory for native buffers.
  33  */
  34 
  35 class NativeBuffers {
  36     private NativeBuffers() { }
  37 
  38     private static final Unsafe unsafe = Unsafe.getUnsafe();
  39 
  40     private static final int TEMP_BUF_POOL_SIZE = 3;
  41     private static ThreadLocal<NativeBuffer[]> threadLocal = new TerminatingThreadLocal<>() {
  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     };
  56 


  57     /**
  58      * Allocates a native buffer, of at least the given size, from the heap.
  59      */
  60     static NativeBuffer allocNativeBuffer(int size) {
  61         // Make a new one of at least 2k
  62         if (size < 2048) size = 2048;
  63         return new NativeBuffer(size);
  64     }
  65 
  66     /**
  67      * Returns a native buffer, of at least the given size, from the thread
  68      * local cache.
  69      */
  70     static NativeBuffer getNativeBufferFromCache(int size) {
  71         // return from cache if possible
  72         NativeBuffer[] buffers = threadLocal.get();
  73         if (buffers != null) {
  74             for (int i=0; i<TEMP_BUF_POOL_SIZE; i++) {
  75                 NativeBuffer buffer = buffers[i];
  76                 if (buffer != null && buffer.size() >= size) {
  77                     buffers[i] = null;
  78                     return buffer;
  79                 }
  80             }
  81         }
  82         return null;
  83     }
  84 
  85     /**
  86      * Returns a native buffer, of at least the given size. The native buffer
  87      * is taken from the thread local cache if possible; otherwise it is
  88      * allocated from the heap.
  89      */
  90     static NativeBuffer getNativeBuffer(int size) {
  91         NativeBuffer buffer = getNativeBufferFromCache(size);
  92         if (buffer != null) {
  93             buffer.setOwner(null);
  94             return buffer;
  95         } else {
  96             return allocNativeBuffer(size);
  97         }
  98     }
  99 
 100     /**
 101      * Releases the given buffer. If there is space in the thread local cache
 102      * then the buffer goes into the cache; otherwise the memory is deallocated.
 103      */
 104     static void releaseNativeBuffer(NativeBuffer buffer) {
 105         // create cache if it doesn't exist
 106         NativeBuffer[] buffers = threadLocal.get();
 107         if (buffers == null) {
 108             buffers = new NativeBuffer[TEMP_BUF_POOL_SIZE];
 109             buffers[0] = buffer;
 110             threadLocal.set(buffers);
 111             return;
 112         }
 113         // Put it in an empty slot if such exists
 114         for (int i=0; i<TEMP_BUF_POOL_SIZE; i++) {
 115             if (buffers[i] == null) {
 116                 buffers[i] = buffer;
 117                 return;
 118             }
 119         }
 120         // Otherwise replace a smaller one in the cache if such exists
 121         for (int i=0; i<TEMP_BUF_POOL_SIZE; i++) {
 122             NativeBuffer existing = buffers[i];
 123             if (existing.size() < buffer.size()) {
 124                 existing.free();
 125                 buffers[i] = buffer;
 126                 return;




   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.nio.fs;
  27 
  28 import jdk.internal.access.JavaLangAccess;
  29 import jdk.internal.access.SharedSecrets;
  30 import jdk.internal.misc.TerminatingThreadLocal;
  31 import jdk.internal.misc.Unsafe;
  32 
  33 /**
  34  * Factory for native buffers.
  35  */
  36 
  37 class NativeBuffers {
  38     private static JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();
  39 
  40     private static final Unsafe unsafe = Unsafe.getUnsafe();
  41 
  42     private static final int TEMP_BUF_POOL_SIZE = 3;
  43     private static ThreadLocal<NativeBuffer[]> threadLocal = new TerminatingThreadLocal<>() {
  44         @Override
  45         protected void threadTerminated(NativeBuffer[] buffers) {
  46             // threadLocal may be initialized but with initialValue of null
  47             if (buffers != null) {
  48                 for (int i = 0; i < TEMP_BUF_POOL_SIZE; i++) {
  49                     NativeBuffer buffer = buffers[i];
  50                     if (buffer != null) {
  51                         buffer.free();
  52                         buffers[i] = null;
  53                     }
  54                 }
  55             }
  56         }
  57     };
  58 
  59     private NativeBuffers() { }
  60 
  61     /**
  62      * Allocates a native buffer, of at least the given size, from the heap.
  63      */
  64     static NativeBuffer allocNativeBuffer(int size) {
  65         // Make a new one of at least 2k
  66         if (size < 2048) size = 2048;
  67         return new NativeBuffer(size);
  68     }
  69 
  70     /**
  71      * Returns a native buffer, of at least the given size, from the thread
  72      * local cache.
  73      */
  74     static NativeBuffer getNativeBufferFromCache(int size) {
  75         // return from cache if possible
  76         NativeBuffer[] buffers = JLA.getCarrierThreadLocal(threadLocal);
  77         if (buffers != null) {
  78             for (int i=0; i<TEMP_BUF_POOL_SIZE; i++) {
  79                 NativeBuffer buffer = buffers[i];
  80                 if (buffer != null && buffer.size() >= size) {
  81                     buffers[i] = null;
  82                     return buffer;
  83                 }
  84             }
  85         }
  86         return null;
  87     }
  88 
  89     /**
  90      * Returns a native buffer, of at least the given size. The native buffer
  91      * is taken from the thread local cache if possible; otherwise it is
  92      * allocated from the heap.
  93      */
  94     static NativeBuffer getNativeBuffer(int size) {
  95         NativeBuffer buffer = getNativeBufferFromCache(size);
  96         if (buffer != null) {
  97             buffer.setOwner(null);
  98             return buffer;
  99         } else {
 100             return allocNativeBuffer(size);
 101         }
 102     }
 103 
 104     /**
 105      * Releases the given buffer. If there is space in the thread local cache
 106      * then the buffer goes into the cache; otherwise the memory is deallocated.
 107      */
 108     static void releaseNativeBuffer(NativeBuffer buffer) {
 109         // create cache if it doesn't exist
 110         NativeBuffer[] buffers = JLA.getCarrierThreadLocal(threadLocal);
 111         if (buffers == null) {
 112             buffers = new NativeBuffer[TEMP_BUF_POOL_SIZE];
 113             buffers[0] = buffer;
 114             threadLocal.set(buffers);
 115             return;
 116         }
 117         // Put it in an empty slot if such exists
 118         for (int i=0; i<TEMP_BUF_POOL_SIZE; i++) {
 119             if (buffers[i] == null) {
 120                 buffers[i] = buffer;
 121                 return;
 122             }
 123         }
 124         // Otherwise replace a smaller one in the cache if such exists
 125         for (int i=0; i<TEMP_BUF_POOL_SIZE; i++) {
 126             NativeBuffer existing = buffers[i];
 127             if (existing.size() < buffer.size()) {
 128                 existing.free();
 129                 buffers[i] = buffer;
 130                 return;


< prev index next >