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); }
public BufferSegment(OwnedBuffer <byte> buffer) { _owned = buffer; Start = 0; End = 0; _owned.Retain(); _buffer = _owned.Buffer; }
static void MemoryHandleDoubleFree(OwnedBuffer <byte> buffer) { var memory = buffer.Buffer; var handle = memory.Retain(pin: true); buffer.Retain(); handle.Dispose(); handle.Dispose(); Assert.False(buffer.Release()); }
static void MemoryHandleDoubleFree(OwnedBuffer <byte> buffer) { var memory = buffer.Buffer; var h = memory.Pin(); Assert.True(buffer.IsRetained); buffer.Retain(); Assert.True(buffer.IsRetained); h.Dispose(); Assert.True(buffer.IsRetained); h.Dispose(); Assert.True(buffer.IsRetained); buffer.Release(); Assert.False(buffer.IsRetained); }
public void MemoryHandleDoubleFree() { var array = new byte[1024]; OwnedBuffer <byte> owned = array; var memory = owned.Buffer; var h = memory.Pin(); Assert.True(owned.IsRetained); owned.Retain(); Assert.True(owned.IsRetained); h.Dispose(); Assert.True(owned.IsRetained); h.Dispose(); Assert.True(owned.IsRetained); owned.Release(); Assert.False(owned.IsRetained); }
public BufferSegment(OwnedBuffer <byte> buffer, int start, int end) { _owned = buffer; Start = start; End = end; ReadOnly = true; // For unowned buffers, we need to make a copy here so that the caller can // give up the give this buffer back to the caller var unowned = buffer as UnownedBuffer; if (unowned != null) { _owned = unowned.MakeCopy(start, end - start, out Start, out End); } _owned.Retain(); _buffer = _owned.Buffer; }
public void ReleasedBlockWorks() { using (var pool = new MemoryPool()) { var block1 = pool.Rent(1); block1.Retain(); block1.Release(); OwnedBuffer <byte> block2 = null; // Lease-return until we get same block while (block1 != block2) { block2 = pool.Rent(1); block2.Retain(); block2.Release(); } Assert.True(block2.AsSpan().Length > 0); } }