Add Roslyn analyzer and code fix for unique ownership
Some checks failed
Publish NuGet Packages / publish (pull_request) Has been cancelled

Introduce a Roslyn analyzer to enforce unique ownership semantics for structs marked with the `[NonCopyable]` attribute. Added a corresponding code fix to resolve violations by suggesting the use of `Share()` or other ownership transfer methods.

Key changes:
- Added `StructCopyCodeAnalyzer` to detect invalid struct copies.
- Implemented `StructCopyCodeFixProvider` to provide code fixes.
- Created `Misaki.HighPerformance.Analyzer` and `CodeFixes` projects.
- Added unit tests for the analyzer and code fixes.
- Introduced `UniquePtr<T>` and `SharedPtr<T>` for pointer ownership.
- Added a Visual Studio extension project and packaging support.
- Updated `UnsafeUtility` to use `nint`/`nuint` for indices.
This commit is contained in:
2025-11-22 18:20:03 +09:00
parent c0a0861897
commit 27dfa67784
34 changed files with 2122 additions and 26 deletions

View File

@@ -0,0 +1,6 @@
namespace Misaki.HighPerformance.LowLevel;
[AttributeUsage(AttributeTargets.Struct)]
public class NonCopyableAttribute : Attribute
{
}

View File

@@ -0,0 +1,176 @@
using System.Runtime.CompilerServices;
namespace Misaki.HighPerformance.LowLevel;
/// <summary>
/// Represents a strongly-typed, read-only pointer to an unmanaged value of type <typeparamref name="T"/>.
/// </summary>
/// <remarks>
/// When a pointer is wrapped in this struct, it indicates that the code does not intend to manage the lifetime of the data being pointed to.
/// </remarks>
/// <typeparam name="T">The unmanaged type to which the pointer refers.</typeparam>
public readonly unsafe struct SharedPtr<T> : IEquatable<SharedPtr<T>>
where T : unmanaged
{
private readonly T* _value;
public SharedPtr(T* value)
{
_value = value;
}
public T* Get()
{
return _value;
}
public bool Equals(SharedPtr<T> other)
{
return _value == other._value;
}
public override bool Equals(object? obj)
{
return obj is SharedPtr<T> ptr && Equals(ptr);
}
public override int GetHashCode()
{
return ((nint)_value).GetHashCode();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator T*(SharedPtr<T> ptr)
{
return ptr._value;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator SharedPtr<T>(T* value)
{
return new SharedPtr<T>(value);
}
public static bool operator ==(SharedPtr<T> left, SharedPtr<T> right)
{
return left.Equals(right);
}
public static bool operator !=(SharedPtr<T> left, SharedPtr<T> right)
{
return !(left == right);
}
}
/// <summary>
/// Provides exclusive ownership and management of an unmanaged pointer to a value of type <typeparamref name="T"/>.
/// Ensures that the pointer is not shared and can be safely transferred or detached.
/// </summary>
/// <remarks>
/// UniquePtr<T> is designed to encapsulate a raw pointer, enforcing unique ownership semantics similar to C++'s std::unique_ptr.
/// </remarks>
/// <typeparam name="T">The unmanaged type of the value to which the pointer refers.</typeparam>
[NonCopyable]
public unsafe struct UniquePtr<T> : IEquatable<UniquePtr<T>>
where T : unmanaged
{
private T* _value;
public UniquePtr(T* value)
{
_value = value;
}
public readonly T* Get()
{
return _value;
}
public readonly SharedPtr<T> Share()
{
return new SharedPtr<T>(_value);
}
public T* Detach()
{
var temp = _value;
_value = null;
return temp;
}
public readonly bool Equals(UniquePtr<T> other)
{
return _value == other._value;
}
public override readonly bool Equals(object? obj)
{
return obj is SharedPtr<T> ptr && Equals(ptr);
}
public override readonly int GetHashCode()
{
return ((nint)_value).GetHashCode();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator T*(UniquePtr<T> ptr)
{
return ptr._value;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator UniquePtr<T>(T* value)
{
return new UniquePtr<T>(value);
}
public static bool operator ==(UniquePtr<T> left, UniquePtr<T> right)
{
return left.Equals(right);
}
public static bool operator !=(UniquePtr<T> left, UniquePtr<T> right)
{
return !(left == right);
}
}
public ref struct Ref<T>
{
private ref T _value;
public Ref(ref T value)
{
_value = ref value;
}
public ref T Get()
{
return ref _value;
}
[Obsolete("Equals() on Ref will always throw an exception. Use the equality operator instead.")]
#pragma warning disable CS0809 // Obsolete member overrides non-obsolete member
public override bool Equals(object? obj)
{
throw new NotSupportedException();
}
[Obsolete("GetHashCode() on Ref will always throw an exception.")]
public override int GetHashCode()
#pragma warning restore CS0809 // Obsolete member overrides non-obsolete member
{
throw new NotSupportedException();
}
public static bool operator ==(Ref<T> left, Ref<T> right)
{
return Unsafe.AreSame(ref left._value, ref right._value);
}
public static bool operator !=(Ref<T> left, Ref<T> right)
{
return !(left == right);
}
}

View File

@@ -1,4 +1,4 @@
using Misaki.HighPerformance.LowLevel.Collections;
using Misaki.HighPerformance.LowLevel.Collections;
using System.Runtime.CompilerServices;
namespace Misaki.HighPerformance.LowLevel.Utilities;
@@ -39,7 +39,7 @@ public static unsafe class UnsafeUtility
/// <param name="index">Indicates the position of the element to be accessed within the array.</param>
/// <returns>Returns a pointer to the element located at the specified index.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T* ReadArrayElementUnsafe<T>(void* ptr, int index)
public static T* ReadArrayElementUnsafe<T>(void* ptr, nint index)
where T : unmanaged
{
return (T*)((byte*)ptr + index * sizeof(T));
@@ -53,10 +53,10 @@ public static unsafe class UnsafeUtility
/// <param name="index">Indicates the position of the element to be accessed within the array.</param>
/// <returns>Returns a pointer to the element located at the specified index.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T* ReadArrayElementUnsafe<T>(void* ptr, uint index)
public static T* ReadArrayElementUnsafe<T>(void* ptr, nuint index)
where T : unmanaged
{
return (T*)((byte*)ptr + index * sizeof(T));
return (T*)((byte*)ptr + index * (nuint)sizeof(T));
}
/// <summary>
@@ -67,7 +67,7 @@ public static unsafe class UnsafeUtility
/// <param name="index">Indicates the position of the element to be accessed in the array.</param>
/// <returns>A reference to the specified element in the unmanaged array.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ref T ReadArrayElementRef<T>(void* ptr, int index)
public static ref T ReadArrayElementRef<T>(void* ptr, nint index)
where T : unmanaged
{
return ref AsRef<T>(ReadArrayElementUnsafe<T>(ptr, index));
@@ -81,7 +81,7 @@ public static unsafe class UnsafeUtility
/// <param name="index">Indicates the position of the element to be accessed in the array.</param>
/// <returns>A reference to the specified element in the unmanaged array.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ref T ReadArrayElementRef<T>(void* ptr, uint index)
public static ref T ReadArrayElementRef<T>(void* ptr, nuint index)
where T : unmanaged
{
return ref AsRef<T>(ReadArrayElementUnsafe<T>(ptr, index));
@@ -95,7 +95,7 @@ public static unsafe class UnsafeUtility
/// <param name="index">Indicates the position of the element to be accessed within the array.</param>
/// <returns>The element located at the specified index in the array.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T ReadArrayElement<T>(void* ptr, int index)
public static T ReadArrayElement<T>(void* ptr, nint index)
where T : unmanaged
{
return *ReadArrayElementUnsafe<T>(ptr, index);
@@ -109,7 +109,7 @@ public static unsafe class UnsafeUtility
/// <param name="index">Indicates the position of the element to be accessed within the array.</param>
/// <returns>The element located at the specified index in the array.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T ReadArrayElement<T>(void* ptr, uint index)
public static T ReadArrayElement<T>(void* ptr, nuint index)
where T : unmanaged
{
return *ReadArrayElementUnsafe<T>(ptr, index);
@@ -123,7 +123,7 @@ public static unsafe class UnsafeUtility
/// <param name="index">Indicates the position in the array where the value should be stored.</param>
/// <param name="value">Represents the value to be written to the specified index of the array.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void WriteArrayElement<T>(void* ptr, int index, T value)
public static void WriteArrayElement<T>(void* ptr, nint index, T value)
where T : unmanaged
{
*ReadArrayElementUnsafe<T>(ptr, index) = value;
@@ -137,26 +137,12 @@ public static unsafe class UnsafeUtility
/// <param name="index">Indicates the position in the array where the value should be stored.</param>
/// <param name="value">Represents the value to be written to the specified index of the array.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void WriteArrayElement<T>(void* ptr, uint index, T value)
public static void WriteArrayElement<T>(void* ptr, nuint index, T value)
where T : unmanaged
{
*ReadArrayElementUnsafe<T>(ptr, index) = value;
}
/// <summary>
/// Converts an UnsafeArray of one unmanaged type to another unmanaged type without copying the elements.
/// </summary>
/// <typeparam name="TIn">Represents the type of elements in the input array.</typeparam>
/// <typeparam name="TOut">Represents the type of elements in the output array.</typeparam>
/// <param name="array">The input array containing elements of the specified input type.</param>
/// <returns>An UnsafeArray containing elements of the specified output type.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static UnsafeArray<TOut> CastArray<TIn, TOut>(UnsafeArray<TIn> array)
where TIn : unmanaged where TOut : unmanaged
{
return new UnsafeArray<TOut>((TOut*)array.GetUnsafePtr(), array.Count * sizeof(TIn) / sizeof(TOut));
}
/// <summary>
/// Returns a pointer to the first element of the specified span. This method enables direct, unsafe access to the underlying data of the span.
/// </summary>