Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public unsafe RuntimeTypeHandle GetNextArgType()

public override int GetHashCode()
{
return ValueType.GetHashCodeOfPtr(ArgCookie);
return RuntimeHelpers.GetHashCodeOfPtr(ArgCookie);
}

// Inherited from object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ public sealed override Delegate[] GetInvocationList()
public sealed override int GetHashCode()
{
if (IsUnmanagedFunctionPtr())
return ValueType.GetHashCodeOfPtr(_methodPtr) ^ ValueType.GetHashCodeOfPtr(_methodPtrAux);
return RuntimeHelpers.GetHashCodeOfPtr(_methodPtr) ^ RuntimeHelpers.GetHashCodeOfPtr(_methodPtrAux);

if (_invocationCount != (IntPtr)0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ internal readonly struct MetadataImport

public override int GetHashCode()
{
return ValueType.GetHashCodeOfPtr(m_metadataImport2);
return RuntimeHelpers.GetHashCodeOfPtr(m_metadataImport2);
}

public override bool Equals(object? obj)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public override int GetHashCode()
{
// See RuntimeMethodInfo.Equals() below.
if (IsGenericMethod)
return ValueType.GetHashCodeOfPtr(m_handle);
return RuntimeHelpers.GetHashCodeOfPtr(m_handle);
else
return base.GetHashCode();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Runtime.Versioning;
using System.Threading;

namespace System.Runtime.CompilerServices
{
public static partial class RuntimeHelpers
{
private static int s_pointerHashSeed;

[Intrinsic]
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern void InitializeArray(Array array, RuntimeFieldHandle fldHandle);
Expand Down Expand Up @@ -397,6 +400,31 @@ public GCFrameRegistration(void* allocation, uint elemCount, bool areByRefs = tr

[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern unsafe void UnregisterForGCReporting(GCFrameRegistration* pRegistration);

internal static int GetHashCodeOfPtr(IntPtr ptr)
{
int hashCode = (int)ptr;

if (hashCode == 0)
{
return 0;
}

int seed = s_pointerHashSeed;

// Initialize s_pointerHashSeed lazily
if (seed == 0)
{
// We use the first non-0 pointer as the seed, all hashcodes will be based off that.
// This is to make sure that we only reveal relative memory addresses and never absolute ones.
seed = hashCode;
Interlocked.CompareExchange(ref s_pointerHashSeed, seed, 0);
seed = s_pointerHashSeed;
}

Debug.Assert(s_pointerHashSeed != 0);
return hashCode - seed;
}
}
// Helper class to assist with unsafe pinning of arbitrary objects.
// It's used by VM code.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,7 @@ public void GetObjectData(SerializationInfo info, StreamingContext context)

public override int GetHashCode()
{
return ValueType.GetHashCodeOfPtr(Value);
return RuntimeHelpers.GetHashCodeOfPtr(Value);
}

public override bool Equals(object? obj)
Expand Down Expand Up @@ -1226,7 +1226,7 @@ internal bool IsNullHandle()

public override int GetHashCode()
{
return ValueType.GetHashCodeOfPtr(Value);
return RuntimeHelpers.GetHashCodeOfPtr(Value);
}

public override bool Equals(object? obj)
Expand Down
29 changes: 0 additions & 29 deletions src/coreclr/System.Private.CoreLib/src/System/ValueType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@
**
===========================================================*/

using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading;

namespace System
{
Expand Down Expand Up @@ -84,33 +82,6 @@ ref RuntimeHelpers.GetRawData(obj),
[MethodImpl(MethodImplOptions.InternalCall)]
public extern override int GetHashCode();

private static int s_seed;

internal static int GetHashCodeOfPtr(IntPtr ptr)
{
int hashCode = (int)ptr;

if (hashCode == 0)
{
return 0;
}

int seed = s_seed;

// Initialize s_seed lazily
if (seed == 0)
{
// We use the first non-0 pointer as the seed, all hashcodes will be based off that.
// This is to make sure that we only reveal relative memory addresses and never absolute ones.
seed = hashCode;
Interlocked.CompareExchange(ref s_seed, seed, 0);
seed = s_seed;
}

Debug.Assert(s_seed != 0);
return hashCode - seed;
}

public override string? ToString()
{
return this.GetType().ToString();
Expand Down