public BufferSegment(OwnedBuffer <byte> buffer) { _owned = buffer; Start = 0; End = 0; _owned.AddReference(); _buffer = _owned.Buffer; }
public void MemoryHandleDoubleFree() { var array = new byte[1024]; OwnedBuffer <byte> owned = array; var memory = owned.Buffer; var h = memory.Pin(); Assert.True(owned.HasOutstandingReferences); owned.AddReference(); Assert.True(owned.HasOutstandingReferences); h.Free(); Assert.True(owned.HasOutstandingReferences); h.Free(); Assert.True(owned.HasOutstandingReferences); owned.Release(); Assert.False(owned.HasOutstandingReferences); }
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.AddReference(); _buffer = _owned.Buffer; }
public PreservedBuffer <T> PreserveBuffer(int length) { if (length <= _smallTreshhold) { if (_sharedBuffer == null) { _sharedBuffer = BufferPool <T> .RentOwnedBuffer(this._sharedBufferSize, false); // NB we must create a reference or the first PreservedBuffer could // dispose _sharedBuffer on PreservedBuffer disposal. _sharedBuffer.AddReference(); _sharedBufferOffset = 0; } var bufferSize = _sharedBuffer.Length; var newOffset = _sharedBufferOffset + length; if (newOffset > bufferSize) { // replace shared buffer, the old one will be disposed // when all ReservedMemory views on it are disposed var previous = _sharedBuffer; _sharedBuffer = BufferPool <T> .RentOwnedBuffer(_sharedBufferSize, false); _sharedBuffer.AddReference(); previous.Release(); _sharedBufferOffset = 0; newOffset = length; } var buffer = _sharedBuffer.Buffer.Slice(_sharedBufferOffset, length); _sharedBufferOffset = newOffset; return(new PreservedBuffer <T>(buffer)); } // NB here we exclusively own the buffer and disposal of PreservedBuffer will cause // disposal and returning to pool of the ownedBuffer instance, unless references were added via // PreservedBuffer.Close() or PreservedBuffer.Buffer.Reserve()/Pin() methods var ownedBuffer = BufferPool <T> .RentOwnedBuffer(length, false); var buffer2 = ownedBuffer.Buffer.Slice(0, length); return(new PreservedBuffer <T>(buffer2)); }