Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/sun/nio/ch/IOVecWrapper.java
41159 views
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
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];
128
}
129
130
int getRemaining(int i) {
131
return remaining[i];
132
}
133
134
ByteBuffer getShadow(int i) {
135
return shadow[i];
136
}
137
138
void clearRefs(int i) {
139
buf[i] = null;
140
shadow[i] = null;
141
}
142
143
void putBase(int i, long base) {
144
int offset = SIZE_IOVEC * i + BASE_OFFSET;
145
if (addressSize == 4)
146
vecArray.putInt(offset, (int)base);
147
else
148
vecArray.putLong(offset, base);
149
}
150
151
void putLen(int i, long len) {
152
int offset = SIZE_IOVEC * i + LEN_OFFSET;
153
if (addressSize == 4)
154
vecArray.putInt(offset, (int)len);
155
else
156
vecArray.putLong(offset, len);
157
}
158
159
static {
160
addressSize = Util.unsafe().addressSize();
161
LEN_OFFSET = addressSize;
162
SIZE_IOVEC = (short) (addressSize * 2);
163
}
164
}
165
166