private static void SwapContents(ref MemorySource <T> a, ref MemorySource <T> b) { IMemoryOwner <T> tempOwner = a.MemoryOwner; Memory <T> tempMemory = a.Memory; a.MemoryOwner = b.MemoryOwner; a.Memory = b.Memory; b.MemoryOwner = tempOwner; b.Memory = tempMemory; }
public static Buffer2D <T> Allocate2D <T>( this MemoryAllocator memoryAllocator, int width, int height, AllocationOptions options = AllocationOptions.None) where T : struct { IMemoryOwner <T> buffer = memoryAllocator.Allocate <T>(width * height, options); var memorySource = new MemorySource <T>(buffer, true); return(new Buffer2D <T>(memorySource, width, height)); }
/// <summary> /// Swaps the contents of 'destination' with 'source' if the buffers are owned (1), /// copies the contents of 'source' to 'destination' otherwise (2). Buffers should be of same size in case 2! /// </summary> public static void SwapOrCopyContent(ref MemorySource <T> destination, ref MemorySource <T> source) { if (source.HasSwappableContents && destination.HasSwappableContents) { SwapContents(ref destination, ref source); } else { if (destination.Memory.Length != source.Memory.Length) { throw new InvalidOperationException("SwapOrCopyContents(): buffers should both owned or the same size!"); } source.Memory.CopyTo(destination.Memory); } }
/// <summary> /// Initializes a new instance of the <see cref="Buffer2D{T}"/> class. /// </summary> /// <param name="memorySource">The buffer to wrap</param> /// <param name="width">The number of elements in a row</param> /// <param name="height">The number of rows</param> public Buffer2D(MemorySource <T> memorySource, int width, int height) { this.memorySource = memorySource; this.Width = width; this.Height = height; }