package io.netty.channel.kqueue;

import g.a.a.a.a;
import io.netty.channel.unix.Limits;
import io.netty.util.internal.PlatformDependent;

/* JADX INFO: loaded from: classes.dex */
public final class NativeLongArray {
    private int capacity;
    private long memoryAddress;
    private int size;

    public NativeLongArray(int i2) {
        if (i2 < 1) {
            throw new IllegalArgumentException(a.l("capacity must be >= 1 but was ", i2));
        }
        this.memoryAddress = PlatformDependent.allocateMemory(Limits.SIZEOF_JLONG * i2);
        this.capacity = i2;
    }

    private void checkSize() {
        if (this.size == this.capacity) {
            realloc();
        }
    }

    private long memoryOffset(int i2) {
        return this.memoryAddress + ((long) (i2 * Limits.SIZEOF_JLONG));
    }

    private void realloc() {
        int i2 = this.capacity;
        int i3 = i2 <= 65536 ? i2 << 1 : (i2 + i2) >> 1;
        long jReallocateMemory = PlatformDependent.reallocateMemory(this.memoryAddress, Limits.SIZEOF_JLONG * i3);
        if (jReallocateMemory != 0) {
            this.memoryAddress = jReallocateMemory;
            this.capacity = i3;
        } else {
            StringBuilder sbG = a.G("unable to allocate ", i3, " new bytes! Existing capacity is: ");
            sbG.append(this.capacity);
            throw new OutOfMemoryError(sbG.toString());
        }
    }

    public void add(long j2) {
        checkSize();
        int i2 = this.size;
        this.size = i2 + 1;
        PlatformDependent.putLong(memoryOffset(i2), j2);
    }

    public void clear() {
        this.size = 0;
    }

    public void free() {
        PlatformDependent.freeMemory(this.memoryAddress);
        this.memoryAddress = 0L;
    }

    public boolean isEmpty() {
        return this.size == 0;
    }

    public long memoryAddress() {
        return this.memoryAddress;
    }

    public long memoryAddressEnd() {
        return memoryOffset(this.size);
    }

    public String toString() {
        StringBuilder sbF = a.F("memoryAddress: ");
        sbF.append(this.memoryAddress);
        sbF.append(" capacity: ");
        sbF.append(this.capacity);
        sbF.append(" size: ");
        sbF.append(this.size);
        return sbF.toString();
    }
}
