< prev index next >

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

Print this page
rev 50306 : imported patch loom-fibers


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


  43     /**
  44      * Allocates a native buffer, of at least the given size, from the heap.
  45      */
  46     static NativeBuffer allocNativeBuffer(int size) {
  47         // Make a new one of at least 2k
  48         if (size < 2048) size = 2048;
  49         return new NativeBuffer(size);
  50     }
  51 
  52     /**
  53      * Returns a native buffer, of at least the given size, from the thread
  54      * local cache.
  55      */
  56     static NativeBuffer getNativeBufferFromCache(int size) {
  57         // return from cache if possible
  58         NativeBuffer[] buffers = threadLocal.get();
  59         if (buffers != null) {
  60             for (int i=0; i<TEMP_BUF_POOL_SIZE; i++) {
  61                 NativeBuffer buffer = buffers[i];
  62                 if (buffer != null && buffer.size() >= size) {
  63                     buffers[i] = null;
  64                     return buffer;
  65                 }
  66             }
  67         }
  68         return null;
  69     }
  70 
  71     /**
  72      * Returns a native buffer, of at least the given size. The native buffer
  73      * is taken from the thread local cache if possible; otherwise it is
  74      * allocated from the heap.
  75      */
  76     static NativeBuffer getNativeBuffer(int size) {
  77         NativeBuffer buffer = getNativeBufferFromCache(size);
  78         if (buffer != null) {
  79             buffer.setOwner(null);
  80             return buffer;
  81         } else {
  82             return allocNativeBuffer(size);
  83         }
  84     }
  85 
  86     /**
  87      * Releases the given buffer. If there is space in the thread local cache
  88      * then the buffer goes into the cache; otherwise the memory is deallocated.
  89      */
  90     static void releaseNativeBuffer(NativeBuffer buffer) {
  91         // create cache if it doesn't exist
  92         NativeBuffer[] buffers = threadLocal.get();
  93         if (buffers == null) {
  94             buffers = new NativeBuffer[TEMP_BUF_POOL_SIZE];
  95             buffers[0] = buffer;
  96             threadLocal.set(buffers);
  97             return;
  98         }
  99         // Put it in an empty slot if such exists
 100         for (int i=0; i<TEMP_BUF_POOL_SIZE; i++) {
 101             if (buffers[i] == null) {
 102                 buffers[i] = buffer;
 103                 return;
 104             }
 105         }
 106         // Otherwise replace a smaller one in the cache if such exists
 107         for (int i=0; i<TEMP_BUF_POOL_SIZE; i++) {
 108             NativeBuffer existing = buffers[i];
 109             if (existing.size() < buffer.size()) {
 110                 existing.free();
 111                 buffers[i] = buffer;
 112                 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.misc.JavaLangAccess;
  29 import jdk.internal.misc.SharedSecrets;
  30 import jdk.internal.misc.Unsafe;
  31 
  32 /**
  33  * Factory for native buffers.
  34  */
  35 
  36 class NativeBuffers {
  37     private static JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();
  38 
  39     private static final Unsafe unsafe = Unsafe.getUnsafe();
  40 
  41     private static final int TEMP_BUF_POOL_SIZE = 3;
  42     private static ThreadLocal<NativeBuffer[]> threadLocal =
  43         new ThreadLocal<NativeBuffer[]>();
  44 
  45     private NativeBuffers() { }
  46 
  47     /**
  48      * Allocates a native buffer, of at least the given size, from the heap.
  49      */
  50     static NativeBuffer allocNativeBuffer(int size) {
  51         // Make a new one of at least 2k
  52         if (size < 2048) size = 2048;
  53         return new NativeBuffer(size);
  54     }
  55 
  56     /**
  57      * Returns a native buffer, of at least the given size, from the thread
  58      * local cache.
  59      */
  60     static NativeBuffer getNativeBufferFromCache(int size) {
  61         // return from cache if possible
  62         NativeBuffer[] buffers = JLA.getKernelThreadLocal(threadLocal);
  63         if (buffers != null) {
  64             for (int i=0; i<TEMP_BUF_POOL_SIZE; i++) {
  65                 NativeBuffer buffer = buffers[i];
  66                 if (buffer != null && buffer.size() >= size) {
  67                     buffers[i] = null;
  68                     return buffer;
  69                 }
  70             }
  71         }
  72         return null;
  73     }
  74 
  75     /**
  76      * Returns a native buffer, of at least the given size. The native buffer
  77      * is taken from the thread local cache if possible; otherwise it is
  78      * allocated from the heap.
  79      */
  80     static NativeBuffer getNativeBuffer(int size) {
  81         NativeBuffer buffer = getNativeBufferFromCache(size);
  82         if (buffer != null) {
  83             buffer.setOwner(null);
  84             return buffer;
  85         } else {
  86             return allocNativeBuffer(size);
  87         }
  88     }
  89 
  90     /**
  91      * Releases the given buffer. If there is space in the thread local cache
  92      * then the buffer goes into the cache; otherwise the memory is deallocated.
  93      */
  94     static void releaseNativeBuffer(NativeBuffer buffer) {
  95         // create cache if it doesn't exist
  96         NativeBuffer[] buffers = JLA.getKernelThreadLocal(threadLocal);
  97         if (buffers == null) {
  98             buffers = new NativeBuffer[TEMP_BUF_POOL_SIZE];
  99             buffers[0] = buffer;
 100             threadLocal.set(buffers);
 101             return;
 102         }
 103         // Put it in an empty slot if such exists
 104         for (int i=0; i<TEMP_BUF_POOL_SIZE; i++) {
 105             if (buffers[i] == null) {
 106                 buffers[i] = buffer;
 107                 return;
 108             }
 109         }
 110         // Otherwise replace a smaller one in the cache if such exists
 111         for (int i=0; i<TEMP_BUF_POOL_SIZE; i++) {
 112             NativeBuffer existing = buffers[i];
 113             if (existing.size() < buffer.size()) {
 114                 existing.free();
 115                 buffers[i] = buffer;
 116                 return;


< prev index next >