View Javadoc
1   /*
2    * Copyright 2012 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    *   http://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  
17  package io.netty.buffer;
18  
19  import io.netty.util.internal.LongCounter;
20  import io.netty.util.internal.PlatformDependent;
21  import io.netty.util.internal.StringUtil;
22  
23  import java.nio.ByteBuffer;
24  import java.util.ArrayList;
25  import java.util.Collections;
26  import java.util.List;
27  import java.util.concurrent.atomic.AtomicInteger;
28  
29  import static java.lang.Math.max;
30  
31  abstract class PoolArena<T> implements PoolArenaMetric {
32      static final boolean HAS_UNSAFE = PlatformDependent.hasUnsafe();
33  
34      enum SizeClass {
35          Tiny,
36          Small,
37          Normal
38      }
39  
40      static final int numTinySubpagePools = 512 >>> 4;
41  
42      final PooledByteBufAllocator parent;
43  
44      private final int maxOrder;
45      final int pageSize;
46      final int pageShifts;
47      final int chunkSize;
48      final int subpageOverflowMask;
49      final int numSmallSubpagePools;
50      final int directMemoryCacheAlignment;
51      final int directMemoryCacheAlignmentMask;
52      private final PoolSubpage<T>[] tinySubpagePools;
53      private final PoolSubpage<T>[] smallSubpagePools;
54  
55      private final PoolChunkList<T> q050;
56      private final PoolChunkList<T> q025;
57      private final PoolChunkList<T> q000;
58      private final PoolChunkList<T> qInit;
59      private final PoolChunkList<T> q075;
60      private final PoolChunkList<T> q100;
61  
62      private final List<PoolChunkListMetric> chunkListMetrics;
63  
64      // Metrics for allocations and deallocations
65      private long allocationsNormal;
66      // We need to use the LongCounter here as this is not guarded via synchronized block.
67      private final LongCounter allocationsTiny = PlatformDependent.newLongCounter();
68      private final LongCounter allocationsSmall = PlatformDependent.newLongCounter();
69      private final LongCounter allocationsHuge = PlatformDependent.newLongCounter();
70      private final LongCounter activeBytesHuge = PlatformDependent.newLongCounter();
71  
72      private long deallocationsTiny;
73      private long deallocationsSmall;
74      private long deallocationsNormal;
75  
76      // We need to use the LongCounter here as this is not guarded via synchronized block.
77      private final LongCounter deallocationsHuge = PlatformDependent.newLongCounter();
78  
79      // Number of thread caches backed by this arena.
80      final AtomicInteger numThreadCaches = new AtomicInteger();
81  
82      // TODO: Test if adding padding helps under contention
83      //private long pad0, pad1, pad2, pad3, pad4, pad5, pad6, pad7;
84  
85      protected PoolArena(PooledByteBufAllocator parent, int pageSize,
86            int maxOrder, int pageShifts, int chunkSize, int cacheAlignment) {
87          this.parent = parent;
88          this.pageSize = pageSize;
89          this.maxOrder = maxOrder;
90          this.pageShifts = pageShifts;
91          this.chunkSize = chunkSize;
92          directMemoryCacheAlignment = cacheAlignment;
93          directMemoryCacheAlignmentMask = cacheAlignment - 1;
94          subpageOverflowMask = ~(pageSize - 1);
95          tinySubpagePools = newSubpagePoolArray(numTinySubpagePools);
96          for (int i = 0; i < tinySubpagePools.length; i ++) {
97              tinySubpagePools[i] = newSubpagePoolHead(pageSize);
98          }
99  
100         numSmallSubpagePools = pageShifts - 9;
101         smallSubpagePools = newSubpagePoolArray(numSmallSubpagePools);
102         for (int i = 0; i < smallSubpagePools.length; i ++) {
103             smallSubpagePools[i] = newSubpagePoolHead(pageSize);
104         }
105 
106         q100 = new PoolChunkList<T>(this, null, 100, Integer.MAX_VALUE, chunkSize);
107         q075 = new PoolChunkList<T>(this, q100, 75, 100, chunkSize);
108         q050 = new PoolChunkList<T>(this, q075, 50, 100, chunkSize);
109         q025 = new PoolChunkList<T>(this, q050, 25, 75, chunkSize);
110         q000 = new PoolChunkList<T>(this, q025, 1, 50, chunkSize);
111         qInit = new PoolChunkList<T>(this, q000, Integer.MIN_VALUE, 25, chunkSize);
112 
113         q100.prevList(q075);
114         q075.prevList(q050);
115         q050.prevList(q025);
116         q025.prevList(q000);
117         q000.prevList(null);
118         qInit.prevList(qInit);
119 
120         List<PoolChunkListMetric> metrics = new ArrayList<PoolChunkListMetric>(6);
121         metrics.add(qInit);
122         metrics.add(q000);
123         metrics.add(q025);
124         metrics.add(q050);
125         metrics.add(q075);
126         metrics.add(q100);
127         chunkListMetrics = Collections.unmodifiableList(metrics);
128     }
129 
130     private PoolSubpage<T> newSubpagePoolHead(int pageSize) {
131         PoolSubpage<T> head = new PoolSubpage<T>(pageSize);
132         head.prev = head;
133         head.next = head;
134         return head;
135     }
136 
137     @SuppressWarnings("unchecked")
138     private PoolSubpage<T>[] newSubpagePoolArray(int size) {
139         return new PoolSubpage[size];
140     }
141 
142     abstract boolean isDirect();
143 
144     PooledByteBuf<T> allocate(PoolThreadCache cache, int reqCapacity, int maxCapacity) {
145         PooledByteBuf<T> buf = newByteBuf(maxCapacity);
146         allocate(cache, buf, reqCapacity);
147         return buf;
148     }
149 
150     static int tinyIdx(int normCapacity) {
151         return normCapacity >>> 4;
152     }
153 
154     static int smallIdx(int normCapacity) {
155         int tableIdx = 0;
156         int i = normCapacity >>> 10;
157         while (i != 0) {
158             i >>>= 1;
159             tableIdx ++;
160         }
161         return tableIdx;
162     }
163 
164     // capacity < pageSize
165     boolean isTinyOrSmall(int normCapacity) {
166         return (normCapacity & subpageOverflowMask) == 0;
167     }
168 
169     // normCapacity < 512
170     static boolean isTiny(int normCapacity) {
171         return (normCapacity & 0xFFFFFE00) == 0;
172     }
173 
174     private void allocate(PoolThreadCache cache, PooledByteBuf<T> buf, final int reqCapacity) {
175         final int normCapacity = normalizeCapacity(reqCapacity);
176         if (isTinyOrSmall(normCapacity)) { // capacity < pageSize
177             int tableIdx;
178             PoolSubpage<T>[] table;
179             boolean tiny = isTiny(normCapacity);
180             if (tiny) { // < 512
181                 if (cache.allocateTiny(this, buf, reqCapacity, normCapacity)) {
182                     // was able to allocate out of the cache so move on
183                     return;
184                 }
185                 tableIdx = tinyIdx(normCapacity);
186                 table = tinySubpagePools;
187             } else {
188                 if (cache.allocateSmall(this, buf, reqCapacity, normCapacity)) {
189                     // was able to allocate out of the cache so move on
190                     return;
191                 }
192                 tableIdx = smallIdx(normCapacity);
193                 table = smallSubpagePools;
194             }
195 
196             final PoolSubpage<T> head = table[tableIdx];
197 
198             /**
199              * Synchronize on the head. This is needed as {@link PoolChunk#allocateSubpage(int)} and
200              * {@link PoolChunk#free(long)} may modify the doubly linked list as well.
201              */
202             synchronized (head) {
203                 final PoolSubpage<T> s = head.next;
204                 if (s != head) {
205                     assert s.doNotDestroy && s.elemSize == normCapacity;
206                     long handle = s.allocate();
207                     assert handle >= 0;
208                     s.chunk.initBufWithSubpage(buf, handle, reqCapacity);
209                     incTinySmallAllocation(tiny);
210                     return;
211                 }
212             }
213             synchronized (this) {
214                 allocateNormal(buf, reqCapacity, normCapacity);
215             }
216 
217             incTinySmallAllocation(tiny);
218             return;
219         }
220         if (normCapacity <= chunkSize) {
221             if (cache.allocateNormal(this, buf, reqCapacity, normCapacity)) {
222                 // was able to allocate out of the cache so move on
223                 return;
224             }
225             synchronized (this) {
226                 allocateNormal(buf, reqCapacity, normCapacity);
227                 ++allocationsNormal;
228             }
229         } else {
230             // Huge allocations are never served via the cache so just call allocateHuge
231             allocateHuge(buf, reqCapacity);
232         }
233     }
234 
235     // Method must be called inside synchronized(this) { ... } block
236     private void allocateNormal(PooledByteBuf<T> buf, int reqCapacity, int normCapacity) {
237         if (q050.allocate(buf, reqCapacity, normCapacity) || q025.allocate(buf, reqCapacity, normCapacity) ||
238             q000.allocate(buf, reqCapacity, normCapacity) || qInit.allocate(buf, reqCapacity, normCapacity) ||
239             q075.allocate(buf, reqCapacity, normCapacity)) {
240             return;
241         }
242 
243         // Add a new chunk.
244         PoolChunk<T> c = newChunk(pageSize, maxOrder, pageShifts, chunkSize);
245         long handle = c.allocate(normCapacity);
246         assert handle > 0;
247         c.initBuf(buf, handle, reqCapacity);
248         qInit.add(c);
249     }
250 
251     private void incTinySmallAllocation(boolean tiny) {
252         if (tiny) {
253             allocationsTiny.increment();
254         } else {
255             allocationsSmall.increment();
256         }
257     }
258 
259     private void allocateHuge(PooledByteBuf<T> buf, int reqCapacity) {
260         PoolChunk<T> chunk = newUnpooledChunk(reqCapacity);
261         activeBytesHuge.add(chunk.chunkSize());
262         buf.initUnpooled(chunk, reqCapacity);
263         allocationsHuge.increment();
264     }
265 
266     void free(PoolChunk<T> chunk, long handle, int normCapacity, PoolThreadCache cache) {
267         if (chunk.unpooled) {
268             int size = chunk.chunkSize();
269             destroyChunk(chunk);
270             activeBytesHuge.add(-size);
271             deallocationsHuge.increment();
272         } else {
273             SizeClass sizeClass = sizeClass(normCapacity);
274             if (cache != null && cache.add(this, chunk, handle, normCapacity, sizeClass)) {
275                 // cached so not free it.
276                 return;
277             }
278 
279             freeChunk(chunk, handle, sizeClass);
280         }
281     }
282 
283     private SizeClass sizeClass(int normCapacity) {
284         if (!isTinyOrSmall(normCapacity)) {
285             return SizeClass.Normal;
286         }
287         return isTiny(normCapacity) ? SizeClass.Tiny : SizeClass.Small;
288     }
289 
290     void freeChunk(PoolChunk<T> chunk, long handle, SizeClass sizeClass) {
291         final boolean destroyChunk;
292         synchronized (this) {
293             switch (sizeClass) {
294             case Normal:
295                 ++deallocationsNormal;
296                 break;
297             case Small:
298                 ++deallocationsSmall;
299                 break;
300             case Tiny:
301                 ++deallocationsTiny;
302                 break;
303             default:
304                 throw new Error();
305             }
306             destroyChunk = !chunk.parent.free(chunk, handle);
307         }
308         if (destroyChunk) {
309             // destroyChunk not need to be called while holding the synchronized lock.
310             destroyChunk(chunk);
311         }
312     }
313 
314     PoolSubpage<T> findSubpagePoolHead(int elemSize) {
315         int tableIdx;
316         PoolSubpage<T>[] table;
317         if (isTiny(elemSize)) { // < 512
318             tableIdx = elemSize >>> 4;
319             table = tinySubpagePools;
320         } else {
321             tableIdx = 0;
322             elemSize >>>= 10;
323             while (elemSize != 0) {
324                 elemSize >>>= 1;
325                 tableIdx ++;
326             }
327             table = smallSubpagePools;
328         }
329 
330         return table[tableIdx];
331     }
332 
333     int normalizeCapacity(int reqCapacity) {
334         if (reqCapacity < 0) {
335             throw new IllegalArgumentException("capacity: " + reqCapacity + " (expected: 0+)");
336         }
337 
338         if (reqCapacity >= chunkSize) {
339             return directMemoryCacheAlignment == 0 ? reqCapacity : alignCapacity(reqCapacity);
340         }
341 
342         if (!isTiny(reqCapacity)) { // >= 512
343             // Doubled
344 
345             int normalizedCapacity = reqCapacity;
346             normalizedCapacity --;
347             normalizedCapacity |= normalizedCapacity >>>  1;
348             normalizedCapacity |= normalizedCapacity >>>  2;
349             normalizedCapacity |= normalizedCapacity >>>  4;
350             normalizedCapacity |= normalizedCapacity >>>  8;
351             normalizedCapacity |= normalizedCapacity >>> 16;
352             normalizedCapacity ++;
353 
354             if (normalizedCapacity < 0) {
355                 normalizedCapacity >>>= 1;
356             }
357             assert directMemoryCacheAlignment == 0 || (normalizedCapacity & directMemoryCacheAlignmentMask) == 0;
358 
359             return normalizedCapacity;
360         }
361 
362         if (directMemoryCacheAlignment > 0) {
363             return alignCapacity(reqCapacity);
364         }
365 
366         // Quantum-spaced
367         if ((reqCapacity & 15) == 0) {
368             return reqCapacity;
369         }
370 
371         return (reqCapacity & ~15) + 16;
372     }
373 
374     int alignCapacity(int reqCapacity) {
375         int delta = reqCapacity & directMemoryCacheAlignmentMask;
376         return delta == 0 ? reqCapacity : reqCapacity + directMemoryCacheAlignment - delta;
377     }
378 
379     void reallocate(PooledByteBuf<T> buf, int newCapacity, boolean freeOldMemory) {
380         if (newCapacity < 0 || newCapacity > buf.maxCapacity()) {
381             throw new IllegalArgumentException("newCapacity: " + newCapacity);
382         }
383 
384         int oldCapacity = buf.length;
385         if (oldCapacity == newCapacity) {
386             return;
387         }
388 
389         PoolChunk<T> oldChunk = buf.chunk;
390         long oldHandle = buf.handle;
391         T oldMemory = buf.memory;
392         int oldOffset = buf.offset;
393         int oldMaxLength = buf.maxLength;
394         int readerIndex = buf.readerIndex();
395         int writerIndex = buf.writerIndex();
396 
397         allocate(parent.threadCache(), buf, newCapacity);
398         if (newCapacity > oldCapacity) {
399             memoryCopy(
400                     oldMemory, oldOffset,
401                     buf.memory, buf.offset, oldCapacity);
402         } else if (newCapacity < oldCapacity) {
403             if (readerIndex < newCapacity) {
404                 if (writerIndex > newCapacity) {
405                     writerIndex = newCapacity;
406                 }
407                 memoryCopy(
408                         oldMemory, oldOffset + readerIndex,
409                         buf.memory, buf.offset + readerIndex, writerIndex - readerIndex);
410             } else {
411                 readerIndex = writerIndex = newCapacity;
412             }
413         }
414 
415         buf.setIndex(readerIndex, writerIndex);
416 
417         if (freeOldMemory) {
418             free(oldChunk, oldHandle, oldMaxLength, buf.cache);
419         }
420     }
421 
422     @Override
423     public int numThreadCaches() {
424         return numThreadCaches.get();
425     }
426 
427     @Override
428     public int numTinySubpages() {
429         return tinySubpagePools.length;
430     }
431 
432     @Override
433     public int numSmallSubpages() {
434         return smallSubpagePools.length;
435     }
436 
437     @Override
438     public int numChunkLists() {
439         return chunkListMetrics.size();
440     }
441 
442     @Override
443     public List<PoolSubpageMetric> tinySubpages() {
444         return subPageMetricList(tinySubpagePools);
445     }
446 
447     @Override
448     public List<PoolSubpageMetric> smallSubpages() {
449         return subPageMetricList(smallSubpagePools);
450     }
451 
452     @Override
453     public List<PoolChunkListMetric> chunkLists() {
454         return chunkListMetrics;
455     }
456 
457     private static List<PoolSubpageMetric> subPageMetricList(PoolSubpage<?>[] pages) {
458         List<PoolSubpageMetric> metrics = new ArrayList<PoolSubpageMetric>();
459         for (PoolSubpage<?> head : pages) {
460             if (head.next == head) {
461                 continue;
462             }
463             PoolSubpage<?> s = head.next;
464             for (;;) {
465                 metrics.add(s);
466                 s = s.next;
467                 if (s == head) {
468                     break;
469                 }
470             }
471         }
472         return metrics;
473     }
474 
475     @Override
476     public long numAllocations() {
477         final long allocsNormal;
478         synchronized (this) {
479             allocsNormal = allocationsNormal;
480         }
481         return allocationsTiny.value() + allocationsSmall.value() + allocsNormal + allocationsHuge.value();
482     }
483 
484     @Override
485     public long numTinyAllocations() {
486         return allocationsTiny.value();
487     }
488 
489     @Override
490     public long numSmallAllocations() {
491         return allocationsSmall.value();
492     }
493 
494     @Override
495     public synchronized long numNormalAllocations() {
496         return allocationsNormal;
497     }
498 
499     @Override
500     public long numDeallocations() {
501         final long deallocs;
502         synchronized (this) {
503             deallocs = deallocationsTiny + deallocationsSmall + deallocationsNormal;
504         }
505         return deallocs + deallocationsHuge.value();
506     }
507 
508     @Override
509     public synchronized long numTinyDeallocations() {
510         return deallocationsTiny;
511     }
512 
513     @Override
514     public synchronized long numSmallDeallocations() {
515         return deallocationsSmall;
516     }
517 
518     @Override
519     public synchronized long numNormalDeallocations() {
520         return deallocationsNormal;
521     }
522 
523     @Override
524     public long numHugeAllocations() {
525         return allocationsHuge.value();
526     }
527 
528     @Override
529     public long numHugeDeallocations() {
530         return deallocationsHuge.value();
531     }
532 
533     @Override
534     public  long numActiveAllocations() {
535         long val = allocationsTiny.value() + allocationsSmall.value() + allocationsHuge.value()
536                 - deallocationsHuge.value();
537         synchronized (this) {
538             val += allocationsNormal - (deallocationsTiny + deallocationsSmall + deallocationsNormal);
539         }
540         return max(val, 0);
541     }
542 
543     @Override
544     public long numActiveTinyAllocations() {
545         return max(numTinyAllocations() - numTinyDeallocations(), 0);
546     }
547 
548     @Override
549     public long numActiveSmallAllocations() {
550         return max(numSmallAllocations() - numSmallDeallocations(), 0);
551     }
552 
553     @Override
554     public long numActiveNormalAllocations() {
555         final long val;
556         synchronized (this) {
557             val = allocationsNormal - deallocationsNormal;
558         }
559         return max(val, 0);
560     }
561 
562     @Override
563     public long numActiveHugeAllocations() {
564         return max(numHugeAllocations() - numHugeDeallocations(), 0);
565     }
566 
567     @Override
568     public long numActiveBytes() {
569         long val = activeBytesHuge.value();
570         synchronized (this) {
571             for (int i = 0; i < chunkListMetrics.size(); i++) {
572                 for (PoolChunkMetric m: chunkListMetrics.get(i)) {
573                     val += m.chunkSize();
574                 }
575             }
576         }
577         return max(0, val);
578     }
579 
580     protected abstract PoolChunk<T> newChunk(int pageSize, int maxOrder, int pageShifts, int chunkSize);
581     protected abstract PoolChunk<T> newUnpooledChunk(int capacity);
582     protected abstract PooledByteBuf<T> newByteBuf(int maxCapacity);
583     protected abstract void memoryCopy(T src, int srcOffset, T dst, int dstOffset, int length);
584     protected abstract void destroyChunk(PoolChunk<T> chunk);
585 
586     @Override
587     public synchronized String toString() {
588         StringBuilder buf = new StringBuilder()
589             .append("Chunk(s) at 0~25%:")
590             .append(StringUtil.NEWLINE)
591             .append(qInit)
592             .append(StringUtil.NEWLINE)
593             .append("Chunk(s) at 0~50%:")
594             .append(StringUtil.NEWLINE)
595             .append(q000)
596             .append(StringUtil.NEWLINE)
597             .append("Chunk(s) at 25~75%:")
598             .append(StringUtil.NEWLINE)
599             .append(q025)
600             .append(StringUtil.NEWLINE)
601             .append("Chunk(s) at 50~100%:")
602             .append(StringUtil.NEWLINE)
603             .append(q050)
604             .append(StringUtil.NEWLINE)
605             .append("Chunk(s) at 75~100%:")
606             .append(StringUtil.NEWLINE)
607             .append(q075)
608             .append(StringUtil.NEWLINE)
609             .append("Chunk(s) at 100%:")
610             .append(StringUtil.NEWLINE)
611             .append(q100)
612             .append(StringUtil.NEWLINE)
613             .append("tiny subpages:");
614         appendPoolSubPages(buf, tinySubpagePools);
615         buf.append(StringUtil.NEWLINE)
616            .append("small subpages:");
617         appendPoolSubPages(buf, smallSubpagePools);
618         buf.append(StringUtil.NEWLINE);
619 
620         return buf.toString();
621     }
622 
623     private static void appendPoolSubPages(StringBuilder buf, PoolSubpage<?>[] subpages) {
624         for (int i = 0; i < subpages.length; i ++) {
625             PoolSubpage<?> head = subpages[i];
626             if (head.next == head) {
627                 continue;
628             }
629 
630             buf.append(StringUtil.NEWLINE)
631                     .append(i)
632                     .append(": ");
633             PoolSubpage<?> s = head.next;
634             for (;;) {
635                 buf.append(s);
636                 s = s.next;
637                 if (s == head) {
638                     break;
639                 }
640             }
641         }
642     }
643 
644     @Override
645     protected final void finalize() throws Throwable {
646         try {
647             super.finalize();
648         } finally {
649             destroyPoolSubPages(smallSubpagePools);
650             destroyPoolSubPages(tinySubpagePools);
651             destroyPoolChunkLists(qInit, q000, q025, q050, q075, q100);
652         }
653     }
654 
655     private static void destroyPoolSubPages(PoolSubpage<?>[] pages) {
656         for (PoolSubpage<?> page : pages) {
657             page.destroy();
658         }
659     }
660 
661     private void destroyPoolChunkLists(PoolChunkList<T>... chunkLists) {
662         for (PoolChunkList<T> chunkList: chunkLists) {
663             chunkList.destroy(this);
664         }
665     }
666 
667     static final class HeapArena extends PoolArena<byte[]> {
668 
669         HeapArena(PooledByteBufAllocator parent, int pageSize, int maxOrder,
670                 int pageShifts, int chunkSize, int directMemoryCacheAlignment) {
671             super(parent, pageSize, maxOrder, pageShifts, chunkSize,
672                     directMemoryCacheAlignment);
673         }
674 
675         private static byte[] newByteArray(int size) {
676             return PlatformDependent.allocateUninitializedArray(size);
677         }
678 
679         @Override
680         boolean isDirect() {
681             return false;
682         }
683 
684         @Override
685         protected PoolChunk<byte[]> newChunk(int pageSize, int maxOrder, int pageShifts, int chunkSize) {
686             return new PoolChunk<byte[]>(this, newByteArray(chunkSize), pageSize, maxOrder, pageShifts, chunkSize, 0);
687         }
688 
689         @Override
690         protected PoolChunk<byte[]> newUnpooledChunk(int capacity) {
691             return new PoolChunk<byte[]>(this, newByteArray(capacity), capacity, 0);
692         }
693 
694         @Override
695         protected void destroyChunk(PoolChunk<byte[]> chunk) {
696             // Rely on GC.
697         }
698 
699         @Override
700         protected PooledByteBuf<byte[]> newByteBuf(int maxCapacity) {
701             return HAS_UNSAFE ? PooledUnsafeHeapByteBuf.newUnsafeInstance(maxCapacity)
702                     : PooledHeapByteBuf.newInstance(maxCapacity);
703         }
704 
705         @Override
706         protected void memoryCopy(byte[] src, int srcOffset, byte[] dst, int dstOffset, int length) {
707             if (length == 0) {
708                 return;
709             }
710 
711             System.arraycopy(src, srcOffset, dst, dstOffset, length);
712         }
713     }
714 
715     static final class DirectArena extends PoolArena<ByteBuffer> {
716 
717         DirectArena(PooledByteBufAllocator parent, int pageSize, int maxOrder,
718                 int pageShifts, int chunkSize, int directMemoryCacheAlignment) {
719             super(parent, pageSize, maxOrder, pageShifts, chunkSize,
720                     directMemoryCacheAlignment);
721         }
722 
723         @Override
724         boolean isDirect() {
725             return true;
726         }
727 
728         private int offsetCacheLine(ByteBuffer memory) {
729             // We can only calculate the offset if Unsafe is present as otherwise directBufferAddress(...) will
730             // throw an NPE.
731             return HAS_UNSAFE ?
732                     (int) (PlatformDependent.directBufferAddress(memory) & directMemoryCacheAlignmentMask) : 0;
733         }
734 
735         @Override
736         protected PoolChunk<ByteBuffer> newChunk(int pageSize, int maxOrder,
737                 int pageShifts, int chunkSize) {
738             if (directMemoryCacheAlignment == 0) {
739                 return new PoolChunk<ByteBuffer>(this,
740                         allocateDirect(chunkSize), pageSize, maxOrder,
741                         pageShifts, chunkSize, 0);
742             }
743             final ByteBuffer memory = allocateDirect(chunkSize
744                     + directMemoryCacheAlignment);
745             return new PoolChunk<ByteBuffer>(this, memory, pageSize,
746                     maxOrder, pageShifts, chunkSize,
747                     offsetCacheLine(memory));
748         }
749 
750         @Override
751         protected PoolChunk<ByteBuffer> newUnpooledChunk(int capacity) {
752             if (directMemoryCacheAlignment == 0) {
753                 return new PoolChunk<ByteBuffer>(this,
754                         allocateDirect(capacity), capacity, 0);
755             }
756             final ByteBuffer memory = allocateDirect(capacity
757                     + directMemoryCacheAlignment);
758             return new PoolChunk<ByteBuffer>(this, memory, capacity,
759                     offsetCacheLine(memory));
760         }
761 
762         private static ByteBuffer allocateDirect(int capacity) {
763             return PlatformDependent.useDirectBufferNoCleaner() ?
764                     PlatformDependent.allocateDirectNoCleaner(capacity) : ByteBuffer.allocateDirect(capacity);
765         }
766 
767         @Override
768         protected void destroyChunk(PoolChunk<ByteBuffer> chunk) {
769             if (PlatformDependent.useDirectBufferNoCleaner()) {
770                 PlatformDependent.freeDirectNoCleaner(chunk.memory);
771             } else {
772                 PlatformDependent.freeDirectBuffer(chunk.memory);
773             }
774         }
775 
776         @Override
777         protected PooledByteBuf<ByteBuffer> newByteBuf(int maxCapacity) {
778             if (HAS_UNSAFE) {
779                 return PooledUnsafeDirectByteBuf.newInstance(maxCapacity);
780             } else {
781                 return PooledDirectByteBuf.newInstance(maxCapacity);
782             }
783         }
784 
785         @Override
786         protected void memoryCopy(ByteBuffer src, int srcOffset, ByteBuffer dst, int dstOffset, int length) {
787             if (length == 0) {
788                 return;
789             }
790 
791             if (HAS_UNSAFE) {
792                 PlatformDependent.copyMemory(
793                         PlatformDependent.directBufferAddress(src) + srcOffset,
794                         PlatformDependent.directBufferAddress(dst) + dstOffset, length);
795             } else {
796                 // We must duplicate the NIO buffers because they may be accessed by other Netty buffers.
797                 src = src.duplicate();
798                 dst = dst.duplicate();
799                 src.position(srcOffset).limit(srcOffset + length);
800                 dst.position(dstOffset);
801                 dst.put(src);
802             }
803         }
804     }
805 }