1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 }