Path: blob/master/src/java.base/share/classes/sun/nio/fs/NativeBuffers.java
41159 views
/*1* Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.nio.fs;2627import jdk.internal.misc.TerminatingThreadLocal;28import jdk.internal.misc.Unsafe;2930/**31* Factory for native buffers.32*/3334class NativeBuffers {35private NativeBuffers() { }3637private static final Unsafe unsafe = Unsafe.getUnsafe();3839private static final int TEMP_BUF_POOL_SIZE = 3;40private static ThreadLocal<NativeBuffer[]> threadLocal = new TerminatingThreadLocal<>() {41@Override42protected void threadTerminated(NativeBuffer[] buffers) {43// threadLocal may be initialized but with initialValue of null44if (buffers != null) {45for (int i = 0; i < TEMP_BUF_POOL_SIZE; i++) {46NativeBuffer buffer = buffers[i];47if (buffer != null) {48buffer.free();49buffers[i] = null;50}51}52}53}54};5556/**57* Allocates a native buffer, of at least the given size, from the heap.58*/59static NativeBuffer allocNativeBuffer(int size) {60// Make a new one of at least 2k61if (size < 2048) size = 2048;62return new NativeBuffer(size);63}6465/**66* Returns a native buffer, of at least the given size, from the thread67* local cache.68*/69static NativeBuffer getNativeBufferFromCache(int size) {70// return from cache if possible71NativeBuffer[] buffers = threadLocal.get();72if (buffers != null) {73for (int i=0; i<TEMP_BUF_POOL_SIZE; i++) {74NativeBuffer buffer = buffers[i];75if (buffer != null && buffer.size() >= size) {76buffers[i] = null;77return buffer;78}79}80}81return null;82}8384/**85* Returns a native buffer, of at least the given size. The native buffer86* is taken from the thread local cache if possible; otherwise it is87* allocated from the heap.88*/89static NativeBuffer getNativeBuffer(int size) {90NativeBuffer buffer = getNativeBufferFromCache(size);91if (buffer != null) {92buffer.setOwner(null);93return buffer;94} else {95return allocNativeBuffer(size);96}97}9899/**100* Releases the given buffer. If there is space in the thread local cache101* then the buffer goes into the cache; otherwise the memory is deallocated.102*/103static void releaseNativeBuffer(NativeBuffer buffer) {104// create cache if it doesn't exist105NativeBuffer[] buffers = threadLocal.get();106if (buffers == null) {107buffers = new NativeBuffer[TEMP_BUF_POOL_SIZE];108buffers[0] = buffer;109threadLocal.set(buffers);110return;111}112// Put it in an empty slot if such exists113for (int i=0; i<TEMP_BUF_POOL_SIZE; i++) {114if (buffers[i] == null) {115buffers[i] = buffer;116return;117}118}119// Otherwise replace a smaller one in the cache if such exists120for (int i=0; i<TEMP_BUF_POOL_SIZE; i++) {121NativeBuffer existing = buffers[i];122if (existing.size() < buffer.size()) {123existing.free();124buffers[i] = buffer;125return;126}127}128129// free it130buffer.free();131}132133/**134* Copies a byte array and zero terminator into a given native buffer.135*/136static void copyCStringToNativeBuffer(byte[] cstr, NativeBuffer buffer) {137long offset = Unsafe.ARRAY_BYTE_BASE_OFFSET;138long len = cstr.length;139assert buffer.size() >= (len + 1);140unsafe.copyMemory(cstr, offset, null, buffer.address(), len);141unsafe.putByte(buffer.address() + len, (byte)0);142}143144/**145* Copies a byte array and zero terminator into a native buffer, returning146* the buffer.147*/148static NativeBuffer asNativeBuffer(byte[] cstr) {149NativeBuffer buffer = getNativeBuffer(cstr.length+1);150copyCStringToNativeBuffer(cstr, buffer);151return buffer;152}153}154155156