public BufferHandle Retain(bool pin = false) { BufferHandle bufferHandle; if (pin) { if (_owner != null) { bufferHandle = _owner.Pin(_index); } else { var handle = GCHandle.Alloc(_array, GCHandleType.Pinned); unsafe { var pointer = OwnedBuffer <T> .Add((void *)handle.AddrOfPinnedObject(), _index); bufferHandle = new BufferHandle(null, pointer, handle); } } } else { if (_owner != null) { _owner.Retain(); } bufferHandle = new BufferHandle(_owner); } return(bufferHandle); }
static void Dispose(OwnedBuffer <byte> buffer) { var length = buffer.Length; buffer.Dispose(); Assert.True(buffer.IsDisposed); Assert.False(buffer.IsRetained); Assert.ThrowsAny <ObjectDisposedException>(() => { buffer.AsSpan(); }); Assert.ThrowsAny <ObjectDisposedException>(() => { buffer.AsSpan(0, length); }); Assert.ThrowsAny <ObjectDisposedException>(() => { buffer.Pin(); }); Assert.ThrowsAny <ObjectDisposedException>(() => { var rwBuffer = buffer.Buffer; }); Assert.ThrowsAny <ObjectDisposedException>(() => { var roBuffer = buffer.ReadOnlyBuffer; }); }