Skip to content

Commit 4736fdc

Browse files
authored
Rework GetHashCode() (#2157)
1 parent 7f80c44 commit 4736fdc

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

src/CLR/Core/CLR_RT_HeapBlock.cpp

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,11 +1042,14 @@ void CLR_RT_HeapBlock::Promote()
10421042

10431043
//--//
10441044

1045-
CLR_UINT32 CLR_RT_HeapBlock::GetHashCode(CLR_RT_HeapBlock *ptr, bool fRecurse, CLR_UINT32 crc = 0)
1045+
CLR_INT32 CLR_RT_HeapBlock::GetHashCode(CLR_RT_HeapBlock *ptr, bool fRecurse, CLR_INT32 crc = 0)
10461046
{
10471047
NATIVE_PROFILE_CLR_CORE();
1048+
10481049
if (!ptr)
1049-
return 0;
1050+
{
1051+
return crc;
1052+
}
10501053

10511054
switch (ptr->DataType())
10521055
{
@@ -1056,14 +1059,17 @@ CLR_UINT32 CLR_RT_HeapBlock::GetHashCode(CLR_RT_HeapBlock *ptr, bool fRecurse, C
10561059

10571060
case DATATYPE_STRING:
10581061
{
1059-
const char *szText = ptr->StringText();
1060-
1061-
crc = SUPPORT_ComputeCRC(szText, (int)hal_strlen_s(szText), crc);
1062+
const char *src = ptr->StringText();
1063+
crc = SUPPORT_ComputeCRC(src, (int)hal_strlen_s(src), crc);
10621064
}
10631065
break;
10641066

10651067
case DATATYPE_CLASS:
10661068
case DATATYPE_VALUETYPE:
1069+
{
1070+
// always starts with the pointer to the object to fully disambiguate
1071+
crc = SUPPORT_ComputeCRC(&ptr, sizeof(ptr), crc);
1072+
10671073
if (fRecurse)
10681074
{
10691075
CLR_RT_TypeDef_Instance cls;
@@ -1072,17 +1078,14 @@ CLR_UINT32 CLR_RT_HeapBlock::GetHashCode(CLR_RT_HeapBlock *ptr, bool fRecurse, C
10721078

10731079
if (totFields > 0)
10741080
{
1075-
while (totFields-- > 0)
1081+
do
10761082
{
1077-
crc = GetHashCode(++ptr, false, crc);
1078-
}
1079-
}
1080-
else
1081-
{
1082-
crc = SUPPORT_ComputeCRC(&ptr, sizeof(ptr), crc);
1083+
crc = GetHashCode(&ptr[totFields + CLR_RT_HeapBlock::HB_Object_Fields_Offset], false, crc);
1084+
} while (--totFields > 0);
10831085
}
10841086
}
1085-
break;
1087+
}
1088+
break;
10861089

10871090
case DATATYPE_DELEGATE_HEAD:
10881091
{
@@ -1105,7 +1108,8 @@ CLR_UINT32 CLR_RT_HeapBlock::GetHashCode(CLR_RT_HeapBlock *ptr, bool fRecurse, C
11051108
break;
11061109

11071110
default:
1108-
crc = SUPPORT_ComputeCRC(&ptr->DataByRefConst(), ptr->GetAtomicDataUsedBytes(), crc);
1111+
crc = SUPPORT_ComputeCRC((const void *)&ptr->DataByRefConst(), ptr->GetAtomicDataUsedBytes(), crc);
1112+
11091113
break;
11101114
}
11111115

src/CLR/Include/nanoCLR_Runtime__HeapBlock.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1307,7 +1307,7 @@ struct CLR_RT_HeapBlock
13071307
bool IsZero() const;
13081308
void Promote();
13091309

1310-
static CLR_UINT32 GetHashCode(CLR_RT_HeapBlock *ptr, bool fRecurse, CLR_UINT32 crc);
1310+
static CLR_INT32 GetHashCode(CLR_RT_HeapBlock *ptr, bool fRecurse, CLR_INT32 crc);
13111311
static bool ObjectsEqual(const CLR_RT_HeapBlock &left, const CLR_RT_HeapBlock &right, bool fSameReference);
13121312

13131313
static CLR_INT32 Compare_Values(const CLR_RT_HeapBlock &left, const CLR_RT_HeapBlock &right, bool fSigned);

0 commit comments

Comments
 (0)