1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.channel.unix;
17
18 import io.netty.buffer.ByteBuf;
19 import io.netty.buffer.Unpooled;
20 import io.netty.channel.ChannelOutboundBuffer.MessageProcessor;
21 import io.netty.util.internal.PlatformDependent;
22
23 import java.nio.ByteBuffer;
24 import java.nio.ByteOrder;
25
26 import static io.netty.channel.unix.Limits.IOV_MAX;
27 import static io.netty.channel.unix.Limits.SSIZE_MAX;
28 import static io.netty.util.internal.ObjectUtil.checkPositive;
29 import static java.lang.Math.min;
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public final class IovArray implements MessageProcessor {
49
50
51 private static final int ADDRESS_SIZE = Buffer.addressSize();
52
53
54
55
56
57 public static final int IOV_SIZE = 2 * ADDRESS_SIZE;
58
59
60
61
62
63 private static final int MAX_CAPACITY = IOV_MAX * IOV_SIZE;
64
65 private final long memoryAddress;
66 private final ByteBuf memory;
67 private int count;
68 private long size;
69 private long maxBytes = SSIZE_MAX;
70
71 public IovArray() {
72 this(Unpooled.wrappedBuffer(Buffer.allocateDirectWithNativeOrder(MAX_CAPACITY)).setIndex(0, 0));
73 }
74
75 @SuppressWarnings("deprecation")
76 public IovArray(ByteBuf memory) {
77 assert memory.writerIndex() == 0;
78 assert memory.readerIndex() == 0;
79 this.memory = PlatformDependent.hasUnsafe() ? memory : memory.order(
80 PlatformDependent.BIG_ENDIAN_NATIVE_ORDER ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN);
81 if (memory.hasMemoryAddress()) {
82 memoryAddress = memory.memoryAddress();
83 } else {
84
85
86
87
88
89 ByteBuffer byteBuffer = memory.internalNioBuffer(0, memory.capacity());
90 memoryAddress = Buffer.memoryAddress(byteBuffer) + byteBuffer.position();
91 }
92 }
93
94 public void clear() {
95 count = 0;
96 size = 0;
97 }
98
99
100
101
102 @Deprecated
103 public boolean add(ByteBuf buf) {
104 return add(buf, buf.readerIndex(), buf.readableBytes());
105 }
106
107 public boolean add(ByteBuf buf, int offset, int len) {
108 if (count == IOV_MAX) {
109
110 return false;
111 }
112 if (buf.nioBufferCount() == 1) {
113 if (len == 0) {
114 return true;
115 }
116 if (buf.hasMemoryAddress()) {
117 return add(memoryAddress, buf.memoryAddress() + offset, len);
118 } else {
119 ByteBuffer nioBuffer = buf.internalNioBuffer(offset, len);
120 return add(memoryAddress, Buffer.memoryAddress(nioBuffer) + nioBuffer.position(), len);
121 }
122 } else {
123 ByteBuffer[] buffers = buf.nioBuffers(offset, len);
124 for (ByteBuffer nioBuffer : buffers) {
125 final int remaining = nioBuffer.remaining();
126 if (remaining != 0 &&
127 (!add(memoryAddress, Buffer.memoryAddress(nioBuffer) + nioBuffer.position(), remaining)
128 || count == IOV_MAX)) {
129 return false;
130 }
131 }
132 return true;
133 }
134 }
135
136 private boolean add(long memoryAddress, long addr, int len) {
137 assert addr != 0;
138
139
140
141 if ((maxBytes - len < size && count > 0) ||
142
143 memory.capacity() < (count + 1) * IOV_SIZE) {
144
145
146
147
148
149
150 return false;
151 }
152 final int baseOffset = idx(count);
153 final int lengthOffset = baseOffset + ADDRESS_SIZE;
154
155 size += len;
156 ++count;
157
158 if (ADDRESS_SIZE == 8) {
159
160 if (PlatformDependent.hasUnsafe()) {
161 PlatformDependent.putLong(baseOffset + memoryAddress, addr);
162 PlatformDependent.putLong(lengthOffset + memoryAddress, len);
163 } else {
164 memory.setLong(baseOffset, addr);
165 memory.setLong(lengthOffset, len);
166 }
167 } else {
168 assert ADDRESS_SIZE == 4;
169 if (PlatformDependent.hasUnsafe()) {
170 PlatformDependent.putInt(baseOffset + memoryAddress, (int) addr);
171 PlatformDependent.putInt(lengthOffset + memoryAddress, len);
172 } else {
173 memory.setInt(baseOffset, (int) addr);
174 memory.setInt(lengthOffset, len);
175 }
176 }
177 return true;
178 }
179
180
181
182
183 public int count() {
184 return count;
185 }
186
187
188
189
190 public long size() {
191 return size;
192 }
193
194
195
196
197
198
199
200
201
202
203
204 public void maxBytes(long maxBytes) {
205 this.maxBytes = min(SSIZE_MAX, checkPositive(maxBytes, "maxBytes"));
206 }
207
208
209
210
211
212 public long maxBytes() {
213 return maxBytes;
214 }
215
216
217
218
219 public long memoryAddress(int offset) {
220 return memoryAddress + idx(offset);
221 }
222
223
224
225
226 public void release() {
227 memory.release();
228 }
229
230 @Override
231 public boolean processMessage(Object msg) throws Exception {
232 if (msg instanceof ByteBuf) {
233 ByteBuf buffer = (ByteBuf) msg;
234 return add(buffer, buffer.readerIndex(), buffer.readableBytes());
235 }
236 return false;
237 }
238
239 private static int idx(int index) {
240 return IOV_SIZE * index;
241 }
242 }