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.util.ReferenceCountUtil;
19  import io.netty.util.ReferenceCounted;
20  import io.netty.util.collection.LongObjectHashMap;
21  
22  import java.util.Arrays;
23  
24  /**
25   * Retains buffers until their zero-copy notifications arrive.
26   *
27   * <p>{@code freeIdStack} is a LIFO stack of available IDs indexing {@code pooled}. Submission pops an ID;
28   * a failed submission, a primary CQE without MORE, or the NOTIF CQE returns it. This allows out-of-order
29   * notifications to recycle IDs independently.
30   *
31   * <p>The pool grows up to {@link Short#MAX_VALUE} IDs. If all are in use, for example because notifications
32   * lag behind new writes, further requests receive increasing long IDs above that range. Their retained
33   * buffers are stored in {@code overflow} until NOTIF, and these IDs are never reused. Returned pooled IDs
34   * are preferred even while overflow requests remain pending.
35   */
36  final class PendingZeroCopyWrites {
37      private static final int MAX_POOLED_ID = Short.MAX_VALUE;
38  
39      private PendingWrite[] pooled;
40      private short[] freeIdStack;
41      // Next insertion index in the free ID stack.
42      private int freeStackTop;
43      // Overflow IDs are never reused; exhausting the positive long range is not feasible in practice.
44      private long nextOverflowId = MAX_POOLED_ID + 1L;
45      private LongObjectHashMap<PendingWrite> overflow;
46  
47      PendingZeroCopyWrites() {
48          pooled = new PendingWrite[4];
49          freeIdStack = new short[pooled.length];
50          // Slot 0 is unused.
51          for (int id = pooled.length - 1; id > 0; id--) {
52              freeIdStack[freeStackTop++] = (short) id;
53          }
54      }
55  
56      long nextUserData() {
57          if (freeStackTop > 0) {
58              return freeIdStack[--freeStackTop];
59          }
60          return nextUserDataSlow();
61      }
62  
63      private long nextUserDataSlow() {
64          int oldCapacity = pooled.length;
65          if (oldCapacity == MAX_POOLED_ID + 1) {
66              return nextOverflowId++;
67          }
68          int newCapacity = oldCapacity << 1;
69          pooled = Arrays.copyOf(pooled, newCapacity);
70          freeIdStack = new short[newCapacity];
71          for (int id = newCapacity - 1; id >= oldCapacity; id--) {
72              freeIdStack[freeStackTop++] = (short) id;
73          }
74          return nextUserData();
75      }
76  
77      PendingWrite register(long userData) {
78          if (userData > MAX_POOLED_ID) {
79              return registerOverflow(userData);
80          }
81          int id = (int) userData;
82          PendingWrite write = pooled[id];
83          if (write == null) {
84              pooled[id] = write = new PendingWrite();
85          }
86          return write;
87      }
88  
89      // Only used when all pooled IDs are in flight; registration preserves these IDs on its slow path.
90      private PendingWrite registerOverflow(long userData) {
91          if (overflow == null) {
92              overflow = new LongObjectHashMap<>(2);
93          }
94          PendingWrite write = new PendingWrite();
95          overflow.put(userData, write);
96          return write;
97      }
98  
99      void release(long userData) {
100         PendingWrite write = userData <= MAX_POOLED_ID ?
101                 pooled[(int) userData] : overflow.remove(userData);
102         write.release();
103         recycle(userData);
104     }
105 
106     // Also used for requests without MORE, which retain no buffers.
107     void recycle(long userData) {
108         if (userData <= MAX_POOLED_ID) {
109             freeIdStack[freeStackTop++] = (short) userData;
110         }
111     }
112 
113     static final class PendingWrite {
114         private ReferenceCounted[] references = new ReferenceCounted[1];
115         private int count;
116 
117         void add(ReferenceCounted reference) {
118             if (count == references.length) {
119                 references = Arrays.copyOf(references, count << 1);
120             }
121             references[count++] = reference;
122         }
123 
124         private void release() {
125             for (int i = 0; i < count; i++) {
126                 ReferenceCountUtil.safeRelease(references[i]);
127                 references[i] = null;
128             }
129             count = 0;
130         }
131     }
132 }