void OnReadUnix(UVBuffer.Unix buffer, IntPtr bytesAvaliable) { long bytesRead = bytesAvaliable.ToInt64(); if (bytesRead == 0) { return; } else if (bytesRead < 0) { var error = UVException.ErrorCodeToError((int)bytesRead); if (error == UVError.EOF) { OnEndOfStream(); Dispose(); } else { Dispose(); throw new UVException((int)bytesRead); } } else { using (var owned = new OwnedNativeBuffer((int)bytesRead, buffer.Buffer)) { OnReadCompleted(owned.Memory); //buffer.Dispose(); // TODO: owned memory frees the memory. this is bad; need to fix } } }
public void SimpleTestS() { using (var owned = new OwnedNativeBuffer(1024)) { var span = owned.Span; span[10] = 10; unsafe { Assert.Equal(10, owned.Pointer[10]); } var memory = owned.Buffer; var array = memory.ToArray(); Assert.Equal(owned.Length, array.Length); Assert.Equal(10, array[10]); Span <byte> copy = new byte[20]; memory.Slice(10, 20).CopyTo(copy); Assert.Equal(10, copy[0]); } using (OwnedPinnedBuffer <byte> owned = new byte[1024]) { var span = owned.Span; span[10] = 10; Assert.Equal(10, owned.Array[10]); unsafe { Assert.Equal(10, owned.Pointer[10]); } var memory = owned.Buffer; var array = memory.ToArray(); Assert.Equal(owned.Length, array.Length); Assert.Equal(10, array[10]); Span <byte> copy = new byte[20]; memory.Slice(10, 20).CopyTo(copy); Assert.Equal(10, copy[0]); } }
void OnReadWindows(UVBuffer.Windows buffer, IntPtr bytesAvaliable) { // TODO: all branches need to release buffer, I think long bytesRead = bytesAvaliable.ToInt64(); if (bytesRead == 0) { buffer.Dispose(); return; } else if (bytesRead < 0) { var error = UVException.ErrorCodeToError((int)bytesRead); if (error == UVError.EOF) { OnEndOfStream(); Dispose(); buffer.Dispose(); } else if (error == UVError.ECONNRESET) { Debug.Assert(buffer.Buffer == IntPtr.Zero && buffer.Length == 0); // no need to dispose // TODO: what should we do here? } else { Dispose(); buffer.Dispose(); throw new UVException((int)bytesRead); } } else { using (var owned = new OwnedNativeBuffer((int)bytesRead, buffer.Buffer)) { OnReadCompleted(owned.Memory); //buffer.Dispose(); // TODO: owned memory frees the memory. this is bad; need to fix } } }
public void NativeMemoryLifetime() { var owner = new OwnedNativeBuffer(1024); TestLifetime(owner); }