patch-quiet now works in systems with 'gsed'
[mono.git] / mono / metadata / boehm-gc.c
index c1f38cdbc43dc0728768b4dbc73d3d53805f6592..641d10baa075a6dba13c2d96c032b0dcf1a12274 100644 (file)
@@ -23,6 +23,7 @@
 #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
 
@@ -34,6 +35,9 @@
 #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;
 
@@ -46,6 +50,8 @@ mono_gc_warning (char *msg, GC_word arg)
 void
 mono_gc_base_init (void)
 {
+       char *env;
+
        if (gc_initialized)
                return;
 
@@ -99,6 +105,8 @@ mono_gc_base_init (void)
 
                GC_stackbottom = (char*)ss.ss_sp;
        }
+#elif defined(__native_client__)
+       /* Do nothing, GC_stackbottom is set correctly in libgc */
 #else
        {
                int dummy;
@@ -123,10 +131,55 @@ mono_gc_base_init (void)
 #ifdef HAVE_GC_GCJ_MALLOC
        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)
 {
@@ -144,35 +197,87 @@ mono_gc_collect (int generation)
 #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)
 {
 }
 
+/**
+ * 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 ();
 }
 
+/**
+ * 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)
 {
@@ -255,6 +360,12 @@ mono_object_is_alive (MonoObject* o)
 #endif
 }
 
+int
+mono_gc_walk_heap (int flags, MonoGCReferences callback, void *data)
+{
+       return 1;
+}
+
 #ifdef USE_INCLUDED_LIBGC
 
 static gint64 gc_start_time;
@@ -388,6 +499,12 @@ mono_gc_make_descr_from_bitmap (gsize *bitmap, int numbits)
 #endif
 }
 
+void*
+mono_gc_make_root_descr_all_refs (int numbits)
+{
+       return NULL;
+}
+
 void*
 mono_gc_alloc_fixed (size_t size, void *descr)
 {
@@ -816,6 +933,7 @@ create_allocator (int atype, int offset)
        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);
 
@@ -847,7 +965,7 @@ mono_gc_get_managed_allocator (MonoVTable *vtable, gboolean for_box)
                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;
@@ -946,11 +1064,133 @@ mono_gc_get_write_barrier (void)
 
 #endif
 
+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);
 }
 
-#endif /* no Boehm GC */
+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 */