예제 #1
0
        internal RetainedMemory(Memory <T> memory)
        {
            if (MemoryMarshal.TryGetMemoryManager <T, RetainableMemory <T> >(memory, out var manager))
            {
                manager.Increment();
                _manager = manager;
                _start   = 0;
                _length  = memory.Length;
            }
            else if (MemoryMarshal.TryGetArray <T>(memory, out var segment))
            {
                _manager = ArrayMemory <T> .Create(segment.Array, segment.Offset, segment.Count, externallyOwned : true);

                _manager.Increment();
                _start  = 0;
                _length = _manager.Length;
            }
            else
            {
                ThrowNotSupportedMemoryType();
                _manager = default;
                _start   = 0;
                _length  = 0;
            }

#if DETECT_LEAKS
            _finalizeChecker = new PanicOnFinalize();
#endif
        }
예제 #2
0
        internal RetainedMemory(RetainableMemory <T> memory, int offset, int length, bool borrow)
#endif
        {
            Debug.Assert(unchecked ((uint)offset + (uint)length <= memory.Length));

            if (borrow)
            {
                memory.Increment();
            }

            _manager = memory;
            _offset  = offset;
            _length  = length;

            // We do not need to Pin arrays, they do not have ref count. Will be pinned when Pointer is accessed.
#if DETECT_LEAKS
            _finalizeChecker = borrow ? new PanicOnFinalize() : checker;
#endif
        }
예제 #3
0
        public RetainedMemory(Memory <T> memory, bool pin = true) // TODO pin param added later and before it behaved like with true, but better to change to false and review usage
        {
            if (MemoryMarshal.TryGetMemoryManager <T, RetainableMemory <T> >(memory, out var manager))
            {
                if (!manager.IsPinned && pin)
                {
                    // TODO review. This uses implementation detail of RetainableMemory:
                    // if pointer is null then it is an non-pinned array for which we did not create
                    // a GCHandle (very expensive). Call to Pin() checks if pointer is null and
                    // creates a GCHandle + pointer. Try to avoid pinning non-pooled ArrayMemory
                    // because it is very expensive.
                    manager.Pin();
                }
                else
                {
                    manager.Increment();
                }
                _manager = manager;
                _offset  = 0;
                _length  = memory.Length;
            }
            else if (MemoryMarshal.TryGetArray <T>(memory, out var segment))
            {
                _manager = ArrayMemory <T> .Create(segment.Array, segment.Offset, segment.Count, externallyOwned : true, pin);

                _manager.Increment();
                _offset = 0;
                _length = _manager.Length;
            }
            else
            {
                ThrowNotSupportedMemoryType();
                _manager = default;
                _offset  = 0;
                _length  = 0;
            }

#if DETECT_LEAKS
            _finalizeChecker = new PanicOnFinalize();
#endif
        }
예제 #4
0
        internal RetainedMemory(RetainableMemory <T> memory, int start, int length, bool borrow)
        {
            if (memory.IsDisposed)
            {
                BuffersThrowHelper.ThrowDisposed <RetainableMemory <T> >();
            }

            Debug.Assert(unchecked ((uint)start + (uint)length <= memory.Length));

            if (borrow)
            {
                memory.Increment();
            }

            _manager = memory;
            _start   = start;
            _length  = length;

#if DETECT_LEAKS
            _finalizeChecker = new PanicOnFinalize();
#endif
        }