Skip to content

Commit

Permalink
Added SafeAllocatedMemoryHandleBase.GetContentHashCode to get a fast …
Browse files Browse the repository at this point in the history
…hash of all allocated bytes.
  • Loading branch information
David Hall committed Dec 6, 2024
1 parent d06d7ba commit e2cc491
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion Core/InteropServices/SafeMemoryHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,11 @@ public virtual IntPtr ReAllocMem(IntPtr hMem, int size)
/// <summary>
/// Abstract base class for all SafeHandle derivatives that encapsulate handling unmanaged memory. This class assumes read-only memory.
/// </summary>
/// <seealso cref="SafeHandle"/>
/// <seealso cref="System.Runtime.InteropServices.SafeHandle" />
/// <seealso cref="System.IComparable{T}" />
/// <seealso cref="System.IComparable{T}" />
/// <seealso cref="System.IEquatable{T}" />
/// <seealso cref="SafeHandle" />
public abstract class SafeAllocatedMemoryHandleBase : SafeHandle, IComparable<SafeAllocatedMemoryHandleBase>, IComparable<IReadOnlyList<byte>>,
IEquatable<SafeAllocatedMemoryHandleBase>
{
Expand Down Expand Up @@ -381,6 +385,20 @@ public virtual int CompareTo(IReadOnlyList<byte>? other)
_ => throw new ArgumentException("Unable to compare type.", nameof(obj)),
};

/// <summary>Gets a hash code value for all bytes within the allocated memory.</summary>
/// <returns>A hash code.</returns>
public virtual int GetContentHashCode()
{
unsafe
{
byte* p = (byte*)handle;
int result = 0;
for (int i = 0; i < Size; i++)
result = (result * 31) ^ p[i];
return result;
}
}

/// <inheritdoc/>
public override int GetHashCode() => handle.GetHashCode();

Expand Down

0 comments on commit e2cc491

Please sign in to comment.