package i;

import com.cvte.vman.instance.InvokeCmd;
import java.util.concurrent.atomic.AtomicReference;

/* JADX INFO: compiled from: SegmentPool.kt */
/* JADX INFO: loaded from: classes.dex */
public final class z {
    private static final int a = 65536;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private static final int f1058c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private static final AtomicReference<y>[] f1059d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public static final z f1060e = new z();
    private static final y b = new y(new byte[0], 0, 0, false, false);

    static {
        int iHighestOneBit = Integer.highestOneBit((Runtime.getRuntime().availableProcessors() * 2) - 1);
        f1058c = iHighestOneBit;
        AtomicReference<y>[] atomicReferenceArr = new AtomicReference[iHighestOneBit];
        for (int i2 = 0; i2 < iHighestOneBit; i2++) {
            atomicReferenceArr[i2] = new AtomicReference<>();
        }
        f1059d = atomicReferenceArr;
    }

    private z() {
    }

    private final AtomicReference<y> a() {
        Thread threadCurrentThread = Thread.currentThread();
        h.x.d.k.c(threadCurrentThread, "Thread.currentThread()");
        return f1059d[(int) (threadCurrentThread.getId() & (((long) f1058c) - 1))];
    }

    public static final void b(y yVar) {
        AtomicReference<y> atomicReferenceA;
        y yVar2;
        h.x.d.k.d(yVar, "segment");
        if (!(yVar.f1056f == null && yVar.f1057g == null)) {
            throw new IllegalArgumentException("Failed requirement.".toString());
        }
        if (yVar.f1054d || (yVar2 = (atomicReferenceA = f1060e.a()).get()) == b) {
            return;
        }
        int i2 = yVar2 != null ? yVar2.f1053c : 0;
        if (i2 >= a) {
            return;
        }
        yVar.f1056f = yVar2;
        yVar.b = 0;
        yVar.f1053c = i2 + InvokeCmd.CMD_TOUCH_CTRL_START;
        if (atomicReferenceA.compareAndSet(yVar2, yVar)) {
            return;
        }
        yVar.f1056f = null;
    }

    public static final y c() {
        AtomicReference<y> atomicReferenceA = f1060e.a();
        y andSet = atomicReferenceA.getAndSet(b);
        if (andSet == b) {
            return new y();
        }
        if (andSet == null) {
            atomicReferenceA.set(null);
            return new y();
        }
        atomicReferenceA.set(andSet.f1056f);
        andSet.f1056f = null;
        andSet.f1053c = 0;
        return andSet;
    }
}
