Skip to content

Commit

Permalink
Code style
Browse files Browse the repository at this point in the history
  • Loading branch information
Windows10CE committed Mar 21, 2024
1 parent 074b623 commit cbc1389
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 20 deletions.
8 changes: 6 additions & 2 deletions src/AsmResolver/Shims/MemoryMappedFileShim.Unix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,20 @@ internal sealed unsafe partial class MemoryMappedFileShim

private void AllocateFileUnix(string path)
{
var fd = open(path, 0);
int fd = open(path, 0);
if (fd == -1)
throw new Win32Exception();

_size = lseek(fd, 0, SEEK_END);
if (_size == -1)
throw new Win32Exception();
var mapping = mmap(null, (nuint)_size, PROT_READ, MAP_PRIVATE, fd, 0);

nint mapping = mmap(null, (nuint)_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (mapping == -1)
throw new Win32Exception();
_file = (byte*)mapping;

// mmap explicitly documents that it is fine to close the fd after mapping
close(fd);
}

Expand Down
21 changes: 15 additions & 6 deletions src/AsmResolver/Shims/MemoryMappedFileShim.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,29 @@ private void AllocateFileWindows(string path)
{
handle = CreateFileW(pathPointer, GENERIC_READ, FILE_SHARE_READ, null, OPEN_EXISTING, 0);
}
if (handle == -1) throw new Win32Exception();
if (handle == -1)
throw new Win32Exception();

_fileHandle = (void*)handle;
if (!GetFileSizeEx(_fileHandle, out _size)) throw new Win32Exception();
if (!GetFileSizeEx(_fileHandle, out _size))
throw new Win32Exception();

handle = CreateFileMappingA(_fileHandle, null, PAGE_READONLY, 0, 0, null);
if (handle == -1) throw new Win32Exception();
if (handle == -1)
throw new Win32Exception();
_mappingHandle = (void*)handle;

_file = (byte*)MapViewOfFile(_mappingHandle, FILE_MAP_READ, 0, 0, 0);
if (_file == null) throw new Win32Exception();
if (_file == null)
throw new Win32Exception();
}

private void DisposeCoreWindows(void* filePointer)
{
if (!UnmapViewOfFile(filePointer)) return;
if (!CloseHandle(_mappingHandle)) return;
if (!UnmapViewOfFile(filePointer))
return;
if (!CloseHandle(_mappingHandle))
return;
CloseHandle(_fileHandle);
}
}
19 changes: 7 additions & 12 deletions src/AsmResolver/Shims/MemoryMappedFileShim.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,9 @@ internal sealed unsafe partial class MemoryMappedFileShim : IDisposable
public MemoryMappedFileShim(string path)
{
if (RuntimeInformationShim.IsRunningOnWindows)
{
AllocateFileWindows(path);
}
else
{
AllocateFileUnix(path);
}
}

public long Size => _size;
Expand All @@ -33,9 +29,11 @@ public byte ReadByte(long address)

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

Expand All @@ -46,15 +44,12 @@ private void DisposeCore()
{
filePointer = (void*)Interlocked.Exchange(ref *(IntPtr*)p, default);
}
if (filePointer == null) return;
if (filePointer == null)
return;
if (RuntimeInformationShim.IsRunningOnWindows)
{
DisposeCoreWindows(filePointer);
}
else
{
DisposeCoreUnix(filePointer);
}
}

public void Dispose()
Expand Down

0 comments on commit cbc1389

Please sign in to comment.