0N/A/*
3261N/A * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.nio.ch;
0N/A
2655N/Aimport java.nio.ByteBuffer;
0N/Aimport sun.misc.*;
0N/A
0N/A
0N/A/**
0N/A * Manipulates a native array of iovec structs on Solaris:
0N/A *
0N/A * typedef struct iovec {
0N/A * caddr_t iov_base;
0N/A int iov_len;
0N/A * } iovec_t;
0N/A *
0N/A * @author Mike McCloskey
0N/A * @since 1.4
0N/A */
0N/A
0N/Aclass IOVecWrapper {
0N/A
0N/A // Miscellaneous constants
2655N/A private static final int BASE_OFFSET = 0;
2655N/A private static final int LEN_OFFSET;
2655N/A private static final int SIZE_IOVEC;
0N/A
0N/A // The iovec array
2655N/A private final AllocatedNativeObject vecArray;
2655N/A
2655N/A // Number of elements in iovec array
2655N/A private final int size;
2655N/A
2655N/A // Buffers and position/remaining corresponding to elements in iovec array
2655N/A private final ByteBuffer[] buf;
2655N/A private final int[] position;
2655N/A private final int[] remaining;
2655N/A
2655N/A // Shadow buffers for cases when original buffer is substituted
2655N/A private final ByteBuffer[] shadow;
0N/A
0N/A // Base address of this array
2655N/A final long address;
0N/A
0N/A // Address size in bytes
0N/A static int addressSize;
0N/A
2655N/A private static class Deallocator implements Runnable {
2655N/A private final AllocatedNativeObject obj;
2655N/A Deallocator(AllocatedNativeObject obj) {
2655N/A this.obj = obj;
2655N/A }
2655N/A public void run() {
2655N/A obj.free();
2655N/A }
2655N/A }
2655N/A
2655N/A // per thread IOVecWrapper
2655N/A private static final ThreadLocal<IOVecWrapper> cached =
2655N/A new ThreadLocal<IOVecWrapper>();
2655N/A
2655N/A private IOVecWrapper(int size) {
2655N/A this.size = size;
2655N/A this.buf = new ByteBuffer[size];
2655N/A this.position = new int[size];
2655N/A this.remaining = new int[size];
2655N/A this.shadow = new ByteBuffer[size];
2655N/A this.vecArray = new AllocatedNativeObject(size * SIZE_IOVEC, false);
2655N/A this.address = vecArray.address();
2655N/A }
2655N/A
2655N/A static IOVecWrapper get(int size) {
2655N/A IOVecWrapper wrapper = cached.get();
2655N/A if (wrapper != null && wrapper.size < size) {
2655N/A // not big enough; eagerly release memory
2655N/A wrapper.vecArray.free();
2655N/A wrapper = null;
2655N/A }
2655N/A if (wrapper == null) {
2655N/A wrapper = new IOVecWrapper(size);
2655N/A Cleaner.create(wrapper, new Deallocator(wrapper.vecArray));
2655N/A cached.set(wrapper);
2655N/A }
2655N/A return wrapper;
2655N/A }
2655N/A
2655N/A void setBuffer(int i, ByteBuffer buf, int pos, int rem) {
2655N/A this.buf[i] = buf;
2655N/A this.position[i] = pos;
2655N/A this.remaining[i] = rem;
2655N/A }
2655N/A
2655N/A void setShadow(int i, ByteBuffer buf) {
2655N/A shadow[i] = buf;
2655N/A }
2655N/A
2655N/A ByteBuffer getBuffer(int i) {
2655N/A return buf[i];
2655N/A }
2655N/A
2655N/A int getPosition(int i) {
2655N/A return position[i];
2655N/A }
2655N/A
2655N/A int getRemaining(int i) {
2655N/A return remaining[i];
2655N/A }
2655N/A
2655N/A ByteBuffer getShadow(int i) {
2655N/A return shadow[i];
2655N/A }
2655N/A
2655N/A void clearRefs(int i) {
2655N/A buf[i] = null;
2655N/A shadow[i] = null;
0N/A }
0N/A
0N/A void putBase(int i, long base) {
0N/A int offset = SIZE_IOVEC * i + BASE_OFFSET;
0N/A if (addressSize == 4)
0N/A vecArray.putInt(offset, (int)base);
0N/A else
0N/A vecArray.putLong(offset, base);
0N/A }
0N/A
0N/A void putLen(int i, long len) {
0N/A int offset = SIZE_IOVEC * i + LEN_OFFSET;
0N/A if (addressSize == 4)
0N/A vecArray.putInt(offset, (int)len);
0N/A else
0N/A vecArray.putLong(offset, len);
0N/A }
0N/A
0N/A static {
0N/A addressSize = Util.unsafe().addressSize();
0N/A LEN_OFFSET = addressSize;
0N/A SIZE_IOVEC = (short) (addressSize * 2);
0N/A }
0N/A}