コード例 #1
0
ファイル: NdrBuffer.Write.cs プロジェクト: zha0/Kerberos.NET
        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;
            }
        }
    }
コード例 #4
0
        /// <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
ファイル: HashTable.cs プロジェクト: rodo-r2r/Snappier
        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
ファイル: GifDecoderCore.cs プロジェクト: br3aker/ImageSharp
        /// <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
ファイル: FastCompare.cs プロジェクト: mztikk/RFReborn
    /// <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);
        }
コード例 #12
0
        /* 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();
            }
        }
コード例 #15
0
 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
ファイル: HeadersReader.cs プロジェクト: attackgithub/Cesil
        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
ファイル: SimpleKcpClient.cs プロジェクト: KumoKyaku/KCP
        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();
     }
 }
コード例 #26
0
 protected override void Dispose(bool disposing)
 {
     if (disposing)
     {
         _ownedMemory?.Dispose();
     }
 }
コード例 #27
0
ファイル: RentedBuffer.cs プロジェクト: moien007/NENet
 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();
     }
 }
コード例 #29
0
        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();
     }
 }