package io.netty.buffer;

import io.netty.util.internal.LongCounter;
import io.netty.util.internal.PlatformDependent;
import io.netty.util.internal.StringUtil;
import java.nio.ByteBuffer;

/* JADX INFO: loaded from: classes.dex */
public final class UnpooledByteBufAllocator extends AbstractByteBufAllocator implements ByteBufAllocatorMetricProvider {
    public static final UnpooledByteBufAllocator DEFAULT = new UnpooledByteBufAllocator(PlatformDependent.directBufferPreferred());
    private final boolean disableLeakDetector;
    private final UnpooledByteBufAllocatorMetric metric;
    private final boolean noCleaner;

    public static final class InstrumentedUnpooledDirectByteBuf extends UnpooledDirectByteBuf {
        public InstrumentedUnpooledDirectByteBuf(UnpooledByteBufAllocator unpooledByteBufAllocator, int i2, int i3) {
            super(unpooledByteBufAllocator, i2, i3);
        }

        @Override // io.netty.buffer.UnpooledDirectByteBuf
        public ByteBuffer allocateDirect(int i2) {
            ByteBuffer byteBufferAllocateDirect = super.allocateDirect(i2);
            ((UnpooledByteBufAllocator) alloc()).incrementDirect(byteBufferAllocateDirect.capacity());
            return byteBufferAllocateDirect;
        }

        @Override // io.netty.buffer.UnpooledDirectByteBuf
        public void freeDirect(ByteBuffer byteBuffer) {
            int iCapacity = byteBuffer.capacity();
            super.freeDirect(byteBuffer);
            ((UnpooledByteBufAllocator) alloc()).decrementDirect(iCapacity);
        }
    }

    public static final class InstrumentedUnpooledHeapByteBuf extends UnpooledHeapByteBuf {
        public InstrumentedUnpooledHeapByteBuf(UnpooledByteBufAllocator unpooledByteBufAllocator, int i2, int i3) {
            super(unpooledByteBufAllocator, i2, i3);
        }

        @Override // io.netty.buffer.UnpooledHeapByteBuf
        public byte[] allocateArray(int i2) {
            byte[] bArrAllocateArray = super.allocateArray(i2);
            ((UnpooledByteBufAllocator) alloc()).incrementHeap(bArrAllocateArray.length);
            return bArrAllocateArray;
        }

        @Override // io.netty.buffer.UnpooledHeapByteBuf
        public void freeArray(byte[] bArr) {
            int length = bArr.length;
            super.freeArray(bArr);
            ((UnpooledByteBufAllocator) alloc()).decrementHeap(length);
        }
    }

    public static final class InstrumentedUnpooledUnsafeDirectByteBuf extends UnpooledUnsafeDirectByteBuf {
        public InstrumentedUnpooledUnsafeDirectByteBuf(UnpooledByteBufAllocator unpooledByteBufAllocator, int i2, int i3) {
            super(unpooledByteBufAllocator, i2, i3);
        }

        @Override // io.netty.buffer.UnpooledUnsafeDirectByteBuf
        public ByteBuffer allocateDirect(int i2) {
            ByteBuffer byteBufferAllocateDirect = super.allocateDirect(i2);
            ((UnpooledByteBufAllocator) alloc()).incrementDirect(byteBufferAllocateDirect.capacity());
            return byteBufferAllocateDirect;
        }

        @Override // io.netty.buffer.UnpooledUnsafeDirectByteBuf
        public void freeDirect(ByteBuffer byteBuffer) {
            int iCapacity = byteBuffer.capacity();
            super.freeDirect(byteBuffer);
            ((UnpooledByteBufAllocator) alloc()).decrementDirect(iCapacity);
        }
    }

    public static final class InstrumentedUnpooledUnsafeHeapByteBuf extends UnpooledUnsafeHeapByteBuf {
        public InstrumentedUnpooledUnsafeHeapByteBuf(UnpooledByteBufAllocator unpooledByteBufAllocator, int i2, int i3) {
            super(unpooledByteBufAllocator, i2, i3);
        }

        @Override // io.netty.buffer.UnpooledUnsafeHeapByteBuf, io.netty.buffer.UnpooledHeapByteBuf
        public byte[] allocateArray(int i2) {
            byte[] bArrAllocateArray = super.allocateArray(i2);
            ((UnpooledByteBufAllocator) alloc()).incrementHeap(bArrAllocateArray.length);
            return bArrAllocateArray;
        }

        @Override // io.netty.buffer.UnpooledHeapByteBuf
        public void freeArray(byte[] bArr) {
            int length = bArr.length;
            super.freeArray(bArr);
            ((UnpooledByteBufAllocator) alloc()).decrementHeap(length);
        }
    }

    public static final class InstrumentedUnpooledUnsafeNoCleanerDirectByteBuf extends UnpooledUnsafeNoCleanerDirectByteBuf {
        public InstrumentedUnpooledUnsafeNoCleanerDirectByteBuf(UnpooledByteBufAllocator unpooledByteBufAllocator, int i2, int i3) {
            super(unpooledByteBufAllocator, i2, i3);
        }

        @Override // io.netty.buffer.UnpooledUnsafeNoCleanerDirectByteBuf, io.netty.buffer.UnpooledUnsafeDirectByteBuf
        public ByteBuffer allocateDirect(int i2) {
            ByteBuffer byteBufferAllocateDirect = super.allocateDirect(i2);
            ((UnpooledByteBufAllocator) alloc()).incrementDirect(byteBufferAllocateDirect.capacity());
            return byteBufferAllocateDirect;
        }

        @Override // io.netty.buffer.UnpooledUnsafeNoCleanerDirectByteBuf, io.netty.buffer.UnpooledUnsafeDirectByteBuf
        public void freeDirect(ByteBuffer byteBuffer) {
            int iCapacity = byteBuffer.capacity();
            super.freeDirect(byteBuffer);
            ((UnpooledByteBufAllocator) alloc()).decrementDirect(iCapacity);
        }

        @Override // io.netty.buffer.UnpooledUnsafeNoCleanerDirectByteBuf
        public ByteBuffer reallocateDirect(ByteBuffer byteBuffer, int i2) {
            int iCapacity = byteBuffer.capacity();
            ByteBuffer byteBufferReallocateDirect = super.reallocateDirect(byteBuffer, i2);
            ((UnpooledByteBufAllocator) alloc()).incrementDirect(byteBufferReallocateDirect.capacity() - iCapacity);
            return byteBufferReallocateDirect;
        }
    }

    public static final class UnpooledByteBufAllocatorMetric implements ByteBufAllocatorMetric {
        public final LongCounter directCounter;
        public final LongCounter heapCounter;

        private UnpooledByteBufAllocatorMetric() {
            this.directCounter = PlatformDependent.newLongCounter();
            this.heapCounter = PlatformDependent.newLongCounter();
        }

        public String toString() {
            return StringUtil.simpleClassName(this) + "(usedHeapMemory: " + usedHeapMemory() + "; usedDirectMemory: " + usedDirectMemory() + ')';
        }

        @Override // io.netty.buffer.ByteBufAllocatorMetric
        public long usedDirectMemory() {
            return this.directCounter.value();
        }

        @Override // io.netty.buffer.ByteBufAllocatorMetric
        public long usedHeapMemory() {
            return this.heapCounter.value();
        }
    }

    public UnpooledByteBufAllocator(boolean z2) {
        this(z2, false);
    }

    public UnpooledByteBufAllocator(boolean z2, boolean z3) {
        this(z2, z3, PlatformDependent.useDirectBufferNoCleaner());
    }

    public UnpooledByteBufAllocator(boolean z2, boolean z3, boolean z4) {
        super(z2);
        this.metric = new UnpooledByteBufAllocatorMetric();
        this.disableLeakDetector = z3;
        this.noCleaner = z4 && PlatformDependent.hasUnsafe() && PlatformDependent.hasDirectBufferNoCleanerConstructor();
    }

    @Override // io.netty.buffer.AbstractByteBufAllocator, io.netty.buffer.ByteBufAllocator
    public CompositeByteBuf compositeDirectBuffer(int i2) {
        CompositeByteBuf compositeByteBuf = new CompositeByteBuf(this, true, i2);
        return this.disableLeakDetector ? compositeByteBuf : AbstractByteBufAllocator.toLeakAwareBuffer(compositeByteBuf);
    }

    @Override // io.netty.buffer.AbstractByteBufAllocator, io.netty.buffer.ByteBufAllocator
    public CompositeByteBuf compositeHeapBuffer(int i2) {
        CompositeByteBuf compositeByteBuf = new CompositeByteBuf(this, false, i2);
        return this.disableLeakDetector ? compositeByteBuf : AbstractByteBufAllocator.toLeakAwareBuffer(compositeByteBuf);
    }

    public void decrementDirect(int i2) {
        this.metric.directCounter.add(-i2);
    }

    public void decrementHeap(int i2) {
        this.metric.heapCounter.add(-i2);
    }

    public void incrementDirect(int i2) {
        this.metric.directCounter.add(i2);
    }

    public void incrementHeap(int i2) {
        this.metric.heapCounter.add(i2);
    }

    @Override // io.netty.buffer.ByteBufAllocator
    public boolean isDirectBufferPooled() {
        return false;
    }

    @Override // io.netty.buffer.ByteBufAllocatorMetricProvider
    public ByteBufAllocatorMetric metric() {
        return this.metric;
    }

    @Override // io.netty.buffer.AbstractByteBufAllocator
    public ByteBuf newDirectBuffer(int i2, int i3) {
        ByteBuf instrumentedUnpooledUnsafeNoCleanerDirectByteBuf = PlatformDependent.hasUnsafe() ? this.noCleaner ? new InstrumentedUnpooledUnsafeNoCleanerDirectByteBuf(this, i2, i3) : new InstrumentedUnpooledUnsafeDirectByteBuf(this, i2, i3) : new InstrumentedUnpooledDirectByteBuf(this, i2, i3);
        return this.disableLeakDetector ? instrumentedUnpooledUnsafeNoCleanerDirectByteBuf : AbstractByteBufAllocator.toLeakAwareBuffer(instrumentedUnpooledUnsafeNoCleanerDirectByteBuf);
    }

    @Override // io.netty.buffer.AbstractByteBufAllocator
    public ByteBuf newHeapBuffer(int i2, int i3) {
        return PlatformDependent.hasUnsafe() ? new InstrumentedUnpooledUnsafeHeapByteBuf(this, i2, i3) : new InstrumentedUnpooledHeapByteBuf(this, i2, i3);
    }
}
