package io.netty.buffer;

/* JADX INFO: loaded from: classes.dex */
public final class PoolSubpage<T> implements PoolSubpageMetric {
    public static final /* synthetic */ boolean $assertionsDisabled = false;
    private final long[] bitmap;
    private int bitmapLength;
    public final PoolChunk<T> chunk;
    public boolean doNotDestroy;
    public int elemSize;
    private int maxNumElems;
    private final int memoryMapIdx;
    public PoolSubpage<T> next;
    private int nextAvail;
    private int numAvail;
    private final int pageSize;
    public PoolSubpage<T> prev;
    private final int runOffset;

    public PoolSubpage(int i2) {
        this.chunk = null;
        this.memoryMapIdx = -1;
        this.runOffset = -1;
        this.elemSize = -1;
        this.pageSize = i2;
        this.bitmap = null;
    }

    public PoolSubpage(PoolSubpage<T> poolSubpage, PoolChunk<T> poolChunk, int i2, int i3, int i4, int i5) {
        this.chunk = poolChunk;
        this.memoryMapIdx = i2;
        this.runOffset = i3;
        this.pageSize = i4;
        this.bitmap = new long[i4 >>> 10];
        init(poolSubpage, i5);
    }

    private void addToPool(PoolSubpage<T> poolSubpage) {
        this.prev = poolSubpage;
        PoolSubpage<T> poolSubpage2 = poolSubpage.next;
        this.next = poolSubpage2;
        poolSubpage2.prev = this;
        poolSubpage.next = this;
    }

    private int findNextAvail() {
        long[] jArr = this.bitmap;
        int i2 = this.bitmapLength;
        for (int i3 = 0; i3 < i2; i3++) {
            long j2 = jArr[i3];
            if ((~j2) != 0) {
                return findNextAvail0(i3, j2);
            }
        }
        return -1;
    }

    private int findNextAvail0(int i2, long j2) {
        int i3 = this.maxNumElems;
        int i4 = i2 << 6;
        for (int i5 = 0; i5 < 64; i5++) {
            if ((1 & j2) == 0) {
                int i6 = i4 | i5;
                if (i6 < i3) {
                    return i6;
                }
                return -1;
            }
            j2 >>>= 1;
        }
        return -1;
    }

    private int getNextAvail() {
        int i2 = this.nextAvail;
        if (i2 < 0) {
            return findNextAvail();
        }
        this.nextAvail = -1;
        return i2;
    }

    private void removeFromPool() {
        PoolSubpage<T> poolSubpage = this.prev;
        poolSubpage.next = this.next;
        this.next.prev = poolSubpage;
        this.next = null;
        this.prev = null;
    }

    private void setNextAvail(int i2) {
        this.nextAvail = i2;
    }

    private long toHandle(int i2) {
        return (((long) i2) << 32) | 4611686018427387904L | ((long) this.memoryMapIdx);
    }

    public long allocate() {
        if (this.elemSize == 0) {
            return toHandle(0);
        }
        if (this.numAvail == 0 || !this.doNotDestroy) {
            return -1L;
        }
        int nextAvail = getNextAvail();
        int i2 = nextAvail >>> 6;
        long[] jArr = this.bitmap;
        jArr[i2] = jArr[i2] | (1 << (nextAvail & 63));
        int i3 = this.numAvail - 1;
        this.numAvail = i3;
        if (i3 == 0) {
            removeFromPool();
        }
        return toHandle(nextAvail);
    }

    public void destroy() {
        PoolChunk<T> poolChunk = this.chunk;
        if (poolChunk != null) {
            poolChunk.destroy();
        }
    }

    @Override // io.netty.buffer.PoolSubpageMetric
    public int elementSize() {
        int i2;
        synchronized (this.chunk.arena) {
            i2 = this.elemSize;
        }
        return i2;
    }

    public boolean free(PoolSubpage<T> poolSubpage, int i2) {
        if (this.elemSize == 0) {
            return true;
        }
        int i3 = i2 >>> 6;
        long[] jArr = this.bitmap;
        jArr[i3] = jArr[i3] ^ (1 << (i2 & 63));
        setNextAvail(i2);
        int i4 = this.numAvail;
        int i5 = i4 + 1;
        this.numAvail = i5;
        if (i4 == 0) {
            addToPool(poolSubpage);
            return true;
        }
        if (i5 != this.maxNumElems || this.prev == this.next) {
            return true;
        }
        this.doNotDestroy = false;
        removeFromPool();
        return false;
    }

    public void init(PoolSubpage<T> poolSubpage, int i2) {
        this.doNotDestroy = true;
        this.elemSize = i2;
        if (i2 != 0) {
            int i3 = this.pageSize / i2;
            this.numAvail = i3;
            this.maxNumElems = i3;
            this.nextAvail = 0;
            int i4 = i3 >>> 6;
            this.bitmapLength = i4;
            if ((i3 & 63) != 0) {
                this.bitmapLength = i4 + 1;
            }
            for (int i5 = 0; i5 < this.bitmapLength; i5++) {
                this.bitmap[i5] = 0;
            }
        }
        addToPool(poolSubpage);
    }

    @Override // io.netty.buffer.PoolSubpageMetric
    public int maxNumElements() {
        int i2;
        synchronized (this.chunk.arena) {
            i2 = this.maxNumElems;
        }
        return i2;
    }

    @Override // io.netty.buffer.PoolSubpageMetric
    public int numAvailable() {
        int i2;
        synchronized (this.chunk.arena) {
            i2 = this.numAvail;
        }
        return i2;
    }

    @Override // io.netty.buffer.PoolSubpageMetric
    public int pageSize() {
        return this.pageSize;
    }

    public String toString() {
        boolean z2;
        int i2;
        int i3;
        int i4;
        synchronized (this.chunk.arena) {
            if (this.doNotDestroy) {
                z2 = true;
                i2 = this.maxNumElems;
                i3 = this.numAvail;
                i4 = this.elemSize;
            } else {
                z2 = false;
                i2 = -1;
                i3 = -1;
                i4 = -1;
            }
        }
        StringBuilder sb = new StringBuilder();
        if (z2) {
            sb.append("(");
            sb.append(this.memoryMapIdx);
            sb.append(": ");
            sb.append(i2 - i3);
            sb.append('/');
            sb.append(i2);
            sb.append(", offset: ");
            sb.append(this.runOffset);
            sb.append(", length: ");
            sb.append(this.pageSize);
            sb.append(", elemSize: ");
            sb.append(i4);
            sb.append(')');
        } else {
            sb.append("(");
            sb.append(this.memoryMapIdx);
            sb.append(": not in use)");
        }
        return sb.toString();
    }
}
