예제 #1
0
        private void EnsureBufferCapacity(int nextWriteSize)
        {
            if (nextWriteSize < 0)
            {
                throw new OverflowException();
            }

            if (backingBuffer.Length == 0 || backingBuffer.Length - Offset < nextWriteSize)
            {
                const int BlockSize = 1024;

                int blocks = checked (Offset + nextWriteSize + (BlockSize - 1)) / BlockSize;

                var oldBytes = backingBuffer;

                var newRental = CryptoPool.Rent <byte>(BlockSize * blocks);

                backingBuffer = newRental.Memory;

                if (oldBytes.Length > 0)
                {
                    oldBytes.CopyTo(backingBuffer);
                    rental.Dispose();
                }

                rental = newRental;
            }

            MoveByOffset(0);
        }
예제 #2
0
        public static void ExtraDisposesAreIgnored()
        {
            IMemoryOwner <int> block = MemoryPool <int> .Shared.Rent(42);

            block.Dispose();
            block.Dispose();
        }
예제 #3
0
    public void Dispose()
    {
        lock (_dataWriterLock)
        {
            if (_disposed)
            {
                return;
            }

            _disposed = true;

            Stop();

            if (_fakeMemoryOwner != null)
            {
                _fakeMemoryOwner.Dispose();
                _fakeMemoryOwner = null;
            }

            if (_fakeMemory != null)
            {
                ArrayPool <byte> .Shared.Return(_fakeMemory);

                _fakeMemory = null;
            }
        }
    }
        /// <summary>
        /// Internal for testing purposes only, do not use in SDK.
        /// </summary>
        internal void HandleOperationCompleted(IMemoryOwner <byte> data, ResponseStatus status)
        {
            try
            {
                if (data != null && (status == ResponseStatus.Success ||
                                     status == ResponseStatus.VBucketBelongsToAnotherServer ||
                                     status == ResponseStatus.AuthenticationContinue ||
                                     status == ResponseStatus.SubDocMultiPathFailure))
                {
                    Read(data);

                    _completed.TrySetResult(status);
                }
                else
                {
                    data?.Dispose();
                }

                _completed.TrySetResult(status);
            }
            catch (Exception ex)
            {
                _completed.TrySetException(ex);
                data?.Dispose();
            }
        }
예제 #5
0
        private async ValueTask <LTrieNodeExternalLinkStruct> GetRemainingExternalLink(LTrieNodeStruct gn, byte value)
        {
            IMemoryOwner <byte>?owner = null;
            var restLength            = gn.GetRestLength();
            var secondLinkPointer     = gn.GetSecondExternalLinkOwnPointer();

            if (!Storage.TryDirectRead(secondLinkPointer, restLength, out var memory))
            {
                owner = MemoryPool.Rent(restLength);
                var outputMemory = owner.Memory.Slice(0, restLength);
                await Storage.Read(secondLinkPointer, outputMemory);

                memory = outputMemory;
            }
            for (int linkIndex = 0; linkIndex < gn.ExternalLinkSlotCount - 1; linkIndex++)
            {
                var linkOwnPointer = secondLinkPointer + linkIndex * Sizes.ExternalLinkLength;
                var l = new LTrieNodeExternalLinkStruct(linkOwnPointer, memory.Span.Slice(linkIndex * Sizes.ExternalLinkLength, Sizes.ExternalLinkLength));
                if (l.Pointer != 0 && l.Value == value)
                {
                    owner?.Dispose();
                    return(l);
                }
            }
            owner?.Dispose();
            return(default);
예제 #6
0
        public void EnsureCapacity(long inputSize)
        {
            var maxFragmentSize = (int)Math.Min(inputSize, Constants.BlockSize);
            var tableSize       = CalculateTableSize(maxFragmentSize);

            if (_buffer == null || tableSize < _buffer.Memory.Length)
            {
                _buffer?.Dispose();
                _buffer = MemoryPool <ushort> .Shared.Rent(tableSize);
            }
        }
예제 #7
0
        public virtual void Reset(ResponseStatus status)
        {
            _data?.Dispose();
            _data = null;

            Header = new OperationHeader
            {
                Magic      = Header.Magic,
                OpCode     = OpCode,
                Cas        = Header.Cas,
                BodyLength = Header.BodyLength,
                Key        = Key,
                Status     = status
            };
        }
예제 #8
0
 private void Dispose(bool disposing)
 {
     if (disposing)
     {
         _memoryOwner?.Dispose();
     }
 }
예제 #9
0
        /// <summary>
        /// Reads an individual gif frame.
        /// </summary>
        /// <typeparam name="TPixel">The pixel format.</typeparam>
        /// <param name="image">The image to decode the information to.</param>
        /// <param name="previousFrame">The previous frame.</param>
        private void ReadFrame <TPixel>(ref Image <TPixel> image, ref ImageFrame <TPixel> previousFrame)
            where TPixel : unmanaged, IPixel <TPixel>
        {
            this.ReadImageDescriptor();

            IMemoryOwner <byte> localColorTable = null;
            Buffer2D <byte>     indices         = null;

            try
            {
                // Determine the color table for this frame. If there is a local one, use it otherwise use the global color table.
                if (this.imageDescriptor.LocalColorTableFlag)
                {
                    int length = this.imageDescriptor.LocalColorTableSize * 3;
                    localColorTable = this.Configuration.MemoryAllocator.Allocate <byte>(length, AllocationOptions.Clean);
                    this.stream.Read(localColorTable.GetSpan());
                }

                indices = this.Configuration.MemoryAllocator.Allocate2D <byte>(this.imageDescriptor.Width, this.imageDescriptor.Height, AllocationOptions.Clean);

                this.ReadFrameIndices(indices);
                ReadOnlySpan <Rgb24> colorTable = MemoryMarshal.Cast <byte, Rgb24>((localColorTable ?? this.globalColorTable).GetSpan());
                this.ReadFrameColors(ref image, ref previousFrame, indices, colorTable, this.imageDescriptor);

                // Skip any remaining blocks
                this.SkipBlock();
            }
            finally
            {
                localColorTable?.Dispose();
                indices?.Dispose();
            }
        }
예제 #10
0
    /// <summary>
    /// Compares the bytes in two streams for equality.
    /// </summary>
    /// <param name="left"><see cref="Stream"/> to compare.</param>
    /// <param name="right"><see cref="Stream"/> to compare.</param>
    /// <returns>TRUE if all bytes are equal in value, FALSE otherwise.</returns>
    public static async Task <bool> EqualsAsync(Stream left, Stream right)
    {
        const int           wantedBuffersize = InternalUtils.StreamBufferSize;
        MemoryPool <byte>   pool             = MemoryPool <byte> .Shared;
        IMemoryOwner <byte> leftOwner        = pool.Rent(wantedBuffersize);
        IMemoryOwner <byte> rightOwner       = pool.Rent(wantedBuffersize);
        Memory <byte>       leftBuffer       = leftOwner.Memory;
        Memory <byte>       rightBuffer      = rightOwner.Memory;

        try
        {
            int leftRead;
            while ((leftRead = await left.ReadAsync(leftBuffer)) > 0)
            {
                int rightRead = await right.ReadAsync(rightBuffer);

                if (leftRead != rightRead)
                {
                    return(false);
                }

                if (!Equals(leftBuffer.Span, rightBuffer.Span))
                {
                    return(false);
                }
            }

            return(true);
        }
        finally
        {
            leftOwner.Dispose();
            rightOwner.Dispose();
        }
    }
예제 #11
0
        public IMemoryOwner <int> Rent1MBClear()
        {
            IMemoryOwner <int> memoryOwner = _NativeMemoryPool !.Rent <int>(1_000_000, 0u, out _, true);

            memoryOwner.Dispose();
            return(memoryOwner);
        }
        /* UTF8 Memory Pool result
         * | Utf8WithMemoryPool | 1,022.4 ns |   9.61 ns |   8.52 ns | 1,020.2 ns | 0.0057 |     - |     - |      24 B |
         */
        public int Utf8WithMemoryPool()
        {
            //get minimal length required for
            var minLength = UTF8Encoding.UTF8.GetMaxByteCount(data.Length);
            // var arr = ArrayPool<byte>.Shared.Rent(minLength);
            IMemoryOwner <byte> owner = MemoryPool <byte> .Shared.Rent(minLength);

            var utf8         = owner.Memory.Span;
            var bytesWritten = UTF8Encoding.UTF8.GetBytes(data, utf8);

            utf8 = utf8.Slice(0, bytesWritten);

            int sum = 0;

            while (true)
            {
                Utf8Parser.TryParse(utf8, out int value, out var bytesConsumed);
                sum += value;
                if (utf8.Length - 1 < bytesConsumed)
                {
                    break;
                }
                utf8 = utf8.Slice(bytesConsumed + 1);
            }

            // ArrayPool<byte>.Shared.Return(arr);
            owner.Dispose();
            return(sum);
        }
예제 #13
0
        public void Complete()
        {
            lock (_dataWriterLock)
            {
                if (_writerComplete)
                {
                    return;
                }

                _writerComplete = true;

                Stop();

                // Make sure the writing side is completed.
                _pipeWriter.Complete();

                if (_fakeMemoryOwner != null)
                {
                    _fakeMemoryOwner.Dispose();
                    _fakeMemoryOwner = null;
                }

                if (_fakeMemory != null)
                {
                    ArrayPool <byte> .Shared.Return(_fakeMemory);

                    _fakeMemory = null;
                }
            }
        }
예제 #14
0
        public static byte[] Serialize <T>(T obj)
        {
            var formatter = JT808FormatterExtensions.GetFormatter <T>();
            var pool      = MemoryPool <byte> .Shared;
            IMemoryOwner <byte> buffer = pool.Rent(10240);

            try
            {
                var len = formatter.Serialize(buffer, 0, obj);
                return(buffer.Memory.Slice(0, len).ToArray());
            }
            catch (JT808Exception ex)
            {
                throw new JT808Exception("Serialize", ex);
            }
            catch (Exception ex)
            {
                throw new Exception("Serialize", ex);
            }
            finally
            {
                // 源码:System.Memory.MemoryPool
                // private static readonly MemoryPool<T> s_shared = new ArrayMemoryPool<T>();
                // 单例内存池 不需要手动释放资源
                buffer.Dispose();
            }
        }
 public void Dispose()
 {
     owner?.Dispose();
     image  = null;
     memory = null;
     owner  = null;
 }
예제 #16
0
 public void Return()
 {
     if (_memoryOwner != null)
     {
         _memoryOwner.Dispose();
     }
 }
예제 #17
0
        public void Dispose()
        {
            try
            {
                _compressor?.Dispose();
            }
            finally
            {
                _compressor = null;

                try
                {
                    _outputBufferOwner?.Dispose();
                }
                finally
                {
                    _outputBufferOwner = null;
                    _outputBuffer      = default;

                    try
                    {
                        _inputBufferOwner?.Dispose();
                    }
                    finally
                    {
                        _inputBufferOwner = null;
                        _inputBuffer      = default;
                    }
                }
            }
        }
예제 #18
0
        private async Task <(MediaBuffer, bool)> ReadBufferAsync(HttpRequest request)
        {
            IMemoryOwner <byte> memoryBuffer = null;

            try
            {
                bool endOfStream  = false;
                int  bufferLength = 0;

                memoryBuffer = _pool.Rent(BUFFER_SIZE);
                var memory = memoryBuffer.Memory;
                while (memory.Length > 0)
                {
                    var bytesRead = await request.Body.ReadAsync(memory);

                    if (bytesRead == 0)
                    {
                        endOfStream = true;
                        break;
                    }

                    bufferLength += bytesRead;
                    memory        = memory.Slice(bytesRead);
                }
                var mediaBuffer = new MediaBuffer(memoryBuffer, bufferLength);
                memoryBuffer = null; // avoid it being released.
                return(mediaBuffer, endOfStream);
            }
            finally
            {
                memoryBuffer?.Dispose();
            }
        }
예제 #19
0
        /// <inheritdoc/>
        protected override Task ReadPacket(ReadOnlySequence <byte> packet)
        {
            if (this.maxPacketSize != null && packet.Length > this.maxPacketSize.Value)
            {
                var bytes = packet.Slice(0, packet.Length).ToArray();
                throw new MaxPacketSizeExceededException(bytes, (int)packet.Length, this.maxPacketSize.Value);
            }

            Span <byte>         packetSpan;
            IMemoryOwner <byte> owner = null;

            if (packet.Length <= this.packetBuffer.Length)
            {
                packetSpan = this.packetBuffer.AsSpan().Slice(0, (int)packet.Length);
            }
            else
            {
                owner = MemoryPool <byte> .Shared.Rent((int)packet.Length);

                packetSpan = owner.Memory.Span.Slice(0, (int)packet.Length);
            }

            packet.CopyTo(packetSpan);
            try {
                this.packetCallback.Invoke(packetSpan);
            } finally {
                owner?.Dispose();
            }

            return(Task.CompletedTask);
        }
예제 #20
0
        private void AddToPushback(ReadOnlySpan <char> c)
        {
            if (PushBackOwner == null)
            {
                PushBackOwner = MemoryPool.Rent(BufferSizeHint);
            }

            if (PushBackLength + c.Length > PushBackOwner.Memory.Length)
            {
                var oldSize = PushBackOwner.Memory.Length;

                var newSize  = (PushBackLength + c.Length) * 2;   // double size, because we're sharing the buffer
                var newOwner = Utils.RentMustIncrease(MemoryPool, newSize, oldSize);
                PushBackOwner.Memory.CopyTo(newOwner.Memory);

                PushBackOwner.Dispose();
                PushBackOwner = newOwner;
            }

            if (PushBackLength + c.Length > PushBackOwner.Memory.Length)
            {
                Throw.InvalidOperationException($"Could not allocate large enough buffer to read headers");
            }

            c.CopyTo(PushBack.Span.Slice(PushBackLength));
            PushBackLength += c.Length;
        }
예제 #21
0
 public void Dispose()
 {
     if (_body is IMemoryOwner <byte> )
     {
         _body.Dispose();
     }
 }
예제 #22
0
        public void Output(IMemoryOwner <byte> buffer, int avalidLength)
        {
            var s = buffer.Memory.Span.Slice(0, avalidLength).ToArray();

            client.SendAsync(s, s.Length, EndPoint);
            buffer.Dispose();
        }
예제 #23
0
        protected override void Dispose(bool disposing)
        {
            try
            {
                lock (_syncObj)
                {
                    if (Volatile.Read(ref _pinCount) > 0)
                    {
                        MemoryPoolThrowHelper.ThrowInvalidOperationException_ReturningPinnedBlock(this);
                    }

                    if (_isDisposed)
                    {
                        MemoryPoolThrowHelper.ThrowInvalidOperationException_BlockDoubleDispose(this);
                    }

                    _memoryOwner.Dispose();

                    _pool.Return(this);

                    _isDisposed = true;
                }
            }
            catch (Exception exception)
            {
                _pool.ReportException(exception);
                throw;
            }
        }
예제 #24
0
        public static void NoMemoryAfterDispose()
        {
            IMemoryOwner <int> block = MemoryPool <int> .Shared.Rent(42);

            block.Dispose();
            Assert.Throws <ObjectDisposedException>(() => block.Memory);
        }
예제 #25
0
 public void Dispose()
 {
     if (_payload is IMemoryOwner <byte> )
     {
         _payload.Dispose();
     }
 }
 protected override void Dispose(bool disposing)
 {
     if (disposing)
     {
         _ownedMemory?.Dispose();
     }
 }
예제 #27
0
 public void Dispose()
 {
     if (MemoryOwner is not null)
     {
         MemoryOwner.Dispose();
     }
 }
예제 #28
0
 protected override async Task Execute(ModelBase model, IAsyncBasicConsumer consumer)
 {
     try
     {
         await consumer.HandleBasicDeliver(_consumerTag,
                                           _deliveryTag,
                                           _redelivered,
                                           _exchange,
                                           _routingKey,
                                           _basicProperties,
                                           _body.Memory.Slice(0, _bodyLength)).ConfigureAwait(false);
     }
     catch (Exception e)
     {
         var details = new Dictionary <string, object>()
         {
             { "consumer", consumer },
             { "context", "HandleBasicDeliver" }
         };
         model.OnCallbackException(CallbackExceptionEventArgs.Build(e, details));
     }
     finally
     {
         _body.Dispose();
     }
 }
        public override IMemoryOwner <char> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            ReadOnlySpan <byte> span = reader.HasValueSequence ? reader.ValueSequence.ToArray() : reader.ValueSpan;

            int idx = span.IndexOf(BackSlash);

            if (idx != -1)
            {
                IMemoryOwner <byte> unescapedArray = null;

                Span <byte> utf8Unescaped = span.Length <= 256 ?
                                            stackalloc byte[span.Length] :
                                            (unescapedArray = BufferAllocator.Instance.AllocateByte(span.Length)).Memory.Span;

                JsonReaderHelperUnescape(span, utf8Unescaped, idx, out var written);

                utf8Unescaped = utf8Unescaped.Slice(0, written);

                var result = TranscodeHelper(utf8Unescaped);

                unescapedArray?.Dispose();

                return(result);
            }

            return(TranscodeHelper(span));
        }
예제 #30
0
 protected virtual void Dispose(bool disposing)
 {
     if (disposing)
     {
         Buffer.Dispose();
     }
 }