Example #1
0
        /// <summary>
        /// Gets an <see cref="MemoryManager{T}"/> from the underlying read-only memory.
        /// If unable to get the <typeparamref name="TManager"/> type, returns false.
        /// </summary>
        /// <typeparam name="T">The element type of the <paramref name="memory" />.</typeparam>
        /// <typeparam name="TManager">The type of <see cref="MemoryManager{T}"/> to try and retrive.</typeparam>
        /// <param name="memory">The memory to get the manager for.</param>
        /// <param name="manager">The returned manager of the <see cref="ReadOnlyMemory{T}"/>.</param>
        /// <returns>A <see cref="bool"/> indicating if it was successful.</returns>
        public static bool TryGetMemoryManager <T, TManager>(ReadOnlyMemory <T> memory, out TManager manager)
            where TManager : MemoryManager <T>
        {
            TManager localManager; // Use register for null comparison rather than byref

            manager = localManager = memory.GetObjectStartLength(out _, out _) as TManager;
            return(manager != null);
        }
Example #2
0
        public static bool TryGetMemoryManager <T, TManager>(ReadOnlyMemory <T> memory, out TManager?manager) // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
            where TManager : MemoryManager <T>
        {
            TManager?localManager;  // Use register for null comparison rather than byref

            manager = localManager = memory.GetObjectStartLength(out _, out _) as TManager;
            return(localManager != null);
        }
Example #3
0
        /// <summary>
        /// Gets a <see cref="OwnedMemory{T}"/> and <paramref name="index" />, <paramref name="length" /> from the underlying memory.
        /// If unable to get the <typeparamref name="TOwner"/> type, returns false.
        /// </summary>
        /// <typeparam name="T">The element type of the <paramref name="readOnlyMemory" />.</typeparam>
        /// <typeparam name="TOwner">The type of <see cref="OwnedMemory{T}"/> to try and retrive.</typeparam>
        /// <param name="readOnlyMemory">The memory to get the owner for.</param>
        /// <param name="ownedMemory">The returned owner of the <see cref="ReadOnlyMemory{T}"/>.</param>
        /// <param name="index">The offset from the start of the <paramref name="ownedMemory" /> that the <paramref name="readOnlyMemory" /> represents.</param>
        /// <param name="length">The length of the <paramref name="ownedMemory" /> that the <paramref name="readOnlyMemory" /> represents.</param>
        /// <returns>A <see cref="bool"/> indicating if it was successful.</returns>
        public static bool TryGetOwnedMemory <T, TOwner>(ReadOnlyMemory <T> readOnlyMemory, out TOwner ownedMemory, out int index, out int length)
            where TOwner : OwnedMemory <T>
        {
            TOwner owner; // Use register for null comparison rather than byref

            ownedMemory = owner = readOnlyMemory.GetObjectStartLength(out index, out length) as TOwner;
            return(!ReferenceEquals(owner, null));
        }
Example #4
0
        /// <summary>
        /// Gets an <see cref="OwnedMemory{T}"/> and <paramref name="start" />, <paramref name="length" /> from the underlying read-only memory.
        /// If unable to get the <typeparamref name="TOwner"/> type, returns false.
        /// </summary>
        /// <typeparam name="T">The element type of the <paramref name="memory" />.</typeparam>
        /// <typeparam name="TOwner">The type of <see cref="OwnedMemory{T}"/> to try and retrive.</typeparam>
        /// <param name="memory">The memory to get the owner for.</param>
        /// <param name="owner">The returned owner of the <see cref="ReadOnlyMemory{T}"/>.</param>
        /// <param name="start">The offset from the start of the <paramref name="owner" /> that the <paramref name="memory" /> represents.</param>
        /// <param name="length">The length of the <paramref name="owner" /> that the <paramref name="memory" /> represents.</param>
        /// <returns>A <see cref="bool"/> indicating if it was successful.</returns>
        public static bool TryGetOwnedMemory <T, TOwner>(ReadOnlyMemory <T> memory, out TOwner owner, out int start, out int length)
            where TOwner : OwnedMemory <T>
        {
            TOwner localOwner; // Use register for null comparison rather than byref

            owner  = localOwner = memory.GetObjectStartLength(out start, out length) as TOwner;
            start &= ReadOnlyMemory <T> .RemoveOwnedFlagBitMask;
            return(!ReferenceEquals(owner, null));
        }
Example #5
0
        /// <summary>
        /// Gets an <see cref="MemoryManager{T}"/> and <paramref name="start" />, <paramref name="length" /> from the underlying read-only memory.
        /// If unable to get the <typeparamref name="TManager"/> type, returns false.
        /// </summary>
        /// <typeparam name="T">The element type of the <paramref name="memory" />.</typeparam>
        /// <typeparam name="TManager">The type of <see cref="MemoryManager{T}"/> to try and retrive.</typeparam>
        /// <param name="memory">The memory to get the manager for.</param>
        /// <param name="manager">The returned manager of the <see cref="ReadOnlyMemory{T}"/>.</param>
        /// <param name="start">The offset from the start of the <paramref name="manager" /> that the <paramref name="memory" /> represents.</param>
        /// <param name="length">The length of the <paramref name="manager" /> that the <paramref name="memory" /> represents.</param>
        /// <returns>A <see cref="bool"/> indicating if it was successful.</returns>
        public static bool TryGetMemoryManager <T, TManager>(ReadOnlyMemory <T> memory, out TManager manager, out int start, out int length)
            where TManager : MemoryManager <T>
        {
            TManager localManager; // Use register for null comparison rather than byref

            manager = localManager = memory.GetObjectStartLength(out start, out length) as TManager;

            Debug.Assert(length >= 0);

            if (manager == null)
            {
                start  = default;
                length = default;
                return(false);
            }
            return(true);
        }
Example #6
0
        public static bool TryGetMemoryManager <T, TManager>(ReadOnlyMemory <T> memory, out TManager?manager, out int start, out int length) // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
            where TManager : MemoryManager <T>
        {
            TManager?localManager;  // Use register for null comparison rather than byref

            manager = localManager = memory.GetObjectStartLength(out start, out length) as TManager;

            Debug.Assert(length >= 0);

            if (localManager == null)
            {
                start  = default;
                length = default;
                return(false);
            }
            return(true);
        }