Esempio n. 1
0
        public static void ExtraDisposesAreIgnored()
        {
            OwnedMemory <int> block = MemoryPool <int> .Shared.Rent(42);

            block.Dispose();
            block.Dispose();
        }
Esempio n. 2
0
        static void TestLifetime(OwnedMemory <byte> owned)
        {
            Memory <byte> copyStoredForLater;

            try {
                Memory <byte> memory      = owned.Memory;
                Memory <byte> memorySlice = memory.Slice(10);
                copyStoredForLater = memorySlice;
                var r = memorySlice.Retain();
                try {
                    Assert.Throws <InvalidOperationException>(() => { // memory is reserved; premature dispose check fires
                        owned.Dispose();
                    });
                }
                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;
            });
        }
Esempio n. 3
0
 public void DisposeStore()
 {
     _secretStore?.Dispose();
     _secretStore = null;
     _state.HandshakeHash?.Dispose();
     _state.HandshakeHash = null;
 }
Esempio n. 4
0
        static void BufferLifetimeBasicsAuto(OwnedMemory <byte> buffer)
        {
            Memory <byte> copyStoredForLater;

            try
            {
                Memory <byte> memory = buffer.Memory;
                Memory <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;
            });

            Assert.True(buffer.IsDisposed);
        }
Esempio n. 5
0
        public void Dispose()
        {
            if (_memory == null)
            {
                throw new InvalidOperationException("Cannot dispose non-root database");
            }

            _memory.Dispose();
        }
Esempio n. 6
0
        public static void IsDisposed()
        {
            OwnedMemory <int> block = MemoryPool <int> .Shared.Rent(42);

            Assert.False(block.IsDisposed);
            block.Dispose();
            Assert.True(block.IsDisposed);
            block.Dispose();
            Assert.True(block.IsDisposed);
        }
Esempio n. 7
0
 public override void Return(OwnedMemory<byte> buffer)
 {
     var ownedArray = buffer as OwnedArray<byte>;
     if (ownedArray == null) throw new InvalidOperationException("buffer not rented from this pool");
     ArrayPool<byte>.Shared.Return(ownedArray.Array);
     buffer.Dispose();
 }
Esempio n. 8
0
        public static void NoPinAfterDispose()
        {
            OwnedMemory <int> block = MemoryPool <int> .Shared.Rent(42);

            block.Dispose();
            Assert.Throws <ObjectDisposedException>(() => block.Pin());
        }
Esempio n. 9
0
        public override void Return(OwnedMemory <byte> buffer)
        {
            var pooledBuffer = buffer as BufferManager;

            if (pooledBuffer == null)
            {
                throw new Exception();
            }

            var memory = pooledBuffer.Pointer.ToInt64();

            if (memory < _memory.ToInt64() || memory > _memory.ToInt64() + _bufferSize * _bufferCount)
            {
                throw new Exception("not rented from this pool");
            }

            var offset = memory - _memory.ToInt64();
            var index  = offset / _bufferSize;

            lock (_lock) {
                buffer.Dispose();
                if (_rented[index] == false)
                {
                    throw new Exception("this buffer is not rented");
                }
                _rented[index] = false;
            }
        }
Esempio n. 10
0
        static void DisposeAuto(OwnedMemory <byte> buffer)
        {
            var length = buffer.Length;

            buffer.Dispose();
            Assert.True(buffer.IsDisposed);

            Assert.ThrowsAny <ObjectDisposedException>(() =>
            {
                var ignore = buffer.Span;
            });

            Assert.ThrowsAny <ObjectDisposedException>(() =>
            {
                buffer.Span.Slice(0, length);
            });

            Assert.ThrowsAny <ObjectDisposedException>(() =>
            {
                buffer.Pin();
            });

            Assert.ThrowsAny <ObjectDisposedException>(() =>
            {
                var rwBuffer = buffer.Memory;
            });

            Assert.ThrowsAny <ObjectDisposedException>((Action)(() =>
            {
                ReadOnlyMemory <byte> roBuffer = buffer.Memory;
            }));
        }
Esempio n. 11
0
        public static void NoTryGetArrayAfterDispose()
        {
            OwnedMemory <int> block = MemoryPool <int> .Shared.Rent(42);

            Memory <int> memory = block.Memory;

            block.Dispose();
            Assert.Throws <ObjectDisposedException>(() => MemoryMarshal.TryGetArray(memory, out ArraySegment <int> arraySegment));
        }
Esempio n. 12
0
        public void Dispose()
        {
            Debug.Assert(_buffer.ReferenceCount >= 1);

            _buffer.Release();

            if (_buffer.ReferenceCount == 0)
            {
                _buffer.Dispose();
            }
        }
Esempio n. 13
0
        public void Dispose()
        {
            Debug.Assert(_buffer.HasOutstandingReferences);

            _buffer.Release();

            if (!_buffer.HasOutstandingReferences)
            {
                _buffer.Dispose();
            }
        }
Esempio n. 14
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;
 }
Esempio n. 15
0
        public override void Return(OwnedMemory <byte> buffer)
        {
            var ownedArray = buffer as OwnedArray <byte>;

            if (ownedArray == null)
            {
                throw new InvalidOperationException("buffer not rented from this pool");
            }
            ArrayPool <byte> .Shared.Return(ownedArray.Array);

            buffer.Dispose();
        }
Esempio n. 16
0
        private void ResizeDb()
        {
            var oldData    = _scratchMemory.Span;
            var newScratch = _pool.Rent(_scratchMemory.Length * 2);
            int dbLength   = newScratch.Length / 2;

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

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

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

            _stack.Resize(newStackMemory);
            _scratchManager.Dispose();
            _scratchManager = newScratch;
        }
Esempio n. 17
0
 protected override void Dispose(bool disposing)
 {
     _pool.CheckDisposed();
     _ownedMemory.Dispose();
 }
Esempio n. 18
0
 public void Dispose()
 {
     _keyStore.Dispose();
     _ctx.Free();
     GC.SuppressFinalize(this);
 }