package io.netty.channel.kqueue;

import g.a.a.a.a;
import io.netty.util.internal.PlatformDependent;

/* JADX INFO: loaded from: classes.dex */
public final class KQueueEventArray {
    private int capacity;
    private long memoryAddress;
    private int size;
    private static final int KQUEUE_EVENT_SIZE = Native.sizeofKEvent();
    private static final int KQUEUE_IDENT_OFFSET = Native.offsetofKEventIdent();
    private static final int KQUEUE_FILTER_OFFSET = Native.offsetofKEventFilter();
    private static final int KQUEUE_FFLAGS_OFFSET = Native.offsetofKEventFFlags();
    private static final int KQUEUE_FLAGS_OFFSET = Native.offsetofKEventFlags();
    private static final int KQUEUE_DATA_OFFSET = Native.offsetofKeventData();

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

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

    public static native void deleteGlobalRefs(long j2, long j3);

    private static native void evSet(long j2, AbstractKQueueChannel abstractKQueueChannel, int i2, short s2, short s3, int i3);

    private static native AbstractKQueueChannel getChannel(long j2);

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

    public AbstractKQueueChannel channel(int i2) {
        return getChannel(getKEventOffset(i2));
    }

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

    public long data(int i2) {
        return PlatformDependent.getLong(getKEventOffset(i2) + ((long) KQUEUE_DATA_OFFSET));
    }

    public void evSet(AbstractKQueueChannel abstractKQueueChannel, short s2, short s3, int i2) {
        checkSize();
        int i3 = this.size;
        this.size = i3 + 1;
        evSet(getKEventOffset(i3), abstractKQueueChannel, abstractKQueueChannel.socket.intValue(), s2, s3, i2);
    }

    public int fd(int i2) {
        return PlatformDependent.getInt(getKEventOffset(i2) + ((long) KQUEUE_IDENT_OFFSET));
    }

    public short fflags(int i2) {
        return PlatformDependent.getShort(getKEventOffset(i2) + ((long) KQUEUE_FFLAGS_OFFSET));
    }

    public short filter(int i2) {
        return PlatformDependent.getShort(getKEventOffset(i2) + ((long) KQUEUE_FILTER_OFFSET));
    }

    public short flags(int i2) {
        return PlatformDependent.getShort(getKEventOffset(i2) + ((long) KQUEUE_FLAGS_OFFSET));
    }

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

    public long getKEventOffset(int i2) {
        return this.memoryAddress + ((long) (i2 * KQUEUE_EVENT_SIZE));
    }

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

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

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