1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.channel.uring;
17
18 import io.netty.util.internal.ObjectUtil;
19
20 import java.util.Objects;
21
22
23
24
25
26 public final class IoUringBufferRingConfig {
27 private final short bgId;
28 private final short bufferRingSize;
29 private final int batchSize;
30 private final int maxUnreleasedBuffers;
31 private final boolean incremental;
32 private final IoUringBufferRingAllocator allocator;
33
34
35
36
37
38
39
40
41
42
43
44
45 public IoUringBufferRingConfig(short bgId, short bufferRingSize, int maxUnreleasedBuffers,
46 IoUringBufferRingAllocator allocator) {
47 this(bgId, bufferRingSize, bufferRingSize / 2, maxUnreleasedBuffers,
48 IoUring.isRegisterBufferRingIncSupported(), allocator);
49 }
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65 public IoUringBufferRingConfig(short bgId, short bufferRingSize, int batchSize, int maxUnreleasedBuffers,
66 boolean incremental, IoUringBufferRingAllocator allocator) {
67 this.bgId = (short) ObjectUtil.checkPositiveOrZero(bgId, "bgId");
68 this.bufferRingSize = checkBufferRingSize(bufferRingSize);
69 this.batchSize = ObjectUtil.checkInRange(batchSize, 1, bufferRingSize, "batchSize");
70 this.maxUnreleasedBuffers = ObjectUtil.checkInRange(
71 maxUnreleasedBuffers, bufferRingSize, Integer.MAX_VALUE, "maxUnreleasedBuffers");
72 if (incremental && !IoUring.isRegisterBufferRingIncSupported()) {
73 throw new IllegalArgumentException("Incremental buffer ring is not supported");
74 }
75 this.incremental = incremental;
76 this.allocator = ObjectUtil.checkNotNull(allocator, "allocator");
77 }
78
79
80
81
82
83
84 public short bufferGroupId() {
85 return bgId;
86 }
87
88
89
90
91
92
93 public short bufferRingSize() {
94 return bufferRingSize;
95 }
96
97
98
99
100
101
102 public int batchSize() {
103 return batchSize;
104 }
105
106
107
108
109
110
111
112 public int maxUnreleasedBuffers() {
113 return maxUnreleasedBuffers;
114 }
115
116
117
118
119
120
121 public IoUringBufferRingAllocator allocator() {
122 return allocator;
123 }
124
125
126
127
128
129
130
131
132 public boolean isIncremental() {
133 return incremental;
134 }
135
136 private static short checkBufferRingSize(short bufferRingSize) {
137 if (bufferRingSize < 1) {
138 throw new IllegalArgumentException("bufferRingSize: " + bufferRingSize + " (expected: > 0)");
139 }
140
141 boolean isPowerOfTwo = (bufferRingSize & (bufferRingSize - 1)) == 0;
142 if (!isPowerOfTwo) {
143 throw new IllegalArgumentException("bufferRingSize: " + bufferRingSize + " (expected: power of 2)");
144 }
145 return bufferRingSize;
146 }
147
148 @Override
149 public boolean equals(Object o) {
150 if (o == null || getClass() != o.getClass()) {
151 return false;
152 }
153 IoUringBufferRingConfig that = (IoUringBufferRingConfig) o;
154 return bgId == that.bgId;
155 }
156
157 @Override
158 public int hashCode() {
159 return Objects.hashCode(bgId);
160 }
161 }