Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/nio/fs/NativeBuffers.java
38918 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 sun.misc.Unsafe;2829/**30* Factory for native buffers.31*/3233class NativeBuffers {34private NativeBuffers() { }3536private static final Unsafe unsafe = Unsafe.getUnsafe();3738private static final int TEMP_BUF_POOL_SIZE = 3;39private static ThreadLocal<NativeBuffer[]> threadLocal =40new ThreadLocal<NativeBuffer[]>();4142/**43* Allocates a native buffer, of at least the given size, from the heap.44*/45static NativeBuffer allocNativeBuffer(int size) {46// Make a new one of at least 2k47if (size < 2048) size = 2048;48return new NativeBuffer(size);49}5051/**52* Returns a native buffer, of at least the given size, from the thread53* local cache.54*/55static NativeBuffer getNativeBufferFromCache(int size) {56// return from cache if possible57NativeBuffer[] buffers = threadLocal.get();58if (buffers != null) {59for (int i=0; i<TEMP_BUF_POOL_SIZE; i++) {60NativeBuffer buffer = buffers[i];61if (buffer != null && buffer.size() >= size) {62buffers[i] = null;63return buffer;64}65}66}67return null;68}6970/**71* Returns a native buffer, of at least the given size. The native buffer72* is taken from the thread local cache if possible; otherwise it is73* allocated from the heap.74*/75static NativeBuffer getNativeBuffer(int size) {76NativeBuffer buffer = getNativeBufferFromCache(size);77if (buffer != null) {78buffer.setOwner(null);79return buffer;80} else {81return allocNativeBuffer(size);82}83}8485/**86* Releases the given buffer. If there is space in the thread local cache87* then the buffer goes into the cache; otherwise the memory is deallocated.88*/89static void releaseNativeBuffer(NativeBuffer buffer) {90// create cache if it doesn't exist91NativeBuffer[] buffers = threadLocal.get();92if (buffers == null) {93buffers = new NativeBuffer[TEMP_BUF_POOL_SIZE];94buffers[0] = buffer;95threadLocal.set(buffers);96return;97}98// Put it in an empty slot if such exists99for (int i=0; i<TEMP_BUF_POOL_SIZE; i++) {100if (buffers[i] == null) {101buffers[i] = buffer;102return;103}104}105// Otherwise replace a smaller one in the cache if such exists106for (int i=0; i<TEMP_BUF_POOL_SIZE; i++) {107NativeBuffer existing = buffers[i];108if (existing.size() < buffer.size()) {109existing.cleaner().clean();110buffers[i] = buffer;111return;112}113}114115// free it116buffer.cleaner().clean();117}118119/**120* Copies a byte array and zero terminator into a given native buffer.121*/122static void copyCStringToNativeBuffer(byte[] cstr, NativeBuffer buffer) {123long offset = Unsafe.ARRAY_BYTE_BASE_OFFSET;124long len = cstr.length;125assert buffer.size() >= (len + 1);126unsafe.copyMemory(cstr, offset, null, buffer.address(), len);127unsafe.putByte(buffer.address() + len, (byte)0);128}129130/**131* Copies a byte array and zero terminator into a native buffer, returning132* the buffer.133*/134static NativeBuffer asNativeBuffer(byte[] cstr) {135NativeBuffer buffer = getNativeBuffer(cstr.length+1);136copyCStringToNativeBuffer(cstr, buffer);137return buffer;138}139}140141142