Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/nio/ch/IOVecWrapper.java
38918 views
/*1* Copyright (c) 2000, 2010, 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 sun.misc.*;293031/**32* Manipulates a native array of iovec structs on Solaris:33*34* typedef struct iovec {35* caddr_t iov_base;36int iov_len;37* } iovec_t;38*39* @author Mike McCloskey40* @since 1.441*/4243class IOVecWrapper {4445// Miscellaneous constants46private static final int BASE_OFFSET = 0;47private static final int LEN_OFFSET;48private static final int SIZE_IOVEC;4950// The iovec array51private final AllocatedNativeObject vecArray;5253// Number of elements in iovec array54private final int size;5556// Buffers and position/remaining corresponding to elements in iovec array57private final ByteBuffer[] buf;58private final int[] position;59private final int[] remaining;6061// Shadow buffers for cases when original buffer is substituted62private final ByteBuffer[] shadow;6364// Base address of this array65final long address;6667// Address size in bytes68static int addressSize;6970private static class Deallocator implements Runnable {71private final AllocatedNativeObject obj;72Deallocator(AllocatedNativeObject obj) {73this.obj = obj;74}75public void run() {76obj.free();77}78}7980// per thread IOVecWrapper81private static final ThreadLocal<IOVecWrapper> cached =82new ThreadLocal<IOVecWrapper>();8384private IOVecWrapper(int size) {85this.size = size;86this.buf = new ByteBuffer[size];87this.position = new int[size];88this.remaining = new int[size];89this.shadow = new ByteBuffer[size];90this.vecArray = new AllocatedNativeObject(size * SIZE_IOVEC, false);91this.address = vecArray.address();92}9394static IOVecWrapper get(int size) {95IOVecWrapper wrapper = cached.get();96if (wrapper != null && wrapper.size < size) {97// not big enough; eagerly release memory98wrapper.vecArray.free();99wrapper = null;100}101if (wrapper == null) {102wrapper = new IOVecWrapper(size);103Cleaner.create(wrapper, new Deallocator(wrapper.vecArray));104cached.set(wrapper);105}106return wrapper;107}108109void setBuffer(int i, ByteBuffer buf, int pos, int rem) {110this.buf[i] = buf;111this.position[i] = pos;112this.remaining[i] = rem;113}114115void setShadow(int i, ByteBuffer buf) {116shadow[i] = buf;117}118119ByteBuffer getBuffer(int i) {120return buf[i];121}122123int getPosition(int i) {124return position[i];125}126127int getRemaining(int i) {128return remaining[i];129}130131ByteBuffer getShadow(int i) {132return shadow[i];133}134135void clearRefs(int i) {136buf[i] = null;137shadow[i] = null;138}139140void putBase(int i, long base) {141int offset = SIZE_IOVEC * i + BASE_OFFSET;142if (addressSize == 4)143vecArray.putInt(offset, (int)base);144else145vecArray.putLong(offset, base);146}147148void putLen(int i, long len) {149int offset = SIZE_IOVEC * i + LEN_OFFSET;150if (addressSize == 4)151vecArray.putInt(offset, (int)len);152else153vecArray.putLong(offset, len);154}155156static {157addressSize = Util.unsafe().addressSize();158LEN_OFFSET = addressSize;159SIZE_IOVEC = (short) (addressSize * 2);160}161}162163164