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.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             JLA.setCarrierThreadLocal(threadLocal, 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;
131             }
132         }
133 
134         // free it
135         buffer.free();
136     }
137 
138     /**
139      * Copies a byte array and zero terminator into a given native buffer.
140      */
141     static void copyCStringToNativeBuffer(byte[] cstr, NativeBuffer buffer) {
142         long offset = Unsafe.ARRAY_BYTE_BASE_OFFSET;
143         long len = cstr.length;
144         assert buffer.size() >= (len + 1);
145         unsafe.copyMemory(cstr, offset, null, buffer.address(), len);
146         unsafe.putByte(buffer.address() + len, (byte)0);
147     }
148 
149     /**
150      * Copies a byte array and zero terminator into a native buffer, returning
151      * the buffer.
152      */
153     static NativeBuffer asNativeBuffer(byte[] cstr) {
154         NativeBuffer buffer = getNativeBuffer(cstr.length+1);
155         copyCStringToNativeBuffer(cstr, buffer);
156         return buffer;
157     }
158 }