using System; using System.Threading; namespace base_kcp { public class RefArrayAccessUtil { public static readonly int RefBufferPad = 64 * 2 / IntPtr.Size; /// /// A plain store (no ordering/fences) of an element to a given offset. /// /// The element type. /// The source buffer. /// Computed via /// An orderly kitty. public static void SpElement(T[] buffer, long offset, T e) => buffer[offset] = e; /// /// An ordered store(store + StoreStore barrier) of an element to a given offset. /// /// The element type. /// The source buffer. /// Computed via /// public static void SoElement(T[] buffer, long offset, T e) where T : class => Volatile.Write(ref buffer[offset], e); /// /// A plain load (no ordering/fences) of an element from a given offset. /// /// The element type. /// The source buffer. /// Computed via /// The element at the given in the given . public static T LpElement(T[] buffer, long offset) => buffer[offset]; /// /// A volatile load (load + LoadLoad barrier) of an element from a given offset. /// /// The element type. /// The source buffer. /// Computed via /// The element at the given in the given . public static T LvElement(T[] buffer, long offset) where T : class => Volatile.Read(ref buffer[offset]); /// /// Gets the offset in bytes within the array for a given index. /// /// The desired element index. /// Mask for the index. /// The offset (in bytes) within the array for a given index. public static long CalcElementOffset(long index, long mask) => RefBufferPad + (index & mask); } }