/*
* Copyright 2001-2003 Ximian, Inc
* Copyright 2003-2010 Novell, Inc.
+ * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
#include <signal.h>
#include <mono/utils/mono-compiler.h>
#include <mono/utils/mono-threads.h>
+#include <mono/io-layer/mono-mutex.h>
#include <mono/metadata/class-internals.h>
#include <mono/metadata/object-internals.h>
#include <mono/metadata/sgen-archdep.h>
#define GC_BITS_PER_WORD (sizeof (mword) * 8)
+/* The method used to clear the nursery */
+/* Clearing at nursery collections is the safest, but has bad interactions with caches.
+ * Clearing at TLAB creation is much faster, but more complex and it might expose hard
+ * to find bugs.
+ */
+typedef enum {
+ CLEAR_AT_GC,
+ CLEAR_AT_TLAB_CREATION
+} NurseryClearPolicy;
+
+NurseryClearPolicy mono_sgen_get_nursery_clear_policy (void) MONO_INTERNAL;
+
#if SIZEOF_VOID_P == 4
typedef guint32 mword;
MonoThreadInfo info;
#if defined(__MACH__)
thread_port_t mach_port;
-#endif
-
- unsigned int stop_count; /* to catch duplicate signals */
+#else
int signal;
+ unsigned int stop_count; /* to catch duplicate signals */
+#endif
int skip;
volatile int in_critical_region;
gboolean doing_handshake;
gpointer stopped_ip; /* only valid if the thread is stopped */
MonoDomain *stopped_domain; /* ditto */
-#if defined(__MACH__)
#ifdef USE_MONO_CTX
+#ifdef __MACH__
MonoContext ctx; /* ditto */
-#else
- gpointer regs[ARCH_NUM_REGS]; /* ditto */
-#endif
#endif
-
-#ifdef USE_MONO_CTX
MonoContext *monoctx; /* ditto */
+
#else
+
+#if defined(__MACH__) || defined(HOST_WIN32)
+ gpointer regs[ARCH_NUM_REGS]; /* ditto */
+#endif
gpointer *stopped_regs; /* ditto */
#endif
+
#ifndef HAVE_KW_THREAD
char *tlab_start;
char *tlab_next;
*/
#define SGEN_MAX_SMALL_OBJ_SIZE 8000
+/*
+ * This is the maximum ammount of memory we're willing to waste in order to speed up allocation.
+ * Wastage comes in thre forms:
+ *
+ * -when building the nursery fragment list, small regions are discarded;
+ * -when allocating memory from a fragment if it ends up below the threshold, we remove it from the fragment list; and
+ * -when allocating a new tlab, we discard the remaining space of the old one
+ *
+ * Increasing this value speeds up allocation but will cause more frequent nursery collections as less space will be used.
+ * Descreasing this value will cause allocation to be slower since we'll have to cycle thru more fragments.
+ * 512 annedoctally keeps wastage under control and doesn't impact allocation performance too much.
+*/
+#define SGEN_MAX_NURSERY_WASTE 512
+
+
/* This is also the MAJOR_SECTION_SIZE for the copying major
collector */
#define SGEN_PINNED_CHUNK_SIZE (128 * 1024)
typedef struct _SgenPinnedChunk SgenPinnedChunk;
-#if defined(__APPLE__) || defined(__OpenBSD__) || defined(__FreeBSD__)
-const static int suspend_signal_num = SIGXFSZ;
-#else
-const static int suspend_signal_num = SIGPWR;
-#endif
-const static int restart_signal_num = SIGXCPU;
-
/*
* Recursion is not allowed for the thread lock.
*/
-#define LOCK_DECLARE(name) pthread_mutex_t name = PTHREAD_MUTEX_INITIALIZER
+#define LOCK_DECLARE(name) mono_mutex_t name
/* if changing LOCK_INIT to something that isn't idempotent, look at
its use in mono_gc_base_init in sgen-gc.c */
-#define LOCK_INIT(name)
-#define LOCK_GC pthread_mutex_lock (&gc_mutex)
-#define TRYLOCK_GC (pthread_mutex_trylock (&gc_mutex) == 0)
-#define UNLOCK_GC pthread_mutex_unlock (&gc_mutex)
-#define LOCK_INTERRUPTION pthread_mutex_lock (&interruption_mutex)
-#define UNLOCK_INTERRUPTION pthread_mutex_unlock (&interruption_mutex)
+#define LOCK_INIT(name) mono_mutex_init (&(name), NULL)
+#define LOCK_GC mono_mutex_lock (&gc_mutex)
+#define TRYLOCK_GC (mono_mutex_trylock (&gc_mutex) == 0)
+#define UNLOCK_GC mono_mutex_unlock (&gc_mutex)
+#define LOCK_INTERRUPTION mono_mutex_lock (&interruption_mutex)
+#define UNLOCK_INTERRUPTION mono_mutex_unlock (&interruption_mutex)
#define SGEN_CAS_PTR InterlockedCompareExchangePointer
#define SGEN_ATOMIC_ADD(x,i) do { \
} while (InterlockedCompareExchange (&(x), __old_x + (i), __old_x) != __old_x); \
} while (0)
-/* non-pthread will need to provide their own version of start/stop */
-#define USE_SIGNAL_BASED_START_STOP_WORLD 1
+#ifndef HOST_WIN32
/* we intercept pthread_create calls to know which threads exist */
#define USE_PTHREAD_INTERCEPT 1
+#endif
#ifdef HEAVY_STATISTICS
#define HEAVY_STAT(x) x
extern int current_collection_generation;
+extern unsigned int mono_sgen_global_stop_count;
+
#define SGEN_ALLOC_ALIGN 8
#define SGEN_ALLOC_ALIGN_BITS 3
void *alloc_prepare_data;
};
+typedef void (*CopyOrMarkObjectFunc) (void**, SgenGrayQueue*);
+typedef void (*ScanObjectFunc) (char*, SgenGrayQueue*);
+typedef void (*ScanVTypeFunc) (char*, mword desc, SgenGrayQueue*);
+
#if SGEN_MAX_DEBUG_LEVEL >= 9
#define GRAY_OBJECT_ENQUEUE gray_object_enqueue
#define GRAY_OBJECT_DEQUEUE(queue,o) ((o) = gray_object_dequeue ((queue)))
void* mono_sgen_alloc_os_memory_aligned (mword size, mword alignment, gboolean activate) MONO_INTERNAL;
void mono_sgen_free_os_memory (void *addr, size_t size) MONO_INTERNAL;
-int mono_sgen_thread_handshake (int signum) MONO_INTERNAL;
+int mono_sgen_thread_handshake (BOOL suspend) MONO_INTERNAL;
gboolean mono_sgen_suspend_thread (SgenThreadInfo *info) MONO_INTERNAL;
gboolean mono_sgen_resume_thread (SgenThreadInfo *info) MONO_INTERNAL;
-
-
void mono_sgen_wait_for_suspend_ack (int count) MONO_INTERNAL;
+gboolean mono_sgen_park_current_thread_if_doing_handshake (SgenThreadInfo *p) MONO_INTERNAL;
+void mono_sgen_os_init (void) MONO_INTERNAL;
+
+void mono_sgen_fill_thread_info_for_suspend (SgenThreadInfo *info) MONO_INTERNAL;
-gboolean mono_sgen_is_worker_thread (pthread_t thread) MONO_INTERNAL;
+gboolean mono_sgen_is_worker_thread (MonoNativeThreadId thread) MONO_INTERNAL;
void mono_sgen_update_heap_boundaries (mword low, mword high) MONO_INTERNAL;
INTERNAL_MEM_SCAN_STARTS,
INTERNAL_MEM_FIN_TABLE,
INTERNAL_MEM_FINALIZE_ENTRY,
+ INTERNAL_MEM_FINALIZE_READY_ENTRY,
INTERNAL_MEM_DISLINK_TABLE,
INTERNAL_MEM_DISLINK,
INTERNAL_MEM_ROOTS_TABLE,
INTERNAL_MEM_ROOT_RECORD,
INTERNAL_MEM_STATISTICS,
+ INTERNAL_MEM_STAT_PINNED_CLASS,
+ INTERNAL_MEM_STAT_REMSET_CLASS,
INTERNAL_MEM_REMSET,
INTERNAL_MEM_GRAY_QUEUE,
INTERNAL_MEM_STORE_REMSET,
void mono_sgen_pin_objects_in_section (GCMemSection *section, SgenGrayQueue *queue) MONO_INTERNAL;
void mono_sgen_pin_stats_register_object (char *obj, size_t size);
+void mono_sgen_pin_stats_register_global_remset (char *obj);
+void mono_sgen_pin_stats_print_class_stats (void);
void mono_sgen_add_to_global_remset (gpointer ptr) MONO_INTERNAL;
int mono_sgen_get_current_collection_generation (void) MONO_INTERNAL;
+gboolean mono_sgen_nursery_collection_is_parallel (void) MONO_INTERNAL;
+CopyOrMarkObjectFunc mono_sgen_get_copy_object (void) MONO_INTERNAL;
+ScanObjectFunc mono_sgen_get_minor_scan_object (void) MONO_INTERNAL;
+ScanVTypeFunc mono_sgen_get_minor_scan_vtype (void) MONO_INTERNAL;
typedef void (*sgen_cardtable_block_callback) (mword start, mword size);
void* (*alloc_degraded) (MonoVTable *vtable, size_t size);
void (*copy_or_mark_object) (void **obj_slot, SgenGrayQueue *queue);
void (*minor_scan_object) (char *start, SgenGrayQueue *queue);
+ void (*nopar_minor_scan_object) (char *start, SgenGrayQueue *queue);
void (*minor_scan_vtype) (char *start, mword desc, SgenGrayQueue *queue);
+ void (*nopar_minor_scan_vtype) (char *start, mword desc, SgenGrayQueue *queue);
void (*major_scan_object) (char *start, SgenGrayQueue *queue);
void (*copy_object) (void **obj_slot, SgenGrayQueue *queue);
+ void (*nopar_copy_object) (void **obj_slot, SgenGrayQueue *queue);
void* (*alloc_object) (int size, gboolean has_references);
void (*free_pinned_object) (char *obj, size_t size);
void (*iterate_objects) (gboolean non_pinned, gboolean pinned, IterateObjectCallbackFunc callback, void *data);
int (*get_num_major_sections) (void);
gboolean (*handle_gc_param) (const char *opt);
void (*print_gc_param_usage) (void);
- gboolean (*is_worker_thread) (pthread_t thread);
+ gboolean (*is_worker_thread) (MonoNativeThreadId thread);
void (*post_param_init) (void);
void* (*alloc_worker_data) (void);
void (*init_worker_thread) (void *data);
}
}
-#define mono_sgen_safe_object_get_size(o) mono_sgen_par_object_get_size ((MonoVTable*)SGEN_LOAD_VTABLE ((o)), (o))
+static inline guint
+mono_sgen_safe_object_get_size (MonoObject *obj)
+{
+ char *forwarded;
+
+ if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj)))
+ obj = (MonoObject*)forwarded;
+
+ return mono_sgen_par_object_get_size ((MonoVTable*)SGEN_LOAD_VTABLE (obj), obj);
+}
const char* mono_sgen_safe_name (void* obj) MONO_INTERNAL;
gboolean mono_sgen_object_is_live (void *obj) MONO_INTERNAL;
gboolean mono_sgen_need_bridge_processing (void) MONO_INTERNAL;
-void mono_sgen_bridge_processing (int num_objs, MonoObject **objs) MONO_INTERNAL;
+void mono_sgen_bridge_processing_start (int num_objs, MonoObject **objs) MONO_INTERNAL;
+void mono_sgen_bridge_processing_finish (int num_objs, MonoObject **objs) MONO_INTERNAL;
void mono_sgen_register_test_bridge_callbacks (void) MONO_INTERNAL;
+gboolean mono_sgen_is_bridge_object (MonoObject *obj) MONO_INTERNAL;
+void mono_sgen_mark_bridge_object (MonoObject *obj) MONO_INTERNAL;
enum {
SPACE_MAJOR,
void mono_sgen_pin_object (void *object, SgenGrayQueue *queue) MONO_INTERNAL;
void sgen_collect_major_no_lock (const char *reason) MONO_INTERNAL;
gboolean mono_sgen_need_major_collection (mword space_needed) MONO_INTERNAL;
+void mono_sgen_set_pinned_from_failed_allocation (mword objsize) MONO_INTERNAL;
/* LOS */
extern LOSObject *los_object_list;
extern mword los_memory_usage;
-extern mword last_los_memory_usage;
void mono_sgen_los_free_object (LOSObject *obj) MONO_INTERNAL;
void* mono_sgen_los_alloc_large_inner (MonoVTable *vtable, size_t size) MONO_INTERNAL;
void mono_sgen_los_scan_card_table (SgenGrayQueue *queue) MONO_INTERNAL;
FILE *mono_sgen_get_logfile (void) MONO_INTERNAL;
+/* nursery allocator */
+
+void mono_sgen_clear_nursery_fragments (void) MONO_INTERNAL;
+void mono_sgen_nursery_allocator_prepare_for_pinning (void) MONO_INTERNAL;
+void mono_sgen_clear_current_nursery_fragment (void) MONO_INTERNAL;
+void mono_sgen_nursery_allocator_set_nursery_bounds (char *nursery_start, char *nursery_end) MONO_INTERNAL;
+mword mono_sgen_build_nursery_fragments (GCMemSection *nursery_section, void **start, int num_entries) MONO_INTERNAL;
+void mono_sgen_init_nursery_allocator (void) MONO_INTERNAL;
+void mono_sgen_nursery_allocator_init_heavy_stats (void) MONO_INTERNAL;
+char* mono_sgen_nursery_alloc_get_upper_alloc_bound (void) MONO_INTERNAL;
+void* mono_sgen_nursery_alloc (size_t size) MONO_INTERNAL;
+void* mono_sgen_nursery_alloc_range (size_t size, size_t min_size, int *out_alloc_size) MONO_INTERNAL;
+MonoVTable* mono_sgen_get_array_fill_vtable (void) MONO_INTERNAL;
+gboolean mono_sgen_can_alloc_size (size_t size) MONO_INTERNAL;
+void mono_sgen_nursery_retire_region (void *address, ptrdiff_t size) MONO_INTERNAL;
+
+/* hash tables */
+
+typedef struct _SgenHashTableEntry SgenHashTableEntry;
+struct _SgenHashTableEntry {
+ SgenHashTableEntry *next;
+ gpointer key;
+ char data [MONO_ZERO_LEN_ARRAY]; /* data is pointer-aligned */
+};
+
+typedef struct {
+ int table_mem_type;
+ int entry_mem_type;
+ size_t data_size;
+ GHashFunc hash_func;
+ GEqualFunc equal_func;
+ SgenHashTableEntry **table;
+ guint size;
+ guint num_entries;
+} SgenHashTable;
+
+#define SGEN_HASH_TABLE_INIT(table_type,entry_type,data_size,hash_func,equal_func) { (table_type), (entry_type), (data_size), (hash_func), (equal_func), NULL, 0, 0 }
+#define SGEN_HASH_TABLE_ENTRY_SIZE(data_size) ((data_size) + sizeof (SgenHashTableEntry*) + sizeof (gpointer))
+
+gpointer mono_sgen_hash_table_lookup (SgenHashTable *table, gpointer key) MONO_INTERNAL;
+gboolean mono_sgen_hash_table_replace (SgenHashTable *table, gpointer key, gpointer data) MONO_INTERNAL;
+gboolean mono_sgen_hash_table_set_value (SgenHashTable *table, gpointer key, gpointer data) MONO_INTERNAL;
+gboolean mono_sgen_hash_table_set_key (SgenHashTable *hash_table, gpointer old_key, gpointer new_key) MONO_INTERNAL;
+gboolean mono_sgen_hash_table_remove (SgenHashTable *table, gpointer key, gpointer data_return) MONO_INTERNAL;
+
+void mono_sgen_hash_table_clean (SgenHashTable *table) MONO_INTERNAL;
+
+#define mono_sgen_hash_table_num_entries(h) ((h)->num_entries)
+
+#define SGEN_HASH_TABLE_FOREACH(h,k,v) do { \
+ SgenHashTable *__hash_table = (h); \
+ SgenHashTableEntry **__table = __hash_table->table; \
+ SgenHashTableEntry *__entry, *__prev; \
+ guint __i; \
+ for (__i = 0; __i < (h)->size; ++__i) { \
+ __prev = NULL; \
+ for (__entry = __table [__i]; __entry; ) { \
+ (k) = __entry->key; \
+ (v) = (gpointer)__entry->data;
+
+/* The loop must be continue'd after using this! */
+#define SGEN_HASH_TABLE_FOREACH_REMOVE(free) do { \
+ SgenHashTableEntry *__next = __entry->next; \
+ if (__prev) \
+ __prev->next = __next; \
+ else \
+ __table [__i] = __next; \
+ if ((free)) \
+ mono_sgen_free_internal (__entry, __hash_table->entry_mem_type); \
+ __entry = __next; \
+ --__hash_table->num_entries; \
+ } while (0)
+
+#define SGEN_HASH_TABLE_FOREACH_SET_KEY(k) ((__entry)->key = (k))
+
+#define SGEN_HASH_TABLE_FOREACH_END \
+ __prev = __entry; \
+ __entry = __entry->next; \
+ } \
+ } \
+ } while (0)
+
#endif /* HAVE_SGEN_GC */
#endif /* __MONO_SGENGC_H__ */