< prev index next >

src/java.base/share/classes/sun/nio/ch/IOVecWrapper.java

Print this page

  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 import jdk.internal.ref.CleanerFactory;
 30 



 31 
 32 /**
 33  * Manipulates a native array of iovec structs on Solaris:
 34  *
 35  * <pre> {@code
 36  * typedef struct iovec {
 37  *     caddr_t  iov_base;
 38  *     int      iov_len;
 39  * } iovec_t;
 40  * }</pre>
 41  *
 42  * @author Mike McCloskey
 43  * @since 1.4
 44  */
 45 
 46 class IOVecWrapper {

 47 
 48     // Miscellaneous constants
 49     private static final int BASE_OFFSET = 0;
 50     private static final int LEN_OFFSET;
 51     private static final int SIZE_IOVEC;
 52 
 53     // The iovec array
 54     private final AllocatedNativeObject vecArray;
 55 
 56     // Number of elements in iovec array
 57     private final int size;
 58 
 59     // Buffers and position/remaining corresponding to elements in iovec array
 60     private final ByteBuffer[] buf;
 61     private final int[] position;
 62     private final int[] remaining;
 63 
 64     // Shadow buffers for cases when original buffer is substituted
 65     private final ByteBuffer[] shadow;
 66 
 67     // Base address of this array
 68     final long address;
 69 
 70     // Address size in bytes
 71     static int addressSize;
 72 
 73     private static class Deallocator implements Runnable {
 74         private final AllocatedNativeObject obj;
 75         Deallocator(AllocatedNativeObject obj) {
 76             this.obj = obj;
 77         }
 78         public void run() {
 79             obj.free();
 80         }
 81     }
 82 
 83     // per thread IOVecWrapper
 84     private static final ThreadLocal<IOVecWrapper> cached =
 85         new ThreadLocal<IOVecWrapper>();
 86 
 87     private IOVecWrapper(int size) {
 88         this.size      = size;
 89         this.buf       = new ByteBuffer[size];
 90         this.position  = new int[size];
 91         this.remaining = new int[size];
 92         this.shadow    = new ByteBuffer[size];
 93         this.vecArray  = new AllocatedNativeObject(size * SIZE_IOVEC, false);
 94         this.address   = vecArray.address();
 95     }
 96 
 97     static IOVecWrapper get(int size) {
 98         IOVecWrapper wrapper = cached.get();
 99         if (wrapper != null && wrapper.size < size) {
100             // not big enough; eagerly release memory
101             wrapper.vecArray.free();
102             wrapper = null;
103         }
104         if (wrapper == null) {
105             wrapper = new IOVecWrapper(size);
106             CleanerFactory.cleaner().register(wrapper, new Deallocator(wrapper.vecArray));
107             cached.set(wrapper);
108         }
109         return wrapper;
110     }
111 
112     void setBuffer(int i, ByteBuffer buf, int pos, int rem) {
113         this.buf[i] = buf;
114         this.position[i] = pos;
115         this.remaining[i] = rem;
116     }
117 
118     void setShadow(int i, ByteBuffer buf) {
119         shadow[i] = buf;
120     }
121 
122     ByteBuffer getBuffer(int i) {
123         return buf[i];
124     }
125 
126     int getPosition(int i) {
127         return position[i];

  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];
< prev index next >