package io.netty.buffer;

import com.cvte.vman.instance.InvokeCmd;
import io.netty.buffer.PoolArena;
import io.netty.util.concurrent.FastThreadLocal;
import io.netty.util.internal.PlatformDependent;
import io.netty.util.internal.StringUtil;
import io.netty.util.internal.SystemPropertyUtil;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.snmp4j.transport.TLSTM;

/* JADX INFO: loaded from: classes.dex */
public class PooledByteBufAllocator extends AbstractByteBufAllocator {
    public static final PooledByteBufAllocator DEFAULT;
    private static final int DEFAULT_CACHE_TRIM_INTERVAL;
    private static final int DEFAULT_MAX_CACHED_BUFFER_CAPACITY;
    private static final int DEFAULT_MAX_ORDER;
    private static final int DEFAULT_NORMAL_CACHE_SIZE;
    private static final int DEFAULT_NUM_DIRECT_ARENA;
    private static final int DEFAULT_NUM_HEAP_ARENA;
    private static final int DEFAULT_PAGE_SIZE;
    private static final int DEFAULT_SMALL_CACHE_SIZE;
    private static final int DEFAULT_TINY_CACHE_SIZE;
    private static final int MAX_CHUNK_SIZE = 1073741824;
    private static final int MIN_PAGE_SIZE = 4096;
    private static final InternalLogger logger = InternalLoggerFactory.getInstance((Class<?>) PooledByteBufAllocator.class);
    private final List<PoolArenaMetric> directArenaMetrics;
    private final PoolArena<ByteBuffer>[] directArenas;
    private final List<PoolArenaMetric> heapArenaMetrics;
    private final PoolArena<byte[]>[] heapArenas;
    private final int normalCacheSize;
    private final int smallCacheSize;
    private final PoolThreadLocalCache threadCache;
    private final int tinyCacheSize;

    final class PoolThreadLocalCache extends FastThreadLocal<PoolThreadCache> {
        PoolThreadLocalCache() {
        }

        private <T> PoolArena<T> leastUsedArena(PoolArena<T>[] poolArenaArr) {
            if (poolArenaArr == null || poolArenaArr.length == 0) {
                return null;
            }
            PoolArena<T> poolArena = poolArenaArr[0];
            for (int i2 = 1; i2 < poolArenaArr.length; i2++) {
                PoolArena<T> poolArena2 = poolArenaArr[i2];
                if (poolArena2.numThreadCaches.get() < poolArena.numThreadCaches.get()) {
                    poolArena = poolArena2;
                }
            }
            return poolArena;
        }

        /* JADX INFO: Access modifiers changed from: protected */
        /* JADX WARN: Can't rename method to resolve collision */
        @Override // io.netty.util.concurrent.FastThreadLocal
        public synchronized PoolThreadCache initialValue() {
            return new PoolThreadCache(leastUsedArena(PooledByteBufAllocator.this.heapArenas), leastUsedArena(PooledByteBufAllocator.this.directArenas), PooledByteBufAllocator.this.tinyCacheSize, PooledByteBufAllocator.this.smallCacheSize, PooledByteBufAllocator.this.normalCacheSize, PooledByteBufAllocator.DEFAULT_MAX_CACHED_BUFFER_CAPACITY, PooledByteBufAllocator.DEFAULT_CACHE_TRIM_INTERVAL);
        }

        /* JADX INFO: Access modifiers changed from: protected */
        @Override // io.netty.util.concurrent.FastThreadLocal
        public void onRemoval(PoolThreadCache poolThreadCache) {
            poolThreadCache.free();
        }
    }

    static {
        Object obj;
        int i2 = SystemPropertyUtil.getInt("io.netty.allocator.pageSize", InvokeCmd.CMD_TOUCH_CTRL_START);
        Object obj2 = null;
        try {
            validateAndCalculatePageShifts(i2);
            obj = null;
        } catch (Throwable th) {
            obj = th;
            i2 = InvokeCmd.CMD_TOUCH_CTRL_START;
        }
        DEFAULT_PAGE_SIZE = i2;
        int i3 = 11;
        int i4 = SystemPropertyUtil.getInt("io.netty.allocator.maxOrder", 11);
        try {
            validateAndCalculateChunkSize(DEFAULT_PAGE_SIZE, i4);
            i3 = i4;
        } catch (Throwable th2) {
            obj2 = th2;
        }
        DEFAULT_MAX_ORDER = i3;
        Runtime runtime = Runtime.getRuntime();
        long jAvailableProcessors = runtime.availableProcessors() * 2;
        long j2 = DEFAULT_PAGE_SIZE << DEFAULT_MAX_ORDER;
        DEFAULT_NUM_HEAP_ARENA = Math.max(0, SystemPropertyUtil.getInt("io.netty.allocator.numHeapArenas", (int) Math.min(jAvailableProcessors, ((runtime.maxMemory() / j2) / 2) / 3)));
        DEFAULT_NUM_DIRECT_ARENA = Math.max(0, SystemPropertyUtil.getInt("io.netty.allocator.numDirectArenas", (int) Math.min(jAvailableProcessors, ((PlatformDependent.maxDirectMemory() / j2) / 2) / 3)));
        DEFAULT_TINY_CACHE_SIZE = SystemPropertyUtil.getInt("io.netty.allocator.tinyCacheSize", 512);
        DEFAULT_SMALL_CACHE_SIZE = SystemPropertyUtil.getInt("io.netty.allocator.smallCacheSize", 256);
        DEFAULT_NORMAL_CACHE_SIZE = SystemPropertyUtil.getInt("io.netty.allocator.normalCacheSize", 64);
        DEFAULT_MAX_CACHED_BUFFER_CAPACITY = SystemPropertyUtil.getInt("io.netty.allocator.maxCachedBufferCapacity", TLSTM.MAX_TLS_PAYLOAD_SIZE);
        DEFAULT_CACHE_TRIM_INTERVAL = SystemPropertyUtil.getInt("io.netty.allocator.cacheTrimInterval", InvokeCmd.CMD_TOUCH_CTRL_START);
        if (logger.isDebugEnabled()) {
            logger.debug("-Dio.netty.allocator.numHeapArenas: {}", Integer.valueOf(DEFAULT_NUM_HEAP_ARENA));
            logger.debug("-Dio.netty.allocator.numDirectArenas: {}", Integer.valueOf(DEFAULT_NUM_DIRECT_ARENA));
            if (obj == null) {
                logger.debug("-Dio.netty.allocator.pageSize: {}", Integer.valueOf(DEFAULT_PAGE_SIZE));
            } else {
                logger.debug("-Dio.netty.allocator.pageSize: {}", Integer.valueOf(DEFAULT_PAGE_SIZE), obj);
            }
            if (obj2 == null) {
                logger.debug("-Dio.netty.allocator.maxOrder: {}", Integer.valueOf(DEFAULT_MAX_ORDER));
            } else {
                logger.debug("-Dio.netty.allocator.maxOrder: {}", Integer.valueOf(DEFAULT_MAX_ORDER), obj2);
            }
            logger.debug("-Dio.netty.allocator.chunkSize: {}", Integer.valueOf(DEFAULT_PAGE_SIZE << DEFAULT_MAX_ORDER));
            logger.debug("-Dio.netty.allocator.tinyCacheSize: {}", Integer.valueOf(DEFAULT_TINY_CACHE_SIZE));
            logger.debug("-Dio.netty.allocator.smallCacheSize: {}", Integer.valueOf(DEFAULT_SMALL_CACHE_SIZE));
            logger.debug("-Dio.netty.allocator.normalCacheSize: {}", Integer.valueOf(DEFAULT_NORMAL_CACHE_SIZE));
            logger.debug("-Dio.netty.allocator.maxCachedBufferCapacity: {}", Integer.valueOf(DEFAULT_MAX_CACHED_BUFFER_CAPACITY));
            logger.debug("-Dio.netty.allocator.cacheTrimInterval: {}", Integer.valueOf(DEFAULT_CACHE_TRIM_INTERVAL));
        }
        DEFAULT = new PooledByteBufAllocator(PlatformDependent.directBufferPreferred());
    }

    public PooledByteBufAllocator() {
        this(false);
    }

    public static int defaultMaxOrder() {
        return DEFAULT_MAX_ORDER;
    }

    public static int defaultNormalCacheSize() {
        return DEFAULT_NORMAL_CACHE_SIZE;
    }

    public static int defaultNumDirectArena() {
        return DEFAULT_NUM_DIRECT_ARENA;
    }

    public static int defaultNumHeapArena() {
        return DEFAULT_NUM_HEAP_ARENA;
    }

    public static int defaultPageSize() {
        return DEFAULT_PAGE_SIZE;
    }

    public static int defaultSmallCacheSize() {
        return DEFAULT_SMALL_CACHE_SIZE;
    }

    public static int defaultTinyCacheSize() {
        return DEFAULT_TINY_CACHE_SIZE;
    }

    private static <T> PoolArena<T>[] newArenaArray(int i2) {
        return new PoolArena[i2];
    }

    private static int validateAndCalculateChunkSize(int i2, int i3) {
        if (i3 > 14) {
            throw new IllegalArgumentException("maxOrder: " + i3 + " (expected: 0-14)");
        }
        int i4 = i2;
        for (int i5 = i3; i5 > 0; i5--) {
            if (i4 > 536870912) {
                throw new IllegalArgumentException(String.format("pageSize (%d) << maxOrder (%d) must not exceed %d", Integer.valueOf(i2), Integer.valueOf(i3), 1073741824));
            }
            i4 <<= 1;
        }
        return i4;
    }

    private static int validateAndCalculatePageShifts(int i2) {
        if (i2 >= 4096) {
            if (((i2 - 1) & i2) == 0) {
                return 31 - Integer.numberOfLeadingZeros(i2);
            }
            throw new IllegalArgumentException("pageSize: " + i2 + " (expected: power of 2)");
        }
        throw new IllegalArgumentException("pageSize: " + i2 + " (expected: 4096)");
    }

    public List<PoolArenaMetric> directArenas() {
        return this.directArenaMetrics;
    }

    public String dumpStats() {
        PoolArena<byte[]>[] poolArenaArr = this.heapArenas;
        int length = poolArenaArr == null ? 0 : poolArenaArr.length;
        StringBuilder sb = new StringBuilder(512);
        sb.append(length);
        sb.append(" heap arena(s):");
        sb.append(StringUtil.NEWLINE);
        if (length > 0) {
            for (PoolArena<byte[]> poolArena : this.heapArenas) {
                sb.append(poolArena);
            }
        }
        PoolArena<ByteBuffer>[] poolArenaArr2 = this.directArenas;
        int length2 = poolArenaArr2 == null ? 0 : poolArenaArr2.length;
        sb.append(length2);
        sb.append(" direct arena(s):");
        sb.append(StringUtil.NEWLINE);
        if (length2 > 0) {
            for (PoolArena<ByteBuffer> poolArena2 : this.directArenas) {
                sb.append(poolArena2);
            }
        }
        return sb.toString();
    }

    @Deprecated
    public void freeThreadLocalCache() throws Throwable {
        this.threadCache.remove();
    }

    @Deprecated
    public boolean hasThreadLocalCache() {
        return this.threadCache.isSet();
    }

    public List<PoolArenaMetric> heapArenas() {
        return this.heapArenaMetrics;
    }

    @Override // io.netty.buffer.ByteBufAllocator
    public boolean isDirectBufferPooled() {
        return this.directArenas != null;
    }

    @Override // io.netty.buffer.AbstractByteBufAllocator
    protected ByteBuf newDirectBuffer(int i2, int i3) {
        PoolThreadCache poolThreadCache = this.threadCache.get();
        PoolArena<ByteBuffer> poolArena = poolThreadCache.directArena;
        return AbstractByteBufAllocator.toLeakAwareBuffer(poolArena != null ? poolArena.allocate(poolThreadCache, i2, i3) : PlatformDependent.hasUnsafe() ? UnsafeByteBufUtil.newUnsafeDirectByteBuf(this, i2, i3) : new UnpooledDirectByteBuf(this, i2, i3));
    }

    @Override // io.netty.buffer.AbstractByteBufAllocator
    protected ByteBuf newHeapBuffer(int i2, int i3) {
        PoolThreadCache poolThreadCache = this.threadCache.get();
        PoolArena<byte[]> poolArena = poolThreadCache.heapArena;
        return AbstractByteBufAllocator.toLeakAwareBuffer(poolArena != null ? poolArena.allocate(poolThreadCache, i2, i3) : new UnpooledHeapByteBuf(this, i2, i3));
    }

    public int normalCacheSize() {
        return this.normalCacheSize;
    }

    public int numDirectArenas() {
        return this.directArenaMetrics.size();
    }

    public int numHeapArenas() {
        return this.heapArenaMetrics.size();
    }

    public int numThreadLocalCaches() {
        PoolArena[] poolArenaArr = this.heapArenas;
        if (poolArenaArr == null) {
            poolArenaArr = this.directArenas;
        }
        if (poolArenaArr == null) {
            return 0;
        }
        int i2 = 0;
        for (PoolArena poolArena : poolArenaArr) {
            i2 += poolArena.numThreadCaches.get();
        }
        return i2;
    }

    public int smallCacheSize() {
        return this.smallCacheSize;
    }

    final PoolThreadCache threadCache() {
        return this.threadCache.get();
    }

    public int tinyCacheSize() {
        return this.tinyCacheSize;
    }

    public PooledByteBufAllocator(boolean z) {
        this(z, DEFAULT_NUM_HEAP_ARENA, DEFAULT_NUM_DIRECT_ARENA, DEFAULT_PAGE_SIZE, DEFAULT_MAX_ORDER);
    }

    public PooledByteBufAllocator(int i2, int i3, int i4, int i5) {
        this(false, i2, i3, i4, i5);
    }

    public PooledByteBufAllocator(boolean z, int i2, int i3, int i4, int i5) {
        this(z, i2, i3, i4, i5, DEFAULT_TINY_CACHE_SIZE, DEFAULT_SMALL_CACHE_SIZE, DEFAULT_NORMAL_CACHE_SIZE);
    }

    public PooledByteBufAllocator(boolean z, int i2, int i3, int i4, int i5, int i6, int i7, int i8) {
        super(z);
        this.threadCache = new PoolThreadLocalCache();
        this.tinyCacheSize = i6;
        this.smallCacheSize = i7;
        this.normalCacheSize = i8;
        int iValidateAndCalculateChunkSize = validateAndCalculateChunkSize(i4, i5);
        if (i2 < 0) {
            throw new IllegalArgumentException("nHeapArena: " + i2 + " (expected: >= 0)");
        }
        if (i3 >= 0) {
            int iValidateAndCalculatePageShifts = validateAndCalculatePageShifts(i4);
            if (i2 > 0) {
                this.heapArenas = newArenaArray(i2);
                ArrayList arrayList = new ArrayList(this.heapArenas.length);
                for (int i9 = 0; i9 < this.heapArenas.length; i9++) {
                    PoolArena.HeapArena heapArena = new PoolArena.HeapArena(this, i4, i5, iValidateAndCalculatePageShifts, iValidateAndCalculateChunkSize);
                    this.heapArenas[i9] = heapArena;
                    arrayList.add(heapArena);
                }
                this.heapArenaMetrics = Collections.unmodifiableList(arrayList);
            } else {
                this.heapArenas = null;
                this.heapArenaMetrics = Collections.emptyList();
            }
            if (i3 > 0) {
                this.directArenas = newArenaArray(i3);
                ArrayList arrayList2 = new ArrayList(this.directArenas.length);
                for (int i10 = 0; i10 < this.directArenas.length; i10++) {
                    PoolArena.DirectArena directArena = new PoolArena.DirectArena(this, i4, i5, iValidateAndCalculatePageShifts, iValidateAndCalculateChunkSize);
                    this.directArenas[i10] = directArena;
                    arrayList2.add(directArena);
                }
                this.directArenaMetrics = Collections.unmodifiableList(arrayList2);
                return;
            }
            this.directArenas = null;
            this.directArenaMetrics = Collections.emptyList();
            return;
        }
        throw new IllegalArgumentException("nDirectArea: " + i3 + " (expected: >= 0)");
    }
}
