コード例 #1
0
        static void TestLifetime(OwnedBuffer <byte> owned)
        {
            Buffer <byte> copyStoredForLater;

            try {
                Buffer <byte> memory      = owned.Buffer;
                Buffer <byte> memorySlice = memory.Slice(10);
                copyStoredForLater = memorySlice;
                var r = memorySlice.Reserve();
                try {
                    Assert.Throws <InvalidOperationException>(() => { // memory is reserved; premature dispose check fires
                        owned.Dispose();
                    });
                    Assert.Throws <ObjectDisposedException>(() => {
                        // memory is disposed
                        Span <byte> span = memorySlice.Span;
                        span[0]          = 255;
                    });
                }
                finally {
                    r.Dispose(); // release reservation
                }
            }
            finally {
                owned.Dispose(); // can finish dispose with no exception
            }
            Assert.Throws <ObjectDisposedException>(() => {
                // memory is disposed; cannot use copy stored for later
                var span = copyStoredForLater.Span;
            });
        }
コード例 #2
0
        static void BufferLifetimeBasics(OwnedBuffer <byte> buffer)
        {
            Buffer <byte> copyStoredForLater;

            try
            {
                Buffer <byte> memory = buffer.Buffer;
                Buffer <byte> slice  = memory.Slice(10);
                copyStoredForLater = slice;
                var handle = slice.Retain();
                try
                {
                    Assert.Throws <InvalidOperationException>(() =>
                    { // memory is reserved; premature dispose check fires
                        buffer.Dispose();
                    });
                }
                finally
                {
                    handle.Dispose(); // release reservation
                }
            }
            finally
            {
                buffer.Dispose(); // can finish dispose with no exception
            }
            Assert.ThrowsAny <ObjectDisposedException>(() =>
            {
                // memory is disposed; cannot use copy stored for later
                var span = copyStoredForLater.Span;
            });
        }
コード例 #3
0
        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;
            });
        }
コード例 #4
0
        public int Read(IntPtr ptr, out string value)
        {
            var version = Marshal.ReadInt32(ptr + 4);

            if (version != 0)
            {
                throw new NotSupportedException();
            }
            var length = Marshal.ReadInt32(ptr);
            OwnedBuffer <byte> ownedBuffer = Buffers.BufferPool.UseTempBuffer(length);
            var buffer = ownedBuffer.Buffer;
            var handle = buffer.Pin();

            try
            {
                if (buffer.TryGetArray(out var segment))
                {
                    Marshal.Copy(ptr + 8, segment.Array, 0, length);

                    value = Encoding.UTF8.GetString(segment.Array, segment.Offset, length - 8);
                }
                else
                {
                    throw new ApplicationException();
                }
                return(length);
            }
            finally
            {
                handle.Free();
                ownedBuffer.Dispose();
            }
        }
コード例 #5
0
ファイル: BufferSegment.cs プロジェクト: nietras/corefxlab
        public void Dispose()
        {
            Debug.Assert(_owned.HasOutstandingReferences);

            _owned.Release();

            if (!_owned.HasOutstandingReferences)
            {
                _owned.Dispose();
            }
        }
コード例 #6
0
 public void Dispose()
 {
     if (_pool == null)
     {
         throw new InvalidOperationException("only root object can (and should) be disposed.");
     }
     _db     = ReadOnlySpan <byte> .Empty;
     _values = ReadOnlySpan <byte> .Empty;
     _dbMemory.Dispose();
     _dbMemory = null;
 }
コード例 #7
0
        public void Dispose()
        {
            Debug.Assert(_owned.IsRetained);

            _owned.Release();

            if (!_owned.IsRetained)
            {
                _owned.Dispose();
            }
        }
コード例 #8
0
ファイル: MemoryTests.cs プロジェクト: nietras/corefxlab
        public void TestThrowOnAccessAfterDipose()
        {
            var array = new byte[1024];
            OwnedBuffer <byte> owned = array;
            var span = owned.Span;

            Assert.Equal(array.Length, span.Length);

            owned.Dispose();

            Assert.Throws <ObjectDisposedException>(() => {
                var spanDisposed = owned.Span;
            });
        }
コード例 #9
0
        private void ResizeDb()
        {
            var oldData    = _scratchMemory.Span;
            var newScratch = _pool.Rent(_scratchMemory.Length * 2);
            int dbLength   = newScratch.Length / 2;

            var newDb = newScratch.Buffer.Slice(0, dbLength);

            _db.Slice(0, _valuesIndex).Span.CopyTo(newDb.Span);
            _db = newDb;

            var newStackMemory = newScratch.Buffer.Slice(dbLength);

            _stack.Resize(newStackMemory);
            _scratchManager.Dispose();
            _scratchManager = newScratch;
        }
コード例 #10
0
ファイル: MemoryPoolTests.cs プロジェクト: nietras/corefxlab
        public void ReleasedBlockWorks()
        {
            var pool  = new MemoryPool();
            var block = pool.Rent(1);

            block.Dispose();

            OwnedBuffer <byte> block2 = null;

            // Lease-return until we get same block
            do
            {
                block2?.Dispose();
                block2 = pool.Rent(1);
            } while (block != block2);

            Assert.True(block2.Span.Length > 0);
        }