This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ref APIs and unit tests for System.Text.Unicode.Utf8
- Loading branch information
1 parent
d866aa6
commit 2f0c80c
Showing
14 changed files
with
1,290 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
<Project> | ||
<PropertyGroup> | ||
<BuildConfigurations> | ||
netcoreapp; | ||
netstandard; | ||
</BuildConfigurations> | ||
</PropertyGroup> | ||
</Project> | ||
</Project> |
11 changes: 9 additions & 2 deletions
11
src/CoreFx.Private.TestUtilities/ref/CoreFx.Private.TestUtilities.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<ProjectGuid>{E2E59C98-998F-9965-991D-99411166AF6F}</ProjectGuid> | ||
<ShouldWriteSigningRequired>false</ShouldWriteSigningRequired> | ||
<AllowReferenceFromRuntime>true</AllowReferenceFromRuntime> | ||
<RuntimeProjectFile>$(RepoRoot)\external\test-runtime\XUnit.Runtime.depproj</RuntimeProjectFile> | ||
<Configurations>netstandard-Debug;netstandard-Release</Configurations> | ||
<Configurations>netcoreapp-Debug;netcoreapp-Release;netstandard-Debug;netstandard-Release</Configurations> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Compile Include="CoreFx.Private.TestUtilities.cs" /> | ||
<ReferenceFromRuntime Include="xunit.core" /> | ||
</ItemGroup> | ||
<ItemGroup Condition="'$(TargetGroup)' == 'netcoreapp'"> | ||
<Compile Include="CoreFx.Private.TestUtilities.netcoreapp.cs" /> | ||
<ProjectReference Include="..\..\System.Collections\ref\System.Collections.csproj" /> | ||
<ProjectReference Include="..\..\System.Diagnostics.Process\ref\System.Diagnostics.Process.csproj" /> | ||
<ProjectReference Include="..\..\System.Runtime\ref\System.Runtime.csproj" /> | ||
<ProjectReference Include="..\..\System.Runtime.InteropServices\ref\System.Runtime.InteropServices.csproj" /> | ||
</ItemGroup> | ||
</Project> |
30 changes: 30 additions & 0 deletions
30
src/CoreFx.Private.TestUtilities/ref/CoreFx.Private.TestUtilities.netcoreapp.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// 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. | ||
// ------------------------------------------------------------------------------ | ||
// Changes to this file must follow the http://aka.ms/api-review process. | ||
// ------------------------------------------------------------------------------ | ||
|
||
namespace System.Buffers | ||
{ | ||
public static partial class BoundedMemory | ||
{ | ||
public static System.Buffers.BoundedMemory<T> Allocate<T>(int elementCount, System.Buffers.PoisonPagePlacement placement = System.Buffers.PoisonPagePlacement.After) where T : unmanaged { throw null; } | ||
public static System.Buffers.BoundedMemory<T> AllocateFromExistingData<T>(System.ReadOnlySpan<T> data, System.Buffers.PoisonPagePlacement placement = System.Buffers.PoisonPagePlacement.After) where T : unmanaged { throw null; } | ||
public static System.Buffers.BoundedMemory<T> AllocateFromExistingData<T>(T[] data, System.Buffers.PoisonPagePlacement placement = System.Buffers.PoisonPagePlacement.After) where T : unmanaged { throw null; } | ||
} | ||
public abstract partial class BoundedMemory<T> : IDisposable where T : unmanaged | ||
{ | ||
public abstract bool IsReadonly { get; } | ||
public abstract System.Memory<T> Memory { get; } | ||
public abstract System.Span<T> Span { get; } | ||
public abstract void Dispose(); | ||
public abstract void MakeReadonly(); | ||
public abstract void MakeWriteable(); | ||
} | ||
public enum PoisonPagePlacement | ||
{ | ||
After = 0, | ||
Before = 1, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/CoreFx.Private.TestUtilities/src/System/Buffers/BoundedMemory.Creation.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// 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. | ||
|
||
using System.Runtime.InteropServices; | ||
|
||
namespace System.Buffers | ||
{ | ||
/// <summary> | ||
/// Contains factory methods to create <see cref="BoundedMemory{T}"/> instances. | ||
/// </summary> | ||
public static partial class BoundedMemory | ||
{ | ||
/// <summary> | ||
/// Allocates a new <see cref="BoundedMemory{T}"/> region which is immediately preceded by | ||
/// or immediately followed by a poison (MEM_NOACCESS) page. If <paramref name="placement"/> | ||
/// is <see cref="PoisonPagePlacement.Before"/>, then attempting to read the memory | ||
/// immediately before the returned <see cref="BoundedMemory{T}"/> will result in an AV. | ||
/// If <paramref name="placement"/> is <see cref="PoisonPagePlacement.After"/>, then | ||
/// attempting to read the memory immediately after the returned <see cref="BoundedMemory{T}"/> | ||
/// will result in AV. | ||
/// </summary> | ||
/// <remarks> | ||
/// The newly-allocated memory will be populated with random data. | ||
/// </remarks> | ||
public static BoundedMemory<T> Allocate<T>(int elementCount, PoisonPagePlacement placement = PoisonPagePlacement.After) where T : unmanaged | ||
{ | ||
if (elementCount < 0) | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(elementCount)); | ||
} | ||
if (placement != PoisonPagePlacement.Before && placement != PoisonPagePlacement.After) | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(placement)); | ||
} | ||
|
||
var retVal = AllocateWithoutDataPopulation<T>(elementCount, placement); | ||
new Random().NextBytes(MemoryMarshal.AsBytes(retVal.Span)); // doesn't need to be cryptographically strong | ||
return retVal; | ||
} | ||
|
||
/// <summary> | ||
/// Similar to <see cref="Allocate(int, PoisonPagePlacement)"/>, but populates the allocated | ||
/// native memory block from existing data rather than using random data. | ||
/// </summary> | ||
public static BoundedMemory<T> AllocateFromExistingData<T>(ReadOnlySpan<T> data, PoisonPagePlacement placement = PoisonPagePlacement.After) where T : unmanaged | ||
{ | ||
if (placement != PoisonPagePlacement.Before && placement != PoisonPagePlacement.After) | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(placement)); | ||
} | ||
|
||
var retVal = AllocateWithoutDataPopulation<T>(data.Length, placement); | ||
data.CopyTo(retVal.Span); | ||
return retVal; | ||
} | ||
|
||
/// <summary> | ||
/// Similar to <see cref="Allocate(int, PoisonPagePlacement)"/>, but populates the allocated | ||
/// native memory block from existing data rather than using random data. | ||
/// </summary> | ||
public static BoundedMemory<T> AllocateFromExistingData<T>(T[] data, PoisonPagePlacement placement = PoisonPagePlacement.After) where T : unmanaged | ||
{ | ||
return AllocateFromExistingData(new ReadOnlySpan<T>(data), placement); | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/CoreFx.Private.TestUtilities/src/System/Buffers/BoundedMemory.Unix.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// 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 System.Buffers | ||
{ | ||
public static partial class BoundedMemory | ||
{ | ||
private static UnixImplementation<T> AllocateWithoutDataPopulation<T>(int elementCount, PoisonPagePlacement placement) where T : unmanaged | ||
{ | ||
// On non-Windows platforms, we don't yet have support for changing the permissions of individual pages. | ||
|
||
return new UnixImplementation<T>(elementCount); | ||
} | ||
|
||
private sealed class UnixImplementation<T> : BoundedMemory<T> where T : unmanaged | ||
{ | ||
private readonly T[] _buffer; | ||
|
||
public UnixImplementation(int elementCount) | ||
{ | ||
_buffer = new T[elementCount]; | ||
} | ||
|
||
public override bool IsReadonly => false; | ||
|
||
public override Memory<T> Memory => _buffer; | ||
|
||
public override Span<T> Span => _buffer; | ||
|
||
public override void Dispose() | ||
{ | ||
// no-op | ||
} | ||
|
||
public override void MakeReadonly() | ||
{ | ||
// no-op | ||
} | ||
|
||
public override void MakeWriteable() | ||
{ | ||
// no-op | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.