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 = -1;
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      MsgHdrMemory nextHdr() {
41          if (idx == hdrs.length) {
42              return null;
43          }
44          return hdrs[idx++];
45      }
46  
47      MsgHdrMemory hdr(int idx) {
48          return hdrs[idx];
49      }
50  
51      long id(int idx) {
52          return ids[idx];
53      }
54  
55      void setId(int idx, long id) {
56          ids[idx] = id;
57      }
58  
59      void clear() {
60          Arrays.fill(ids, 0, idx, NO_ID);
61          idx = 0;
62      }
63  
64      int length() {
65          return idx;
66      }
67  
68      void release() {
69          assert !released;
70          released = true;
71          for (MsgHdrMemory hdr: hdrs) {
72              hdr.release();
73          }
74      }
75  
76      int capacity() {
77          return capacity;
78      }
79  }