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