/*
* boehm-gc.c: GC implementation using either the installed or included Boehm GC.
*
+ * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
+ * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
*/
#include "config.h"
+
+#include <string.h>
+
#define GC_I_HIDE_POINTERS
-#include <mono/os/gc_wrapper.h>
+#include <mono/metadata/gc-internal.h>
#include <mono/metadata/mono-gc.h>
#include <mono/metadata/gc-internal.h>
#include <mono/metadata/profiler-private.h>
#include <mono/metadata/class-internals.h>
#include <mono/metadata/method-builder.h>
#include <mono/metadata/opcodes.h>
-#include <mono/utils/mono-logger.h>
+#include <mono/metadata/domain-internals.h>
+#include <mono/metadata/metadata-internals.h>
+#include <mono/metadata/marshal.h>
+#include <mono/utils/mono-logger-internal.h>
+#include <mono/utils/mono-time.h>
+#include <mono/utils/dtrace.h>
+#include <mono/utils/gc_wrapper.h>
#if HAVE_BOEHM_GC
#endif
#define GC_NO_DESCRIPTOR ((gpointer)(0 | GC_DS_LENGTH))
+/*Boehm max heap cannot be smaller than 16MB*/
+#define MIN_BOEHM_MAX_HEAP_SIZE_IN_MB 16
+#define MIN_BOEHM_MAX_HEAP_SIZE (MIN_BOEHM_MAX_HEAP_SIZE_IN_MB << 20)
static gboolean gc_initialized = FALSE;
void
mono_gc_base_init (void)
{
+ char *env;
+
if (gc_initialized)
return;
}
#elif defined(HAVE_PTHREAD_GET_STACKSIZE_NP) && defined(HAVE_PTHREAD_GET_STACKADDR_NP)
GC_stackbottom = (char*)pthread_get_stackaddr_np (pthread_self ());
+#elif defined(__OpenBSD__)
+# include <pthread_np.h>
+ {
+ stack_t ss;
+ int rslt;
+
+ rslt = pthread_stackseg_np(pthread_self(), &ss);
+ g_assert (rslt == 0);
+
+ GC_stackbottom = (char*)ss.ss_sp;
+ }
+#elif defined(__native_client__)
+ /* Do nothing, GC_stackbottom is set correctly in libgc */
#else
{
int dummy;
}
#endif
- GC_init ();
+#if !defined(PLATFORM_ANDROID)
+ /* If GC_no_dls is set to true, GC_find_limit is not called. This causes a seg fault on Android. */
GC_no_dls = TRUE;
+#endif
+ GC_init ();
GC_oom_fn = mono_gc_out_of_memory;
GC_set_warn_proc (mono_gc_warning);
GC_finalize_on_demand = 1;
GC_init_gcj_malloc (5, NULL);
#endif
+#ifdef HAVE_GC_ALLOW_REGISTER_THREADS
+ GC_allow_register_threads();
+#endif
+
+ if ((env = getenv ("MONO_GC_PARAMS"))) {
+ char **ptr, **opts = g_strsplit (env, ",", -1);
+ for (ptr = opts; *ptr; ++ptr) {
+ char *opt = *ptr;
+ if (g_str_has_prefix (opt, "max-heap-size=")) {
+ glong max_heap;
+
+ opt = strchr (opt, '=') + 1;
+ if (*opt && mono_gc_parse_environment_string_extract_number (opt, &max_heap)) {
+ if (max_heap < MIN_BOEHM_MAX_HEAP_SIZE) {
+ fprintf (stderr, "max-heap-size must be at least %dMb.\n", MIN_BOEHM_MAX_HEAP_SIZE_IN_MB);
+ exit (1);
+ }
+ GC_set_max_heap_size (max_heap);
+ } else {
+ fprintf (stderr, "max-heap-size must be an integer.\n");
+ exit (1);
+ }
+ continue;
+ } else {
+ fprintf (stderr, "MONO_GC_PARAMS must be a comma-delimited list of one or more of the following:\n");
+ fprintf (stderr, " max-heap-size=N (where N is an integer, possibly with a k, m or a g suffix)\n");
+ exit (1);
+ }
+ }
+ g_strfreev (opts);
+ }
+
+ mono_gc_enable_events ();
gc_initialized = TRUE;
}
+/**
+ * mono_gc_collect:
+ * @generation: GC generation identifier
+ *
+ * Perform a garbage collection for the given generation, higher numbers
+ * mean usually older objects. Collecting a high-numbered generation
+ * implies collecting also the lower-numbered generations.
+ * The maximum value for @generation can be retrieved with a call to
+ * mono_gc_max_generation(), so this function is usually called as:
+ *
+ * mono_gc_collect (mono_gc_max_generation ());
+ */
void
mono_gc_collect (int generation)
{
+ MONO_PROBE_GC_BEGIN (generation);
+
+ mono_perfcounters->gc_induced++;
GC_gcollect ();
+
+ MONO_PROBE_GC_END (generation);
+#if defined(ENABLE_DTRACE) && defined(__sun__)
+ /* This works around a dtrace -G problem on Solaris.
+ Limit its actual use to when the probe is enabled. */
+ if (MONO_PROBE_GC_END_ENABLED ())
+ sleep(0);
+#endif
}
+/**
+ * mono_gc_max_generation:
+ *
+ * Get the maximum generation number used by the current garbage
+ * collector. The value will be 0 for the Boehm collector, 1 or more
+ * for the generational collectors.
+ *
+ * Returns: the maximum generation number.
+ */
int
mono_gc_max_generation (void)
{
return 0;
}
+/**
+ * mono_gc_get_generation:
+ * @object: a managed object
+ *
+ * Get the garbage collector's generation that @object belongs to.
+ * Use this has a hint only.
+ *
+ * Returns: a garbage collector generation number
+ */
int
mono_gc_get_generation (MonoObject *object)
{
return 0;
}
+/**
+ * mono_gc_collection_count:
+ * @generation: a GC generation number
+ *
+ * Get how many times a garbage collection has been performed
+ * for the given @generation number.
+ *
+ * Returns: the number of garbage collections
+ */
int
mono_gc_collection_count (int generation)
{
return GC_gc_no;
}
+/**
+ * mono_gc_add_memory_pressure:
+ * @value: amount of bytes
+ *
+ * Adjust the garbage collector's view of how many bytes of memory
+ * are indirectly referenced by managed objects (for example unmanaged
+ * memory holding image or other binary data).
+ * This is a hint only to the garbage collector algorithm.
+ * Note that negative amounts of @value will decrease the memory
+ * pressure.
+ */
void
mono_gc_add_memory_pressure (gint64 value)
{
}
-gint64
+/**
+ * mono_gc_get_used_size:
+ *
+ * Get the approximate amount of memory used by managed objects.
+ *
+ * Returns: the amount of memory used in bytes
+ */
+int64_t
mono_gc_get_used_size (void)
{
return GC_get_heap_size () - GC_get_free_bytes ();
}
-gint64
+/**
+ * mono_gc_get_heap_size:
+ *
+ * Get the amount of memory used by the garbage collector.
+ *
+ * Returns: the size of the heap in bytes
+ */
+int64_t
mono_gc_get_heap_size (void)
{
return GC_get_heap_size ();
gboolean
mono_gc_is_gc_thread (void)
{
-#ifdef USE_INCLUDED_LIBGC
+#if GC_VERSION_MAJOR >= 7
+ return TRUE;
+#elif defined(USE_INCLUDED_LIBGC)
return GC_thread_is_registered ();
#else
return TRUE;
gboolean
mono_gc_register_thread (void *baseptr)
{
+#if GC_VERSION_MAJOR >= 7
+ struct GC_stack_base sb;
+ int res;
+
+ res = GC_get_stack_base (&sb);
+ if (res != GC_SUCCESS) {
+ sb.mem_base = baseptr;
+#ifdef __ia64__
+ /* Can't determine the register stack bounds */
+ g_error ("mono_gc_register_thread failed ().\n");
+#endif
+ }
+ res = GC_register_my_thread (&sb);
+ if ((res != GC_SUCCESS) && (res != GC_DUPLICATE)) {
+ g_warning ("GC_register_my_thread () failed.\n");
+ return FALSE;
+ }
+ return TRUE;
+#else
if (mono_gc_is_gc_thread())
return TRUE;
-#if defined(USE_INCLUDED_LIBGC) && !defined(PLATFORM_WIN32)
+#if defined(USE_INCLUDED_LIBGC) && !defined(HOST_WIN32)
return GC_thread_register_foreign (baseptr);
#else
return FALSE;
#endif
+#endif
}
gboolean
#endif
}
+int
+mono_gc_walk_heap (int flags, MonoGCReferences callback, void *data)
+{
+ return 1;
+}
+
#ifdef USE_INCLUDED_LIBGC
+static gint64 gc_start_time;
+
static void
on_gc_notification (GCEventType event)
{
+ if (event == MONO_GC_EVENT_START) {
+ mono_perfcounters->gc_collections0++;
+ mono_stats.major_gc_count ++;
+ gc_start_time = mono_100ns_ticks ();
+ } else if (event == MONO_GC_EVENT_END) {
+ guint64 heap_size = GC_get_heap_size ();
+ guint64 used_size = heap_size - GC_get_free_bytes ();
+ mono_perfcounters->gc_total_bytes = used_size;
+ mono_perfcounters->gc_committed_bytes = heap_size;
+ mono_perfcounters->gc_reserved_bytes = heap_size;
+ mono_perfcounters->gc_gen0size = heap_size;
+ mono_stats.major_gc_time_usecs += (mono_100ns_ticks () - gc_start_time) / 10;
+ mono_trace_message (MONO_TRACE_GC, "gc took %d usecs", (mono_100ns_ticks () - gc_start_time) / 10);
+ }
mono_profiler_gc_event ((MonoGCEvent) event, 0);
}
static void
on_gc_heap_resize (size_t new_size)
{
+ guint64 heap_size = GC_get_heap_size ();
+ mono_perfcounters->gc_committed_bytes = heap_size;
+ mono_perfcounters->gc_reserved_bytes = heap_size;
+ mono_perfcounters->gc_gen0size = heap_size;
mono_profiler_gc_heap_resize (new_size);
}
}
void
-mono_gc_weak_link_add (void **link_addr, MonoObject *obj)
+mono_gc_deregister_root (char* addr)
+{
+#ifndef HOST_WIN32
+ /* FIXME: libgc doesn't define this work win32 for some reason */
+ /* FIXME: No size info */
+ GC_remove_roots (addr, addr + sizeof (gpointer) + 1);
+#endif
+}
+
+void
+mono_gc_weak_link_add (void **link_addr, MonoObject *obj, gboolean track)
{
/* libgc requires that we use HIDE_POINTER... */
*link_addr = (void*)HIDE_POINTER (obj);
*link_addr = NULL;
}
+static gpointer
+reveal_link (gpointer link_addr)
+{
+ void **link_a = link_addr;
+ return REVEAL_POINTER (*link_a);
+}
+
MonoObject*
mono_gc_weak_link_get (void **link_addr)
{
- MonoObject *obj = REVEAL_POINTER (*link_addr);
+ MonoObject *obj = GC_call_with_alloc_lock (reveal_link, link_addr);
if (obj == (MonoObject *) -1)
return NULL;
return obj;
#endif
}
+void*
+mono_gc_make_root_descr_all_refs (int numbits)
+{
+ return NULL;
+}
+
void*
mono_gc_alloc_fixed (size_t size, void *descr)
{
- return GC_MALLOC (size);
+ /* To help track down typed allocation bugs */
+ /*
+ static int count;
+ count ++;
+ if (count == atoi (getenv ("COUNT2")))
+ printf ("HIT!\n");
+ if (count > atoi (getenv ("COUNT2")))
+ return GC_MALLOC (size);
+ */
+
+ if (descr)
+ return GC_MALLOC_EXPLICITLY_TYPED (size, (GC_descr)descr);
+ else
+ return GC_MALLOC (size);
}
void
return GC_should_invoke_finalizers ();
}
+/*
+ * LOCKING: Assumes the domain_finalizers lock is held.
+ */
+static void
+add_weak_track_handle_internal (MonoDomain *domain, MonoObject *obj, guint32 gchandle)
+{
+ GSList *refs;
+
+ if (!domain->track_resurrection_objects_hash)
+ domain->track_resurrection_objects_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
+
+ refs = g_hash_table_lookup (domain->track_resurrection_objects_hash, obj);
+ refs = g_slist_prepend (refs, GUINT_TO_POINTER (gchandle));
+ g_hash_table_insert (domain->track_resurrection_objects_hash, obj, refs);
+}
+
+void
+mono_gc_add_weak_track_handle (MonoObject *obj, guint32 handle)
+{
+ MonoDomain *domain;
+
+ if (!obj)
+ return;
+
+ domain = mono_object_get_domain (obj);
+
+ mono_domain_finalizers_lock (domain);
+
+ add_weak_track_handle_internal (domain, obj, handle);
+
+ g_hash_table_insert (domain->track_resurrection_handles_hash, GUINT_TO_POINTER (handle), obj);
+
+ mono_domain_finalizers_unlock (domain);
+}
+
+/*
+ * LOCKING: Assumes the domain_finalizers lock is held.
+ */
+static void
+remove_weak_track_handle_internal (MonoDomain *domain, MonoObject *obj, guint32 gchandle)
+{
+ GSList *refs;
+
+ if (!domain->track_resurrection_objects_hash)
+ return;
+
+ refs = g_hash_table_lookup (domain->track_resurrection_objects_hash, obj);
+ refs = g_slist_remove (refs, GUINT_TO_POINTER (gchandle));
+ g_hash_table_insert (domain->track_resurrection_objects_hash, obj, refs);
+}
+
+void
+mono_gc_change_weak_track_handle (MonoObject *old_obj, MonoObject *obj, guint32 gchandle)
+{
+ MonoDomain *domain = mono_domain_get ();
+
+ mono_domain_finalizers_lock (domain);
+
+ if (old_obj)
+ remove_weak_track_handle_internal (domain, old_obj, gchandle);
+ if (obj)
+ add_weak_track_handle_internal (domain, obj, gchandle);
+
+ mono_domain_finalizers_unlock (domain);
+}
+
+void
+mono_gc_remove_weak_track_handle (guint32 gchandle)
+{
+ MonoDomain *domain = mono_domain_get ();
+ MonoObject *obj;
+
+ /* Clean our entries in the two hashes in MonoDomain */
+
+ mono_domain_finalizers_lock (domain);
+
+ /* Get the original object this handle pointed to */
+ obj = g_hash_table_lookup (domain->track_resurrection_handles_hash, GUINT_TO_POINTER (gchandle));
+ if (obj) {
+ g_hash_table_remove (domain->track_resurrection_handles_hash, GUINT_TO_POINTER (gchandle));
+
+ remove_weak_track_handle_internal (domain, obj, gchandle);
+ }
+
+ mono_domain_finalizers_unlock (domain);
+}
+
+GSList*
+mono_gc_remove_weak_track_object (MonoDomain *domain, MonoObject *obj)
+{
+ GSList *refs = NULL;
+
+ if (domain->track_resurrection_objects_hash) {
+ refs = g_hash_table_lookup (domain->track_resurrection_objects_hash, obj);
+
+ if (refs)
+ /*
+ * Since we don't run finalizers again for resurrected objects,
+ * no need to keep these around.
+ */
+ g_hash_table_remove (domain->track_resurrection_objects_hash, obj);
+ }
+
+ return refs;
+}
+
void
mono_gc_wbarrier_set_field (MonoObject *obj, gpointer field_ptr, MonoObject* value)
{
}
void
-mono_gc_wbarrier_arrayref_copy (MonoArray *arr, gpointer slot_ptr, int count)
+mono_gc_wbarrier_arrayref_copy (gpointer dest_ptr, gpointer src_ptr, int count)
{
- /* no need to do anything */
+ memmove (dest_ptr, src_ptr, count * sizeof (gpointer));
}
void
*(void**)ptr = value;
}
+void
+mono_gc_wbarrier_generic_nostore (gpointer ptr)
+{
+}
+
void
mono_gc_wbarrier_value_copy (gpointer dest, gpointer src, int count, MonoClass *klass)
{
+ memmove (dest, src, count * mono_class_value_size (klass, NULL));
+}
+
+void
+mono_gc_wbarrier_object_copy (MonoObject* obj, MonoObject *src)
+{
+ /* do not copy the sync state */
+ memcpy ((char*)obj + sizeof (MonoObject), (char*)src + sizeof (MonoObject),
+ mono_object_class (obj)->instance_size - sizeof (MonoObject));
}
void
-mono_gc_wbarrier_object (MonoObject *object)
+mono_gc_clear_domain (MonoDomain *domain)
{
}
+int
+mono_gc_get_suspend_signal (void)
+{
+#ifdef USE_INCLUDED_GC
+ return GC_get_suspend_signal ();
+#else
+ return -1;
+#endif
+}
+
#if defined(USE_INCLUDED_LIBGC) && defined(USE_COMPILER_TLS) && defined(__linux__) && (defined(__i386__) || defined(__x86_64__))
extern __thread MONO_TLS_FAST void* GC_thread_tls;
#include "metadata-internals.h"
MonoMethodBuilder *mb;
MonoMethod *res;
MonoMethodSignature *csig;
+ AllocatorWrapperInfo *info;
if (atype == ATYPE_STRING) {
csig = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
res = mono_mb_create_method (mb, csig, 8);
mono_mb_free (mb);
mono_method_get_header (res)->init_locals = FALSE;
+
+ info = mono_image_alloc0 (mono_defaults.corlib, sizeof (AllocatorWrapperInfo));
+ info->gc_name = "boehm";
+ info->alloc_type = atype;
+ mono_marshal_set_wrapper_info (res, info);
+
return res;
}
return NULL;
if (!SMALL_ENOUGH (klass->instance_size))
return NULL;
- if (klass->has_finalize || klass->marshalbyref || (mono_profiler_get_events () & MONO_PROFILE_ALLOCATIONS))
+ if (mono_class_has_finalizer (klass) || klass->marshalbyref || (mono_profiler_get_events () & MONO_PROFILE_ALLOCATIONS))
return NULL;
if (klass->rank)
return NULL;
return mono_gc_get_managed_allocator_by_type (atype);
}
-/**
- * mono_gc_get_managed_allocator_id:
- *
- * Return a type for the managed allocator method MANAGED_ALLOC which can later be passed
- * to mono_gc_get_managed_allocator_by_type () to get back this allocator method. This can be
- * used by the AOT code to encode references to managed allocator methods.
- */
-int
-mono_gc_get_managed_allocator_type (MonoMethod *managed_alloc)
+MonoMethod*
+mono_gc_get_managed_array_allocator (MonoVTable *vtable, int rank)
{
- int i;
-
- mono_loader_lock ();
- for (i = 0; i < ATYPE_NUM; ++i) {
- if (alloc_method_cache [i] == managed_alloc) {
- mono_loader_unlock ();
- return i;
- }
- }
- mono_loader_unlock ();
-
- return -1;
+ return NULL;
}
/**
return res;
}
+guint32
+mono_gc_get_managed_allocator_types (void)
+{
+ return ATYPE_NUM;
+}
+
+MonoMethod*
+mono_gc_get_write_barrier (void)
+{
+ g_assert_not_reached ();
+ return NULL;
+}
+
#else
MonoMethod*
return NULL;
}
-int
-mono_gc_get_managed_allocator_type (MonoMethod *managed_alloc)
+MonoMethod*
+mono_gc_get_managed_array_allocator (MonoVTable *vtable, int rank)
{
- return -1;
+ return NULL;
}
MonoMethod*
return NULL;
}
+guint32
+mono_gc_get_managed_allocator_types (void)
+{
+ return 0;
+}
+
+MonoMethod*
+mono_gc_get_write_barrier (void)
+{
+ g_assert_not_reached ();
+ return NULL;
+}
+
#endif
-#endif /* no Boehm GC */
+const char *
+mono_gc_get_gc_name (void)
+{
+ return "boehm";
+}
+
+void*
+mono_gc_invoke_with_gc_lock (MonoGCLockedCallbackFunc func, void *data)
+{
+ return GC_call_with_alloc_lock (func, data);
+}
+char*
+mono_gc_get_description (void)
+{
+ return g_strdup (DEFAULT_GC_NAME);
+}
+
+void
+mono_gc_set_desktop_mode (void)
+{
+ GC_dont_expand = 1;
+}
+
+gboolean
+mono_gc_is_moving (void)
+{
+ return FALSE;
+}
+
+gboolean
+mono_gc_is_disabled (void)
+{
+ if (GC_dont_gc || g_getenv ("GC_DONT_GC"))
+ return TRUE;
+ else
+ return FALSE;
+}
+
+void
+mono_gc_wbarrier_value_copy_bitmap (gpointer _dest, gpointer _src, int size, unsigned bitmap)
+{
+ g_assert_not_reached ();
+}
+
+
+guint8*
+mono_gc_get_card_table (int *shift_bits, gpointer *card_mask)
+{
+ g_assert_not_reached ();
+ return NULL;
+}
+
+void*
+mono_gc_get_nursery (int *shift_bits, size_t *size)
+{
+ return NULL;
+}
+
+gboolean
+mono_gc_precise_stack_mark_enabled (void)
+{
+ return FALSE;
+}
+
+FILE *
+mono_gc_get_logfile (void)
+{
+ return NULL;
+}
+
+void
+mono_gc_conservatively_scan_area (void *start, void *end)
+{
+ g_assert_not_reached ();
+}
+
+void *
+mono_gc_scan_object (void *obj)
+{
+ g_assert_not_reached ();
+ return NULL;
+}
+
+gsize*
+mono_gc_get_bitmap_for_descr (void *descr, int *numbits)
+{
+ g_assert_not_reached ();
+ return NULL;
+}
+
+void
+mono_gc_set_gc_callbacks (MonoGCCallbacks *callbacks)
+{
+}
+
+/*
+ * These will call the redefined versions in libgc.
+ */
+
+#ifndef HOST_WIN32
+
+int
+mono_gc_pthread_create (pthread_t *new_thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)
+{
+ return pthread_create (new_thread, attr, start_routine, arg);
+}
+
+int
+mono_gc_pthread_join (pthread_t thread, void **retval)
+{
+ return pthread_join (thread, retval);
+}
+
+int
+mono_gc_pthread_detach (pthread_t thread)
+{
+ return pthread_detach (thread);
+}
+
+#endif
+
+#ifdef HOST_WIN32
+BOOL APIENTRY mono_gc_dllmain (HMODULE module_handle, DWORD reason, LPVOID reserved)
+{
+ return GC_DllMain (module_handle, reason, reserved);
+}
+#endif
+
+#endif /* no Boehm GC */