package org.jctools.queues;

import org.jctools.util.JvmInfo;
import org.jctools.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> 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);
    }
}

@@ -22,16 +22,53 @@
 ueues;%0A%0A
+import org.jctools.util.InternalAPI;%0A
 import o
@@ -139,16 +139,29 @@
 ccess;%0A%0A
+@InternalAPI%0A
 public f
@@ -206,17 +206,17 @@
 lculator
- 
+%0A
 %7B%0A    st
@@ -284,16 +284,17 @@
 Y_BASE;%0A
+%0A
     stat
@@ -295,16 +295,20 @@
   static
+%0A   
  %7B%0A     
@@ -671,67 +671,8 @@
  %7D%0A%0A
-    private PaddedCircularArrayOffsetCalculator() %7B%0A    %7D%0A%0A
     
@@ -750,16 +750,20 @@
 apacity)
+%0A   
  %7B%0A     
@@ -1044,16 +1044,20 @@
 ng mask)
+%0A   
  %7B%0A     

Format and mark as InternalAPI