1 /*
  2  * Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.  Oracle designates this
  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 package sun.nio.ch;
 27 
 28 import java.nio.ByteBuffer;
 29 
 30 import jdk.internal.access.JavaLangAccess;
 31 import jdk.internal.access.SharedSecrets;
 32 import jdk.internal.ref.CleanerFactory;
 33 
 34 /**
 35  * Manipulates a native array of iovec structs on Solaris:
 36  *
 37  * <pre> {@code
 38  * typedef struct iovec {
 39  *     caddr_t  iov_base;
 40  *     int      iov_len;
 41  * } iovec_t;
 42  * }</pre>
 43  *
 44  * @author Mike McCloskey
 45  * @since 1.4
 46  */
 47 
 48 class IOVecWrapper {
 49     private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();
 50 
 51     // Miscellaneous constants
 52     private static final int BASE_OFFSET = 0;
 53     private static final int LEN_OFFSET;
 54     private static final int SIZE_IOVEC;
 55 
 56     // The iovec array
 57     private final AllocatedNativeObject vecArray;
 58 
 59     // Number of elements in iovec array
 60     private final int size;
 61 
 62     // Buffers and position/remaining corresponding to elements in iovec array
 63     private final ByteBuffer[] buf;
 64     private final int[] position;
 65     private final int[] remaining;
 66 
 67     // Shadow buffers for cases when original buffer is substituted
 68     private final ByteBuffer[] shadow;
 69 
 70     // Base address of this array
 71     final long address;
 72 
 73     // Address size in bytes
 74     static int addressSize;
 75 
 76     private static class Deallocator implements Runnable {
 77         private final AllocatedNativeObject obj;
 78         Deallocator(AllocatedNativeObject obj) {
 79             this.obj = obj;
 80         }
 81         public void run() {
 82             obj.free();
 83         }
 84     }
 85 
 86     // per thread IOVecWrapper
 87     private static final ThreadLocal<IOVecWrapper> cached = new ThreadLocal<>();
 88 
 89     private IOVecWrapper(int size) {
 90         this.size      = size;
 91         this.buf       = new ByteBuffer[size];
 92         this.position  = new int[size];
 93         this.remaining = new int[size];
 94         this.shadow    = new ByteBuffer[size];
 95         this.vecArray  = new AllocatedNativeObject(size * SIZE_IOVEC, false);
 96         this.address   = vecArray.address();
 97     }
 98 
 99     static IOVecWrapper get(int size) {
100         IOVecWrapper wrapper = JLA.getCarrierThreadLocal(cached);
101         if (wrapper != null && wrapper.size < size) {
102             // not big enough; eagerly release memory
103             wrapper.vecArray.free();
104             wrapper = null;
105         }
106         if (wrapper == null) {
107             wrapper = new IOVecWrapper(size);
108             CleanerFactory.cleaner().register(wrapper, new Deallocator(wrapper.vecArray));
109             JLA.setCarrierThreadLocal(cached, wrapper);
110         }
111         return wrapper;
112     }
113 
114     void setBuffer(int i, ByteBuffer buf, int pos, int rem) {
115         this.buf[i] = buf;
116         this.position[i] = pos;
117         this.remaining[i] = rem;
118     }
119 
120     void setShadow(int i, ByteBuffer buf) {
121         shadow[i] = buf;
122     }
123 
124     ByteBuffer getBuffer(int i) {
125         return buf[i];
126     }
127 
128     int getPosition(int i) {
129         return position[i];
130     }
131 
132     int getRemaining(int i) {
133         return remaining[i];
134     }
135 
136     ByteBuffer getShadow(int i) {
137         return shadow[i];
138     }
139 
140     void clearRefs(int i) {
141         buf[i] = null;
142         shadow[i] = null;
143     }
144 
145     void putBase(int i, long base) {
146         int offset = SIZE_IOVEC * i + BASE_OFFSET;
147         if (addressSize == 4)
148             vecArray.putInt(offset, (int)base);
149         else
150             vecArray.putLong(offset, base);
151     }
152 
153     void putLen(int i, long len) {
154         int offset = SIZE_IOVEC * i + LEN_OFFSET;
155         if (addressSize == 4)
156             vecArray.putInt(offset, (int)len);
157         else
158             vecArray.putLong(offset, len);
159     }
160 
161     static {
162         addressSize = Util.unsafe().addressSize();
163         LEN_OFFSET = addressSize;
164         SIZE_IOVEC = (short) (addressSize * 2);
165     }
166 }