Skip to content

Commit

Permalink
Add UnsafeUtility.AlignmentOf
Browse files Browse the repository at this point in the history
  • Loading branch information
Exanite committed Jan 21, 2025
1 parent 49c9150 commit 29e2e13
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions CsLatest/Utilities/UnsafeUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,20 @@ public static class UnsafeUtility
{
return (byte*)Unsafe.AsPointer(ref Unsafe.AsRef(in MemoryMarshal.AsRef<byte>(utf8StringLiteral)));
}

/// <summary>
/// Gets the alignment of an unmanaged type.
/// See: https://stackoverflow.com/questions/77212211/how-do-i-find-the-alignment-of-a-struct-in-c
/// </summary>
public static int AlignmentOf<T>() where T : unmanaged
{
return (int)Marshal.OffsetOf<AlignmentHelper<T>>(nameof(AlignmentHelper<T>.Target));
}

internal struct AlignmentHelper<T> where T : unmanaged
{
public byte Padding;
public T Target;
}
}
#endif

0 comments on commit 29e2e13

Please sign in to comment.