package org.jctools.queues;

import org.jctools.util.InternalAPI;
import org.jctools.util.UnsafeRefArrayAccess;

import java.util.Arrays;

import static org.jctools.util.UnsafeAccess.UNSAFE;
import static org.jctools.util.UnsafeAccess.fieldOffset;
import static org.jctools.util.UnsafeLongArrayAccess.*;
import static org.jctools.util.UnsafeRefArrayAccess.allocateRefArray;

@InternalAPI
final class MpmcUnboundedXaddChunk<E>
{
    private static final long PREV_OFFSET = fieldOffset(MpmcUnboundedXaddChunk.class, "prev");
    private static final long NEXT_OFFSET = fieldOffset(MpmcUnboundedXaddChunk.class, "next");
    private static final long INDEX_OFFSET = fieldOffset(MpmcUnboundedXaddChunk.class, "index");

    final static int NIL_CHUNK_INDEX = -1;

    private final E[] buffer;
    private final long[] sequence;

    private volatile MpmcUnboundedXaddChunk<E> prev;
    private volatile long index;
    private volatile MpmcUnboundedXaddChunk<E> next;

    MpmcUnboundedXaddChunk(long index, MpmcUnboundedXaddChunk<E> prev, int size, boolean pooled)
    {
        buffer = allocateRefArray(size);
        // next is null
        spPrev(prev);
        spIndex(index);
        if (pooled)
        {
            sequence = allocateLongArray(size);
            Arrays.fill(sequence, MpmcUnboundedXaddChunk.NIL_CHUNK_INDEX);
        }
        else
        {
            sequence = null;
        }
    }

    boolean isPooled()
    {
        return sequence != null;
    }

    private static long calcSequenceOffset(long index)
    {
        return calcLongElementOffset(index);
    }

    void soSequence(int index, long e)
    {
        soLongElement(sequence, calcSequenceOffset(index), e);
    }

    long lvSequence(int index)
    {
        return lvLongElement(sequence, calcSequenceOffset(index));
    }

    MpmcUnboundedXaddChunk<E> lvNext()
    {
        return next;
    }

    MpmcUnboundedXaddChunk<E> lpPrev()
    {
        return (MpmcUnboundedXaddChunk<E>) UNSAFE.getObject(this, PREV_OFFSET);
    }

    long lvIndex()
    {
        return index;
    }

    void soIndex(long index)
    {
        UNSAFE.putOrderedLong(this, INDEX_OFFSET, index);
    }

    void spIndex(long index)
    {
        UNSAFE.putLong(this, INDEX_OFFSET, index);
    }

    void soNext(MpmcUnboundedXaddChunk<E> value)
    {
        UNSAFE.putOrderedObject(this, NEXT_OFFSET, value);
    }

    void spPrev(MpmcUnboundedXaddChunk<E> value)
    {
        UNSAFE.putObject(this, PREV_OFFSET, value);
    }

    void soElement(int index, E e)
    {
        UnsafeRefArrayAccess.soRefElement(buffer, UnsafeRefArrayAccess.calcRefElementOffset(index), e);
    }

    E lvElement(int index)
    {
        return UnsafeRefArrayAccess.lvRefElement(buffer, UnsafeRefArrayAccess.calcRefElementOffset(index));
    }
}

@@ -1,8 +1,566 @@
+/*%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A * http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A
 package 
@@ -620,54 +620,8 @@
 API;
-%0Aimport org.jctools.util.UnsafeRefArrayAccess;
 %0A%0Aim
@@ -861,32 +861,17 @@
 yAccess.
-allocateRefArray
+*
 ;%0A%0A@Inte
@@ -1227,31 +1227,30 @@
 tic int 
-NIL_
 CHUNK_
-INDEX
+CONSUMED
  = -1;%0A%0A
@@ -1632,17 +1632,17 @@
        s
-p
+o
 Prev(pre
@@ -1808,23 +1808,22 @@
 unk.
-NIL_
 CHUNK_
-INDEX
+CONSUMED
 );%0A 
@@ -1973,121 +1973,8 @@
  %7D%0A%0A
-    private static long calcSequenceOffset(long index)%0A    %7B%0A        return calcLongElementOffset(index);%0A    %7D%0A%0A
     
@@ -2046,32 +2046,35 @@
 quence, calc
-Sequence
+LongElement
 Offset(index
@@ -2167,24 +2167,27 @@
 ce, calc
-Sequence
+LongElement
 Offset(i
@@ -2784,17 +2784,17 @@
   void s
-p
+o
 Prev(Mpm
@@ -2941,71 +2941,29 @@
     
-UnsafeRefArrayAccess.soRefElement(buffer, UnsafeRefArrayAccess.
+soRefElement(buffer, 
 calc
@@ -3050,71 +3050,29 @@
 urn 
-UnsafeRefArrayAccess.lvRefElement(buffer, UnsafeRefArrayAccess.
+lvRefElement(buffer, 
 calc

Add license