Skip to content

Commit a48baf5

Browse files
committed
Use extended hash table lookup to properly check for key when value is 0
Fixed in .NET 3.5 memory profiler here: #893 #918
1 parent 9f3d051 commit a48baf5

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

mono/metadata/unity-memory-info.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,13 @@ typedef struct CollectMetadataContext
2222

2323
static void ContextInsertClass(CollectMetadataContext* context, MonoClass* klass)
2424
{
25-
if (klass->inited && g_hash_table_lookup(context->allTypes, klass) == NULL)
26-
g_hash_table_insert(context->allTypes, klass, (gpointer)(context->currentIndex++));
25+
gpointer orig_key, value;
26+
/* use g_hash_table_lookup_extended as it returns boolean to indicate if value was found.
27+
* If we use g_hash_table_lookup it returns the value which we were comparing to NULL. The problem is
28+
* that 0 is a valid class index and was confusing our logic.
29+
*/
30+
if (klass->inited && !g_hash_table_lookup_extended(context->allTypes, klass, &orig_key, &value))
31+
g_hash_table_insert(context->allTypes, klass, GINT_TO_POINTER(context->currentIndex++));
2732
}
2833

2934
static void CollectHashMapClass(gpointer key, gpointer value, gpointer user_data)
@@ -104,18 +109,18 @@ static void CollectImageMetaData(MonoImage* image, gpointer value, CollectMetada
104109

105110
static int FindClassIndex(GHashTable* hashTable, MonoClass* klass)
106111
{
107-
gpointer value = g_hash_table_lookup(hashTable, klass);
112+
gpointer orig_key, value;
108113

109-
if (!value)
114+
if (!g_hash_table_lookup_extended(hashTable, klass, &orig_key, &value))
110115
return -1;
111116

112-
return (int)value;
117+
return GPOINTER_TO_INT(value);
113118
}
114119

115120
static void AddMetadataType(gpointer key, gpointer value, gpointer user_data)
116121
{
117122
MonoClass* klass = (MonoClass*)key;
118-
int index = (int)value;
123+
int index = GPOINTER_TO_INT(value);
119124
CollectMetadataContext* context = (CollectMetadataContext*)user_data;
120125
MonoMetadataSnapshot* metadata = context->metadata;
121126
MonoMetadataType* type = &metadata->types[index];
@@ -562,7 +567,7 @@ static void VerifySnapshot(MonoManagedMemorySnapshot* snapshot, GHashTable* mono
562567
while (g_hash_table_iter_next(&iter, &key, NULL))
563568
{
564569
MonoImage* image = (MonoImage*)key;
565-
fprintf(file, "MonoImage [0x%016llX] dynamic: %i name: '%s'\n", (void*)image, image->dynamic, image->name);
570+
fprintf(file, "MonoImage [0x%016llX] dynamic: %i name: '%s'\n", (uint64_t)image, image->dynamic, image->name);
566571
}
567572

568573
/* Verify that we have collected memory sections for all types */

0 commit comments

Comments
 (0)