Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ The System.Reflection.Metadata library is built-in as part of the shared framewo
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.ReadFile_SafeHandle_IntPtr.cs"
Link="Common\Interop\Windows\kernel32\Interop.ReadFile_SafeHandle_IntPtr.cs"
Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'" />
<Compile Include="$(CoreLibSharedDir)System\Runtime\CompilerServices\IsExternalInit.cs" Link="System\Runtime\CompilerServices\IsExternalInit.cs" Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'" />
<Compile Include="System\Reflection\Internal\Utilities\IBinaryReader.cs" />
<Compile Include="System\Reflection\Internal\Utilities\PinnedObject.cs" />
<Compile Include="System\Reflection\Internal\Utilities\CriticalDisposableObject.cs" />
<Compile Include="System\Reflection\Internal\Utilities\ExceptionUtilities.cs" />
Expand Down Expand Up @@ -104,7 +106,6 @@ The System.Reflection.Metadata library is built-in as part of the shared framewo
<Compile Include="System\Reflection\Internal\MemoryBlocks\MemoryBlockProvider.cs" />
<Compile Include="System\Reflection\Internal\MemoryBlocks\MemoryMappedFileBlock.cs" />
<Compile Include="System\Reflection\Internal\MemoryBlocks\NativeHeapMemoryBlock.cs" />
<Compile Include="System\Reflection\Internal\MemoryBlocks\StreamConstraints.cs" />
<Compile Include="System\Reflection\Internal\MemoryBlocks\StreamMemoryBlockProvider.cs" />
<Compile Include="System\Reflection\Internal\Utilities\BitArithmetic.cs" />
<Compile Include="System\Reflection\Internal\Utilities\StringUtils.cs" />
Expand All @@ -113,7 +114,6 @@ The System.Reflection.Metadata library is built-in as part of the shared framewo
<Compile Include="System\Reflection\Internal\Utilities\StreamExtensions.netcoreapp.cs" Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp'" />
<Compile Include="System\Reflection\Internal\Utilities\StreamExtensions.netstandard2.0.cs" Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'" />
<Compile Include="System\Reflection\Internal\Utilities\Hash.cs" />
<Compile Include="System\Reflection\Internal\Utilities\ImmutableMemoryStream.cs" />
<Compile Include="System\Reflection\Internal\Utilities\MemoryBlock.cs" />
<Compile Include="System\Reflection\Internal\Utilities\PooledStringBuilder.cs" />
<Compile Include="System\Reflection\Internal\Utilities\ObjectPool`1.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,6 @@ protected override AbstractMemoryBlock GetMemoryBlockImpl(int start, int size)
return new ByteArrayMemoryBlock(this, start, size);
}

public override Stream GetStream(out StreamConstraints constraints)
{
constraints = new StreamConstraints(null, 0, Size);
return new ImmutableMemoryStream(_array);
}

internal unsafe byte* Pointer
{
get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ protected override AbstractMemoryBlock GetMemoryBlockImpl(int start, int size)
return new ExternalMemoryBlock(this, _memory + start, size);
}

public override Stream GetStream(out StreamConstraints constraints)
{
constraints = new StreamConstraints(null, 0, _size);
return new ReadOnlyUnmanagedMemoryStream(_memory, _size);
}

protected override void Dispose(bool disposing)
{
Debug.Assert(disposing);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,6 @@ public AbstractMemoryBlock GetMemoryBlock(int start, int size)
/// <exception cref="IOException">IO error while reading from the underlying stream.</exception>
protected abstract AbstractMemoryBlock GetMemoryBlockImpl(int start, int size);

/// <summary>
/// Gets a seekable and readable <see cref="Stream"/> that can be used to read all data.
/// The operations on the stream has to be done under a lock of <see cref="StreamConstraints.GuardOpt"/> if non-null.
/// The image starts at <see cref="StreamConstraints.ImageStart"/> and has size <see cref="StreamConstraints.ImageSize"/>.
/// It is the caller's responsibility not to read outside those bounds.
/// </summary>
public abstract Stream GetStream(out StreamConstraints constraints);

/// <summary>
/// The size of the data.
/// </summary>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,7 @@ internal static unsafe NativeHeapMemoryBlock ReadMemoryBlockNoLock(Stream stream
try
{
stream.Seek(start, SeekOrigin.Begin);

int bytesRead = 0;

if ((bytesRead = stream.Read(block.Pointer, size)) != size)
{
stream.CopyTo(block.Pointer + bytesRead, size - bytesRead);
}
stream.ReadExactly(block.Pointer, size);

fault = false;
}
Expand All @@ -101,12 +95,7 @@ protected override AbstractMemoryBlock GetMemoryBlockImpl(int start, int size)

if (_useMemoryMap && size > MemoryMapThreshold)
{
if (TryCreateMemoryMappedFileBlock(absoluteStart, size, out MemoryMappedFileBlock? block))
{
return block;
}

_useMemoryMap = false;
return CreateMemoryMappedFileBlock(absoluteStart, size);
}

lock (_streamGuard)
Expand All @@ -115,26 +104,18 @@ protected override AbstractMemoryBlock GetMemoryBlockImpl(int start, int size)
}
}

public override Stream GetStream(out StreamConstraints constraints)
{
constraints = new StreamConstraints(_streamGuard, _imageStart, _imageSize);
return _stream;
}

/// <exception cref="IOException">IO error while mapping memory or not enough memory to create the mapping.</exception>
private unsafe bool TryCreateMemoryMappedFileBlock(long start, int size, [NotNullWhen(true)] out MemoryMappedFileBlock? block)
private unsafe MemoryMappedFileBlock CreateMemoryMappedFileBlock(long start, int size)
{
if (_lazyMemoryMap == null)
{
// leave the underlying stream open. It will be closed by the Dispose method.
MemoryMappedFile newMemoryMap;

// CreateMemoryMap might modify the stream (calls FileStream.Flush)
lock (_streamGuard)
{
try
{
newMemoryMap =
// leave the underlying stream open. It will be closed by the Dispose method.
_lazyMemoryMap ??=
MemoryMappedFile.CreateFromFile(
fileStream: (FileStream)_stream,
mapName: null,
Expand All @@ -148,17 +129,6 @@ private unsafe bool TryCreateMemoryMappedFileBlock(long start, int size, [NotNul
throw new IOException(e.Message, e);
}
}

if (newMemoryMap == null)
{
block = null;
return false;
}

if (Interlocked.CompareExchange(ref _lazyMemoryMap, newMemoryMap, null) != null)
{
newMemoryMap.Dispose();
}
}

MemoryMappedViewAccessor accessor;
Expand All @@ -168,14 +138,7 @@ private unsafe bool TryCreateMemoryMappedFileBlock(long start, int size, [NotNul
accessor = _lazyMemoryMap.CreateViewAccessor(start, size, MemoryMappedFileAccess.Read);
}

if (accessor == null)
{
block = null;
return false;
}

block = new MemoryMappedFileBlock(accessor, accessor.SafeMemoryMappedViewHandle, accessor.PointerOffset, size);
return true;
return new MemoryMappedFileBlock(accessor, accessor.SafeMemoryMappedViewHandle, accessor.PointerOffset, size);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace System.Reflection
{
/// <summary>
/// Provides an abstraction for reading binary data from a stream.
/// </summary>
/// <seealso cref="Metadata.BlobReader"/>
/// <seealso cref="PortableExecutable.PEBinaryReader"/>
internal interface IBinaryReader
{
public int Offset { get; set; }

public byte ReadByte();

public short ReadInt16();

public ushort ReadUInt16();

public int ReadInt32();

public uint ReadUInt32();

public ulong ReadUInt64();

/// <summary>
/// Reads a fixed-length byte block as a null-padded UTF-8 encoded string.
/// The padding is not included in the returned string.
///
/// Note that it is legal for UTF-8 strings to contain NUL; if NUL occurs
/// between non-NUL codepoints, it is not considered to be padding and
/// is included in the result.
/// </summary>
public string ReadNullPaddedUTF8(int byteCount);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,6 @@ namespace System.Reflection.Internal
{
internal static partial class StreamExtensions
{
// From System.IO.Stream.CopyTo:
// We pick a value that is the largest multiple of 4096 that is still smaller than the large object heap threshold (85K).
// The CopyTo/CopyToAsync buffer is short-lived and is likely to be collected at Gen0, and it offers a significant
// improvement in Copy performance.
internal const int StreamCopyBufferSize = 81920;

/// <summary>
/// Copies specified amount of data from given stream to a target memory pointer.
/// </summary>
/// <exception cref="IOException">unexpected stream end.</exception>
internal static unsafe void CopyTo(this Stream source, byte* destination, int size)
{
byte[] buffer = new byte[Math.Min(StreamCopyBufferSize, size)];
while (size > 0)
{
int readSize = Math.Min(size, buffer.Length);
int bytesRead = source.Read(buffer, 0, readSize);

if (bytesRead <= 0 || bytesRead > readSize)
{
throw new IOException(SR.UnexpectedStreamEnd);
}

Marshal.Copy(buffer, 0, (IntPtr)destination, bytesRead);

destination += bytesRead;
size -= bytesRead;
}
}

/// <summary>
/// Attempts to read all of the requested bytes from the stream into the buffer
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace System.Reflection.Internal
{
internal static partial class StreamExtensions
{
internal static unsafe int Read(this Stream stream, byte* buffer, int size)
=> stream.Read(new Span<byte>(buffer, size));
internal static unsafe void ReadExactly(this Stream stream, byte* buffer, int size)
=> stream.ReadExactly(new Span<byte>(buffer, size));
}
}
Loading
Loading