Skip to content

Commit c41badc

Browse files
committed
Implement mono_unity_capture_memory_snapshot and mono_unity_free_captured_memory_snapshot.
Based upon the implementation in Unity .NET 3.5 Mono: https://github.com/Unity-Technologies/mono/blob/unity-trunk/unity/unity_memory_info.c
1 parent d37a5bf commit c41badc

File tree

7 files changed

+531
-0
lines changed

7 files changed

+531
-0
lines changed

mono/metadata/boehm-gc.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2058,6 +2058,30 @@ mono_gc_register_obj_with_weak_fields (void *obj)
20582058
g_error ("Weak fields not supported by boehm gc");
20592059
}
20602060

2061+
void
2062+
mono_gc_strong_handle_foreach(GFunc func, gpointer user_data)
2063+
{
2064+
int gcHandleTypeIndex;
2065+
uint32_t i;
2066+
2067+
lock_handles(handles);
2068+
2069+
for (gcHandleTypeIndex = HANDLE_NORMAL; gcHandleTypeIndex <= HANDLE_PINNED; gcHandleTypeIndex++)
2070+
{
2071+
HandleData* handles = &gc_handles[gcHandleTypeIndex];
2072+
2073+
for (i = 0; i < handles->size; i++)
2074+
{
2075+
if (!slot_occupied(handles, i))
2076+
continue;
2077+
if (handles->entries[i] != NULL)
2078+
func(handles->entries[i], user_data);
2079+
}
2080+
}
2081+
2082+
unlock_handles(handles);
2083+
}
2084+
20612085
#else
20622086

20632087
MONO_EMPTY_SOURCE_FILE (boehm_gc);

mono/metadata/gc-internals.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ MonoGCDescriptor mono_gc_make_descr_for_string (gsize *bitmap, int numbits);
140140

141141
void mono_gc_register_obj_with_weak_fields (void *obj);
142142

143+
void mono_gc_strong_handle_foreach(GFunc func, gpointer user_data);
144+
143145
void mono_gc_register_for_finalization (MonoObject *obj, void *user_data);
144146
void mono_gc_add_memory_pressure (gint64 value);
145147
MONO_API int mono_gc_register_root (char *start, size_t size, MonoGCDescriptor descr, MonoGCRootSource source, void *key, const char *msg);

mono/metadata/mempool.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,3 +434,21 @@ mono_mempool_get_bytes_allocated (void)
434434
{
435435
return UnlockedRead64 (&total_bytes_allocated);
436436
}
437+
438+
void
439+
mono_mempool_foreach_block(MonoMemPool* pool, mono_mempool_block_proc callback, void* user_data)
440+
{
441+
MonoMemPool *current = pool;
442+
443+
while (current)
444+
{
445+
gpointer start = (guint8*)current + SIZEOF_MEM_POOL;
446+
gpointer end = (guint8*)start + current->size;
447+
448+
callback(start, end, user_data);
449+
current = current->next;
450+
}
451+
}
452+
453+
454+

mono/metadata/mempool.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ mono_mempool_strdup (MonoMemPool *pool, const char *s);
4141
MONO_API uint32_t
4242
mono_mempool_get_allocated (MonoMemPool *pool);
4343

44+
typedef void(*mono_mempool_block_proc)(void* start, void* end, void* user_data);
45+
46+
MONO_API void
47+
mono_mempool_foreach_block(MonoMemPool* pool, mono_mempool_block_proc callback, void* user_data);
48+
4449
MONO_END_DECLS
4550

4651
#endif

mono/metadata/metadata.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5080,6 +5080,44 @@ mono_metadata_generic_class_is_valuetype (MonoGenericClass *gclass)
50805080
return gclass->container_class->valuetype;
50815081
}
50825082

5083+
typedef struct
5084+
{
5085+
GFunc func;
5086+
gpointer user_data;
5087+
} GenericClassForeachData;
5088+
5089+
5090+
static void
5091+
generic_class_foreach_callback(gpointer key, gpointer value, gpointer user_data)
5092+
{
5093+
GenericClassForeachData* data = (GenericClassForeachData*)user_data;
5094+
data->func(key, data->user_data);
5095+
}
5096+
5097+
void
5098+
mono_metadata_generic_class_foreach(GFunc func, gpointer user_data)
5099+
{
5100+
GenericClassForeachData data;
5101+
guint i;
5102+
5103+
data.func = func;
5104+
data.user_data = user_data;
5105+
5106+
for(i = 0; i < HASH_TABLE_SIZE; ++i)
5107+
{
5108+
MonoImageSet* imageSet = img_set_cache[i];
5109+
5110+
if (imageSet == NULL || imageSet->gclass_cache == NULL)
5111+
continue;
5112+
5113+
mono_image_set_lock(imageSet);
5114+
5115+
mono_conc_hashtable_foreach(imageSet->gclass_cache, generic_class_foreach_callback, &data);
5116+
5117+
mono_image_set_unlock(imageSet);
5118+
}
5119+
}
5120+
50835121
static gboolean
50845122
_mono_metadata_generic_class_equal (const MonoGenericClass *g1, const MonoGenericClass *g2, gboolean signature_only)
50855123
{

mono/metadata/metadata.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <mono/metadata/blob.h>
1111
#include <mono/metadata/row-indexes.h>
1212
#include <mono/metadata/image.h>
13+
#include <glib.h>
1314

1415
MONO_BEGIN_DECLS
1516

@@ -423,6 +424,8 @@ MONO_API int mono_type_stack_size (MonoType *type,
423424

424425
MONO_API mono_bool mono_type_generic_inst_is_valuetype (MonoType *type);
425426
MONO_API mono_bool mono_metadata_generic_class_is_valuetype (MonoGenericClass *gclass);
427+
MONO_API void mono_metadata_generic_class_foreach(GFunc func, gpointer user_data);
428+
426429

427430
MONO_API unsigned int mono_metadata_type_hash (MonoType *t1);
428431
MONO_API mono_bool mono_metadata_type_equal (MonoType *t1, MonoType *t2);

0 commit comments

Comments
 (0)