-
-
Notifications
You must be signed in to change notification settings - Fork 15.8k
/
AdaptivePoolingAllocator.java
1393 lines (1202 loc) · 46.6 KB
/
AdaptivePoolingAllocator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2022 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.buffer;
import io.netty.util.ByteProcessor;
import io.netty.util.NettyRuntime;
import io.netty.util.ReferenceCounted;
import io.netty.util.concurrent.FastThreadLocalThread;
import io.netty.util.internal.ObjectPool;
import io.netty.util.internal.ObjectUtil;
import io.netty.util.internal.PlatformDependent;
import io.netty.util.internal.SystemPropertyUtil;
import io.netty.util.internal.UnstableApi;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.FileChannel;
import java.nio.channels.GatheringByteChannel;
import java.nio.channels.ScatteringByteChannel;
import java.util.Arrays;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import java.util.concurrent.locks.StampedLock;
/**
* An auto-tuning pooling allocator, that follows an anti-generational hypothesis.
* <p>
* The allocator is organized into a list of Magazines, and each magazine has a chunk-buffer that they allocate buffers
* from.
* <p>
* The magazines hold the mutexes that ensure the thread-safety of the allocator, and each thread picks a magazine
* based on the id of the thread. This spreads the contention of multi-threaded access across the magazines.
* If contention is detected above a certain threshold, the number of magazines are increased in response to the
* contention.
* <p>
* The magazines maintain histograms of the sizes of the allocations they do. The histograms are used to compute the
* preferred chunk size. The preferred chunk size is one that is big enough to service 10 allocations of the
* 99-percentile size. This way, the chunk size is adapted to the allocation patterns.
* <p>
* Computing the preferred chunk size is a somewhat expensive operation. Therefore, the frequency with which this is
* done, is also adapted to the allocation pattern. If a newly computed preferred chunk is the same as the previous
* preferred chunk size, then the frequency is reduced. Otherwise, the frequency is increased.
* <p>
* This allows the allocator to quickly respond to changes in the application workload,
* without suffering undue overhead from maintaining its statistics.
* <p>
* Since magazines are "relatively thread-local", the allocator has a central queue that allow excess chunks from any
* magazine, to be shared with other magazines.
* The {@link #createSharedChunkQueue()} method can be overridden to customize this queue.
*/
@UnstableApi
final class AdaptivePoolingAllocator {
private static final int RETIRE_CAPACITY = 4 * 1024;
private static final int MIN_CHUNK_SIZE = 128 * 1024;
private static final int MAX_STRIPES = NettyRuntime.availableProcessors() * 2;
private static final int BUFS_PER_CHUNK = 10; // For large buffers, aim to have about this many buffers per chunk.
/**
* The maximum size of a pooled chunk, in bytes. Allocations bigger than this will never be pooled.
* <p>
* This number is 10 MiB, and is derived from the limitations of internal histograms.
*/
private static final int MAX_CHUNK_SIZE =
BUFS_PER_CHUNK * (1 << AllocationStatistics.HISTO_MAX_BUCKET_SHIFT); // 10 MiB.
/**
* The capacity if the central queue that allow chunks to be shared across magazines.
* The default size is {@link NettyRuntime#availableProcessors()},
* and the maximum number of magazines is twice this.
* <p>
* This means the maximum amount of memory that we can have allocated-but-not-in-use is
* 5 * {@link NettyRuntime#availableProcessors()} * {@link #MAX_CHUNK_SIZE} bytes.
*/
private static final int CENTRAL_QUEUE_CAPACITY = SystemPropertyUtil.getInt(
"io.netty.allocator.centralQueueCapacity", NettyRuntime.availableProcessors());
private final ChunkAllocator chunkAllocator;
private final Queue<ChunkByteBuf> centralQueue;
private final StampedLock magazineExpandLock;
private volatile Magazine[] magazines;
AdaptivePoolingAllocator(ChunkAllocator chunkAllocator) {
this.chunkAllocator = chunkAllocator;
centralQueue = ObjectUtil.checkNotNull(createSharedChunkQueue(), "centralQueue");
magazineExpandLock = new StampedLock();
Magazine[] mags = new Magazine[4];
for (int i = 0; i < mags.length; i++) {
mags[i] = new Magazine(this);
}
magazines = mags;
}
/**
* Create a thread-safe multi-producer, multi-consumer queue to hold chunks that spill over from the
* internal Magazines.
* <p>
* Each Magazine can only hold two chunks at any one time: the chunk it currently allocates from,
* and the next-in-line chunk which will be used for allocation once the current one has been used up.
* This queue will be used by magazines to share any excess chunks they allocate, so that they don't need to
* allocate new chunks when their current and next-in-line chunks have both been used up.
* <p>
* The simplest implementation of this method is to return a new {@link ConcurrentLinkedQueue}.
* However, the {@code CLQ} is unbounded, and this means there's no limit to how many chunks can be cached in this
* queue.
* <p>
* Each chunk in this queue can be up to {@link #MAX_CHUNK_SIZE} in size, so it is recommended to use a bounded
* queue to limit the maximum memory usage.
* <p>
* The default implementation will create a bounded queue with a capacity of {@link #CENTRAL_QUEUE_CAPACITY}.
*
* @return A new multi-producer, multi-consumer queue.
*/
private static Queue<ChunkByteBuf> createSharedChunkQueue() {
return PlatformDependent.newFixedMpmcQueue(CENTRAL_QUEUE_CAPACITY);
}
ByteBuf allocate(int size, int maxCapacity) {
if (size <= MAX_CHUNK_SIZE) {
Thread currentThread = Thread.currentThread();
boolean willCleanupFastThreadLocals = FastThreadLocalThread.willCleanupFastThreadLocals(currentThread);
AdaptiveByteBuf buf = AdaptiveByteBuf.newInstance(willCleanupFastThreadLocals);
AdaptiveByteBuf result = allocate(size, maxCapacity, currentThread, buf);
if (result != null) {
return result;
}
// Return the buffer we pulled from the recycler but didn't use.
buf.release();
}
// The magazines failed us, or the buffer is too big to be pooled.
return chunkAllocator.allocate(size, maxCapacity);
}
private AdaptiveByteBuf allocate(int size, int maxCapacity, Thread currentThread, AdaptiveByteBuf buf) {
long threadId = currentThread.getId();
int sizeBucket = AllocationStatistics.sizeBucket(size); // Compute outside of Magazine lock for better ILP.
Magazine[] mags;
int expansions = 0;
do {
mags = magazines;
int mask = mags.length - 1;
int index = (int) (threadId & mask);
for (int i = 0, m = Integer.numberOfTrailingZeros(~mask); i < m; i++) {
Magazine mag = mags[index + i & mask];
long writeLock = mag.tryWriteLock();
if (writeLock != 0) {
try {
return mag.allocate(size, sizeBucket, maxCapacity, buf);
} finally {
mag.unlockWrite(writeLock);
}
}
}
expansions++;
} while (expansions <= 3 && tryExpandMagazines(mags.length));
return null;
}
/**
* Allocate into the given buffer. Used by {@link AdaptiveByteBuf#capacity(int)}.
*/
void allocate(int size, int maxCapacity, AdaptiveByteBuf into) {
Magazine magazine = into.chunk.magazine;
AdaptiveByteBuf result = allocate(size, maxCapacity, Thread.currentThread(), into);
if (result == null) {
// Create a one-off chunk for this allocation.
AbstractByteBuf innerChunk = (AbstractByteBuf) chunkAllocator.allocate(size, maxCapacity);
ChunkByteBuf chunk = new ChunkByteBuf(innerChunk, magazine, false);
chunk.readInitInto(into, size, maxCapacity);
}
}
long usedMemory() {
long sum = 0;
for (ByteBuf byteBuf : centralQueue) {
sum += byteBuf.capacity();
}
for (Magazine magazine : magazines) {
sum += magazine.usedMemory.get();
}
return sum;
}
private boolean tryExpandMagazines(int currentLength) {
if (currentLength >= MAX_STRIPES) {
return true;
}
long writeLock = magazineExpandLock.tryWriteLock();
if (writeLock != 0) {
try {
Magazine[] mags = magazines;
if (mags.length >= MAX_STRIPES || mags.length > currentLength) {
return true;
}
Magazine[] expanded = Arrays.copyOf(mags, mags.length * 2);
for (int i = mags.length, m = expanded.length; i < m; i++) {
expanded[i] = new Magazine(this);
}
magazines = expanded;
} finally {
magazineExpandLock.unlockWrite(writeLock);
}
}
return true;
}
private boolean offerToQueue(ChunkByteBuf buffer) {
return centralQueue.offer(buffer);
}
@SuppressWarnings("checkstyle:finalclass") // Checkstyle mistakenly believes this class should be final.
private static class AllocationStatistics extends StampedLock {
private static final long serialVersionUID = -8319929980932269688L;
private static final int MIN_DATUM_TARGET = 1024;
private static final int MAX_DATUM_TARGET = 65534;
private static final int INIT_DATUM_TARGET = 8192;
private static final int HISTO_MIN_BUCKET_SHIFT = 13; // Smallest bucket is 1 << 13 = 8192 bytes in size.
private static final int HISTO_MAX_BUCKET_SHIFT = 20; // Biggest bucket is 1 << 20 = 1 MiB bytes in size.
private static final int HISTO_BUCKET_COUNT = 1 + HISTO_MAX_BUCKET_SHIFT - HISTO_MIN_BUCKET_SHIFT; // 8 buckets.
private static final int HISTO_MAX_BUCKET_MASK = HISTO_BUCKET_COUNT - 1;
protected final AdaptivePoolingAllocator parent;
private final short[][] histos = {
new short[HISTO_BUCKET_COUNT], new short[HISTO_BUCKET_COUNT],
new short[HISTO_BUCKET_COUNT], new short[HISTO_BUCKET_COUNT],
};
private short[] histo = histos[0];
private final int[] sums = new int[HISTO_BUCKET_COUNT];
private int histoIndex;
private int datumCount;
private int datumTarget = INIT_DATUM_TARGET;
private volatile int sharedPrefChunkSize = MIN_CHUNK_SIZE;
protected volatile int localPrefChunkSize = MIN_CHUNK_SIZE;
private AllocationStatistics(AdaptivePoolingAllocator parent) {
this.parent = parent;
}
protected void recordAllocationSize(int bucket) {
histo[bucket]++;
if (datumCount++ == datumTarget) {
rotateHistograms();
}
}
static int sizeBucket(int size) {
// Minimum chunk size is 128 KiB. We'll only make bigger chunks if the 99-percentile is 16 KiB or greater,
// so we truncate and roll up the bottom part of the histogram to 8 KiB.
// The upper size band is 1 MiB, and that gives us exactly 8 size buckets,
// which is a magical number for JIT optimisations.
int normalizedSize = size - 1 >> HISTO_MIN_BUCKET_SHIFT & HISTO_MAX_BUCKET_MASK;
return Integer.SIZE - Integer.numberOfLeadingZeros(normalizedSize);
}
private void rotateHistograms() {
short[][] hs = histos;
for (int i = 0; i < HISTO_BUCKET_COUNT; i++) {
sums[i] = (hs[0][i] & 0xFFFF) + (hs[1][i] & 0xFFFF) + (hs[2][i] & 0xFFFF) + (hs[3][i] & 0xFFFF);
}
int sum = 0;
for (int count : sums) {
sum += count;
}
int targetPercentile = (int) (sum * 0.99);
int sizeBucket = 0;
for (; sizeBucket < sums.length; sizeBucket++) {
if (sums[sizeBucket] > targetPercentile) {
break;
}
targetPercentile -= sums[sizeBucket];
}
int percentileSize = 1 << sizeBucket + HISTO_MIN_BUCKET_SHIFT;
int prefChunkSize = Math.max(percentileSize * BUFS_PER_CHUNK, MIN_CHUNK_SIZE);
localPrefChunkSize = prefChunkSize;
for (Magazine mag : parent.magazines) {
prefChunkSize = Math.max(prefChunkSize, mag.localPrefChunkSize);
}
if (sharedPrefChunkSize != prefChunkSize) {
// Preferred chunk size changed. Increase check frequency.
datumTarget = Math.max(datumTarget >> 1, MIN_DATUM_TARGET);
sharedPrefChunkSize = prefChunkSize;
} else {
// Preferred chunk size did not change. Check less often.
datumTarget = Math.min(datumTarget << 1, MAX_DATUM_TARGET);
}
histoIndex = histoIndex + 1 & 3;
histo = histos[histoIndex];
datumCount = 0;
Arrays.fill(histo, (short) 0);
}
/**
* Get the preferred chunk size, based on statistics from the {@linkplain #recordAllocationSize(int) recorded}
* allocation sizes.
* <p>
* This method must be thread-safe.
*
* @return The currently preferred chunk allocation size.
*/
protected int preferredChunkSize() {
return sharedPrefChunkSize;
}
}
private static final class Magazine extends AllocationStatistics {
private static final long serialVersionUID = -4068223712022528165L;
private static final AtomicReferenceFieldUpdater<Magazine, ChunkByteBuf> NEXT_IN_LINE;
static {
NEXT_IN_LINE = AtomicReferenceFieldUpdater.newUpdater(Magazine.class, ChunkByteBuf.class, "nextInLine");
}
private ChunkByteBuf current;
@SuppressWarnings("unused") // updated via NEXT_IN_LINE
private volatile ChunkByteBuf nextInLine;
private final AtomicLong usedMemory;
Magazine(AdaptivePoolingAllocator parent) {
super(parent);
usedMemory = new AtomicLong();
}
public AdaptiveByteBuf allocate(int size, int sizeBucket, int maxCapacity, AdaptiveByteBuf buf) {
recordAllocationSize(sizeBucket);
ChunkByteBuf curr = current;
if (curr != null && curr.readableBytes() >= size) {
if (curr.readableBytes() == size) {
current = null;
try {
return curr.readInitInto(buf, size, maxCapacity);
} finally {
curr.release();
}
}
return curr.readInitInto(buf, size, maxCapacity);
}
if (curr != null) {
curr.release();
}
if (nextInLine != null) {
curr = NEXT_IN_LINE.getAndSet(this, null);
} else {
curr = parent.centralQueue.poll();
if (curr == null) {
curr = newChunkAllocation(size);
}
}
current = curr;
final AdaptiveByteBuf result;
if (curr.readableBytes() > size) {
result = curr.readInitInto(buf, size, maxCapacity);
} else if (curr.readableBytes() == size) {
result = curr.readInitInto(buf, size, maxCapacity);
curr.release();
current = null;
} else {
ChunkByteBuf newChunk = newChunkAllocation(size);
result = newChunk.readInitInto(buf, size, maxCapacity);
if (curr.readableBytes() < RETIRE_CAPACITY) {
curr.release();
current = newChunk;
} else if (!(boolean) NEXT_IN_LINE.compareAndSet(this, null, newChunk)) {
if (!parent.offerToQueue(newChunk)) {
// Next-in-line is occupied AND the central queue is full.
// Rare that we should get here, but we'll only do one allocation out of this chunk, then.
newChunk.release();
}
}
}
return result;
}
private ChunkByteBuf newChunkAllocation(int promptingSize) {
int size = Math.max(promptingSize * BUFS_PER_CHUNK, preferredChunkSize());
ChunkAllocator chunkAllocator = parent.chunkAllocator;
ChunkByteBuf chunk = new ChunkByteBuf((AbstractByteBuf) chunkAllocator.allocate(size, size), this, true);
chunk.writerIndex(size);
return chunk;
}
boolean trySetNextInLine(ChunkByteBuf buffer) {
return NEXT_IN_LINE.compareAndSet(this, null, buffer);
}
}
private static final class ChunkByteBuf extends AbstractReferenceCountedByteBuf {
private final AbstractByteBuf delegate;
private final Magazine magazine;
private final boolean pooled;
ChunkByteBuf(AbstractByteBuf delegate, Magazine magazine, boolean pooled) {
super(delegate.maxCapacity());
this.delegate = delegate;
this.magazine = magazine;
this.pooled = pooled;
magazine.usedMemory.getAndAdd(capacity());
}
@Override
protected void deallocate() {
Magazine mag = magazine;
AdaptivePoolingAllocator parent = mag.parent;
int chunkSize = mag.preferredChunkSize();
int memSize = delegate.capacity();
if (!pooled || memSize < chunkSize || memSize > chunkSize + (chunkSize >> 1)) {
// Drop the chunk if the parent allocator is closed, or if the chunk is smaller than the
// preferred chunk size, or over 50% larger than the preferred chunk size.
mag.usedMemory.getAndAdd(-capacity());
delegate.release();
} else {
resetRefCnt();
delegate.setIndex(0, 0);
setIndex(0, capacity());
if (!mag.trySetNextInLine(this)) {
if (!parent.offerToQueue(this)) {
// The central queue is full. Drop the memory with the original Drop instance.
delegate.release();
}
}
}
}
public AdaptiveByteBuf readInitInto(AdaptiveByteBuf buf, int size, int maxCapacity) {
int startIndex = readerIndex();
skipBytes(size);
buf.init(delegate, this, 0, 0, startIndex, size, maxCapacity);
return buf;
}
@Override
protected byte _getByte(int index) {
return delegate._getByte(index);
}
@Override
protected short _getShort(int index) {
return delegate._getShort(index);
}
@Override
protected short _getShortLE(int index) {
return delegate._getShortLE(index);
}
@Override
protected int _getUnsignedMedium(int index) {
return delegate._getUnsignedMedium(index);
}
@Override
protected int _getUnsignedMediumLE(int index) {
return delegate._getUnsignedMediumLE(index);
}
@Override
protected int _getInt(int index) {
return delegate._getInt(index);
}
@Override
protected int _getIntLE(int index) {
return delegate._getIntLE(index);
}
@Override
protected long _getLong(int index) {
return delegate._getLong(index);
}
@Override
protected long _getLongLE(int index) {
return delegate._getLongLE(index);
}
@Override
protected void _setByte(int index, int value) {
delegate._setByte(index, value);
}
@Override
protected void _setShort(int index, int value) {
delegate._setShort(index, value);
}
@Override
protected void _setShortLE(int index, int value) {
delegate._setShortLE(index, value);
}
@Override
protected void _setMedium(int index, int value) {
delegate._setMedium(index, value);
}
@Override
protected void _setMediumLE(int index, int value) {
delegate._setMediumLE(index, value);
}
@Override
protected void _setInt(int index, int value) {
delegate._setInt(index, value);
}
@Override
protected void _setIntLE(int index, int value) {
delegate._setIntLE(index, value);
}
@Override
protected void _setLong(int index, long value) {
delegate._setLong(index, value);
}
@Override
protected void _setLongLE(int index, long value) {
delegate._setLongLE(index, value);
}
@Override
public int capacity() {
return delegate.capacity();
}
@Override
public ByteBuf capacity(int newCapacity) {
delegate.capacity(newCapacity);
return this;
}
@Override
public ByteBufAllocator alloc() {
return delegate.alloc();
}
@Override
public ByteOrder order() {
return delegate.order();
}
@Override
public ByteBuf unwrap() {
return delegate;
}
@Override
public boolean isDirect() {
return delegate.isDirect();
}
@Override
public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) {
delegate.getBytes(index, dst, dstIndex, length);
return this;
}
@Override
public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) {
delegate.getBytes(index, dst, dstIndex, length);
return this;
}
@Override
public ByteBuf getBytes(int index, ByteBuffer dst) {
delegate.getBytes(index, dst);
return this;
}
@Override
public ByteBuf getBytes(int index, OutputStream out, int length) throws IOException {
delegate.getBytes(index, out, length);
return this;
}
@Override
public int getBytes(int index, GatheringByteChannel out, int length) throws IOException {
return delegate.getBytes(index, out, length);
}
@Override
public int getBytes(int index, FileChannel out, long position, int length) throws IOException {
return delegate.getBytes(index, out, position, length);
}
@Override
public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) {
delegate.setBytes(index, src, srcIndex, length);
return this;
}
@Override
public ByteBuf setBytes(int index, byte[] src, int srcIndex, int length) {
delegate.setBytes(index, src, srcIndex, length);
return this;
}
@Override
public ByteBuf setBytes(int index, ByteBuffer src) {
delegate.setBytes(index, src);
return this;
}
@Override
public int setBytes(int index, InputStream in, int length) throws IOException {
return delegate.setBytes(index, in, length);
}
@Override
public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException {
return delegate.setBytes(index, in, length);
}
@Override
public int setBytes(int index, FileChannel in, long position, int length) throws IOException {
return delegate.setBytes(index, in, position, length);
}
@Override
public ByteBuf copy(int index, int length) {
return delegate.copy(index, length);
}
@Override
public int nioBufferCount() {
return delegate.nioBufferCount();
}
@Override
public ByteBuffer nioBuffer(int index, int length) {
return delegate.nioBuffer(index, length);
}
@Override
public ByteBuffer internalNioBuffer(int index, int length) {
return delegate.internalNioBuffer(index, length);
}
@Override
public ByteBuffer[] nioBuffers(int index, int length) {
return delegate.nioBuffers(index, length);
}
@Override
public boolean hasArray() {
return delegate.hasArray();
}
@Override
public byte[] array() {
return delegate.array();
}
@Override
public int arrayOffset() {
return delegate.arrayOffset();
}
@Override
public boolean hasMemoryAddress() {
return delegate.hasMemoryAddress();
}
@Override
public long memoryAddress() {
return delegate.memoryAddress();
}
}
static final class AdaptiveByteBuf extends AbstractReferenceCountedByteBuf {
static final ObjectPool<AdaptiveByteBuf> RECYCLER = ObjectPool.newPool(
new ObjectPool.ObjectCreator<AdaptiveByteBuf>() {
@Override
public AdaptiveByteBuf newObject(ObjectPool.Handle<AdaptiveByteBuf> handle) {
return new AdaptiveByteBuf(handle);
}
});
static AdaptiveByteBuf newInstance(boolean useThreadLocal) {
if (useThreadLocal) {
AdaptiveByteBuf buf = RECYCLER.get();
buf.resetRefCnt();
buf.discardMarks();
return buf;
}
return new AdaptiveByteBuf(null);
}
private final ObjectPool.Handle<AdaptiveByteBuf> handle;
int adjustment;
private AbstractByteBuf rootParent;
private ChunkByteBuf chunk;
private int length;
private ByteBuffer tmpNioBuf;
AdaptiveByteBuf(ObjectPool.Handle<AdaptiveByteBuf> recyclerHandle) {
super(0);
handle = recyclerHandle;
}
void init(AbstractByteBuf unwrapped, ChunkByteBuf wrapped, int readerIndex, int writerIndex,
int adjustment, int capacity, int maxCapacity) {
wrapped.retain();
this.adjustment = adjustment;
chunk = wrapped;
length = capacity;
maxCapacity(maxCapacity);
setIndex0(readerIndex, writerIndex);
rootParent = unwrapped;
tmpNioBuf = rootParent.internalNioBuffer(adjustment, capacity).slice();
}
@Override
public int capacity() {
return length;
}
@Override
public ByteBuf capacity(int newCapacity) {
if (newCapacity == capacity()) {
ensureAccessible();
return this;
}
checkNewCapacity(newCapacity);
if (newCapacity < capacity()) {
length = newCapacity;
setIndex0(Math.min(readerIndex(), newCapacity), Math.min(writerIndex(), newCapacity));
return this;
}
// Reallocation required.
ByteBuffer data = tmpNioBuf;
data.clear();
tmpNioBuf = null;
ChunkByteBuf chunk = this.chunk;
Magazine magazine = chunk.magazine;
AdaptivePoolingAllocator allocator = magazine.parent;
int readerIndex = this.readerIndex;
int writerIndex = this.writerIndex;
allocator.allocate(newCapacity, maxCapacity(), this);
tmpNioBuf.put(data);
tmpNioBuf.clear();
chunk.release();
this.readerIndex = readerIndex;
this.writerIndex = writerIndex;
return this;
}
@Override
public ByteBufAllocator alloc() {
return rootParent.alloc();
}
@Override
public ByteOrder order() {
return rootParent.order();
}
@Override
public ByteBuf unwrap() {
return null;
}
@Override
public boolean isDirect() {
return rootParent.isDirect();
}
@Override
public int arrayOffset() {
return idx(rootParent.arrayOffset());
}
@Override
public boolean hasMemoryAddress() {
return rootParent.hasMemoryAddress();
}
@Override
public long memoryAddress() {
ensureAccessible();
return rootParent.memoryAddress() + adjustment;
}
@Override
public ByteBuffer nioBuffer(int index, int length) {
checkIndex(index, length);
return rootParent.nioBuffer(idx(index), length);
}
@Override
public ByteBuffer internalNioBuffer(int index, int length) {
checkIndex(index, length);
return (ByteBuffer) internalNioBuffer().position(index).limit(index + length);
}
private ByteBuffer internalNioBuffer() {
return (ByteBuffer) tmpNioBuf.clear();
}
@Override
public ByteBuffer[] nioBuffers(int index, int length) {
checkIndex(index, length);
return rootParent.nioBuffers(idx(index), length);
}
@Override
public boolean hasArray() {
return rootParent.hasArray();
}
@Override
public byte[] array() {
return rootParent.array();
}
@Override
public ByteBuf copy(int index, int length) {
checkIndex(index, length);
return rootParent.copy(idx(index), length);
}
@Override
public ByteBuf slice(int index, int length) {
checkIndex(index, length);
return new PooledNonRetainedSlicedByteBuf(this, rootParent, idx(index), length);
}
@Override
public ByteBuf retainedSlice(int index, int length) {
return slice(index, length).retain();
}
@Override
public ByteBuf duplicate() {
ensureAccessible();
return new PooledNonRetainedDuplicateByteBuf(this, this).setIndex(readerIndex(), writerIndex());
}
@Override
public ByteBuf retainedDuplicate() {
return duplicate().retain();
}
@Override
public int nioBufferCount() {
return rootParent.nioBufferCount();
}
@Override
public byte getByte(int index) {
checkIndex(index, 1);
return rootParent.getByte(idx(index));
}
@Override
protected byte _getByte(int index) {
return rootParent._getByte(idx(index));
}
@Override
public short getShort(int index) {
checkIndex(index, 2);
return rootParent.getShort(idx(index));
}
@Override
protected short _getShort(int index) {
return rootParent._getShort(idx(index));
}
@Override
public short getShortLE(int index) {
checkIndex(index, 2);
return rootParent.getShortLE(idx(index));
}
@Override
protected short _getShortLE(int index) {
return rootParent._getShortLE(idx(index));
}
@Override
public int getUnsignedMedium(int index) {
checkIndex(index, 3);
return rootParent.getUnsignedMedium(idx(index));
}
@Override
protected int _getUnsignedMedium(int index) {
return rootParent._getUnsignedMedium(idx(index));
}
@Override
public int getUnsignedMediumLE(int index) {
checkIndex(index, 3);
return rootParent.getUnsignedMediumLE(idx(index));
}
@Override
protected int _getUnsignedMediumLE(int index) {
return rootParent._getUnsignedMediumLE(idx(index));
}
@Override
public int getInt(int index) {
checkIndex(index, 4);
return rootParent.getInt(idx(index));
}
@Override
protected int _getInt(int index) {
return rootParent._getInt(idx(index));
}
@Override
public int getIntLE(int index) {
checkIndex(index, 4);
return rootParent.getIntLE(idx(index));
}
@Override
protected int _getIntLE(int index) {
return rootParent._getIntLE(idx(index));
}
@Override
public long getLong(int index) {
checkIndex(index, 8);
return rootParent.getLong(idx(index));
}
@Override
protected long _getLong(int index) {
return rootParent._getLong(idx(index));
}
@Override
public long getLongLE(int index) {
checkIndex(index, 8);
return rootParent.getLongLE(idx(index));
}
@Override
protected long _getLongLE(int index) {
return rootParent._getLongLE(idx(index));
}
@Override
public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) {
checkIndex(index, length);
rootParent.getBytes(idx(index), dst, dstIndex, length);
return this;
}
@Override
public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) {
checkIndex(index, length);
rootParent.getBytes(idx(index), dst, dstIndex, length);
return this;
}
@Override
public ByteBuf getBytes(int index, ByteBuffer dst) {
checkIndex(index, dst.remaining());
rootParent.getBytes(idx(index), dst);
return this;
}
@Override
public ByteBuf setByte(int index, int value) {
checkIndex(index, 1);
rootParent.setByte(idx(index), value);
return this;
}
@Override
protected void _setByte(int index, int value) {
rootParent._setByte(idx(index), value);
}