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 5854050
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 81 deletions.
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();
}
}
}
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 5854050

Please sign in to comment.