public static void Concatenation(MemoryAllocator <char> allocator) { MemoryOwner <char> owner = string.Empty.AsSpan().Concat(string.Empty, allocator); True(owner.IsEmpty); True(owner.Memory.IsEmpty); owner.Dispose(); owner = "Hello, ".AsSpan().Concat("world!", allocator); False(owner.IsEmpty); False(owner.Memory.IsEmpty); Equal("Hello, world!", new string(owner.Memory.Span)); owner.Dispose(); owner = "Hello, ".AsSpan().Concat("world", "!", allocator); False(owner.IsEmpty); False(owner.Memory.IsEmpty); Equal("Hello, world!", new string(owner.Memory.Span)); owner.Dispose(); }
protected virtual void Dispose(bool disposing) { if (!disposedValue) { if (disposing) { chunk.Dispose(); } chunk = null; disposedValue = true; } }
/// <summary> /// Runs a test for a pair of input text files /// </summary> /// <param name="expected">The expected results for the test</param> /// <param name="oldText">The reference text to compare to</param> /// <param name="newText">The updated text to compare</param> private static void Test(LineModificationType[] expected, string oldText, string newText) { MemoryOwner <LineModificationType> result = LineDiffer.ComputeDiff(oldText, newText, '\r'); try { Assert.AreEqual(expected.Length, result.Length); if (expected.Length == 0) { return; } Assert.IsTrue(expected.SequenceEqual(result.Span.ToArray())); } finally { result.Dispose(); } }
public void Dispose() { MemoryOwner.Dispose(); NextElement?.Dispose(); }
public void Dispose() { Stack?.Dispose(); UndoData?.Dispose(); }
async ValueTask IBufferedMessage.LoadFromAsync(IDataTransferObject source, CancellationToken token) { buffer.Dispose(); buffer = await source.ToMemoryAsync(token : token).ConfigureAwait(false); }
public void Dispose() => owner.Dispose();
public async Task <T> ReadMessage <T>(int size, CancellationToken ct = default, UInt32?checksum = null) where T : IStreamable, new() { top: if (BufferedData >= size) { //we have enough data buffered if (checksum.HasValue) { var chk = BitConverter.ToUInt32(InternalBuffer.Span.Slice(0, size).SHA256d()); if (chk != checksum.Value) { //skip this payload because checksum failed if (BufferedData > size) { InternalBuffer.Span.Slice(size, BufferedData - size).CopyTo(InternalBuffer.Span); BufferedData -= size; } throw new Exception("Checksum failed"); } } var obj = new T(); obj.ReadFromPayload(InternalBuffer.Span); if (BufferedData > size) { //copy extra data to start of the buffer InternalBuffer.Span.Slice(size, BufferedData - size).CopyTo(InternalBuffer.Span); } BufferedData -= size; return(obj); } else { //resize the buffer if (size > InternalBuffer.Length) { if (size > MemoryPool <byte> .Shared.MaxBufferSize) { throw new Exception($"Buffer size is larger than the max size {MemoryPool<byte>.Shared.MaxBufferSize}"); } var newowner = MemoryPool <byte> .Shared.Rent((int)(Math.Ceiling(size / (decimal)InternalBufferSize) * InternalBufferSize)); var newbuf = newowner.Memory; if (BufferedData > 0) { InternalBuffer.Slice(0, BufferedData).CopyTo(newbuf); } MemoryOwner.Dispose(); MemoryOwner = newowner; InternalBuffer = newbuf; } read_more: try { var rlen = await Stream.ReadAsync(InternalBuffer.Slice(BufferedData), ct); BufferedData += rlen; if (rlen == 0) { return(default);
public void Dispose() { MemoryOwner?.Dispose(); MemoryOwner = null; }