Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

[WIP] Don't Merge MemoryMarshal #14833

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/mscorlib/System.Private.CoreLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,7 @@
<Compile Include="$(BclSourcesRoot)\mscorlib.Friends.cs" />
</ItemGroup>
<ItemGroup>
<Compile Include="src\System\Runtime\InteropServices\MemoryMarshal.cs" />
<Compile Include="src\System\Runtime\RuntimeImports.cs" />
</ItemGroup>
<Import Project="shared\System.Private.CoreLib.Shared.projitems" />
Expand Down Expand Up @@ -690,4 +691,4 @@
<Win32Resource Condition="'$(GenerateNativeVersionInfo)'=='true'">$(IntermediateOutputPath)\System.Private.CoreLib.res</Win32Resource>
</PropertyGroup>
<Import Project="GenerateCompilerResponseFile.targets" />
</Project>
</Project>
9 changes: 9 additions & 0 deletions src/mscorlib/shared/System/Memory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ internal Memory(OwnedMemory<T> owner, int index, int length)
_length = length;
}

// Constructor for internal use only
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal Memory(int start, int length, T[] array)
{
_arrayOrOwnedMemory = array;
_index = start;
_length = length;
}

/// <summary>
/// Defines an implicit conversion of an array to a <see cref="Memory{T}"/>
/// </summary>
Expand Down
31 changes: 31 additions & 0 deletions src/mscorlib/src/System/Runtime/InteropServices/MemoryMarshal.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace src.System.Runtime.InteropServices
{
using System;
using System.Runtime.CompilerServices;

public static class MemoryMarshal
{
/// <summary>
/// Creates a span over the range specified. However this does no bounds checking therefore is dangerous
/// all validation should be done prior to calling this method
/// </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>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Span<T> CreateSpan<T>(T[] array, int start, int length) => new Span<T>(ref Unsafe.Add(ref Unsafe.As<byte, T>(ref array.GetRawSzArrayData()), start), length);

/// <summary>
/// Creates a memory instance over the range. However this does no bounds checking therefore is dangerous
/// </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>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Memory<T> CreateMemory<T>(T[] array, int start, int length) => new Memory<T>(start, length, array);
}
}