Exemplo n.º 1
0
        public BufferHandle Retain(bool pin = false)
        {
            BufferHandle bufferHandle;

            if (pin)
            {
                if (_owner != null)
                {
                    bufferHandle = _owner.Pin(_index);
                }
                else
                {
                    var handle = GCHandle.Alloc(_array, GCHandleType.Pinned);
                    unsafe
                    {
                        var pointer = OwnedBuffer <T> .Add((void *)handle.AddrOfPinnedObject(), _index);

                        bufferHandle = new BufferHandle(null, pointer, handle);
                    }
                }
            }
            else
            {
                if (_owner != null)
                {
                    _owner.Retain();
                }
                bufferHandle = new BufferHandle(_owner);
            }
            return(bufferHandle);
        }
Exemplo n.º 2
0
        public BufferSegment(OwnedBuffer <byte> buffer)
        {
            _owned = buffer;
            Start  = 0;
            End    = 0;

            _owned.Retain();
            _buffer = _owned.Buffer;
        }
Exemplo n.º 3
0
        static void MemoryHandleDoubleFree(OwnedBuffer <byte> buffer)
        {
            var memory = buffer.Buffer;
            var handle = memory.Retain(pin: true);

            buffer.Retain();
            handle.Dispose();
            handle.Dispose();
            Assert.False(buffer.Release());
        }
Exemplo n.º 4
0
        static void MemoryHandleDoubleFree(OwnedBuffer <byte> buffer)
        {
            var memory = buffer.Buffer;
            var h      = memory.Pin();

            Assert.True(buffer.IsRetained);
            buffer.Retain();
            Assert.True(buffer.IsRetained);
            h.Dispose();
            Assert.True(buffer.IsRetained);
            h.Dispose();
            Assert.True(buffer.IsRetained);
            buffer.Release();
            Assert.False(buffer.IsRetained);
        }
Exemplo n.º 5
0
        public void MemoryHandleDoubleFree()
        {
            var array = new byte[1024];
            OwnedBuffer <byte> owned = array;
            var memory = owned.Buffer;
            var h      = memory.Pin();

            Assert.True(owned.IsRetained);
            owned.Retain();
            Assert.True(owned.IsRetained);
            h.Dispose();
            Assert.True(owned.IsRetained);
            h.Dispose();
            Assert.True(owned.IsRetained);
            owned.Release();
            Assert.False(owned.IsRetained);
        }
Exemplo n.º 6
0
        public BufferSegment(OwnedBuffer <byte> buffer, int start, int end)
        {
            _owned   = buffer;
            Start    = start;
            End      = end;
            ReadOnly = true;

            // For unowned buffers, we need to make a copy here so that the caller can
            // give up the give this buffer back to the caller
            var unowned = buffer as UnownedBuffer;

            if (unowned != null)
            {
                _owned = unowned.MakeCopy(start, end - start, out Start, out End);
            }

            _owned.Retain();
            _buffer = _owned.Buffer;
        }
Exemplo n.º 7
0
        public void ReleasedBlockWorks()
        {
            using (var pool = new MemoryPool())
            {
                var block1 = pool.Rent(1);
                block1.Retain();
                block1.Release();

                OwnedBuffer <byte> block2 = null;

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

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