# Mono Internals

This section documents some of the internal APIs used inside Mono that developers extending or altering Mono might want to use.

## Marshalling functions

mono_marshal_alloc
Prototype: mono_marshal_alloc

mono_marshal_asany
Prototype: mono_marshal_asany

mono_marshal_free_array
Prototype: mono_marshal_free_array

mono_marshal_free_asany
Prototype: mono_marshal_free_asany

mono_marshal_free
Prototype: mono_marshal_free

mono_marshal_get_castclass
Prototype: mono_marshal_get_castclass

mono_marshal_get_delegate_begin_invoke
Prototype: mono_marshal_get_delegate_begin_invoke

mono_marshal_get_delegate_end_invoke
Prototype: mono_marshal_get_delegate_end_invoke

mono_marshal_get_delegate_invoke
Prototype: mono_marshal_get_delegate_invoke

mono_marshal_get_icall_wrapper
Prototype: mono_marshal_get_icall_wrapper

mono_marshal_get_isinst
Prototype: mono_marshal_get_isinst

mono_marshal_get_ldfld_remote_wrapper
Prototype: mono_marshal_get_ldfld_remote_wrapper

mono_marshal_get_ldfld_wrapper
Prototype: mono_marshal_get_ldfld_wrapper

mono_marshal_get_managed_wrapper
Prototype: mono_marshal_get_managed_wrapper

mono_marshal_get_native_wrapper
MonoMethod* mono_marshal_get_native_wrapper (MonoMethod *method, gboolean check_exceptions, gboolean aot)

Parameters

> method:
>   The MonoMethod to wrap.
> check_exceptions:
>   Whenever to check for pending exceptions

Remarks

generates IL code for the pinvoke wrapper (the generated method calls the unmanaged code in piinfo->addr) The wrapper info for the wrapper is a WrapperInfo structure.

mono_marshal_get_proxy_cancast
Prototype: mono_marshal_get_proxy_cancast

mono_marshal_get_ptr_to_struct
MonoMethod* mono_marshal_get_ptr_to_struct (MonoClass *klass)

Parameters

> klass:

Remarks

generates IL code for PtrToStructure (IntPtr src, object structure) The wrapper info for the wrapper is a WrapperInfo structure.

mono_marshal_get_remoting_invoke_for_target
Prototype: mono_marshal_get_remoting_invoke_for_target

mono_marshal_get_remoting_invoke
Prototype: mono_marshal_get_remoting_invoke

mono_marshal_get_remoting_invoke_with_check
Prototype: mono_marshal_get_remoting_invoke_with_check

mono_marshal_get_runtime_invoke
Prototype: mono_marshal_get_runtime_invoke

mono_marshal_get_stelemref
Prototype: mono_marshal_get_stelemref

mono_marshal_get_stfld_remote_wrapper
Prototype: mono_marshal_get_stfld_remote_wrapper

mono_marshal_get_stfld_wrapper
Prototype: mono_marshal_get_stfld_wrapper

mono_marshal_get_struct_to_ptr
MonoMethod* mono_marshal_get_struct_to_ptr (MonoClass *klass)

Parameters

> klass:

Remarks

generates IL code for StructureToPtr (object structure, IntPtr ptr, bool fDeleteOld) The wrapper info for the wrapper is a WrapperInfo structure.

mono_marshal_get_synchronized_wrapper
Prototype: mono_marshal_get_synchronized_wrapper

mono_marshal_get_unbox_wrapper
Prototype: mono_marshal_get_unbox_wrapper

mono_marshal_get_xappdomain_invoke
Prototype: mono_marshal_get_xappdomain_invoke

mono_marshal_load_type_info
MonoMarshalType* mono_marshal_load_type_info (MonoClass* klass)

Remarks

Initialize klass->marshal_info using information from metadata. This function can recursively call itself, and the caller is responsible to avoid that by calling mono_marshal_is_loading_type_info () beforehand. LOCKING: Acquires the loader lock.

mono_marshal_method_from_wrapper
Prototype: mono_marshal_method_from_wrapper

mono_marshal_realloc
Prototype: mono_marshal_realloc

mono_marshal_set_last_error
void mono_marshal_set_last_error (void)

Remarks

This function is invoked to set the last error value from a P/Invoke call which has SetLastError set.

mono_marshal_type_size
Prototype: mono_marshal_type_size

## Metadata Loading Errors

The routines in this section are used to cope with errors during metadata loading. Errors in metadata handling can happen for many reason, and these include (this is not an exhaustive list).

- An assembly referenced is missing.
- Fields referenced are missing.
- Methods referenced are missing.

The `mono_loader_set_*` routines are invoked during metadata loading to flag that an error has happened. The class loading errros are flagged in a per-thread basis.

In various spots in the runtime the `mono_loader_get_last_error` routine is called to check if there was a problem, and then errors are propagated upwards on the stack until we reach a point where an exception can be raised and no runtime locks are held.

The `mono_loader_error_prepare_exception` takes a `MonoLoaderError` structure (the value returned from `mono_loader_get_last_error`), turns that into an exception and clears the error condition from the current thread.

mono_loader_set_error_field_load
Prototype: mono_loader_set_error_field_load

mono_loader_set_error_method_load
Prototype: mono_loader_set_error_method_load

mono_loader_set_error_type_load
void mono_loader_set_error_type_load (const char *class_name, const char *assembly_name)

Remarks

Set the loader error for this thread.

mono_loader_get_last_error
Prototype: mono_loader_get_last_error

mono_loader_clear_error
void mono_loader_clear_error (void)

Remarks

Disposes any loader error messages on this thread

mono_loader_error_prepare_exception
MonoException* mono_loader_error_prepare_exception (MonoLoaderError *error)

Parameters

> error:
>   The MonoLoaderError to turn into an exception

Remarks

This turns a MonoLoaderError into an exception that can be thrown and resets the Mono Loader Error state during this process.

## Metadata Loader Locking: Internals

The locking functions here are used by code in class.c and metadata.c to lock access to the shared hashtables inside the MonoImage.

mono_loader_lock
void mono_loader_lock (void)

Remarks

See docs/thread-safety.txt for the locking strategy.

mono_loader_unlock
Prototype: mono_loader_unlock

## Garbage Collector Internal Interface

The internal interface of the Mono GC is the interface used between the runtime engine and the garbage collector.

mono_gc_disable
Prototype: mono_gc_disable

mono_gc_enable
Prototype: mono_gc_enable

mono_gc_start_world
Prototype: mono_gc_start_world

mono_gc_stop_world
Prototype: mono_gc_stop_world

mono_gc_alloc_fixed
Prototype: mono_gc_alloc_fixed

mono_gc_free_fixed
Prototype: mono_gc_free_fixed

mono_gc_make_descr_from_bitmap
Prototype: mono_gc_make_descr_from_bitmap

mono_gc_base_init
Prototype: mono_gc_base_init

mono_gc_invoke_finalizers
Prototype: mono_gc_invoke_finalizers

mono_gc_is_gc_thread
Prototype: mono_gc_is_gc_thread

mono_gc_pending_finalizers
Prototype: mono_gc_pending_finalizers

mono_gc_register_thread
Prototype: mono_gc_register_thread