Path: blob/master/src/java.base/share/classes/sun/nio/ch/IOVecWrapper.java
41159 views
/*1* Copyright (c) 2000, 2020, 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.ch;2627import java.nio.ByteBuffer;28import jdk.internal.ref.CleanerFactory;293031/**32* Manipulates a native array of iovec structs on Solaris:33*34* <pre> {@code35* typedef struct iovec {36* caddr_t iov_base;37* int iov_len;38* } iovec_t;39* }</pre>40*41* @author Mike McCloskey42* @since 1.443*/4445class IOVecWrapper {4647// Miscellaneous constants48private static final int BASE_OFFSET = 0;49private static final int LEN_OFFSET;50private static final int SIZE_IOVEC;5152// The iovec array53private final AllocatedNativeObject vecArray;5455// Number of elements in iovec array56private final int size;5758// Buffers and position/remaining corresponding to elements in iovec array59private final ByteBuffer[] buf;60private final int[] position;61private final int[] remaining;6263// Shadow buffers for cases when original buffer is substituted64private final ByteBuffer[] shadow;6566// Base address of this array67final long address;6869// Address size in bytes70static int addressSize;7172private static class Deallocator implements Runnable {73private final AllocatedNativeObject obj;74Deallocator(AllocatedNativeObject obj) {75this.obj = obj;76}77public void run() {78obj.free();79}80}8182// per thread IOVecWrapper83private static final ThreadLocal<IOVecWrapper> cached =84new ThreadLocal<IOVecWrapper>();8586private IOVecWrapper(int size) {87this.size = size;88this.buf = new ByteBuffer[size];89this.position = new int[size];90this.remaining = new int[size];91this.shadow = new ByteBuffer[size];92this.vecArray = new AllocatedNativeObject(size * SIZE_IOVEC, false);93this.address = vecArray.address();94}9596static IOVecWrapper get(int size) {97IOVecWrapper wrapper = cached.get();98if (wrapper != null && wrapper.size < size) {99// not big enough; eagerly release memory100wrapper.vecArray.free();101wrapper = null;102}103if (wrapper == null) {104wrapper = new IOVecWrapper(size);105CleanerFactory.cleaner().register(wrapper, new Deallocator(wrapper.vecArray));106cached.set(wrapper);107}108return wrapper;109}110111void setBuffer(int i, ByteBuffer buf, int pos, int rem) {112this.buf[i] = buf;113this.position[i] = pos;114this.remaining[i] = rem;115}116117void setShadow(int i, ByteBuffer buf) {118shadow[i] = buf;119}120121ByteBuffer getBuffer(int i) {122return buf[i];123}124125int getPosition(int i) {126return position[i];127}128129int getRemaining(int i) {130return remaining[i];131}132133ByteBuffer getShadow(int i) {134return shadow[i];135}136137void clearRefs(int i) {138buf[i] = null;139shadow[i] = null;140}141142void putBase(int i, long base) {143int offset = SIZE_IOVEC * i + BASE_OFFSET;144if (addressSize == 4)145vecArray.putInt(offset, (int)base);146else147vecArray.putLong(offset, base);148}149150void putLen(int i, long len) {151int offset = SIZE_IOVEC * i + LEN_OFFSET;152if (addressSize == 4)153vecArray.putInt(offset, (int)len);154else155vecArray.putLong(offset, len);156}157158static {159addressSize = Util.unsafe().addressSize();160LEN_OFFSET = addressSize;161SIZE_IOVEC = (short) (addressSize * 2);162}163}164165166