View Javadoc
1   /*
2    * Copyright 2024 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 java.util.Arrays;
19  
20  final class MsgHdrMemoryArray {
21      static final long NO_ID = 0;
22  
23      private final MsgHdrMemory[] hdrs;
24      private final int capacity;
25      private final long[] ids;
26      private boolean released;
27      private int idx;
28  
29      MsgHdrMemoryArray(short capacity) {
30          assert capacity >= 0;
31          this.capacity = capacity;
32          hdrs = new MsgHdrMemory[capacity];
33          ids = new long[capacity];
34          for (int i = 0; i < hdrs.length; i++) {
35              hdrs[i] = new MsgHdrMemory((short) i);
36              ids[i] = NO_ID;
37          }
38      }
39  
40      boolean isFull() {
41          return idx == hdrs.length;
42      }
43  
44      MsgHdrMemory nextHdr() {
45          if (isFull()) {
46              return null;
47          }
48          return hdrs[idx++];
49      }
50  
51      void restoreNextHdr(MsgHdrMemory hdr) {
52          assert hdr.idx() == idx - 1;
53          idx--;
54      }
55  
56      MsgHdrMemory hdr(int idx) {
57          return hdrs[idx];
58      }
59  
60      long id(int idx) {
61          return ids[idx];
62      }
63  
64      void setId(int idx, long id) {
65          ids[idx] = id;
66      }
67  
68      void clear() {
69          Arrays.fill(ids, 0, idx, NO_ID);
70          idx = 0;
71      }
72  
73      int length() {
74          return idx;
75      }
76  
77      void release() {
78          assert !released;
79          released = true;
80          for (MsgHdrMemory hdr: hdrs) {
81              hdr.release();
82          }
83      }
84  
85      int capacity() {
86          return capacity;
87      }
88  }