internal static byte[] DangerousGetUnderlyingArray(this ImmutableArray <byte> array) { var union = new ByteArrayUnion(); union.ImmutableArray = array; return(union.MutableArray); }
/// <summary> /// Access the backing mutable array instance for the given <see cref="ImmutableArray{T}"/>, without /// creating a defensive copy. It is the responsibility of the caller to ensure the array is not modified /// through the returned mutable reference. Do not mutate the returned array. /// </summary> /// <param name="array">The <see cref="ImmutableArray{T}"/> from which to retrieve the backing field.</param> /// <remarks> /// Users of this method should take extra care to ensure that the returned mutable array is never modified. /// The returned mutable array continues to be used as the backing field of the given <see cref="ImmutableArray{T}"/> /// without creating a defensive copy, so changes made to the returned mutable array will be observable /// on the given <see cref="ImmutableArray{T}"/>. Instance and static methods of <see cref="ImmutableArray{T}"/> /// and <see cref="ImmutableArray"/> may malfunction if they operate on an <see cref="ImmutableArray{T}"/> instance /// whose underlying backing field is modified. /// </remarks> /// <returns>The underlying array, or null if <see cref="ImmutableArray{T}.IsDefault"/> is true.</returns> internal static byte[]? DangerousGetUnderlyingArray(ImmutableArray <byte> array) { ByteArrayUnion union = default; union.ImmutableArray = array; return(union.UnderlyingArray); }
internal static ImmutableArray<byte> DangerousCreateFromUnderlyingArray(ref byte[] array) { var union = new ByteArrayUnion(); union.MutableArray = array; array = null; return union.ImmutableArray; }
// Avoid unnecessary copy private static byte[] DangerousGetUnderlyingArray(ImmutableArray <byte> array) { var union = new ByteArrayUnion(); union.ImmutableArray = array; return(union.UnderlyingArray); }
internal static ImmutableArray <byte> DangerousToImmutableArray(ref byte[] array) { var union = new ByteArrayUnion(); union.MutableArray = array; array = null; return(union.ImmutableArray); }
internal static ImmutableArray <byte> DangerousCreateFromUnderlyingArray(ref byte[] array) { ByteArrayUnion union = new ByteArrayUnion(); union.MutableArray = array; array = null; return(union.ImmutableArray); }
/// <summary> /// Creates a new instance of <see cref="ImmutableArray{Byte}"/> using a given mutable array as the backing /// field, without creating a defensive copy. It is the responsibility of the caller to ensure no other mutable /// references exist to the array. Do not mutate the array after calling this method. /// </summary> /// <param name="array">The mutable array to use as the backing field. The incoming reference is set to null /// since it should not be retained by the caller.</param> /// <remarks> /// Users of this method should take extra care to ensure that the mutable array given as a parameter /// is never modified. The returned <see cref="ImmutableArray{T}"/> will use the given array as its backing /// field without creating a defensive copy, so changes made to the given mutable array will be observable /// on the returned <see cref="ImmutableArray{T}"/>. Instance and static methods of <see cref="ImmutableArray{T}"/> /// and <see cref="ImmutableArray"/> may malfunction if they operate on an <see cref="ImmutableArray{T}"/> instance /// whose underlying backing field is modified. /// </remarks> /// <returns>An immutable array.</returns> internal static ImmutableArray <byte> DangerousCreateFromUnderlyingArray(ref byte[]?array) { byte[] givenArray = array !; array = null; ByteArrayUnion union = default; union.UnderlyingArray = givenArray; return(union.ImmutableArray); }
public long ReadArray <T>(T[, ,] array, long count) where T : struct { if (count < 1) { return(0); } unsafe { var sizeOfT = Marshal.SizeOf(typeof(T)); var hack = new ByteArrayUnion(); hack.structs = array; var bytesToRead = (int)(sizeOfT * count); #if __MonoCS__ var skip = 0; #else var skip = 3 * 2 * sizeof(int); #endif IntPtr byteLen = (IntPtr)(array.Length * sizeOfT + skip); var offset = skip; fixed(byte *pBytes = hack.bytes) { IntPtr *pId = (IntPtr *)(pBytes - 2 * IntPtr.Size), pLen = (IntPtr *)(pBytes - IntPtr.Size); IntPtr backupId = *pId, backupLen = *pLen; *pId = s_byteId; *pLen = byteLen; do { int finished = base.Read(hack.bytes, offset, bytesToRead); if (finished == 0) { break; } offset += finished; bytesToRead -= finished; }while (bytesToRead > 0); *pId = backupId; *pLen = backupLen; } return((long)((offset - skip) / sizeOfT)); } }
public void WriteArray <T>(T[, ,] array, long count) where T : struct { if (count < 1) { return; } unsafe { var sizeOfT = Marshal.SizeOf(typeof(T)); var hack = new ByteArrayUnion(); hack.structs = array; fixed(byte *pBytes = hack.bytes) { #if __MonoCS__ long offset = 0; #else long offset = 3 * 2 * sizeof(int); #endif int itemsPerBlock = c_bufferSize / sizeOfT; do { int blockSize = count > (long)itemsPerBlock ? itemsPerBlock : (int)count; var byteBlockSize = blockSize * sizeOfT; fixed(byte *p = m_buffer) { for (int i = 0; i < byteBlockSize; i++) { p[i] = pBytes[offset++]; } } base.Write(m_buffer, 0, byteBlockSize); count -= (long)blockSize; }while (count > 0); } } }
// Avoid unnecessary copy private static byte[] DangerousGetUnderlyingArray(ImmutableArray<byte> array) { var union = new ByteArrayUnion(); union.ImmutableArray = array; return union.UnderlyingArray; }
internal static byte[] DangerousGetUnderlyingArray(this ImmutableArray<byte> array) { var union = new ByteArrayUnion(); union.ImmutableArray = array; return union.MutableArray; }