Beispiel #1
0
        public unsafe ReadOnlySpan(void *pointer, int length)
        {
            if (RuntimeHelpers.IsReferenceOrContainsReferences <T>())
            {
                ThrowHelper.ThrowInvalidTypeWithPointersNotSupported(typeof(T));
            }
            if (length < 0)
            {
                ThrowHelper.ThrowArgumentOutOfRangeException();
            }

            _pointer = new ByReference <T>(ref Unsafe.As <byte, T>(ref *(byte *)pointer));
            _length  = length;
        }
Beispiel #2
0
        public ReadOnlySpan(T[] array, int start, int length)
        {
            if (array == null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
            }
            if ((uint)start > (uint)array.Length || (uint)length > (uint)(array.Length - start))
            {
                ThrowHelper.ThrowArgumentOutOfRangeException();
            }

            _pointer = new ByReference <T>(ref Unsafe.Add(ref Unsafe.As <byte, T>(ref array.GetRawSzArrayData()), start));
            _length  = length;
        }
Beispiel #3
0
        public Span(T[] array)
        {
            if (array == null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
            }
            if (default(T) == null && array.GetType() != typeof(T[]))
            {
                ThrowHelper.ThrowArrayTypeMismatchException();
            }

            _pointer = new ByReference <T>(ref Unsafe.As <byte, T>(ref array.GetRawSzArrayData()));
            _length  = array.Length;
        }
Beispiel #4
0
        public unsafe Span(void *pointer, int length)
        {
            if (RuntimeHelpers.IsReferenceOrContainsReferences <T>())
            {
                throw new ArgumentException(nameof(pointer));
            }
            if (length < 0)
            {
                throw new ArgumentException(nameof(length));
            }

            _pointer = new ByReference <T>(ref Unsafe.As <byte, T>(ref *(byte *)pointer));
            _length  = length;
        }
Beispiel #5
0
        public ReadOnlySpan(T[] array, int start)
        {
            if (array == null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
            }
            if ((uint)start > (uint)array.Length)
            {
                ThrowHelper.ThrowArgumentOutOfRangeException();
            }

            _pointer = new ByReference <T>(ref Unsafe.Add(ref JitHelpers.GetArrayData(array), start));
            _length  = array.Length - start;
        }
Beispiel #6
0
        public Span(T[]?array)
        {
            if (array == null)
            {
                this = default;
                return;                                                 // returns default
            }
            if (default(T) ! == null && array.GetType() != typeof(T[])) // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/34757
            {
                ThrowHelper.ThrowArrayTypeMismatchException();
            }

            _pointer = new ByReference <T>(ref Unsafe.As <byte, T>(ref array.GetRawSzArrayData()));
            _length  = array.Length;
        }
Beispiel #7
0
        public Span(T[]?array)
        {
            if (array == null)
            {
                this = default;
                return;                                                 // returns default
            }
            if (default(T) ! == null && array.GetType() != typeof(T[])) // TODO-NULLABLE: default(T) == null warning (https://github.com/dotnet/roslyn/issues/34757)
            {
                ThrowHelper.ThrowArrayTypeMismatchException();
            }

            _pointer = new ByReference <T>(ref MemoryMarshal.GetArrayDataReference(array));
            _length  = array.Length;
        }
Beispiel #8
0
        public Span(T[]?array)
        {
            if (array == null)
            {
                this = default;
                return; // returns default
            }
            if (!typeof(T).IsValueType && array.GetType() != typeof(T[]))
            {
                ThrowHelper.ThrowArrayTypeMismatchException();
            }

            _pointer = new ByReference <T>(ref MemoryMarshal.GetArrayDataReference(array));
            _length  = array.Length;
        }
        public unsafe MemoryMappedPtr(void *pointer,
                                      ulong length)
        {
            if (RuntimeHelpers.IsReferenceOrContainsReferences <T>())
            {
                throw new ArgumentOutOfRangeException();
            }

            if (length < 0)
            {
                throw new ArgumentOutOfRangeException();
            }

            _pointer = new ByReference <T>(ref Unsafe.As <byte, T>(ref *(byte *)pointer));
            _length  = length;
        }
Beispiel #10
0
        /// <summary>
        /// Creates a new span over the entirety of the target array.
        /// </summary>
        /// <param name="array">The target array.</param>
        /// <exception cref="System.ArgumentNullException">Thrown when <paramref name="array"/> is a null
        /// reference (Nothing in Visual Basic).</exception>
        /// <exception cref="System.ArrayTypeMismatchException">Thrown when <paramref name="array"/> is covariant and array's type is not exactly T[].</exception>
        public Span(T[] array)
        {
            if (array == null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
            }
            if (default(T) == null)   // Arrays of valuetypes are never covariant
            {
                if (array.GetType() != typeof(T[]))
                {
                    ThrowHelper.ThrowArrayTypeMismatchException();
                }
            }

            _pointer = new ByReference <T>(ref JitHelpers.GetArrayData(array));
            _length  = array.Length;
        }
Beispiel #11
0
        public Span(T[] array, int start, int length)
        {
            if (array == null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
            }
            if (default(T) == null && array.GetInternalTypeID() != RuntimeHelpers.GetInternalTypeID <T[]>())
            {
                ThrowHelper.ThrowArrayTypeMismatchException();
            }
            if ((uint)start > (uint)array.Length || (uint)length > (uint)(array.Length - start))
            {
                ThrowHelper.ThrowArgumentOutOfRangeException();
            }

            _pointer = new ByReference <T>(ref Unsafe.Add(ref Unsafe.As <byte, T>(ref array.GetRawSzArrayData()), start));
            _length  = length;
        }
Beispiel #12
0
        public Span(T[] array, int start)
        {
            if (array == null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
            }
            if (default(T) == null && array.GetType() != typeof(T[]))
            {
                ThrowHelper.ThrowArrayTypeMismatchException();
            }
            if ((uint)start > (uint)array.Length)
            {
                ThrowHelper.ThrowArgumentOutOfRangeException();
            }

            _pointer = new ByReference <T>(ref Unsafe.Add(ref JitHelpers.GetArrayData(array), start));
            _length  = array.Length - start;
        }
Beispiel #13
0
        public ReadOnlySpan(T[] array, int start, int length)
        {
            if (array == null)
            {
                if (start != 0 || length != 0)
                {
                    ThrowHelper.ThrowArgumentOutOfRangeException();
                }
                this = default;
                return; // returns default
            }
            if ((uint)start > (uint)array.Length || (uint)length > (uint)(array.Length - start))
            {
                ThrowHelper.ThrowArgumentOutOfRangeException();
            }

            _pointer = new ByReference <T>(ref Unsafe.Add(ref Unsafe.As <byte, T>(ref array.GetRawSzArrayData()), start));
            _length  = length;
        }
Beispiel #14
0
        /// <summary>
        /// Creates a new span over the portion of the target array beginning
        /// at 'start' index and ending at 'end' index (exclusive).
        /// </summary>
        /// <param name="array">The target array.</param>
        /// <param name="start">The index at which to begin the span.</param>
        /// <param name="length">The number of items in the span.</param>
        /// <exception cref="System.ArgumentNullException">Thrown when <paramref name="array"/> is a null
        /// reference (Nothing in Visual Basic).</exception>
        /// <exception cref="System.ArrayTypeMismatchException">Thrown when <paramref name="array"/> is covariant and array's type is not exactly T[].</exception>
        /// <exception cref="System.ArgumentOutOfRangeException">
        /// Thrown when the specified <paramref name="start"/> or end index is not in the range (&lt;0 or &gt;=Length).
        /// </exception>
        public Span(T[] array, int start, int length)
        {
            if (array == null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
            }
            if (default(T) == null)   // Arrays of valuetypes are never covariant
            {
                if (array.GetType() != typeof(T[]))
                {
                    ThrowHelper.ThrowArrayTypeMismatchException();
                }
            }
            if ((uint)start > (uint)array.Length || (uint)length > (uint)(array.Length - start))
            {
                ThrowHelper.ThrowArgumentOutOfRangeException();
            }

            _pointer = new ByReference <T>(ref Unsafe.Add(ref JitHelpers.GetArrayData(array), start));
            _length  = length;
        }
Beispiel #15
0
        public ReadOnlySpan(T[]? array, int start, int length)
        {
            if (array == null)
            {
                if (start != 0 || length != 0)
                    ThrowHelper.ThrowArgumentOutOfRangeException();
                this = default;
                return; // returns default
            }
#if BIT64
            // See comment in Span<T>.Slice for how this works.
            if ((ulong)(uint)start + (ulong)(uint)length > (ulong)(uint)array.Length)
                ThrowHelper.ThrowArgumentOutOfRangeException();
#else
            if ((uint)start > (uint)array.Length || (uint)length > (uint)(array.Length - start))
                ThrowHelper.ThrowArgumentOutOfRangeException();
#endif

            _pointer = new ByReference<T>(ref Unsafe.Add(ref Unsafe.As<byte, T>(ref array.GetRawSzArrayData()), start));
            _length = length;
        }
Beispiel #16
0
        private static ByReference <T> InternalAddress(T[,] array, int index1, int index2)
        {
            MDArrayRank2 <T> mdArrayObj = Unsafe.As <MDArrayRank2 <T> >(array);

            if ((index1 < 0) || (index1 >= mdArrayObj._upperBound1))
            {
                throw new IndexOutOfRangeException();
            }
            if ((index2 < 0) || (index2 >= mdArrayObj._upperBound2))
            {
                throw new IndexOutOfRangeException();
            }

            int index = (index1 * mdArrayObj._upperBound2) + index2;

            int offset = ByReference <T> .SizeOfT() * index + 2 * 8;

            ByReference <int> _upperBound1Ref = ByReference <int> .FromRef(ref mdArrayObj._upperBound1);

            return(ByReference <int> .Cast <T>(ByReference <int> .AddRaw(_upperBound1Ref, offset)));
        }
Beispiel #17
0
 private TypedReference(object target, int offset, RuntimeTypeHandle typeHandle)
 {
     _value      = new ByReference <byte>(ref Unsafe.Add <byte>(ref target.GetRawData(), offset));
     _typeHandle = typeHandle;
 }
Beispiel #18
0
 internal Span(ref T reference)
 {
     _reference = new ByReference <T>(ref reference);
     _length    = 1;
 }
Beispiel #19
0
 public static T Get(T[,] array, int index1, int index2)
 {
     return(ByReference <T> .Load(InternalAddress(array, index1, index2)));
 }
Beispiel #20
0
 public MemoryMappedPtr(ref T ptr,
                        ulong length)
 {
     _pointer = new ByReference <T>(ref ptr);
     _length  = length;
 }
Beispiel #21
0
 /// <summary>
 /// An internal helper for creating spans.
 /// </summary>
 internal ReadOnlySpan(ref T ptr, int length)
 {
     _pointer = new ByReference <T>(ref ptr);
     _length  = length;
 }