All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jctools_voltpatches.queues.PaddedCircularArrayOffsetCalculator Maven / Gradle / Ivy

There is a newer version: 13.3.2-preview1
Show newest version
package org.jctools_voltpatches.queues;

import org.jctools_voltpatches.util.JvmInfo;
import org.jctools_voltpatches.util.UnsafeRefArrayAccess;

public final class PaddedCircularArrayOffsetCalculator {
    static final int REF_BUFFER_PAD;
    static final long REF_ARRAY_BASE;
    static {
        // 2 cache lines pad
        REF_BUFFER_PAD = (JvmInfo.CACHE_LINE_SIZE * 2) >> UnsafeRefArrayAccess.REF_ELEMENT_SHIFT;
        // Including the buffer pad in the array base offset
        final int paddingOffset = REF_BUFFER_PAD << UnsafeRefArrayAccess.REF_ELEMENT_SHIFT;
        REF_ARRAY_BASE = UnsafeRefArrayAccess.REF_ARRAY_BASE + paddingOffset;
    }

    private PaddedCircularArrayOffsetCalculator() {
    }

    @SuppressWarnings("unchecked")
    public static  E[] allocate(int capacity) {
        return (E[]) new Object[capacity + REF_BUFFER_PAD * 2];
    }

    /**
     * @param index desirable element index
     * @param mask
     * @return the offset in bytes within the array for a given index.
     */
    protected static long calcElementOffset(long index, long mask) {
        return REF_ARRAY_BASE + ((index & mask) << UnsafeRefArrayAccess.REF_ELEMENT_SHIFT);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy