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.JdkThreadLocal; 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 JdkThreadLocal<>() { 42 @Override 43 protected void threadTerminated() { 44 NativeBuffer[] buffers = get(); 45 // threadLocal may be initialized but with initialValue of null 46 if (buffers != null) { 47 for (int i = 0; i < TEMP_BUF_POOL_SIZE; i++) { 48 NativeBuffer buffer = buffers[i]; 49 if (buffer != null) { 50 buffer.free(); 51 buffers[i] = null; 52 } 53 } 54 } 55 } 56 }; 57 58 /** 59 * Allocates a native buffer, of at least the given size, from the heap. 60 */ 61 static NativeBuffer allocNativeBuffer(int size) { 62 // Make a new one of at least 2k 63 if (size < 2048) size = 2048; 64 return new NativeBuffer(size); 65 } 66 67 /** 68 * Returns a native buffer, of at least the given size, from the thread 69 * local cache. 70 */ 71 static NativeBuffer getNativeBufferFromCache(int size) { 72 // return from cache if possible 73 NativeBuffer[] buffers = threadLocal.get(); 74 if (buffers != null) { 75 for (int i=0; i<TEMP_BUF_POOL_SIZE; i++) { 76 NativeBuffer buffer = buffers[i]; 77 if (buffer != null && buffer.size() >= size) { 78 buffers[i] = null; 79 return buffer; 80 } 81 } 82 } 83 return null; 84 } 85 86 /** 87 * Returns a native buffer, of at least the given size. The native buffer 88 * is taken from the thread local cache if possible; otherwise it is 89 * allocated from the heap. 90 */ 91 static NativeBuffer getNativeBuffer(int size) { 92 NativeBuffer buffer = getNativeBufferFromCache(size); 93 if (buffer != null) { 94 buffer.setOwner(null); 95 return buffer; 96 } else { 97 return allocNativeBuffer(size); 98 } 99 } 100 101 /** 102 * Releases the given buffer. If there is space in the thread local cache 103 * then the buffer goes into the cache; otherwise the memory is deallocated. 104 */ 105 static void releaseNativeBuffer(NativeBuffer buffer) { 106 // create cache if it doesn't exist 107 NativeBuffer[] buffers = threadLocal.get(); 108 if (buffers == null) { 109 buffers = new NativeBuffer[TEMP_BUF_POOL_SIZE]; 110 buffers[0] = buffer; 111 threadLocal.set(buffers); 112 return; 113 } 114 // Put it in an empty slot if such exists 115 for (int i=0; i<TEMP_BUF_POOL_SIZE; i++) { 116 if (buffers[i] == null) { 117 buffers[i] = buffer; 118 return; 119 } 120 } 121 // Otherwise replace a smaller one in the cache if such exists 122 for (int i=0; i<TEMP_BUF_POOL_SIZE; i++) { 123 NativeBuffer existing = buffers[i]; 124 if (existing.size() < buffer.size()) { 125 existing.free(); 126 buffers[i] = buffer; 127 return; 128 } 129 } 130 131 // free it 132 buffer.free(); 133 } 134 135 /** 136 * Copies a byte array and zero terminator into a given native buffer. 137 */ 138 static void copyCStringToNativeBuffer(byte[] cstr, NativeBuffer buffer) { 139 long offset = Unsafe.ARRAY_BYTE_BASE_OFFSET; 140 long len = cstr.length; 141 assert buffer.size() >= (len + 1); 142 unsafe.copyMemory(cstr, offset, null, buffer.address(), len); 143 unsafe.putByte(buffer.address() + len, (byte)0); 144 } 145 146 /** 147 * Copies a byte array and zero terminator into a native buffer, returning 148 * the buffer. 149 */ 150 static NativeBuffer asNativeBuffer(byte[] cstr) { 151 NativeBuffer buffer = getNativeBuffer(cstr.length+1); 152 copyCStringToNativeBuffer(cstr, buffer); 153 return buffer; 154 } 155 }