@@ -488,7 +488,15 @@ mono_interp_get_imethod (MonoMethod *method)
488488
489489 sig = mono_method_signature_internal (method );
490490
491- imethod = (InterpMethod * )m_method_alloc0 (method , sizeof (InterpMethod ));
491+ MonoMemPool * dyn_mp = NULL ;
492+ if (method -> dynamic )
493+ // FIXME: Use this in more places
494+ dyn_mp = mono_mempool_new_size (sizeof (InterpMethod ));
495+
496+ if (dyn_mp )
497+ imethod = (InterpMethod * )mono_mempool_alloc0 (dyn_mp , sizeof (InterpMethod ));
498+ else
499+ imethod = (InterpMethod * )m_method_alloc0 (method , sizeof (InterpMethod ));
492500 imethod -> method = method ;
493501 imethod -> param_count = sig -> param_count ;
494502 imethod -> hasthis = sig -> hasthis ;
@@ -504,15 +512,22 @@ mono_interp_get_imethod (MonoMethod *method)
504512 imethod -> rtype = m_class_get_byval_arg (mono_defaults .string_class );
505513 else
506514 imethod -> rtype = mini_get_underlying_type (sig -> ret );
507- imethod -> param_types = (MonoType * * )m_method_alloc0 (method , sizeof (MonoType * ) * sig -> param_count );
515+ if (dyn_mp )
516+ imethod -> param_types = (MonoType * * )mono_mempool_alloc0 (dyn_mp , sizeof (MonoType * ) * sig -> param_count );
517+ else
518+ imethod -> param_types = (MonoType * * )m_method_alloc0 (method , sizeof (MonoType * ) * sig -> param_count );
508519 for (i = 0 ; i < sig -> param_count ; ++ i )
509520 imethod -> param_types [i ] = mini_get_underlying_type (sig -> params [i ]);
510521
511522 jit_mm_lock (jit_mm );
512523 InterpMethod * old_imethod ;
513- if (!((old_imethod = mono_internal_hash_table_lookup (& jit_mm -> interp_code_hash , method ))))
524+ if (!((old_imethod = mono_internal_hash_table_lookup (& jit_mm -> interp_code_hash , method )))) {
514525 mono_internal_hash_table_insert (& jit_mm -> interp_code_hash , method , imethod );
515- else {
526+ if (method -> dynamic )
527+ ((MonoDynamicMethod * )method )-> mp = dyn_mp ;
528+ } else {
529+ if (dyn_mp )
530+ mono_mempool_destroy (dyn_mp );
516531 imethod = old_imethod ; /* leak the newly allocated InterpMethod to the mempool */
517532 }
518533 jit_mm_unlock (jit_mm );
@@ -1268,6 +1283,15 @@ compute_arg_offset (MonoMethodSignature *sig, int index)
12681283 return offset ;
12691284}
12701285
1286+ static gpointer
1287+ imethod_alloc0 (InterpMethod * imethod , size_t size )
1288+ {
1289+ if (imethod -> method -> dynamic )
1290+ return mono_mempool_alloc0 (((MonoDynamicMethod * )imethod -> method )-> mp , (guint )size );
1291+ else
1292+ return m_method_alloc0 (imethod -> method , (guint )size );
1293+ }
1294+
12711295static guint32 *
12721296initialize_arg_offsets (InterpMethod * imethod , MonoMethodSignature * csig )
12731297{
@@ -1280,7 +1304,7 @@ initialize_arg_offsets (InterpMethod *imethod, MonoMethodSignature *csig)
12801304 if (!sig )
12811305 sig = mono_method_signature_internal (imethod -> method );
12821306 int arg_count = sig -> hasthis + sig -> param_count ;
1283- guint32 * arg_offsets = (guint32 * ) g_malloc ( (arg_count + 1 ) * sizeof (int ));
1307+ guint32 * arg_offsets = (guint32 * )imethod_alloc0 ( imethod , (arg_count + 1 ) * sizeof (int ));
12841308 int index = 0 , offset = 0 ;
12851309
12861310 if (sig -> hasthis ) {
@@ -1302,8 +1326,8 @@ initialize_arg_offsets (InterpMethod *imethod, MonoMethodSignature *csig)
13021326 arg_offsets [index ] = ALIGN_TO (offset , MINT_STACK_SLOT_SIZE );
13031327
13041328 mono_memory_write_barrier ();
1305- if ( mono_atomic_cas_ptr (( gpointer * ) & imethod -> arg_offsets , arg_offsets , NULL ) != NULL )
1306- g_free ( arg_offsets );
1329+ /* If this fails, the new one is leaked in the mem manager */
1330+ mono_atomic_cas_ptr (( gpointer * ) & imethod -> arg_offsets , arg_offsets , NULL );
13071331 return imethod -> arg_offsets ;
13081332}
13091333
@@ -3259,8 +3283,7 @@ interp_create_method_pointer_llvmonly (MonoMethod *method, gboolean unbox, MonoE
32593283
32603284 addr = mini_llvmonly_create_ftndesc (method , entry_wrapper , entry_ftndesc );
32613285
3262- // FIXME:
3263- MonoJitMemoryManager * jit_mm = get_default_jit_mm ();
3286+ MonoJitMemoryManager * jit_mm = jit_mm_for_method (method );
32643287 jit_mm_lock (jit_mm );
32653288 if (!jit_mm -> interp_method_pointer_hash )
32663289 jit_mm -> interp_method_pointer_hash = g_hash_table_new (NULL , NULL );
@@ -3438,12 +3461,24 @@ static void
34383461interp_free_method (MonoMethod * method )
34393462{
34403463 MonoJitMemoryManager * jit_mm = jit_mm_for_method (method );
3464+ InterpMethod * imethod ;
3465+ MonoDynamicMethod * dmethod = (MonoDynamicMethod * )method ;
34413466
34423467 jit_mm_lock (jit_mm );
3443- /* InterpMethod is allocated in the domain mempool. We might haven't
3444- * allocated an InterpMethod for this instance yet */
3468+ imethod = (InterpMethod * )mono_internal_hash_table_lookup (& jit_mm -> interp_code_hash , method );
34453469 mono_internal_hash_table_remove (& jit_mm -> interp_code_hash , method );
3470+ if (imethod && jit_mm -> interp_method_pointer_hash ) {
3471+ if (imethod -> jit_entry )
3472+ g_hash_table_remove (jit_mm -> interp_method_pointer_hash , imethod -> jit_entry );
3473+ if (imethod -> llvmonly_unbox_entry )
3474+ g_hash_table_remove (jit_mm -> interp_method_pointer_hash , imethod -> llvmonly_unbox_entry );
3475+ }
34463476 jit_mm_unlock (jit_mm );
3477+
3478+ if (dmethod -> mp ) {
3479+ mono_mempool_destroy (dmethod -> mp );
3480+ dmethod -> mp = NULL ;
3481+ }
34473482}
34483483
34493484#if COUNT_OPS
@@ -8007,7 +8042,7 @@ interp_run_clause_with_il_state (gpointer il_state_ptr, int clause_index, MonoOb
80078042 imethod = mono_interp_get_imethod (il_state - > method );
80088043 if (!imethod - > transformed ) {
80098044 // In case method is in process of being tiered up, make sure it is compiled
8010- mono_interp_transform_method (imethod , context , error );
8045+ mono_interp_transform_method (imethod , context , error );
80118046 mono_error_assert_ok (error );
80128047 }
80138048
0 commit comments