public void SimpleTestS() { using(var owned = new OwnedNativeMemory(1024)) { var span = owned.Span; span[10] = 10; unsafe { Assert.Equal(10, owned.Pointer[10]); } var memory = owned.Memory; var array = memory.ToArray(); Assert.Equal(owned.Length, array.Length); Assert.Equal(10, array[10]); var copy = new byte[20]; memory.Slice(10, 20).CopyTo(copy); Assert.Equal(10, copy[0]); } using (OwnedPinnedArray<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.Memory; var array = memory.ToArray(); Assert.Equal(owned.Length, array.Length); Assert.Equal(10, array[10]); var copy = new byte[20]; memory.Slice(10, 20).CopyTo(copy); Assert.Equal(10, copy[0]); } }
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 OwnedNativeMemory((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 OwnedNativeMemory(1024)) { var span = owned.Span; span[10] = 10; unsafe { Assert.Equal(10, owned.Pointer[10]); } var memory = owned.Memory; 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 (OwnedPinnedArray <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.Memory; 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]); } }
public void NativeMemoryLifetime() { Memory<byte> copyStoredForLater; using (var owner = new OwnedNativeMemory(1024)) { Memory<byte> memory = owner.Memory; Memory<byte> memorySlice = memory.Slice(10); copyStoredForLater = memorySlice; using (memorySlice.Reserve()) { Assert.Throws<InvalidOperationException>(() => { // memory is reserved; cannot dispose owner.Dispose(); }); Span<byte> span = memorySlice.Span; span[0] = 255; } } Assert.Throws<ObjectDisposedException>(() => { // manager is disposed var span = copyStoredForLater.Span; }); }
public void NativeMemoryLifetime() { Memory <byte> copyStoredForLater; using (var owner = new OwnedNativeMemory(1024)) { Memory <byte> memory = owner.Memory; Memory <byte> memorySlice = memory.Slice(10); copyStoredForLater = memorySlice; using (memorySlice.Reserve()) { Assert.Throws <InvalidOperationException>(() => { // memory is reserved; cannot dispose owner.Dispose(); }); Span <byte> span = memorySlice.Span; span[0] = 255; } } Assert.Throws <ObjectDisposedException>(() => { // manager is disposed var span = copyStoredForLater.Span; }); }
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 OwnedNativeMemory((int)bytesRead, buffer.Buffer)) { OnReadCompleted(owned.Memory); //buffer.Dispose(); // TODO: owned memory frees the memory. this is bad; need to fix } } }
public void SimpleTestS() { { OwnedArray <byte> memory = new byte[1024]; var span = memory.Span; span[10] = 10; Assert.Equal(10, memory.Array[10]); } using (var memory = new OwnedNativeMemory(1024)) { var span = memory.Span; span[10] = 10; unsafe { Assert.Equal(10, memory.Pointer[10]); } } using (OwnedPinnedArray <byte> memory = new byte[1024]) { var span = memory.Span; span[10] = 10; Assert.Equal(10, memory.Array[10]); unsafe { Assert.Equal(10, memory.Pointer[10]); } } }
public void NativeMemoryLifetime() { Memory <byte> copyStoredForLater; var owner = new OwnedNativeMemory(1024); try { Memory <byte> memory = owner.Memory; Memory <byte> memorySlice = memory.Slice(10); copyStoredForLater = memorySlice; var r = memorySlice.Reserve(); try { Assert.Throws <InvalidOperationException>(() => { // memory is reserved; cannot dispose owner.Dispose(); }); Assert.Throws <ObjectDisposedException>(() => { // memory is disposed Span <byte> span = memorySlice.Span; span[0] = 255; }); } finally { Assert.Throws <ObjectDisposedException>(() => { // memory is disposed r.Dispose(); }); } } finally { Assert.Throws <InvalidOperationException>(() => { // memory is still reserved as Release failed. owner.Dispose(); }); } Assert.Throws <ObjectDisposedException>(() => { // manager is disposed var span = copyStoredForLater.Span; }); }
public void NativeMemoryLifetime() { var owner = new OwnedNativeMemory(1024); TestLifetime(owner); }