Example #1
0
        public static void NoRetainAfterDispose()
        {
            OwnedMemory <int> block = MemoryPool <int> .Shared.Rent(42);

            block.Dispose();
            Assert.Throws <ObjectDisposedException>(() => block.Retain());
        }
        static void MemoryHandleDoubleFreeAuto(OwnedMemory <byte> buffer)
        {
            var memory = buffer.Memory;
            var handle = memory.Retain(pin: true);

            buffer.Retain();
            handle.Dispose();
            handle.Dispose();
            Assert.False(buffer.Release());
        }
Example #3
0
        public void SetMemory(OwnedMemory <byte> buffer, int start, int end, bool readOnly = false)
        {
            _owned = buffer;
            _owned.Retain();
            _buffer = _owned.Memory;

            RunningLength = 0;
            Start         = start;
            End           = end;
            Next          = null;
        }
Example #4
0
        public static void RefCounting()
        {
            using (OwnedMemory <int> block = MemoryPool <int> .Shared.Rent(42))
            {
                block.Retain();
                block.Retain();
                block.Retain();

                bool moreToGo;
                moreToGo = block.Release();
                Assert.True(moreToGo);

                moreToGo = block.Release();
                Assert.True(moreToGo);

                moreToGo = block.Release();
                Assert.False(moreToGo);

                Assert.Throws <InvalidOperationException>(() => block.Release());
            }
        }
Example #5
0
        public void SetMemory(OwnedMemory <byte> ownedMemory, int start, int end, bool readOnly = false)
        {
            _ownedMemory = ownedMemory;
            _ownedMemory.Retain();

            _memory = _ownedMemory.Memory;

            ReadOnly      = readOnly;
            RunningLength = 0;
            Start         = start;
            End           = end;
            Next          = null;
        }
Example #6
0
        public PreservedBuffer <T> PreserveBuffer(int length)
        {
            if (length <= _smallTreshhold)
            {
                if (_sharedBuffer == null)
                {
                    _sharedBuffer = BufferPool <T> .RentOwnedBuffer(_sharedBufferSize, false);

                    // NB we must create a reference or the first PreservedBuffer could
                    // dispose _sharedBuffer on PreservedBuffer disposal.
                    _sharedBuffer.Retain();
                    _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.Retain();
                    previous.Release();
                    _sharedBufferOffset = 0;
                    newOffset           = length;
                }
                var buffer = _sharedBuffer.Memory.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.Memory.Slice(0, length);

            return(new PreservedBuffer <T>(buffer2));
        }
Example #7
0
        public void ReleasedBlockWorks()
        {
            using (var pool = new MemoryPool())
            {
                var block1 = pool.Rent(1);
                block1.Retain();
                block1.Release();

                OwnedMemory <byte> block2 = null;

                // Lease-return until we get same block
                while (block1 != block2)
                {
                    block2 = pool.Rent(1);
                    block2.Retain();
                    block2.Release();
                }

                Assert.True(block2.Span.Length > 0);
            }
        }
Example #8
0
 public override void Retain()
 {
     _pool.CheckDisposed();
     _ownedMemory.Retain();
     Interlocked.Increment(ref _referenceCount);
 }