text: /*
   * sgen-gc.c: Simple generational GC.
   *
   * Copyright 2001-2003 Ximian, Inc
   * Copyright 2003-2010 Novell, Inc.
   * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
   * Copyright (C) 2012 Xamarin Inc
   *
   * This library is free software; you can redistribute it and/or
   * modify it under the terms of the GNU Library General Public
   * License 2.0 as published by the Free Software Foundation;
   *
   * This library is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   * Library General Public License for more details.
   *
   * You should have received a copy of the GNU Library General Public
   * License 2.0 along with this library; if not, write to the Free
   * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   */
  #ifndef __MONO_SGENGC_H__
  #define __MONO_SGENGC_H__
  
  /* pthread impl */
  #include "config.h"
  
  #ifdef HAVE_SGEN_GC
  
  typedef struct _SgenThreadInfo SgenThreadInfo;
  #undef THREAD_INFO_TYPE
  #define THREAD_INFO_TYPE SgenThreadInfo
  
  #include <glib.h>
  #include <stdio.h>
  #ifdef HAVE_PTHREAD_H
  #include <pthread.h>
  #endif
  #include <stdint.h>
  #include "mono/utils/mono-compiler.h"
  #include "mono/utils/atomic.h"
  #include "mono/utils/mono-mutex.h"
  #include "mono/sgen/sgen-conf.h"
  #include "mono/sgen/sgen-hash-table.h"
  #include "mono/sgen/sgen-protocol.h"
  #include "mono/sgen/gc-internal-agnostic.h"
  
  /* 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,
  	CLEAR_AT_TLAB_CREATION_DEBUG
  } NurseryClearPolicy;
  
  NurseryClearPolicy sgen_get_nursery_clear_policy (void);
  
  #if !defined(__MACH__) && !MONO_MACH_ARCH_SUPPORTED && defined(HAVE_PTHREAD_KILL)
  #define SGEN_POSIX_STW 1
  #endif
  
  /*
   * The nursery section uses this struct.
   */
  typedef struct _GCMemSection GCMemSection;
  struct _GCMemSection {
  	char *data;
  	mword size;
  	/* pointer where more data could be allocated if it fits */
  	char *next_data;
  	char *end_data;
  	/*
  	 * scan starts is an array of pointers to objects equally spaced in the allocation area
  	 * They let use quickly find pinned objects from pinning pointers.
  	 */
  	char **scan_starts;
  	/* in major collections indexes in the pin_queue for objects that pin this section */
  	size_t pin_queue_first_entry;
  	size_t pin_queue_last_entry;
  	size_t num_scan_start;
  };
  
  /*
   * Recursion is not allowed for the thread lock.
   */
  #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)	mono_mutex_init (&(name))
  #define LOCK_GC do {						\
  		MONO_TRY_BLOCKING	\
  		mono_mutex_lock (&gc_mutex);			\
  		MONO_FINISH_TRY_BLOCKING	\
  	} while (0)
  #define UNLOCK_GC do { sgen_gc_unlock (); } while (0)
  
  extern LOCK_DECLARE (sgen_interruption_mutex);
  
  #define LOCK_INTERRUPTION do {	\
  	MONO_TRY_BLOCKING	\
  	mono_mutex_lock (&sgen_interruption_mutex);	\
  	MONO_FINISH_TRY_BLOCKING	\
  } while (0)
  
  #define UNLOCK_INTERRUPTION mono_mutex_unlock (&sgen_interruption_mutex)
  
  /* FIXME: Use InterlockedAdd & InterlockedAdd64 to reduce the CAS cost. */
  #define SGEN_CAS	InterlockedCompareExchange
  #define SGEN_CAS_PTR	InterlockedCompareExchangePointer
  #define SGEN_ATOMIC_ADD(x,i)	do {					\
  		int __old_x;						\
  		do {							\
  			__old_x = (x);					\
  		} while (InterlockedCompareExchange (&(x), __old_x + (i), __old_x) != __old_x); \
  	} while (0)
  #define SGEN_ATOMIC_ADD_P(x,i) do { \
  		size_t __old_x;                                            \
  		do {                                                    \
  			__old_x = (x);                                  \
  		} while (InterlockedCompareExchangePointer ((void**)&(x), (void*)(__old_x + (i)), (void*)__old_x) != (void*)__old_x); \
  	} while (0)
  
  #ifdef HEAVY_STATISTICS
  extern guint64 stat_objects_alloced_degraded;
  extern guint64 stat_bytes_alloced_degraded;
  extern guint64 stat_copy_object_called_major;
  extern guint64 stat_objects_copied_major;
  #endif
  
  #define SGEN_ASSERT(level, a, ...) do {	\
  	if (G_UNLIKELY ((level) <= SGEN_MAX_ASSERT_LEVEL && !(a))) {	\
  		g_error (__VA_ARGS__);	\
  } } while (0)
  
  
  #define SGEN_LOG(level, format, ...) do {      \
  	if (G_UNLIKELY ((level) <= SGEN_MAX_DEBUG_LEVEL && (level) <= gc_debug_level)) {	\
  		mono_gc_printf (gc_debug_file, format "\n", ##__VA_ARGS__);	\
  } } while (0)
  
  #define SGEN_COND_LOG(level, cond, format, ...) do {	\
  	if (G_UNLIKELY ((level) <= SGEN_MAX_DEBUG_LEVEL && (level) <= gc_debug_level)) {	\
  		if (cond)	\
  			mono_gc_printf (gc_debug_file, format "\n", ##__VA_ARGS__);	\
  } } while (0)
  
  extern int gc_debug_level;
  extern FILE* gc_debug_file;
  
  extern int current_collection_generation;
  
  extern unsigned int sgen_global_stop_count;
  
  #define SGEN_ALLOC_ALIGN		8
  #define SGEN_ALLOC_ALIGN_BITS	3
  
  /* s must be non-negative */
  #define SGEN_CAN_ALIGN_UP(s)		((s) <= SIZE_MAX - (SGEN_ALLOC_ALIGN - 1))
  #define SGEN_ALIGN_UP(s)		(((s)+(SGEN_ALLOC_ALIGN-1)) & ~(SGEN_ALLOC_ALIGN-1))
  
  #if SIZEOF_VOID_P == 4
  #define ONE_P 1
  #else
  #define ONE_P 1ll
  #endif
  
  static inline guint
  sgen_aligned_addr_hash (gconstpointer ptr)
  {
  	return GPOINTER_TO_UINT (ptr) >> 3;
  }
  
  #define SGEN_PTR_IN_NURSERY(p,bits,start,end)	(((mword)(p) & ~((1 << (bits)) - 1)) == (mword)(start))
  
  #ifdef USER_CONFIG
  
  /* good sizes are 512KB-1MB: larger ones increase a lot memzeroing time */
  #define DEFAULT_NURSERY_SIZE (sgen_nursery_size)
  extern size_t sgen_nursery_size;
  /* The number of trailing 0 bits in DEFAULT_NURSERY_SIZE */
  #define DEFAULT_NURSERY_BITS (sgen_nursery_bits)
  extern int sgen_nursery_bits;
  
  #else
  
  #define DEFAULT_NURSERY_SIZE (4*1024*1024)
  #define DEFAULT_NURSERY_BITS 22
  
  #endif
  
  extern char *sgen_nursery_start;
  extern char *sgen_nursery_end;
  
  static inline MONO_ALWAYS_INLINE gboolean
  sgen_ptr_in_nursery (void *p)
  {
  	return SGEN_PTR_IN_NURSERY ((p), DEFAULT_NURSERY_BITS, sgen_nursery_start, sgen_nursery_end);
  }
  
  static inline MONO_ALWAYS_INLINE char*
  sgen_get_nursery_start (void)
  {
  	return sgen_nursery_start;
  }
  
  static inline MONO_ALWAYS_INLINE char*
  sgen_get_nursery_end (void)
  {
  	return sgen_nursery_end;
  }
  
  /*
   * We use the lowest three bits in the vtable pointer of objects to tag whether they're
   * forwarded, pinned, and/or cemented.  These are the valid states:
   *
   * | State            | bits |
   * |------------------+------+
   * | default          |  000 |
   * | forwarded        |  001 |
   * | pinned           |  010 |
   * | pinned, cemented |  110 |
   *
   * We store them in the vtable slot because the bits are used in the sync block for other
   * purposes: if we merge them and alloc the sync blocks aligned to 8 bytes, we can change
   * this and use bit 3 in the syncblock (with the lower two bits both set for forwarded, that
   * would be an invalid combination for the monitor and hash code).
   */
  
  #include "sgen-tagged-pointer.h"
  
  #define SGEN_VTABLE_BITS_MASK	SGEN_TAGGED_POINTER_MASK
  
  #define SGEN_POINTER_IS_TAGGED_FORWARDED(p)	SGEN_POINTER_IS_TAGGED_1((p))
  #define SGEN_POINTER_TAG_FORWARDED(p)		SGEN_POINTER_TAG_1((p))
  
  #define SGEN_POINTER_IS_TAGGED_PINNED(p)	SGEN_POINTER_IS_TAGGED_2((p))
  #define SGEN_POINTER_TAG_PINNED(p)		SGEN_POINTER_TAG_2((p))
  
  #define SGEN_POINTER_IS_TAGGED_CEMENTED(p)	SGEN_POINTER_IS_TAGGED_4((p))
  #define SGEN_POINTER_TAG_CEMENTED(p)		SGEN_POINTER_TAG_4((p))
  
  #define SGEN_POINTER_UNTAG_VTABLE(p)		SGEN_POINTER_UNTAG_ALL((p))
  
  /* returns NULL if not forwarded, or the forwarded address */
  #define SGEN_VTABLE_IS_FORWARDED(vtable) (SGEN_POINTER_IS_TAGGED_FORWARDED ((vtable)) ? SGEN_POINTER_UNTAG_VTABLE ((vtable)) : NULL)
  #define SGEN_OBJECT_IS_FORWARDED(obj) (SGEN_VTABLE_IS_FORWARDED (((mword*)(obj))[0]))
  
  #define SGEN_VTABLE_IS_PINNED(vtable) SGEN_POINTER_IS_TAGGED_PINNED ((vtable))
  #define SGEN_OBJECT_IS_PINNED(obj) (SGEN_VTABLE_IS_PINNED (((mword*)(obj))[0]))
  
  #define SGEN_OBJECT_IS_CEMENTED(obj) (SGEN_POINTER_IS_TAGGED_CEMENTED (((mword*)(obj))[0]))
  
  /* set the forwarded address fw_addr for object obj */
  #define SGEN_FORWARD_OBJECT(obj,fw_addr) do {				\
  		*(void**)(obj) = SGEN_POINTER_TAG_FORWARDED ((fw_addr));	\
  	} while (0)
  #define SGEN_PIN_OBJECT(obj) do {	\
  		*(void**)(obj) = SGEN_POINTER_TAG_PINNED (*(void**)(obj)); \
  	} while (0)
  #define SGEN_CEMENT_OBJECT(obj) do {	\
  		*(void**)(obj) = SGEN_POINTER_TAG_CEMENTED (*(void**)(obj)); \
  	} while (0)
  /* Unpins and uncements */
  #define SGEN_UNPIN_OBJECT(obj) do {	\
  		*(void**)(obj) = SGEN_POINTER_UNTAG_VTABLE (*(void**)(obj)); \
  	} while (0)
  
  /*
   * Since we set bits in the vtable, use the macro to load it from the pointer to
   * an object that is potentially pinned.
   */
  #define SGEN_LOAD_VTABLE(obj)		((GCVTable)(SGEN_POINTER_UNTAG_ALL (SGEN_LOAD_VTABLE_UNCHECKED ((obj)))))
  
  /*
  List of what each bit on of the vtable gc bits means. 
  */
  enum {
  	SGEN_GC_BIT_BRIDGE_OBJECT = 1,
  	SGEN_GC_BIT_BRIDGE_OPAQUE_OBJECT = 2,
  	SGEN_GC_BIT_FINALIZER_AWARE = 4,
  };
  
  void sgen_gc_init (void);
  
  void sgen_os_init (void);
  
  void sgen_update_heap_boundaries (mword low, mword high);
  
  void sgen_check_section_scan_starts (GCMemSection *section);
  
  void sgen_conservatively_pin_objects_from (void **start, void **end, void *start_nursery, void *end_nursery, int pin_type);
  
  /* Keep in sync with description_for_type() in sgen-internal.c! */
  enum {
  	INTERNAL_MEM_PIN_QUEUE,
  	INTERNAL_MEM_FRAGMENT,
  	INTERNAL_MEM_SECTION,
  	INTERNAL_MEM_SCAN_STARTS,
  	INTERNAL_MEM_FIN_TABLE,
  	INTERNAL_MEM_FINALIZE_ENTRY,
  	INTERNAL_MEM_FINALIZE_READY,
  	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_GRAY_QUEUE,
  	INTERNAL_MEM_MS_TABLES,
  	INTERNAL_MEM_MS_BLOCK_INFO,
  	INTERNAL_MEM_MS_BLOCK_INFO_SORT,
  	INTERNAL_MEM_WORKER_DATA,
  	INTERNAL_MEM_THREAD_POOL_JOB,
  	INTERNAL_MEM_BRIDGE_DATA,
  	INTERNAL_MEM_OLD_BRIDGE_HASH_TABLE,
  	INTERNAL_MEM_OLD_BRIDGE_HASH_TABLE_ENTRY,
  	INTERNAL_MEM_BRIDGE_HASH_TABLE,
  	INTERNAL_MEM_BRIDGE_HASH_TABLE_ENTRY,
  	INTERNAL_MEM_BRIDGE_ALIVE_HASH_TABLE,
  	INTERNAL_MEM_BRIDGE_ALIVE_HASH_TABLE_ENTRY,
  	INTERNAL_MEM_TARJAN_BRIDGE_HASH_TABLE,
  	INTERNAL_MEM_TARJAN_BRIDGE_HASH_TABLE_ENTRY,
  	INTERNAL_MEM_TARJAN_OBJ_BUCKET,
  	INTERNAL_MEM_BRIDGE_DEBUG,
  	INTERNAL_MEM_TOGGLEREF_DATA,
  	INTERNAL_MEM_CARDTABLE_MOD_UNION,
  	INTERNAL_MEM_BINARY_PROTOCOL,
  	INTERNAL_MEM_TEMPORARY,
  	INTERNAL_MEM_FIRST_CLIENT
  };
  
  enum {
  	GENERATION_NURSERY,
  	GENERATION_OLD,
  	GENERATION_MAX
  };
  
  #ifdef SGEN_HEAVY_BINARY_PROTOCOL
  #define BINARY_PROTOCOL_ARG(x)	,x
  #else
  #define BINARY_PROTOCOL_ARG(x)
  #endif
  
  void sgen_init_internal_allocator (void);
  
  #define SGEN_DEFINE_OBJECT_VTABLE
  #ifdef SGEN_CLIENT_HEADER
  #include SGEN_CLIENT_HEADER
  #else
  #include "metadata/sgen-client-mono.h"
  #endif
  #undef SGEN_DEFINE_OBJECT_VTABLE
  
  #include "mono/sgen/sgen-descriptor.h"
  #include "mono/sgen/sgen-gray.h"
  
  /* the runtime can register areas of memory as roots: we keep two lists of roots,
   * a pinned root set for conservatively scanned roots and a normal one for
   * precisely scanned roots (currently implemented as a single list).
   */
  typedef struct _RootRecord RootRecord;
  struct _RootRecord {
  	char *end_root;
  	SgenDescriptor root_desc;
  	int source;
  	const char *msg;
  };
  
  enum {
  	ROOT_TYPE_NORMAL = 0, /* "normal" roots */
  	ROOT_TYPE_PINNED = 1, /* roots without a GC descriptor */
  	ROOT_TYPE_WBARRIER = 2, /* roots with a write barrier */
  	ROOT_TYPE_NUM
  };
  
  extern SgenHashTable roots_hash [ROOT_TYPE_NUM];
  
  int sgen_register_root (char *start, size_t size, SgenDescriptor descr, int root_type, int source, const char *msg);
  void sgen_deregister_root (char* addr);
  
  typedef void (*IterateObjectCallbackFunc) (GCObject*, size_t, void*);
  
  void sgen_scan_area_with_callback (char *start, char *end, IterateObjectCallbackFunc callback, void *data, gboolean allow_flags, gboolean fail_on_canaries);
  
  /* eventually share with MonoThread? */
  /*
   * This structure extends the MonoThreadInfo structure.
   */
  struct _SgenThreadInfo {
  	SgenClientThreadInfo client_info;
  
  	char **tlab_next_addr;
  	char **tlab_start_addr;
  	char **tlab_temp_end_addr;
  	char **tlab_real_end_addr;
  
  #ifndef HAVE_KW_THREAD
  	char *tlab_start;
  	char *tlab_next;
  	char *tlab_temp_end;
  	char *tlab_real_end;
  #endif
  };
  
  gboolean sgen_is_worker_thread (MonoNativeThreadId thread);
  
  typedef void (*CopyOrMarkObjectFunc) (GCObject**, SgenGrayQueue*);
  typedef void (*ScanObjectFunc) (GCObject *obj, SgenDescriptor desc, SgenGrayQueue*);
  typedef void (*ScanVTypeFunc) (GCObject *full_object, char *start, SgenDescriptor desc, SgenGrayQueue* BINARY_PROTOCOL_ARG (size_t size));
  typedef gboolean (*DrainGrayStackFunc) (SgenGrayQueue *queue);
  
  typedef struct {
  	CopyOrMarkObjectFunc copy_or_mark_object;
  	ScanObjectFunc scan_object;
  	ScanVTypeFunc scan_vtype;
  	/* Drain stack optimized for the above functions */
  	DrainGrayStackFunc drain_gray_stack;
  	/*FIXME add allocation function? */
  } SgenObjectOperations;
  
  typedef struct
  {
  	SgenObjectOperations *ops;
  	SgenGrayQueue *queue;
  } ScanCopyContext;
  
  #define CONTEXT_FROM_OBJECT_OPERATIONS(ops, queue) ((ScanCopyContext) { (ops), (queue) })
  
  void sgen_report_internal_mem_usage (void);
  void sgen_dump_internal_mem_usage (FILE *heap_dump_file);
  void sgen_dump_section (GCMemSection *section, const char *type);
  void sgen_dump_occupied (char *start, char *end, char *section_start);
  
  void sgen_register_fixed_internal_mem_type (int type, size_t size);
  
  void* sgen_alloc_internal (int type);
  void sgen_free_internal (void *addr, int type);
  
  void* sgen_alloc_internal_dynamic (size_t size, int type, gboolean assert_on_failure);
  void sgen_free_internal_dynamic (void *addr, size_t size, int type);
  
  void sgen_pin_stats_enable (void);
  void sgen_pin_stats_register_object (GCObject *obj, size_t size);
  void sgen_pin_stats_register_global_remset (GCObject *obj);
  void sgen_pin_stats_print_class_stats (void);
  
  void sgen_sort_addresses (void **array, size_t size);
  void sgen_add_to_global_remset (gpointer ptr, gpointer obj);
  
  int sgen_get_current_collection_generation (void);
  gboolean sgen_collection_is_concurrent (void);
  gboolean sgen_concurrent_collection_in_progress (void);
  
  typedef struct _SgenFragment SgenFragment;
  
  struct _SgenFragment {
  	SgenFragment *next;
  	char *fragment_start;
  	char *fragment_next; /* the current soft limit for allocation */
  	char *fragment_end;
  	SgenFragment *next_in_order; /* We use a different entry for all active fragments so we can avoid SMR. */
  };
  
  typedef struct {
  	SgenFragment *alloc_head; /* List head to be used when allocating memory. Walk with fragment_next. */
  	SgenFragment *region_head; /* List head of the region used by this allocator. Walk with next_in_order. */
  } SgenFragmentAllocator;
  
  void sgen_fragment_allocator_add (SgenFragmentAllocator *allocator, char *start, char *end);
  void sgen_fragment_allocator_release (SgenFragmentAllocator *allocator);
  void* sgen_fragment_allocator_serial_alloc (SgenFragmentAllocator *allocator, size_t size);
  void* sgen_fragment_allocator_par_alloc (SgenFragmentAllocator *allocator, size_t size);
  void* sgen_fragment_allocator_serial_range_alloc (SgenFragmentAllocator *allocator, size_t desired_size, size_t minimum_size, size_t *out_alloc_size);
  void* sgen_fragment_allocator_par_range_alloc (SgenFragmentAllocator *allocator, size_t desired_size, size_t minimum_size, size_t *out_alloc_size);
  SgenFragment* sgen_fragment_allocator_alloc (void);
  void sgen_clear_allocator_fragments (SgenFragmentAllocator *allocator);
  void sgen_clear_range (char *start, char *end);
  
  
  /*
  This is a space/speed compromise as we need to make sure the from/to space check is both O(1)
  and only hit cache hot memory. On a 4Mb nursery it requires 1024 bytes, or 3% of your average
  L1 cache. On small configs with a 512kb nursery, this goes to 0.4%.
  
  Experimental results on how much space we waste with a 4Mb nursery:
  
  Note that the wastage applies to the half nursery, or 2Mb:
  
  Test 1 (compiling corlib):
  9: avg: 3.1k
  8: avg: 1.6k
  
  */
  #define SGEN_TO_SPACE_GRANULE_BITS 9
  #define SGEN_TO_SPACE_GRANULE_IN_BYTES (1 << SGEN_TO_SPACE_GRANULE_BITS)
  
  extern char *sgen_space_bitmap;
  extern size_t sgen_space_bitmap_size;
  
  static inline gboolean
  sgen_nursery_is_to_space (void *object)
  {
  	size_t idx = ((char*)object - sgen_nursery_start) >> SGEN_TO_SPACE_GRANULE_BITS;
  	size_t byte = idx >> 3;
  	size_t bit = idx & 0x7;
  
  	SGEN_ASSERT (4, sgen_ptr_in_nursery (object), "object %p is not in nursery [%p - %p]", object, sgen_get_nursery_start (), sgen_get_nursery_end ());
  	SGEN_ASSERT (4, byte < sgen_space_bitmap_size, "byte index %zd out of range (%zd)", byte, sgen_space_bitmap_size);
  
  	return (sgen_space_bitmap [byte] & (1 << bit)) != 0;
  }
  
  static inline gboolean
  sgen_nursery_is_from_space (void *object)
  {
  	return !sgen_nursery_is_to_space (object);
  }
  
  static inline gboolean
  sgen_nursery_is_object_alive (GCObject *obj)
  {
  	/* FIXME put this asserts under a non default level */
  	g_assert (sgen_ptr_in_nursery (obj));
  
  	if (sgen_nursery_is_to_space (obj))
  		return TRUE;
  
  	if (SGEN_OBJECT_IS_PINNED (obj) || SGEN_OBJECT_IS_FORWARDED (obj))
  		return TRUE;
  
  	return FALSE;
  }
  
  typedef struct {
  	gboolean is_split;
  
  	GCObject* (*alloc_for_promotion) (GCVTable vtable, GCObject *obj, size_t objsize, gboolean has_references);
  
  	SgenObjectOperations serial_ops;
  
  	void (*prepare_to_space) (char *to_space_bitmap, size_t space_bitmap_size);
  	void (*clear_fragments) (void);
  	SgenFragment* (*build_fragments_get_exclude_head) (void);
  	void (*build_fragments_release_exclude_head) (void);
  	void (*build_fragments_finish) (SgenFragmentAllocator *allocator);
  	void (*init_nursery) (SgenFragmentAllocator *allocator, char *start, char *end);
  
  	gboolean (*handle_gc_param) (const char *opt); /* Optional */
  	void (*print_gc_param_usage) (void); /* Optional */
  } SgenMinorCollector;
  
  extern SgenMinorCollector sgen_minor_collector;
  
  void sgen_simple_nursery_init (SgenMinorCollector *collector);
  void sgen_split_nursery_init (SgenMinorCollector *collector);
  
  /* Updating references */
  
  #ifdef SGEN_CHECK_UPDATE_REFERENCE
  gboolean sgen_thread_pool_is_thread_pool_thread (MonoNativeThreadId some_thread) MONO_INTERNAL;
  static inline void
  sgen_update_reference (GCObject **p, GCObject *o, gboolean allow_null)
  {
  	if (!allow_null)
  		SGEN_ASSERT (0, o, "Cannot update a reference with a NULL pointer");
  	SGEN_ASSERT (0, !sgen_thread_pool_is_thread_pool_thread (mono_native_thread_id_get ()), "Can't update a reference in the worker thread");
  	*p = o;
  }
  
  #define SGEN_UPDATE_REFERENCE_ALLOW_NULL(p,o)	sgen_update_reference ((GCObject**)(p), (GCObject*)(o), TRUE)
  #define SGEN_UPDATE_REFERENCE(p,o)		sgen_update_reference ((GCObject**)(p), (GCObject*)(o), FALSE)
  #else
  #define SGEN_UPDATE_REFERENCE_ALLOW_NULL(p,o)	(*(GCObject**)(p) = (GCObject*)(o))
  #define SGEN_UPDATE_REFERENCE(p,o)		SGEN_UPDATE_REFERENCE_ALLOW_NULL ((p), (o))
  #endif
  
  /* Major collector */
  
  typedef void (*sgen_cardtable_block_callback) (mword start, mword size);
  void sgen_major_collector_iterate_live_block_ranges (sgen_cardtable_block_callback callback);
  
  typedef enum {
  	ITERATE_OBJECTS_SWEEP = 1,
  	ITERATE_OBJECTS_NON_PINNED = 2,
  	ITERATE_OBJECTS_PINNED = 4,
  	ITERATE_OBJECTS_ALL = ITERATE_OBJECTS_NON_PINNED | ITERATE_OBJECTS_PINNED,
  	ITERATE_OBJECTS_SWEEP_NON_PINNED = ITERATE_OBJECTS_SWEEP | ITERATE_OBJECTS_NON_PINNED,
  	ITERATE_OBJECTS_SWEEP_PINNED = ITERATE_OBJECTS_SWEEP | ITERATE_OBJECTS_PINNED,
  	ITERATE_OBJECTS_SWEEP_ALL = ITERATE_OBJECTS_SWEEP | ITERATE_OBJECTS_NON_PINNED | ITERATE_OBJECTS_PINNED
  } IterateObjectsFlags;
  
  typedef struct
  {
  	size_t num_scanned_objects;
  	size_t num_unique_scanned_objects;
  } ScannedObjectCounts;
  
  typedef struct _SgenMajorCollector SgenMajorCollector;
  struct _SgenMajorCollector {
  	size_t section_size;
  	gboolean is_concurrent;
  	gboolean needs_thread_pool;
  	gboolean supports_cardtable;
  	gboolean sweeps_lazily;
  
  	/*
  	 * This is set to TRUE by the sweep if the next major
  	 * collection should be synchronous (for evacuation).  For
  	 * non-concurrent collectors, this should be NULL.
  	 */
  	gboolean *want_synchronous_collection;
  
  	void* (*alloc_heap) (mword nursery_size, mword nursery_align, int nursery_bits);
  	gboolean (*is_object_live) (GCObject *obj);
  	GCObject* (*alloc_small_pinned_obj) (GCVTable vtable, size_t size, gboolean has_references);
  	GCObject* (*alloc_degraded) (GCVTable vtable, size_t size);
  
  	SgenObjectOperations major_ops_serial;
  	SgenObjectOperations major_ops_concurrent_start;
  	SgenObjectOperations major_ops_concurrent_finish;
  
  	GCObject* (*alloc_object) (GCVTable vtable, size_t size, gboolean has_references);
  	void (*free_pinned_object) (GCObject *obj, size_t size);
  
  	/*
  	 * This is used for domain unloading, heap walking from the logging profiler, and
  	 * debugging.  Can assume the world is stopped.
  	 */
  	void (*iterate_objects) (IterateObjectsFlags flags, IterateObjectCallbackFunc callback, void *data);
  
  	void (*free_non_pinned_object) (GCObject *obj, size_t size);
  	void (*pin_objects) (SgenGrayQueue *queue);
  	void (*pin_major_object) (GCObject *obj, SgenGrayQueue *queue);
  	void (*scan_card_table) (gboolean mod_union, ScanCopyContext ctx);
  	void (*iterate_live_block_ranges) (sgen_cardtable_block_callback callback);
  	void (*update_cardtable_mod_union) (void);
  	void (*init_to_space) (void);
  	void (*sweep) (void);
  	gboolean (*have_swept) (void);
  	void (*finish_sweeping) (void);
  	void (*free_swept_blocks) (size_t allowance);
  	void (*check_scan_starts) (void);
  	void (*dump_heap) (FILE *heap_dump_file);
  	gint64 (*get_used_size) (void);
  	void (*start_nursery_collection) (void);
  	void (*finish_nursery_collection) (void);
  	void (*start_major_collection) (void);
  	void (*finish_major_collection) (ScannedObjectCounts *counts);
  	gboolean (*ptr_is_in_non_pinned_space) (char *ptr, char **start);
  	gboolean (*ptr_is_from_pinned_alloc) (char *ptr);
  	void (*report_pinned_memory_usage) (void);
  	size_t (*get_num_major_sections) (void);
  	size_t (*get_bytes_survived_last_sweep) (void);
  	gboolean (*handle_gc_param) (const char *opt);
  	void (*print_gc_param_usage) (void);
  	void (*post_param_init) (SgenMajorCollector *collector);
  	gboolean (*is_valid_object) (char *ptr);
  	GCVTable (*describe_pointer) (char *pointer);
  	guint8* (*get_cardtable_mod_union_for_reference) (char *object);
  	long long (*get_and_reset_num_major_objects_marked) (void);
  	void (*count_cards) (long long *num_total_cards, long long *num_marked_cards);
  };
  
  extern SgenMajorCollector major_collector;
  
  void sgen_marksweep_init (SgenMajorCollector *collector);
  void sgen_marksweep_fixed_init (SgenMajorCollector *collector);
  void sgen_marksweep_par_init (SgenMajorCollector *collector);
  void sgen_marksweep_fixed_par_init (SgenMajorCollector *collector);
  void sgen_marksweep_conc_init (SgenMajorCollector *collector);
  SgenMajorCollector* sgen_get_major_collector (void);
  
  
  typedef struct _SgenRememberedSet {
  	void (*wbarrier_set_field) (GCObject *obj, gpointer field_ptr, GCObject* value);
  	void (*wbarrier_arrayref_copy) (gpointer dest_ptr, gpointer src_ptr, int count);
  	void (*wbarrier_value_copy) (gpointer dest, gpointer src, int count, size_t element_size);
  	void (*wbarrier_object_copy) (GCObject* obj, GCObject *src);
  	void (*wbarrier_generic_nostore) (gpointer ptr);
  	void (*record_pointer) (gpointer ptr);
  
  	void (*scan_remsets) (ScanCopyContext ctx);
  
  	void (*clear_cards) (void);
  
  	void (*finish_minor_collection) (void);
  	gboolean (*find_address) (char *addr);
  	gboolean (*find_address_with_cards) (char *cards_start, guint8 *cards, char *addr);
  } SgenRememberedSet;
  
  SgenRememberedSet *sgen_get_remset (void);
  
  /*
   * These must be kept in sync with object.h.  They're here for using SGen independently of
   * Mono.
   */
  void mono_gc_wbarrier_arrayref_copy (gpointer dest_ptr, gpointer src_ptr, int count);
  void mono_gc_wbarrier_generic_nostore (gpointer ptr);
  void mono_gc_wbarrier_generic_store (gpointer ptr, GCObject* value);
  void mono_gc_wbarrier_generic_store_atomic (gpointer ptr, GCObject *value);
  
  void sgen_wbarrier_value_copy_bitmap (gpointer _dest, gpointer _src, int size, unsigned bitmap);
  
  static inline SgenDescriptor
  sgen_obj_get_descriptor (GCObject *obj)
  {
  	GCVTable vtable = SGEN_LOAD_VTABLE_UNCHECKED (obj);
  	SGEN_ASSERT (9, !SGEN_POINTER_IS_TAGGED_ANY (vtable), "Object can't be tagged");
  	return sgen_vtable_get_descriptor (vtable);
  }
  
  static inline SgenDescriptor
  sgen_obj_get_descriptor_safe (GCObject *obj)
  {
  	GCVTable vtable = SGEN_LOAD_VTABLE (obj);
  	return sgen_vtable_get_descriptor (vtable);
  }
  
  static mword sgen_client_par_object_get_size (GCVTable vtable, GCObject* o);
  static mword sgen_client_slow_object_get_size (GCVTable vtable, GCObject* o);
  
  static inline mword
  sgen_safe_object_get_size (GCObject *obj)
  {
         GCObject *forwarded;
  
         if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj)))
                 obj = forwarded;
  
         return sgen_client_par_object_get_size (SGEN_LOAD_VTABLE (obj), obj);
  }
  
  static inline gboolean
  sgen_safe_object_is_small (GCObject *obj, int type)
  {
  	if (type <= DESC_TYPE_MAX_SMALL_OBJ)
  		return TRUE;
  	return SGEN_ALIGN_UP (sgen_safe_object_get_size ((GCObject*)obj)) <= SGEN_MAX_SMALL_OBJ_SIZE;
  }
  
  /*
   * This variant guarantees to return the exact size of the object
   * before alignment. Needed for canary support.
   */
  static inline guint
  sgen_safe_object_get_size_unaligned (GCObject *obj)
  {
  	GCObject *forwarded;
  
  	if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
  		obj = (GCObject*)forwarded;
  	}
  
  	return sgen_client_slow_object_get_size (SGEN_LOAD_VTABLE (obj), obj);
  }
  
  #ifdef SGEN_CLIENT_HEADER
  #include SGEN_CLIENT_HEADER
  #else
  #include "metadata/sgen-client-mono.h"
  #endif
  
  gboolean sgen_object_is_live (GCObject *obj);
  
  void  sgen_init_fin_weak_hash (void);
  
  /* FIXME: move the toggleref stuff out of here */
  void sgen_mark_togglerefs (char *start, char *end, ScanCopyContext ctx);
  void sgen_clear_togglerefs (char *start, char *end, ScanCopyContext ctx);
  
  void sgen_process_togglerefs (void);
  void sgen_register_test_toggleref_callback (void);
  
  void sgen_mark_bridge_object (GCObject *obj);
  void sgen_collect_bridge_objects (int generation, ScanCopyContext ctx);
  
  typedef gboolean (*SgenObjectPredicateFunc) (GCObject *obj, void *user_data);
  
  void sgen_null_links_if (SgenObjectPredicateFunc predicate, void *data, int generation, gboolean track);
  
  gboolean sgen_gc_is_object_ready_for_finalization (GCObject *object);
  void sgen_gc_lock (void);
  void sgen_gc_unlock (void);
  
  void sgen_queue_finalization_entry (GCObject *obj);
  const char* sgen_generation_name (int generation);
  
  void sgen_finalize_in_range (int generation, ScanCopyContext ctx);
  void sgen_null_link_in_range (int generation, ScanCopyContext ctx, gboolean track);
  void sgen_process_fin_stage_entries (void);
  gboolean sgen_have_pending_finalizers (void);
  void sgen_object_register_for_finalization (GCObject *obj, void *user_data);
  
  int sgen_gather_finalizers_if (SgenObjectPredicateFunc predicate, void *user_data, GCObject **out_array, int out_size);
  void sgen_remove_finalizers_if (SgenObjectPredicateFunc predicate, void *user_data, int generation);
  
  void sgen_register_disappearing_link (GCObject *obj, void **link, gboolean track, gboolean in_gc);
  
  GCObject* sgen_weak_link_get (void **link_addr);
  
  gboolean sgen_drain_gray_stack (ScanCopyContext ctx);
  
  enum {
  	SPACE_NURSERY,
  	SPACE_MAJOR,
  	SPACE_LOS
  };
  
  void sgen_pin_object (GCObject *object, SgenGrayQueue *queue);
  void sgen_set_pinned_from_failed_allocation (mword objsize);
  
  void sgen_ensure_free_space (size_t size);
  void sgen_gc_collect (int generation);
  void sgen_perform_collection (size_t requested_size, int generation_to_collect, const char *reason, gboolean wait_to_finish);
  
  int sgen_gc_collection_count (int generation);
  /* FIXME: what exactly does this return? */
  size_t sgen_gc_get_used_size (void);
  size_t sgen_gc_get_total_heap_allocation (void);
  
  /* STW */
  
  typedef struct {
  	int generation;
  	const char *reason;
  	gboolean is_overflow;
  	gint64 total_time;
  	gint64 stw_time;
  	gint64 bridge_time;
  } GGTimingInfo;
  
  void sgen_stop_world (int generation);
  void sgen_restart_world (int generation, GGTimingInfo *timing);
  gboolean sgen_is_world_stopped (void);
  
  gboolean sgen_set_allow_synchronous_major (gboolean flag);
  
  /* LOS */
  
  typedef struct _LOSObject LOSObject;
  struct _LOSObject {
  	LOSObject *next;
  	mword size; /* this is the object size, lowest bit used for pin/mark */
  	guint8 * volatile cardtable_mod_union; /* only used by the concurrent collector */
  #if SIZEOF_VOID_P < 8
  	mword dummy;		/* to align object to sizeof (double) */
  #endif
  	GCObject data [MONO_ZERO_LEN_ARRAY];
  };
  
  extern LOSObject *los_object_list;
  extern mword los_memory_usage;
  
  void sgen_los_free_object (LOSObject *obj);
  void* sgen_los_alloc_large_inner (GCVTable vtable, size_t size);
  void sgen_los_sweep (void);
  gboolean sgen_ptr_is_in_los (char *ptr, char **start);
  void sgen_los_iterate_objects (IterateObjectCallbackFunc cb, void *user_data);
  void sgen_los_iterate_live_block_ranges (sgen_cardtable_block_callback callback);
  void sgen_los_scan_card_table (gboolean mod_union, ScanCopyContext ctx);
  void sgen_los_update_cardtable_mod_union (void);
  void sgen_los_count_cards (long long *num_total_cards, long long *num_marked_cards);
  gboolean sgen_los_is_valid_object (char *object);
  gboolean mono_sgen_los_describe_pointer (char *ptr);
  LOSObject* sgen_los_header_for_object (GCObject *data);
  mword sgen_los_object_size (LOSObject *obj);
  void sgen_los_pin_object (GCObject *obj);
  gboolean sgen_los_object_is_pinned (GCObject *obj);
  void sgen_los_mark_mod_union_card (GCObject *mono_obj, void **ptr);
  
  
  /* nursery allocator */
  
  void sgen_clear_nursery_fragments (void);
  void sgen_nursery_allocator_prepare_for_pinning (void);
  void sgen_nursery_allocator_set_nursery_bounds (char *nursery_start, char *nursery_end);
  mword sgen_build_nursery_fragments (GCMemSection *nursery_section, SgenGrayQueue *unpin_queue);
  void sgen_init_nursery_allocator (void);
  void sgen_nursery_allocator_init_heavy_stats (void);
  void sgen_init_allocator (void);
  char* sgen_nursery_alloc_get_upper_alloc_bound (void);
  void* sgen_nursery_alloc (size_t size);
  void* sgen_nursery_alloc_range (size_t size, size_t min_size, size_t *out_alloc_size);
  gboolean sgen_can_alloc_size (size_t size);
  void sgen_nursery_retire_region (void *address, ptrdiff_t size);
  
  void sgen_nursery_alloc_prepare_for_minor (void);
  void sgen_nursery_alloc_prepare_for_major (void);
  
  GCObject* sgen_alloc_for_promotion (GCObject *obj, size_t objsize, gboolean has_references);
  
  GCObject* sgen_alloc_obj_nolock (GCVTable vtable, size_t size);
  GCObject* sgen_try_alloc_obj_nolock (GCVTable vtable, size_t size);
  
  /* Threads */
  
  void* sgen_thread_register (SgenThreadInfo* info, void *addr);
  void sgen_thread_unregister (SgenThreadInfo *p);
  
  /* Finalization/ephemeron support */
  
  static inline gboolean
  sgen_major_is_object_alive (GCObject *object)
  {
  	mword objsize;
  
  	/* Oldgen objects can be pinned and forwarded too */
  	if (SGEN_OBJECT_IS_PINNED (object) || SGEN_OBJECT_IS_FORWARDED (object))
  		return TRUE;
  
  	/*
  	 * FIXME: major_collector.is_object_live() also calculates the
  	 * size.  Avoid the double calculation.
  	 */
  	objsize = SGEN_ALIGN_UP (sgen_safe_object_get_size (object));
  	if (objsize > SGEN_MAX_SMALL_OBJ_SIZE)
  		return sgen_los_object_is_pinned (object);
  
  	return major_collector.is_object_live (object);
  }
  
  /*
   * This function returns true if @object is either alive or it belongs to the old gen
   * and we're currently doing a minor collection.
   */
  static inline int
  sgen_is_object_alive_for_current_gen (GCObject *object)
  {
  	if (sgen_ptr_in_nursery (object))
  		return sgen_nursery_is_object_alive (object);
  
  	if (current_collection_generation == GENERATION_NURSERY)
  		return TRUE;
  
  	return sgen_major_is_object_alive (object);
  }
  
  int sgen_gc_invoke_finalizers (void);
  
  /* GC handles */
  
  void sgen_init_gchandles (void);
  
  void sgen_null_links_if (SgenObjectPredicateFunc predicate, void *data, int generation, gboolean track);
  
  typedef gpointer (*SgenGCHandleIterateCallback) (gpointer hidden, GCHandleType handle_type, int max_generation, gpointer user);
  
  void sgen_gchandle_iterate (GCHandleType handle_type, int max_generation, SgenGCHandleIterateCallback callback, gpointer user);
  void sgen_gchandle_set_target (guint32 gchandle, GCObject *obj);
  void sgen_mark_normal_gc_handles (void *addr, SgenUserMarkFunc mark_func, void *gc_data);
  gpointer sgen_gchandle_get_metadata (guint32 gchandle);
  
  /* Other globals */
  
  extern GCMemSection *nursery_section;
  extern guint32 collect_before_allocs;
  extern guint32 verify_before_allocs;
  extern gboolean has_per_allocation_action;
  extern size_t degraded_mode;
  extern int default_nursery_size;
  extern guint32 tlab_size;
  extern NurseryClearPolicy nursery_clear_policy;
  extern gboolean sgen_try_free_some_memory;
  
  extern LOCK_DECLARE (gc_mutex);
  
  /* Nursery helpers. */
  
  static inline void
  sgen_set_nursery_scan_start (char *p)
  {
  	size_t idx = (p - (char*)nursery_section->data) / SGEN_SCAN_START_SIZE;
  	char *old = nursery_section->scan_starts [idx];
  	if (!old || old > p)
  		nursery_section->scan_starts [idx] = p;
  }
  
  
  /* Object Allocation */
  
  typedef enum {
  	ATYPE_NORMAL,
  	ATYPE_VECTOR,
  	ATYPE_SMALL,
  	ATYPE_STRING,
  	ATYPE_NUM
  } SgenAllocatorType;
  
  void sgen_init_tlab_info (SgenThreadInfo* info);
  void sgen_clear_tlabs (void);
  
  GCObject* sgen_alloc_obj (GCVTable vtable, size_t size);
  GCObject* sgen_alloc_obj_pinned (GCVTable vtable, size_t size);
  GCObject* sgen_alloc_obj_mature (GCVTable vtable, size_t size);
  
  /* Debug support */
  
  void sgen_check_consistency (void);
  void sgen_check_mod_union_consistency (void);
  void sgen_check_major_refs (void);
  void sgen_check_whole_heap (gboolean allow_missing_pinning);
  void sgen_check_whole_heap_stw (void);
  void sgen_check_objref (char *obj);
  void sgen_check_heap_marked (gboolean nursery_must_be_pinned);
  void sgen_check_nursery_objects_pinned (gboolean pinned);
  void sgen_check_for_xdomain_refs (void);
  GCObject* sgen_find_object_for_ptr (char *ptr);
  
  void mono_gc_scan_for_specific_ref (GCObject *key, gboolean precise);
  
  void sgen_debug_enable_heap_dump (const char *filename);
  void sgen_debug_dump_heap (const char *type, int num, const char *reason);
  
  void sgen_debug_verify_nursery (gboolean do_dump_nursery_content);
  void sgen_debug_check_nursery_is_clean (void);
  
  /* Environment variable parsing */
  
  #define MONO_GC_PARAMS_NAME	"MONO_GC_PARAMS"
  #define MONO_GC_DEBUG_NAME	"MONO_GC_DEBUG"
  
  void sgen_env_var_error (const char *env_var, const char *fallback, const char *description_format, ...);
  
  /* Utilities */
  
  void sgen_qsort (void *base, size_t nel, size_t width, int (*compar) (const void*, const void*));
  gint64 sgen_timestamp (void);
  
  /*
   * Canary (guard word) support
   * Notes:
   * - CANARY_SIZE must be multiple of word size in bytes
   * - Canary space is not included on checks against SGEN_MAX_SMALL_OBJ_SIZE
   */
   
  gboolean nursery_canaries_enabled (void);
  
  #define CANARY_SIZE 8
  #define CANARY_STRING  "koupepia"
  
  #define CANARIFY_SIZE(size) if (nursery_canaries_enabled ()) {	\
  			size = size + CANARY_SIZE;	\
  		}
  
  #define CANARIFY_ALLOC(addr,size) if (nursery_canaries_enabled ()) {	\
  				memcpy ((char*) (addr) + (size), CANARY_STRING, CANARY_SIZE);	\
  			}
  
  #define CANARY_VALID(addr) (strncmp ((char*) (addr), CANARY_STRING, CANARY_SIZE) == 0)
  
  #define CHECK_CANARY_FOR_OBJECT(addr,fail) if (nursery_canaries_enabled ()) {	\
  				char* canary_ptr = (char*) (addr) + sgen_safe_object_get_size_unaligned ((GCObject *) (addr));	\
  				if (!CANARY_VALID(canary_ptr)) {	\
  					char canary_copy[CANARY_SIZE +1];	\
  					strncpy (canary_copy, canary_ptr, CANARY_SIZE);	\
  					canary_copy[CANARY_SIZE] = 0;	\
  					if ((fail))			\
  						g_error ("CORRUPT CANARY:\naddr->%p\ntype->%s\nexcepted->'%s'\nfound->'%s'\n", (char*) addr, sgen_client_vtable_get_name (SGEN_LOAD_VTABLE ((addr))), CANARY_STRING, canary_copy); \
  					else				\
  						g_warning ("CORRUPT CANARY:\naddr->%p\ntype->%s\nexcepted->'%s'\nfound->'%s'\n", (char*) addr, sgen_client_vtable_get_name (SGEN_LOAD_VTABLE ((addr))), CANARY_STRING, canary_copy); \
  				} }
  
  /*
   * This causes the compile to extend the liveness of 'v' till the call to dummy_use
   */
  static inline void
  sgen_dummy_use (gpointer v)
  {
  #if defined(__GNUC__)
  	__asm__ volatile ("" : "=r"(v) : "r"(v));
  #elif defined(_MSC_VER)
  	static volatile gpointer ptr;
  	ptr = v;
  #else
  #error "Implement sgen_dummy_use for your compiler"
  #endif
  }
  
  #endif /* HAVE_SGEN_GC */
  
  #endif /* __MONO_SGENGC_H__ */
  