-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The new certificate loader only loads one data type per method, unlike the previous loader mechanism (new X509Certiicate2(bytes, ...)). It also allows for caller configuration to control cost-of-work limits and some common usability gotchas around Windows PFX loading. This change adds the new loader, and changes the X509Certificate2 ctors to use it; a followup will mark the ctors as Obsolete and update usage in the dotnet/runtime codebase.
- Loading branch information
Showing
86 changed files
with
7,995 additions
and
2,480 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
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
108 changes: 108 additions & 0 deletions
108
src/libraries/Common/src/System/IO/MemoryMappedFiles/MemoryMappedFileMemoryManager.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,108 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Buffers; | ||
|
||
namespace System.IO.MemoryMappedFiles | ||
{ | ||
internal sealed unsafe class MemoryMappedFileMemoryManager : MemoryManager<byte> | ||
{ | ||
private byte* _pointer; | ||
private int _length; | ||
private MemoryMappedFile _mappedFile; | ||
private MemoryMappedViewAccessor _accessor; | ||
|
||
public MemoryMappedFileMemoryManager( | ||
byte* pointer, | ||
int length, | ||
MemoryMappedFile mappedFile, | ||
MemoryMappedViewAccessor accessor) | ||
{ | ||
_pointer = pointer; | ||
_length = length; | ||
_mappedFile = mappedFile; | ||
_accessor = accessor; | ||
} | ||
|
||
#if DEBUG | ||
#pragma warning disable CA2015 | ||
~MemoryMappedFileMemoryManager() | ||
#pragma warning restore CA2015 | ||
{ | ||
Environment.FailFast("MemoryMappedFileMemoryManager was finalized."); | ||
} | ||
#endif | ||
|
||
internal static MemoryMappedFileMemoryManager CreateFromFileClamped( | ||
FileStream fileStream, | ||
MemoryMappedFileAccess access = MemoryMappedFileAccess.Read, | ||
HandleInheritability inheritability = HandleInheritability.None, | ||
bool leaveOpen = false) | ||
{ | ||
int length = (int)Math.Min(int.MaxValue, fileStream.Length); | ||
MemoryMappedFile mapped = MemoryMappedFile.CreateFromFile(fileStream, null, 0, access, inheritability, leaveOpen); | ||
MemoryMappedViewAccessor? accessor = null; | ||
byte* pointer = null; | ||
|
||
try | ||
{ | ||
accessor = mapped.CreateViewAccessor(0, length, access); | ||
accessor.SafeMemoryMappedViewHandle.AcquirePointer(ref pointer); | ||
|
||
return new MemoryMappedFileMemoryManager(pointer, length, mapped, accessor); | ||
} | ||
catch (Exception) | ||
{ | ||
if (pointer != null) | ||
{ | ||
accessor!.SafeMemoryMappedViewHandle.ReleasePointer(); | ||
} | ||
|
||
accessor?.Dispose(); | ||
mapped.Dispose(); | ||
throw; | ||
} | ||
} | ||
|
||
protected override void Dispose(bool disposing) | ||
{ | ||
_pointer = null; | ||
_length = -1; | ||
_accessor?.SafeMemoryMappedViewHandle.ReleasePointer(); | ||
_accessor?.Dispose(); | ||
_mappedFile?.Dispose(); | ||
_accessor = null!; | ||
_mappedFile = null!; | ||
} | ||
|
||
public override Span<byte> GetSpan() | ||
{ | ||
ThrowIfDisposed(); | ||
return new Span<byte>(_pointer, _length); | ||
} | ||
|
||
public override MemoryHandle Pin(int elementIndex = 0) | ||
{ | ||
ThrowIfDisposed(); | ||
return default; | ||
} | ||
|
||
public override void Unpin() | ||
{ | ||
ThrowIfDisposed(); | ||
// nop | ||
} | ||
|
||
private void ThrowIfDisposed() | ||
{ | ||
#if NET | ||
ObjectDisposedException.ThrowIf(_length < 0, this); | ||
#else | ||
if (_length < 0) | ||
{ | ||
throw new ObjectDisposedException(GetType().FullName); | ||
} | ||
#endif | ||
} | ||
} | ||
} |
Oops, something went wrong.