View Javadoc
1   /*
2    * Copyright 2026 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.channel.uring;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.buffer.Unpooled;
20  import io.netty.channel.unix.Buffer;
21  import io.netty.channel.unix.IovArray;
22  import io.netty.util.internal.CleanableDirectBuffer;
23  
24  /**
25   * The iovec memory of an {@link IoUringIoHandler}, shared by all writes until the next submit.
26   * <p>
27   * Each write gets its own empty {@link IovArray} that starts after the entries of the previous writes, so the
28   * {@code IOV_MAX} limit of an {@link IovArray} applies to each write and not to all of them together.
29   */
30  final class IoUringSharedIovArray {
31  
32      private final CleanableDirectBuffer cleanable;
33      private final ByteBuf internalBuffer;
34      private final int numEntries;
35      // Index of the first entry of the last write.
36      private int current;
37      // The IovArray of the last write since clear(), null if there is none.
38      private IovArray last;
39  
40      @SuppressWarnings("deprecation")
41      IoUringSharedIovArray(int numEntries) {
42          this.numEntries = numEntries;
43          this.cleanable = Buffer.allocateDirectBufferWithNativeOrder(Math.multiplyExact(numEntries, IovArray.IOV_SIZE));
44          this.internalBuffer = Unpooled.wrappedBuffer(cleanable.buffer());
45      }
46  
47      private int nextStart() {
48          return last == null ? 0 : current + last.count();
49      }
50  
51      /**
52       * Returns {@code true} if the next write can not add any entry.
53       */
54      boolean isFull() {
55          return nextStart() == numEntries;
56      }
57  
58      /**
59       * Returns the empty {@link IovArray} for the next write.
60       */
61      IovArray next() {
62          current = nextStart();
63          // IovArray expects an empty buffer, while a slice is full.
64          last = new IovArray(internalBuffer.slice(
65                  current * IovArray.IOV_SIZE, (numEntries - current) * IovArray.IOV_SIZE).clear());
66          return last;
67      }
68  
69      /**
70       * Must only be called once the kernel does not read the entries anymore, which is after the submit.
71       */
72      void clear() {
73          current = 0;
74          last = null;
75      }
76  
77      void release() {
78          cleanable.clean();
79      }
80  }