1 /*
   2  * Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   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;
 117             }
 118         }
 119 
 120         // free it
 121         buffer.free();
 122     }
 123 
 124     /**
 125      * Copies a byte array and zero terminator into a given native buffer.
 126      */
 127     static void copyCStringToNativeBuffer(byte[] cstr, NativeBuffer buffer) {
 128         long offset = Unsafe.ARRAY_BYTE_BASE_OFFSET;
 129         long len = cstr.length;
 130         assert buffer.size() >= (len + 1);
 131         unsafe.copyMemory(cstr, offset, null, buffer.address(), len);
 132         unsafe.putByte(buffer.address() + len, (byte)0);
 133     }
 134 
 135     /**
 136      * Copies a byte array and zero terminator into a native buffer, returning
 137      * the buffer.
 138      */
 139     static NativeBuffer asNativeBuffer(byte[] cstr) {
 140         NativeBuffer buffer = getNativeBuffer(cstr.length+1);
 141         copyCStringToNativeBuffer(cstr, buffer);
 142         return buffer;
 143     }
 144 }