Skip to content

Commit

Permalink
Switch MappedMappedDataSource to UnmanagedDataSource
Browse files Browse the repository at this point in the history
  • Loading branch information
Windows10CE committed Apr 11, 2024
1 parent cbc1389 commit 2b4897c
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 87 deletions.
6 changes: 1 addition & 5 deletions src/AsmResolver/AsmResolver.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,9 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net35'">
<PackageReference Include="MonoMod.Backports" Version="1.1.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="System.Memory" Version="4.5.5" />
</ItemGroup>

</Project>
65 changes: 0 additions & 65 deletions src/AsmResolver/IO/MemoryMappedDataSource.cs

This file was deleted.

11 changes: 5 additions & 6 deletions src/AsmResolver/IO/MemoryMappedInputFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ namespace AsmResolver.IO
public sealed class MemoryMappedInputFile : IInputFile
{
private readonly MemoryMappedFileShim _file;
private readonly MemoryMappedDataSource _dataSource;
private readonly UnmanagedDataSource _dataSource;

/// <summary>
/// Creates a new reader factory for the provided file.
/// </summary>
/// <param name="filePath">The path to the file to read.</param>
public MemoryMappedInputFile(string filePath)
public unsafe MemoryMappedInputFile(string filePath)
{
FilePath = filePath ?? throw new ArgumentNullException(nameof(filePath));
_file = new MemoryMappedFileShim(filePath);
_dataSource = new MemoryMappedDataSource(_file);
_dataSource = new UnmanagedDataSource(_file.BasePointer, (ulong)_file.Size);
}

/// <inheritdoc />
Expand All @@ -33,14 +33,13 @@ public string FilePath
public uint Length => (uint) _dataSource.Length;

/// <inheritdoc />
public BinaryStreamReader CreateReader(ulong address, uint rva, uint length) =>
new(_dataSource, address, rva, length);
public unsafe BinaryStreamReader CreateReader(ulong address, uint rva, uint length) =>
new(_dataSource, address != 0 ? address : (ulong)_file.BasePointer, rva, length);

/// <inheritdoc />
public void Dispose()
{
_file.Dispose();
_dataSource.Dispose();
}
}
}
2 changes: 1 addition & 1 deletion src/AsmResolver/Shims/MemoryMappedFileShim.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal sealed unsafe partial class MemoryMappedFileShim
private const uint FILE_SHARE_READ = 0x2;
private const uint OPEN_EXISTING = 3;
private const uint PAGE_READONLY = 2;
// not on msdn, stole from terrafx
// not on msdn, taken from https://github.com/terrafx/terrafx.interop.windows/blob/e681ccb7239bf9f5629083cd1e396b2876c24aae/sources/Interop/Windows/Windows/um/memoryapi/FILE.cs#L14
private const uint FILE_MAP_READ = 4;

private void* _fileHandle;
Expand Down
11 changes: 1 addition & 10 deletions src/AsmResolver/Shims/MemoryMappedFileShim.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public MemoryMappedFileShim(string path)
}

public long Size => _size;
public byte* BasePointer => _file;

public byte ReadByte(long address)
{
Expand All @@ -27,16 +28,6 @@ public byte ReadByte(long address)
return _file[address];
}

public ReadOnlySpan<byte> GetSpan(long offset, int length)
{
if (_file == null)
throw new ObjectDisposedException("disposed");
long newSize = _size - offset;
if (offset < 0 || newSize < 0 || length > newSize)
throw new ArgumentOutOfRangeException();
return new(_file + offset, length);
}

private void DisposeCore()
{
void* filePointer;
Expand Down
1 change: 1 addition & 0 deletions test/AsmResolver.Tests/IO/MemoryMappedIOTest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.IO;
using AsmResolver.IO;
using AsmResolver.Tests.Runners;
Expand Down

0 comments on commit 2b4897c

Please sign in to comment.