package j;

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 x {

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static final int f4655b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public static final AtomicReference<w>[] f4656c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public static final x f4657d = new x();
    public static final w a = new w(new byte[0], 0, 0, false, false);

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

    public static final void a(w wVar) {
        g.h0.c.l.f(wVar, "segment");
        if (!(wVar.f4653f == null && wVar.f4654g == null)) {
            throw new IllegalArgumentException("Failed requirement.".toString());
        }
        if (wVar.f4651d) {
            return;
        }
        Thread threadCurrentThread = Thread.currentThread();
        g.h0.c.l.e(threadCurrentThread, "Thread.currentThread()");
        AtomicReference<w> atomicReference = f4656c[(int) (threadCurrentThread.getId() & (((long) f4655b) - 1))];
        w wVar2 = atomicReference.get();
        if (wVar2 == a) {
            return;
        }
        int i2 = wVar2 != null ? wVar2.f4650c : 0;
        if (i2 >= 65536) {
            return;
        }
        wVar.f4653f = wVar2;
        wVar.f4649b = 0;
        wVar.f4650c = i2 + InvokeCmd.CMD_TOUCH_CTRL_START;
        if (atomicReference.compareAndSet(wVar2, wVar)) {
            return;
        }
        wVar.f4653f = null;
    }

    public static final w b() {
        Thread threadCurrentThread = Thread.currentThread();
        g.h0.c.l.e(threadCurrentThread, "Thread.currentThread()");
        AtomicReference<w> atomicReference = f4656c[(int) (threadCurrentThread.getId() & (((long) f4655b) - 1))];
        w wVar = a;
        w andSet = atomicReference.getAndSet(wVar);
        if (andSet == wVar) {
            return new w();
        }
        if (andSet == null) {
            atomicReference.set(null);
            return new w();
        }
        atomicReference.set(andSet.f4653f);
        andSet.f4653f = null;
        andSet.f4650c = 0;
        return andSet;
    }
}
