Skip to content

Commit

Permalink
misc: Replace references to IntPtr/UIntPtr with nint/nuint + code cle…
Browse files Browse the repository at this point in the history
…anups.
  • Loading branch information
GreemDev committed Oct 26, 2024
1 parent a09d314 commit dfb4854
Show file tree
Hide file tree
Showing 172 changed files with 902 additions and 914 deletions.
4 changes: 2 additions & 2 deletions src/ARMeilleure/CodeGen/Arm64/HardwareCapabilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,13 @@ public enum LinuxFeatureFlagsHwCap2 : ulong
#region macOS

[LibraryImport("libSystem.dylib", SetLastError = true)]
private static unsafe partial int sysctlbyname([MarshalAs(UnmanagedType.LPStr)] string name, out int oldValue, ref ulong oldSize, IntPtr newValue, ulong newValueSize);
private static unsafe partial int sysctlbyname([MarshalAs(UnmanagedType.LPStr)] string name, out int oldValue, ref ulong oldSize, nint newValue, ulong newValueSize);

[SupportedOSPlatform("macos")]
private static bool CheckSysctlName(string name)
{
ulong size = sizeof(int);
if (sysctlbyname(name, out int val, ref size, IntPtr.Zero, 0) == 0 && size == sizeof(int))
if (sysctlbyname(name, out int val, ref size, nint.Zero, 0) == 0 && size == sizeof(int))
{
return val != 0;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ARMeilleure/CodeGen/CompiledFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public T Map<T>()
/// <typeparam name="T">Type of delegate</typeparam>
/// <param name="codePointer">Pointer to the function code in memory</param>
/// <returns>A delegate of type <typeparamref name="T"/> pointing to the mapped function</returns>
public T MapWithPointer<T>(out IntPtr codePointer)
public T MapWithPointer<T>(out nint codePointer)
{
codePointer = JitCache.Map(this);

Expand Down
2 changes: 1 addition & 1 deletion src/ARMeilleure/CodeGen/RegisterAllocators/LiveInterval.cs
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ public override bool Equals(object obj)

public override int GetHashCode()
{
return HashCode.Combine((IntPtr)_data);
return HashCode.Combine((nint)_data);
}

public override string ToString()
Expand Down
2 changes: 1 addition & 1 deletion src/ARMeilleure/CodeGen/RegisterAllocators/LiveRange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public override bool Equals(object obj)

public override int GetHashCode()
{
return HashCode.Combine((IntPtr)_data);
return HashCode.Combine((nint)_data);
}

public override string ToString()
Expand Down
16 changes: 8 additions & 8 deletions src/ARMeilleure/Common/AddressTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public int GetValue(ulong address)

private bool _disposed;
private TEntry** _table;
private readonly List<IntPtr> _pages;
private readonly List<nint> _pages;

/// <summary>
/// Gets the bits used by the <see cref="Levels"/> of the <see cref="AddressTable{TEntry}"/> instance.
Expand All @@ -76,15 +76,15 @@ public int GetValue(ulong address)
/// Gets the base address of the <see cref="EntryTable{TEntry}"/>.
/// </summary>
/// <exception cref="ObjectDisposedException"><see cref="EntryTable{TEntry}"/> instance was disposed</exception>
public IntPtr Base
public nint Base
{
get
{
ObjectDisposedException.ThrowIf(_disposed, this);

lock (_pages)
{
return (IntPtr)GetRootPage();
return (nint)GetRootPage();
}
}
}
Expand All @@ -104,7 +104,7 @@ public AddressTable(Level[] levels)
throw new ArgumentException("Table must be at least 2 levels deep.", nameof(levels));
}

_pages = new List<IntPtr>(capacity: 16);
_pages = new List<nint>(capacity: 16);

Levels = levels;
Mask = 0;
Expand Down Expand Up @@ -168,7 +168,7 @@ public ref TEntry GetValue(ulong address)

nextPage = i == Levels.Length - 2 ?
(TEntry*)Allocate(1 << nextLevel.Length, Fill, leaf: true) :
(TEntry*)Allocate(1 << nextLevel.Length, IntPtr.Zero, leaf: false);
(TEntry*)Allocate(1 << nextLevel.Length, nint.Zero, leaf: false);
}

page = (TEntry**)nextPage;
Expand All @@ -185,7 +185,7 @@ public ref TEntry GetValue(ulong address)
{
if (_table == null)
{
_table = (TEntry**)Allocate(1 << Levels[0].Length, fill: IntPtr.Zero, leaf: false);
_table = (TEntry**)Allocate(1 << Levels[0].Length, fill: nint.Zero, leaf: false);
}

return _table;
Expand All @@ -199,10 +199,10 @@ public ref TEntry GetValue(ulong address)
/// <param name="fill">Fill value</param>
/// <param name="leaf"><see langword="true"/> if leaf; otherwise <see langword="false"/></param>
/// <returns>Allocated block</returns>
private IntPtr Allocate<T>(int length, T fill, bool leaf) where T : unmanaged
private nint Allocate<T>(int length, T fill, bool leaf) where T : unmanaged
{
var size = sizeof(T) * length;
var page = (IntPtr)NativeAllocator.Instance.Allocate((uint)size);
var page = (nint)NativeAllocator.Instance.Allocate((uint)size);
var span = new Span<T>((void*)page, length);

span.Fill(fill);
Expand Down
14 changes: 7 additions & 7 deletions src/ARMeilleure/Common/ArenaAllocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ private class PageInfo
private List<PageInfo> _pages;
private readonly ulong _pageSize;
private readonly uint _pageCount;
private readonly List<IntPtr> _extras;
private readonly List<nint> _extras;

public ArenaAllocator(uint pageSize, uint pageCount)
{
Expand All @@ -31,11 +31,11 @@ public ArenaAllocator(uint pageSize, uint pageCount)
_pageIndex = -1;

_page = null;
_pages = new List<PageInfo>();
_pages = [];
_pageSize = pageSize;
_pageCount = pageCount;

_extras = new List<IntPtr>();
_extras = [];
}

public Span<T> AllocateSpan<T>(ulong count) where T : unmanaged
Expand Down Expand Up @@ -64,7 +64,7 @@ public Span<T> AllocateSpan<T>(ulong count) where T : unmanaged
{
void* extra = NativeAllocator.Instance.Allocate(size);

_extras.Add((IntPtr)extra);
_extras.Add((nint)extra);

return extra;
}
Expand All @@ -84,7 +84,7 @@ public Span<T> AllocateSpan<T>(ulong count) where T : unmanaged
{
_page = new PageInfo
{
Pointer = (byte*)NativeAllocator.Instance.Allocate(_pageSize),
Pointer = (byte*)NativeAllocator.Instance.Allocate(_pageSize)
};

_pages.Add(_page);
Expand Down Expand Up @@ -114,7 +114,7 @@ public void Reset()
}

// Free extra blocks that are not page-sized
foreach (IntPtr ptr in _extras)
foreach (nint ptr in _extras)
{
NativeAllocator.Instance.Free((void*)ptr);
}
Expand Down Expand Up @@ -173,7 +173,7 @@ protected override void Dispose(bool disposing)
NativeAllocator.Instance.Free(info.Pointer);
}

foreach (IntPtr ptr in _extras)
foreach (nint ptr in _extras)
{
NativeAllocator.Instance.Free((void*)ptr);
}
Expand Down
8 changes: 4 additions & 4 deletions src/ARMeilleure/Common/EntryTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class EntryTable<TEntry> : IDisposable where TEntry : unmanaged
private int _freeHint;
private readonly int _pageCapacity; // Number of entries per page.
private readonly int _pageLogCapacity;
private readonly Dictionary<int, IntPtr> _pages;
private readonly Dictionary<int, nint> _pages;
private readonly BitMap _allocated;

/// <summary>
Expand All @@ -41,7 +41,7 @@ public unsafe EntryTable(int pageSize = 4096)
}

_allocated = new BitMap(NativeAllocator.Instance);
_pages = new Dictionary<int, IntPtr>();
_pages = new Dictionary<int, nint>();
_pageLogCapacity = BitOperations.Log2((uint)(pageSize / sizeof(TEntry)));
_pageCapacity = 1 << _pageLogCapacity;
}
Expand Down Expand Up @@ -138,9 +138,9 @@ private unsafe Span<TEntry> GetPage(int index)
{
var pageIndex = (int)((uint)(index & ~(_pageCapacity - 1)) >> _pageLogCapacity);

if (!_pages.TryGetValue(pageIndex, out IntPtr page))
if (!_pages.TryGetValue(pageIndex, out nint page))
{
page = (IntPtr)NativeAllocator.Instance.Allocate((uint)sizeof(TEntry) * (uint)_pageCapacity);
page = (nint)NativeAllocator.Instance.Allocate((uint)sizeof(TEntry) * (uint)_pageCapacity);

_pages.Add(pageIndex, page);
}
Expand Down
4 changes: 2 additions & 2 deletions src/ARMeilleure/Common/NativeAllocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ unsafe sealed class NativeAllocator : Allocator

public override void* Allocate(ulong size)
{
void* result = (void*)Marshal.AllocHGlobal((IntPtr)size);
void* result = (void*)Marshal.AllocHGlobal((nint)size);

if (result == null)
{
Expand All @@ -21,7 +21,7 @@ unsafe sealed class NativeAllocator : Allocator

public override void Free(void* block)
{
Marshal.FreeHGlobal((IntPtr)block);
Marshal.FreeHGlobal((nint)block);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class IntrusiveList<T> where T : IEquatable<T>, IIntrusiveListNode<T>
/// <exception cref="ArgumentException"><typeparamref name="T"/> is not pointer sized.</exception>
public IntrusiveList()
{
if (Unsafe.SizeOf<T>() != IntPtr.Size)
if (Unsafe.SizeOf<T>() != nint.Size)
{
throw new ArgumentException("T must be a reference type or a pointer sized struct.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public MemoryOperand(Operand operand)
{
Debug.Assert(operand.Kind == OperandKind.Memory);

_data = (Data*)Unsafe.As<Operand, IntPtr>(ref operand);
_data = (Data*)Unsafe.As<Operand, nint>(ref operand);
}

public Operand BaseAddress
Expand Down
2 changes: 1 addition & 1 deletion src/ARMeilleure/IntermediateRepresentation/Operation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public readonly override bool Equals(object obj)

public readonly override int GetHashCode()
{
return HashCode.Combine((IntPtr)_data);
return HashCode.Combine((nint)_data);
}

public static bool operator ==(Operation a, Operation b)
Expand Down
2 changes: 1 addition & 1 deletion src/ARMeilleure/Memory/IJitMemoryBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace ARMeilleure.Memory
{
public interface IJitMemoryBlock : IDisposable
{
IntPtr Pointer { get; }
nint Pointer { get; }

void Commit(ulong offset, ulong size);

Expand Down
2 changes: 1 addition & 1 deletion src/ARMeilleure/Memory/IMemoryManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public interface IMemoryManager
{
int AddressSpaceBits { get; }

IntPtr PageTablePointer { get; }
nint PageTablePointer { get; }

MemoryManagerType Type { get; }

Expand Down
2 changes: 1 addition & 1 deletion src/ARMeilleure/Memory/ReservedRegion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class ReservedRegion

public IJitMemoryBlock Block { get; }

public IntPtr Pointer => Block.Pointer;
public nint Pointer => Block.Pointer;

private readonly ulong _maxSize;
private readonly ulong _sizeGranularity;
Expand Down
2 changes: 1 addition & 1 deletion src/ARMeilleure/Native/JitSupportDarwin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ namespace ARMeilleure.Native
static partial class JitSupportDarwin
{
[LibraryImport("libarmeilleure-jitsupport", EntryPoint = "armeilleure_jit_memcpy")]
public static partial void Copy(IntPtr dst, IntPtr src, ulong n);
public static partial void Copy(nint dst, nint src, ulong n);
}
}
6 changes: 3 additions & 3 deletions src/ARMeilleure/Signal/NativeSignalHandlerGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static class NativeSignalHandlerGenerator

private const uint EXCEPTION_ACCESS_VIOLATION = 0xc0000005;

private static Operand EmitGenericRegionCheck(EmitterContext context, IntPtr signalStructPtr, Operand faultAddress, Operand isWrite, int rangeStructSize)
private static Operand EmitGenericRegionCheck(EmitterContext context, nint signalStructPtr, Operand faultAddress, Operand isWrite, int rangeStructSize)
{
Operand inRegionLocal = context.AllocateLocal(OperandType.I32);
context.Copy(inRegionLocal, Const(0));
Expand Down Expand Up @@ -155,7 +155,7 @@ private static Operand GenerateUnixWriteFlag(EmitterContext context, Operand uco
throw new PlatformNotSupportedException();
}

public static byte[] GenerateUnixSignalHandler(IntPtr signalStructPtr, int rangeStructSize)
public static byte[] GenerateUnixSignalHandler(nint signalStructPtr, int rangeStructSize)
{
EmitterContext context = new();

Expand Down Expand Up @@ -203,7 +203,7 @@ public static byte[] GenerateUnixSignalHandler(IntPtr signalStructPtr, int range
return Compiler.Compile(cfg, argTypes, OperandType.None, CompilerOptions.HighCq, RuntimeInformation.ProcessArchitecture).Code;
}

public static byte[] GenerateWindowsSignalHandler(IntPtr signalStructPtr, int rangeStructSize)
public static byte[] GenerateWindowsSignalHandler(nint signalStructPtr, int rangeStructSize)
{
EmitterContext context = new();

Expand Down
4 changes: 2 additions & 2 deletions src/ARMeilleure/Signal/TestMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static class TestMethods
{
public delegate bool DebugPartialUnmap();
public delegate int DebugThreadLocalMapGetOrReserve(int threadId, int initialState);
public delegate void DebugNativeWriteLoop(IntPtr nativeWriteLoopPtr, IntPtr writePtr);
public delegate void DebugNativeWriteLoop(nint nativeWriteLoopPtr, nint writePtr);

public static DebugPartialUnmap GenerateDebugPartialUnmap()
{
Expand All @@ -35,7 +35,7 @@ public static DebugPartialUnmap GenerateDebugPartialUnmap()
return Compiler.Compile(cfg, argTypes, OperandType.I32, CompilerOptions.HighCq, RuntimeInformation.ProcessArchitecture).Map<DebugPartialUnmap>();
}

public static DebugThreadLocalMapGetOrReserve GenerateDebugThreadLocalMapGetOrReserve(IntPtr structPtr)
public static DebugThreadLocalMapGetOrReserve GenerateDebugThreadLocalMapGetOrReserve(nint structPtr)
{
EmitterContext context = new();

Expand Down
Loading

0 comments on commit dfb4854

Please sign in to comment.