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