2 * Copyright 2001-2003 Ximian, Inc
3 * Copyright 2003-2010 Novell, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 #ifndef __MONO_SGENGC_H__
25 #define __MONO_SGENGC_H__
35 #include <mono/utils/mono-compiler.h>
36 #include <mono/metadata/class-internals.h>
37 #include <mono/metadata/object-internals.h>
40 * Turning on heavy statistics will turn off the managed allocator and
41 * the managed write barrier.
43 //#define HEAVY_STATISTICS
46 * If this is set, the nursery is aligned to an address aligned to its size, ie.
47 * a 1MB nursery will be aligned to an address divisible by 1MB. This allows us to
48 * speed up ptr_in_nursery () checks which are very frequent. This requires the
49 * nursery size to be a compile time constant.
51 #define SGEN_ALIGN_NURSERY 1
53 //#define SGEN_BINARY_PROTOCOL
55 #define SGEN_MAX_DEBUG_LEVEL 2
57 #define THREAD_HASH_SIZE 11
59 #define GC_BITS_PER_WORD (sizeof (mword) * 8)
61 #define ARCH_THREAD_TYPE pthread_t
62 #define ARCH_GET_THREAD pthread_self
63 #define ARCH_THREAD_EQUALS(a,b) pthread_equal (a, b)
65 #if SIZEOF_VOID_P == 4
66 typedef guint32 mword;
68 typedef guint64 mword;
71 #define SGEN_TV_DECLARE(name) gint64 name
72 #define SGEN_TV_GETTIME(tv) tv = mono_100ns_ticks ()
73 #define SGEN_TV_ELAPSED(start,end) (int)((end-start) / 10)
74 #define SGEN_TV_ELAPSED_MS(start,end) ((SGEN_TV_ELAPSED((start),(end)) + 500) / 1000)
76 /* for use with write barriers */
77 typedef struct _RememberedSet RememberedSet;
78 struct _RememberedSet {
82 mword data [MONO_ZERO_LEN_ARRAY];
85 /* eventually share with MonoThread? */
86 typedef struct _SgenThreadInfo SgenThreadInfo;
88 struct _SgenThreadInfo {
91 unsigned int stop_count; /* to catch duplicate signals */
94 volatile int in_critical_region;
97 void *stack_start_limit;
98 char **tlab_next_addr;
99 char **tlab_start_addr;
100 char **tlab_temp_end_addr;
101 char **tlab_real_end_addr;
102 gpointer **store_remset_buffer_addr;
103 long *store_remset_buffer_index_addr;
104 RememberedSet *remset;
105 gpointer runtime_data;
106 gpointer stopped_ip; /* only valid if the thread is stopped */
107 MonoDomain *stopped_domain; /* ditto */
108 gpointer *stopped_regs; /* ditto */
109 #ifndef HAVE_KW_THREAD
114 gpointer *store_remset_buffer;
115 long store_remset_buffer_index;
126 typedef struct _SgenBlock SgenBlock;
133 * The nursery section and the major copying collector's sections use
136 typedef struct _GCMemSection GCMemSection;
137 struct _GCMemSection {
141 /* pointer where more data could be allocated if it fits */
145 * scan starts is an array of pointers to objects equally spaced in the allocation area
146 * They let use quickly find pinned objects from pinning pointers.
149 /* in major collections indexes in the pin_queue for objects that pin this section */
150 void **pin_queue_start;
151 int pin_queue_num_entries;
152 unsigned short num_scan_start;
153 gboolean is_to_space;
156 #define SGEN_SIZEOF_GC_MEM_SECTION ((sizeof (GCMemSection) + 7) & ~7)
159 * to quickly find the head of an object pinned by a conservative
160 * address we keep track of the objects allocated for each
161 * SGEN_SCAN_START_SIZE memory chunk in the nursery or other memory
162 * sections. Larger values have less memory overhead and bigger
163 * runtime cost. 4-8 KB are reasonable values.
165 #define SGEN_SCAN_START_SIZE (4096*2)
168 * Objects bigger then this go into the large object space. This size
169 * has a few constraints. It must fit into the major heap, which in
170 * the case of the copying collector means that it must fit into a
171 * pinned chunk. It must also play well with the GC descriptors, some
172 * of which (DESC_TYPE_RUN_LENGTH, DESC_TYPE_SMALL_BITMAP) encode the
175 #define SGEN_MAX_SMALL_OBJ_SIZE 8000
177 /* This is also the MAJOR_SECTION_SIZE for the copying major
179 #define SGEN_PINNED_CHUNK_SIZE (128 * 1024)
181 #define SGEN_PINNED_CHUNK_FOR_PTR(o) ((SgenBlock*)(((mword)(o)) & ~(SGEN_PINNED_CHUNK_SIZE - 1)))
183 typedef struct _SgenPinnedChunk SgenPinnedChunk;
185 #if defined(__APPLE__) || defined(__OpenBSD__) || defined(__FreeBSD__)
186 const static int suspend_signal_num = SIGXFSZ;
188 const static int suspend_signal_num = SIGPWR;
190 const static int restart_signal_num = SIGXCPU;
193 * Recursion is not allowed for the thread lock.
195 #define LOCK_DECLARE(name) pthread_mutex_t name = PTHREAD_MUTEX_INITIALIZER
196 /* if changing LOCK_INIT to something that isn't idempotent, look at
197 its use in mono_gc_base_init in sgen-gc.c */
198 #define LOCK_INIT(name)
199 #define LOCK_GC pthread_mutex_lock (&gc_mutex)
200 #define UNLOCK_GC pthread_mutex_unlock (&gc_mutex)
201 #define LOCK_INTERRUPTION pthread_mutex_lock (&interruption_mutex)
202 #define UNLOCK_INTERRUPTION pthread_mutex_unlock (&interruption_mutex)
204 #define SGEN_CAS_PTR InterlockedCompareExchangePointer
205 #define SGEN_ATOMIC_ADD(x,i) do { \
209 } while (InterlockedCompareExchange (&(x), __old_x + (i), __old_x) != __old_x); \
212 /* non-pthread will need to provide their own version of start/stop */
213 #define USE_SIGNAL_BASED_START_STOP_WORLD 1
214 /* we intercept pthread_create calls to know which threads exist */
215 #define USE_PTHREAD_INTERCEPT 1
217 #ifdef HEAVY_STATISTICS
218 #define HEAVY_STAT(x) x
220 extern long long stat_objects_alloced_degraded;
221 extern long long stat_bytes_alloced_degraded;
222 extern long long stat_copy_object_called_major;
223 extern long long stat_objects_copied_major;
225 #define HEAVY_STAT(x)
228 #define DEBUG(level,a) do {if (G_UNLIKELY ((level) <= SGEN_MAX_DEBUG_LEVEL && (level) <= gc_debug_level)) a;} while (0)
230 extern int gc_debug_level;
231 extern FILE* gc_debug_file;
233 extern int current_collection_generation;
235 #define SGEN_ALLOC_ALIGN 8
236 #define SGEN_ALLOC_ALIGN_BITS 3
238 #define SGEN_ALIGN_UP(s) (((s)+(SGEN_ALLOC_ALIGN-1)) & ~(SGEN_ALLOC_ALIGN-1))
240 #ifdef SGEN_ALIGN_NURSERY
241 #define SGEN_PTR_IN_NURSERY(p,bits,start,end) (((mword)(p) & ~((1 << (bits)) - 1)) == (mword)(start))
243 #define SGEN_PTR_IN_NURSERY(p,bits,start,end) ((char*)(p) >= (start) && (char*)(p) < (end))
246 /* Structure that corresponds to a MonoVTable: desc is a mword so requires
247 * no cast from a pointer to an integer
254 /* these bits are set in the object vtable: we could merge them since an object can be
255 * either pinned or forwarded but not both.
256 * We store them in the vtable slot because the bits are used in the sync block for
257 * other purposes: if we merge them and alloc the sync blocks aligned to 8 bytes, we can change
258 * this and use bit 3 in the syncblock (with the lower two bits both set for forwarded, that
259 * would be an invalid combination for the monitor and hash code).
260 * The values are already shifted.
261 * The forwarding address is stored in the sync block.
263 #define SGEN_FORWARDED_BIT 1
264 #define SGEN_PINNED_BIT 2
265 #define SGEN_VTABLE_BITS_MASK 0x3
267 /* returns NULL if not forwarded, or the forwarded address */
268 #define SGEN_OBJECT_IS_FORWARDED(obj) (((mword*)(obj))[0] & SGEN_FORWARDED_BIT ? (void*)(((mword*)(obj))[0] & ~SGEN_VTABLE_BITS_MASK) : NULL)
269 #define SGEN_OBJECT_IS_PINNED(obj) (((mword*)(obj))[0] & SGEN_PINNED_BIT)
271 /* set the forwarded address fw_addr for object obj */
272 #define SGEN_FORWARD_OBJECT(obj,fw_addr) do { \
273 ((mword*)(obj))[0] = (mword)(fw_addr) | SGEN_FORWARDED_BIT; \
275 #define SGEN_PIN_OBJECT(obj) do { \
276 ((mword*)(obj))[0] |= SGEN_PINNED_BIT; \
278 #define SGEN_UNPIN_OBJECT(obj) do { \
279 ((mword*)(obj))[0] &= ~SGEN_PINNED_BIT; \
283 * Since we set bits in the vtable, use the macro to load it from the pointer to
284 * an object that is potentially pinned.
286 #define SGEN_LOAD_VTABLE(addr) ((*(mword*)(addr)) & ~SGEN_VTABLE_BITS_MASK)
289 * ######################################################################
290 * ######## GC descriptors
291 * ######################################################################
292 * Used to quickly get the info the GC needs about an object: size and
293 * where the references are held.
295 #define OBJECT_HEADER_WORDS (sizeof(MonoObject)/sizeof(gpointer))
296 #define LOW_TYPE_BITS 3
297 #define SMALL_BITMAP_SHIFT 16
298 #define SMALL_BITMAP_SIZE (GC_BITS_PER_WORD - SMALL_BITMAP_SHIFT)
299 #define VECTOR_INFO_SHIFT 14
300 #define VECTOR_ELSIZE_SHIFT 3
301 #define LARGE_BITMAP_SIZE (GC_BITS_PER_WORD - LOW_TYPE_BITS)
302 #define MAX_ELEMENT_SIZE 0x3ff
303 #define VECTOR_SUBTYPE_PTRFREE (DESC_TYPE_V_PTRFREE << VECTOR_INFO_SHIFT)
304 #define VECTOR_SUBTYPE_REFS (DESC_TYPE_V_REFS << VECTOR_INFO_SHIFT)
305 #define VECTOR_SUBTYPE_RUN_LEN (DESC_TYPE_V_RUN_LEN << VECTOR_INFO_SHIFT)
306 #define VECTOR_SUBTYPE_BITMAP (DESC_TYPE_V_BITMAP << VECTOR_INFO_SHIFT)
308 /* objects are aligned to 8 bytes boundaries
309 * A descriptor is a pointer in MonoVTable, so 32 or 64 bits of size.
310 * The low 3 bits define the type of the descriptor. The other bits
311 * depend on the type.
312 * As a general rule the 13 remaining low bits define the size, either
313 * of the whole object or of the elements in the arrays. While for objects
314 * the size is already in bytes, for arrays we need to shift, because
315 * array elements might be smaller than 8 bytes. In case of arrays, we
316 * use two bits to describe what the additional high bits represents,
317 * so the default behaviour can handle element sizes less than 2048 bytes.
318 * The high 16 bits, if 0 it means the object is pointer-free.
319 * This design should make it easy and fast to skip over ptr-free data.
320 * The first 4 types should cover >95% of the objects.
321 * Note that since the size of objects is limited to 64K, larger objects
322 * will be allocated in the large object heap.
323 * If we want 4-bytes alignment, we need to put vector and small bitmap
328 * We don't use 0 so that 0 isn't a valid GC descriptor. No
329 * deep reason for this other than to be able to identify a
330 * non-inited descriptor for debugging.
332 * If an object contains no references, its GC descriptor is
333 * always DESC_TYPE_RUN_LENGTH, without a size, no exceptions.
334 * This is so that we can quickly check for that in
335 * copy_object_no_checks(), without having to fetch the
338 DESC_TYPE_RUN_LENGTH = 1, /* 15 bits aligned byte size | 1-3 (offset, numptr) bytes tuples */
339 DESC_TYPE_SMALL_BITMAP, /* 15 bits aligned byte size | 16-48 bit bitmap */
340 DESC_TYPE_COMPLEX, /* index for bitmap into complex_descriptors */
341 DESC_TYPE_VECTOR, /* 10 bits element size | 1 bit array | 2 bits desc | element desc */
342 DESC_TYPE_ARRAY, /* 10 bits element size | 1 bit array | 2 bits desc | element desc */
343 DESC_TYPE_LARGE_BITMAP, /* | 29-61 bitmap bits */
344 DESC_TYPE_COMPLEX_ARR, /* index for bitmap into complex_descriptors */
345 /* subtypes for arrays and vectors */
346 DESC_TYPE_V_PTRFREE = 0,/* there are no refs: keep first so it has a zero value */
347 DESC_TYPE_V_REFS, /* all the array elements are refs */
348 DESC_TYPE_V_RUN_LEN, /* elements are run-length encoded as DESC_TYPE_RUN_LENGTH */
349 DESC_TYPE_V_BITMAP /* elements are as the bitmap in DESC_TYPE_SMALL_BITMAP */
352 #define SGEN_VTABLE_HAS_REFERENCES(vt) (((MonoVTable*)(vt))->gc_descr != (void*)DESC_TYPE_RUN_LENGTH)
353 #define SGEN_CLASS_HAS_REFERENCES(c) ((c)->gc_descr != (void*)DESC_TYPE_RUN_LENGTH)
355 /* helper macros to scan and traverse objects, macros because we resue them in many functions */
356 #define OBJ_RUN_LEN_SIZE(size,desc,obj) do { \
357 (size) = ((desc) & 0xfff8) >> 1; \
360 #define OBJ_BITMAP_SIZE(size,desc,obj) do { \
361 (size) = ((desc) & 0xfff8) >> 1; \
364 //#define PREFETCH(addr) __asm__ __volatile__ (" prefetchnta %0": : "m"(*(char *)(addr)))
365 #define PREFETCH(addr)
367 /* code using these macros must define a HANDLE_PTR(ptr) macro that does the work */
368 #define OBJ_RUN_LEN_FOREACH_PTR(desc,obj) do { \
369 if ((desc) & 0xffff0000) { \
370 /* there are pointers */ \
371 void **_objptr_end; \
372 void **_objptr = (void**)(obj); \
373 _objptr += ((desc) >> 16) & 0xff; \
374 _objptr_end = _objptr + (((desc) >> 24) & 0xff); \
375 while (_objptr < _objptr_end) { \
376 HANDLE_PTR (_objptr, (obj)); \
382 /* a bitmap desc means that there are pointer references or we'd have
383 * choosen run-length, instead: add an assert to check.
385 #define OBJ_BITMAP_FOREACH_PTR(desc,obj) do { \
386 /* there are pointers */ \
387 void **_objptr = (void**)(obj); \
388 gsize _bmap = (desc) >> 16; \
389 _objptr += OBJECT_HEADER_WORDS; \
392 HANDLE_PTR (_objptr, (obj)); \
399 #define OBJ_LARGE_BITMAP_FOREACH_PTR(vt,obj) do { \
400 /* there are pointers */ \
401 void **_objptr = (void**)(obj); \
402 gsize _bmap = (vt)->desc >> LOW_TYPE_BITS; \
403 _objptr += OBJECT_HEADER_WORDS; \
406 HANDLE_PTR (_objptr, (obj)); \
413 gsize* mono_sgen_get_complex_descriptor (GCVTable *vt) MONO_INTERNAL;
415 #define OBJ_COMPLEX_FOREACH_PTR(vt,obj) do { \
416 /* there are pointers */ \
417 void **_objptr = (void**)(obj); \
418 gsize *bitmap_data = mono_sgen_get_complex_descriptor ((vt)); \
419 int bwords = (*bitmap_data) - 1; \
420 void **start_run = _objptr; \
423 MonoObject *myobj = (MonoObject*)obj; \
424 g_print ("found %d at %p (0x%zx): %s.%s\n", bwords, (obj), (vt)->desc, myobj->vtable->klass->name_space, myobj->vtable->klass->name); \
426 while (bwords-- > 0) { \
427 gsize _bmap = *bitmap_data++; \
428 _objptr = start_run; \
429 /*g_print ("bitmap: 0x%x/%d at %p\n", _bmap, bwords, _objptr);*/ \
432 HANDLE_PTR (_objptr, (obj)); \
437 start_run += GC_BITS_PER_WORD; \
441 /* this one is untested */
442 #define OBJ_COMPLEX_ARR_FOREACH_PTR(vt,obj) do { \
443 /* there are pointers */ \
444 gsize *mbitmap_data = mono_sgen_get_complex_descriptor ((vt)); \
445 int mbwords = (*mbitmap_data++) - 1; \
446 int el_size = mono_array_element_size (vt->klass); \
447 char *e_start = (char*)(obj) + G_STRUCT_OFFSET (MonoArray, vector); \
448 char *e_end = e_start + el_size * mono_array_length_fast ((MonoArray*)(obj)); \
450 g_print ("found %d at %p (0x%zx): %s.%s\n", mbwords, (obj), (vt)->desc, vt->klass->name_space, vt->klass->name); \
451 while (e_start < e_end) { \
452 void **_objptr = (void**)e_start; \
453 gsize *bitmap_data = mbitmap_data; \
454 unsigned int bwords = mbwords; \
455 while (bwords-- > 0) { \
456 gsize _bmap = *bitmap_data++; \
457 void **start_run = _objptr; \
458 /*g_print ("bitmap: 0x%x\n", _bmap);*/ \
461 HANDLE_PTR (_objptr, (obj)); \
466 _objptr = start_run + GC_BITS_PER_WORD; \
468 e_start += el_size; \
472 #define OBJ_VECTOR_FOREACH_PTR(vt,obj) do { \
473 /* note: 0xffffc000 excludes DESC_TYPE_V_PTRFREE */ \
474 if ((vt)->desc & 0xffffc000) { \
475 int el_size = ((vt)->desc >> 3) & MAX_ELEMENT_SIZE; \
476 /* there are pointers */ \
477 int etype = (vt)->desc & 0xc000; \
478 if (etype == (DESC_TYPE_V_REFS << 14)) { \
479 void **p = (void**)((char*)(obj) + G_STRUCT_OFFSET (MonoArray, vector)); \
480 void **end_refs = (void**)((char*)p + el_size * mono_array_length_fast ((MonoArray*)(obj))); \
481 /* Note: this code can handle also arrays of struct with only references in them */ \
482 while (p < end_refs) { \
483 HANDLE_PTR (p, (obj)); \
486 } else if (etype == DESC_TYPE_V_RUN_LEN << 14) { \
487 int offset = ((vt)->desc >> 16) & 0xff; \
488 int num_refs = ((vt)->desc >> 24) & 0xff; \
489 char *e_start = (char*)(obj) + G_STRUCT_OFFSET (MonoArray, vector); \
490 char *e_end = e_start + el_size * mono_array_length_fast ((MonoArray*)(obj)); \
491 while (e_start < e_end) { \
492 void **p = (void**)e_start; \
495 for (i = 0; i < num_refs; ++i) { \
496 HANDLE_PTR (p + i, (obj)); \
498 e_start += el_size; \
500 } else if (etype == DESC_TYPE_V_BITMAP << 14) { \
501 char *e_start = (char*)(obj) + G_STRUCT_OFFSET (MonoArray, vector); \
502 char *e_end = e_start + el_size * mono_array_length_fast ((MonoArray*)(obj)); \
503 while (e_start < e_end) { \
504 void **p = (void**)e_start; \
505 gsize _bmap = (vt)->desc >> 16; \
506 /* Note: there is no object header here to skip */ \
509 HANDLE_PTR (p, (obj)); \
514 e_start += el_size; \
520 typedef struct _SgenInternalAllocator SgenInternalAllocator;
522 #define SGEN_GRAY_QUEUE_SECTION_SIZE (128 - 3)
525 * This is a stack now instead of a queue, so the most recently added items are removed
526 * first, improving cache locality, and keeping the stack size manageable.
528 typedef struct _GrayQueueSection GrayQueueSection;
529 struct _GrayQueueSection {
531 GrayQueueSection *next;
532 char *objects [SGEN_GRAY_QUEUE_SECTION_SIZE];
535 typedef struct _SgenGrayQueue SgenGrayQueue;
537 typedef void (*GrayQueueAllocPrepareFunc) (SgenGrayQueue*);
539 struct _SgenGrayQueue {
540 SgenInternalAllocator *allocator;
541 GrayQueueSection *first;
542 GrayQueueSection *free_list;
544 GrayQueueAllocPrepareFunc alloc_prepare_func;
545 void *alloc_prepare_data;
548 #if SGEN_MAX_DEBUG_LEVEL >= 9
549 #define GRAY_OBJECT_ENQUEUE gray_object_enqueue
550 #define GRAY_OBJECT_DEQUEUE(queue,o) ((o) = gray_object_dequeue ((queue)))
552 #define GRAY_OBJECT_ENQUEUE(queue,o) do { \
553 if (G_UNLIKELY (!(queue)->first || (queue)->first->end == SGEN_GRAY_QUEUE_SECTION_SIZE)) \
554 mono_sgen_gray_object_enqueue ((queue), (o)); \
556 (queue)->first->objects [(queue)->first->end++] = (o); \
558 #define GRAY_OBJECT_DEQUEUE(queue,o) do { \
559 if (!(queue)->first) \
561 else if (G_UNLIKELY ((queue)->first->end == 1)) \
562 (o) = mono_sgen_gray_object_dequeue ((queue)); \
564 (o) = (queue)->first->objects [--(queue)->first->end]; \
568 void mono_sgen_gray_object_enqueue (SgenGrayQueue *queue, char *obj) MONO_INTERNAL;
569 char* mono_sgen_gray_object_dequeue (SgenGrayQueue *queue) MONO_INTERNAL;
571 typedef void (*IterateObjectCallbackFunc) (char*, size_t, void*);
573 void* mono_sgen_alloc_os_memory (size_t size, int activate) MONO_INTERNAL;
574 void* mono_sgen_alloc_os_memory_aligned (mword size, mword alignment, gboolean activate) MONO_INTERNAL;
575 void mono_sgen_free_os_memory (void *addr, size_t size) MONO_INTERNAL;
577 int mono_sgen_thread_handshake (int signum) MONO_INTERNAL;
578 SgenThreadInfo* mono_sgen_thread_info_lookup (ARCH_THREAD_TYPE id) MONO_INTERNAL;
579 SgenThreadInfo** mono_sgen_get_thread_table (void) MONO_INTERNAL;
580 void mono_sgen_wait_for_suspend_ack (int count) MONO_INTERNAL;
582 gboolean mono_sgen_is_worker_thread (pthread_t thread) MONO_INTERNAL;
584 void mono_sgen_update_heap_boundaries (mword low, mword high) MONO_INTERNAL;
586 void mono_sgen_register_major_sections_alloced (int num_sections) MONO_INTERNAL;
587 mword mono_sgen_get_minor_collection_allowance (void) MONO_INTERNAL;
589 void mono_sgen_scan_area_with_callback (char *start, char *end, IterateObjectCallbackFunc callback, void *data, gboolean allow_flags) MONO_INTERNAL;
590 void mono_sgen_check_section_scan_starts (GCMemSection *section) MONO_INTERNAL;
592 /* Keep in sync with mono_sgen_dump_internal_mem_usage() in dump_heap()! */
594 INTERNAL_MEM_MANAGED,
595 INTERNAL_MEM_PIN_QUEUE,
596 INTERNAL_MEM_FRAGMENT,
597 INTERNAL_MEM_SECTION,
598 INTERNAL_MEM_SCAN_STARTS,
599 INTERNAL_MEM_FIN_TABLE,
600 INTERNAL_MEM_FINALIZE_ENTRY,
601 INTERNAL_MEM_DISLINK_TABLE,
602 INTERNAL_MEM_DISLINK,
603 INTERNAL_MEM_ROOTS_TABLE,
604 INTERNAL_MEM_ROOT_RECORD,
605 INTERNAL_MEM_STATISTICS,
607 INTERNAL_MEM_GRAY_QUEUE,
608 INTERNAL_MEM_STORE_REMSET,
609 INTERNAL_MEM_MS_TABLES,
610 INTERNAL_MEM_MS_BLOCK_INFO,
611 INTERNAL_MEM_EPHEMERON_LINK,
612 INTERNAL_MEM_WORKER_DATA,
613 INTERNAL_MEM_BRIDGE_DATA,
617 #define SGEN_INTERNAL_FREELIST_NUM_SLOTS 30
619 struct _SgenInternalAllocator {
620 SgenPinnedChunk *chunk_list;
621 SgenPinnedChunk *free_lists [SGEN_INTERNAL_FREELIST_NUM_SLOTS];
622 void *delayed_free_lists [SGEN_INTERNAL_FREELIST_NUM_SLOTS];
623 long small_internal_mem_bytes [INTERNAL_MEM_MAX];
632 void mono_sgen_init_internal_allocator (void) MONO_INTERNAL;
634 SgenInternalAllocator* mono_sgen_get_unmanaged_allocator (void) MONO_INTERNAL;
636 const char* mono_sgen_internal_mem_type_name (int type) MONO_INTERNAL;
637 void mono_sgen_report_internal_mem_usage (void) MONO_INTERNAL;
638 void mono_sgen_report_internal_mem_usage_full (SgenInternalAllocator *alc) MONO_INTERNAL;
639 void mono_sgen_dump_internal_mem_usage (FILE *heap_dump_file) MONO_INTERNAL;
640 void mono_sgen_dump_section (GCMemSection *section, const char *type) MONO_INTERNAL;
641 void mono_sgen_dump_occupied (char *start, char *end, char *section_start) MONO_INTERNAL;
643 void mono_sgen_register_moved_object (void *obj, void *destination) MONO_INTERNAL;
645 void mono_sgen_register_fixed_internal_mem_type (int type, size_t size) MONO_INTERNAL;
647 void* mono_sgen_alloc_internal (int type) MONO_INTERNAL;
648 void mono_sgen_free_internal (void *addr, int type) MONO_INTERNAL;
650 void* mono_sgen_alloc_internal_dynamic (size_t size, int type) MONO_INTERNAL;
651 void mono_sgen_free_internal_dynamic (void *addr, size_t size, int type) MONO_INTERNAL;
653 void* mono_sgen_alloc_internal_fixed (SgenInternalAllocator *allocator, int type) MONO_INTERNAL;
654 void mono_sgen_free_internal_fixed (SgenInternalAllocator *allocator, void *addr, int type) MONO_INTERNAL;
656 void* mono_sgen_alloc_internal_full (SgenInternalAllocator *allocator, size_t size, int type) MONO_INTERNAL;
657 void mono_sgen_free_internal_full (SgenInternalAllocator *allocator, void *addr, size_t size, int type) MONO_INTERNAL;
659 void mono_sgen_free_internal_delayed (void *addr, int type, SgenInternalAllocator *thread_allocator) MONO_INTERNAL;
661 void mono_sgen_debug_printf (int level, const char *format, ...) MONO_INTERNAL;
663 gboolean mono_sgen_parse_environment_string_extract_number (const char *str, glong *out) MONO_INTERNAL;
665 void mono_sgen_internal_scan_objects (SgenInternalAllocator *alc, IterateObjectCallbackFunc callback, void *callback_data) MONO_INTERNAL;
666 void mono_sgen_internal_scan_pinned_objects (SgenInternalAllocator *alc, IterateObjectCallbackFunc callback, void *callback_data) MONO_INTERNAL;
668 void mono_sgen_internal_update_heap_boundaries (SgenInternalAllocator *alc) MONO_INTERNAL;
670 void** mono_sgen_find_optimized_pin_queue_area (void *start, void *end, int *num) MONO_INTERNAL;
671 void mono_sgen_find_section_pin_queue_start_end (GCMemSection *section) MONO_INTERNAL;
672 void mono_sgen_pin_objects_in_section (GCMemSection *section, SgenGrayQueue *queue) MONO_INTERNAL;
674 void mono_sgen_pin_stats_register_object (char *obj, size_t size);
676 void mono_sgen_add_to_global_remset (gpointer ptr) MONO_INTERNAL;
678 int mono_sgen_get_current_collection_generation (void) MONO_INTERNAL;
680 typedef void (*sgen_cardtable_block_callback) (mword start, mword size);
682 typedef struct _SgenMajorCollector SgenMajorCollector;
683 struct _SgenMajorCollector {
685 gboolean is_parallel;
686 gboolean supports_cardtable;
689 * This is set to TRUE if the sweep for the last major
690 * collection has been completed.
692 gboolean *have_swept;
694 void* (*alloc_heap) (mword nursery_size, mword nursery_align, int nursery_bits);
695 gboolean (*is_object_live) (char *obj);
696 void* (*alloc_small_pinned_obj) (size_t size, gboolean has_references);
697 void* (*alloc_degraded) (MonoVTable *vtable, size_t size);
698 void (*copy_or_mark_object) (void **obj_slot, SgenGrayQueue *queue);
699 void (*minor_scan_object) (char *start, SgenGrayQueue *queue);
700 char* (*minor_scan_vtype) (char *start, mword desc, char* from_start, char* from_end, SgenGrayQueue *queue);
701 void (*major_scan_object) (char *start, SgenGrayQueue *queue);
702 void (*copy_object) (void **obj_slot, SgenGrayQueue *queue);
703 void* (*alloc_object) (int size, gboolean has_references);
704 void (*free_pinned_object) (char *obj, size_t size);
705 void (*iterate_objects) (gboolean non_pinned, gboolean pinned, IterateObjectCallbackFunc callback, void *data);
706 void (*free_non_pinned_object) (char *obj, size_t size);
707 void (*find_pin_queue_start_ends) (SgenGrayQueue *queue);
708 void (*pin_objects) (SgenGrayQueue *queue);
709 void (*scan_card_table) (SgenGrayQueue *queue);
710 void (*iterate_live_block_ranges) (sgen_cardtable_block_callback callback);
711 void (*init_to_space) (void);
712 void (*sweep) (void);
713 void (*check_scan_starts) (void);
714 void (*dump_heap) (FILE *heap_dump_file);
715 gint64 (*get_used_size) (void);
716 void (*start_nursery_collection) (void);
717 void (*finish_nursery_collection) (void);
718 void (*start_major_collection) (void);
719 void (*finish_major_collection) (void);
720 void (*have_computed_minor_collection_allowance) (void);
721 gboolean (*ptr_is_in_non_pinned_space) (char *ptr);
722 gboolean (*obj_is_from_pinned_alloc) (char *obj);
723 void (*report_pinned_memory_usage) (void);
724 int (*get_num_major_sections) (void);
725 gboolean (*handle_gc_param) (const char *opt);
726 void (*print_gc_param_usage) (void);
727 gboolean (*is_worker_thread) (pthread_t thread);
728 void (*post_param_init) (void);
731 void mono_sgen_marksweep_init (SgenMajorCollector *collector) MONO_INTERNAL;
732 void mono_sgen_marksweep_fixed_init (SgenMajorCollector *collector) MONO_INTERNAL;
733 void mono_sgen_marksweep_par_init (SgenMajorCollector *collector) MONO_INTERNAL;
734 void mono_sgen_marksweep_fixed_par_init (SgenMajorCollector *collector) MONO_INTERNAL;
735 void mono_sgen_copying_init (SgenMajorCollector *collector) MONO_INTERNAL;
738 * This function can be called on an object whose first word, the
739 * vtable field, is not intact. This is necessary for the parallel
743 mono_sgen_par_object_get_size (MonoVTable *vtable, MonoObject* o)
745 MonoClass *klass = vtable->klass;
747 * We depend on mono_string_length_fast and
748 * mono_array_length_fast not using the object's vtable.
750 if (klass == mono_defaults.string_class) {
751 return sizeof (MonoString) + 2 * mono_string_length_fast ((MonoString*) o) + 2;
752 } else if (klass->rank) {
753 MonoArray *array = (MonoArray*)o;
754 size_t size = sizeof (MonoArray) + klass->sizes.element_size * mono_array_length_fast (array);
755 if (G_UNLIKELY (array->bounds)) {
756 size += sizeof (mono_array_size_t) - 1;
757 size &= ~(sizeof (mono_array_size_t) - 1);
758 size += sizeof (MonoArrayBounds) * klass->rank;
762 /* from a created object: the class must be inited already */
763 return klass->instance_size;
767 #define mono_sgen_safe_object_get_size(o) mono_sgen_par_object_get_size ((MonoVTable*)SGEN_LOAD_VTABLE ((o)), (o))
769 const char* mono_sgen_safe_name (void* obj) MONO_INTERNAL;
771 gboolean mono_sgen_object_is_live (void *obj) MONO_INTERNAL;
773 gboolean mono_sgen_need_bridge_processing (void) MONO_INTERNAL;
774 void mono_sgen_bridge_processing (int num_objs, MonoObject **objs) MONO_INTERNAL;
781 gboolean mono_sgen_try_alloc_space (mword size, int space) MONO_INTERNAL;
782 void mono_sgen_release_space (mword size, int space) MONO_INTERNAL;
783 void mono_sgen_pin_object (void *object, SgenGrayQueue *queue) MONO_INTERNAL;
784 void sgen_collect_major_no_lock (const char *reason) MONO_INTERNAL;
785 gboolean mono_sgen_need_major_collection (mword space_needed) MONO_INTERNAL;
789 typedef struct _LOSObject LOSObject;
792 mword size; /* this is the object size */
794 int dummy; /* to have a sizeof (LOSObject) a multiple of ALLOC_ALIGN and data starting at same alignment */
795 char data [MONO_ZERO_LEN_ARRAY];
798 #define ARRAY_OBJ_INDEX(ptr,array,elem_size) (((char*)(ptr) - ((char*)(array) + G_STRUCT_OFFSET (MonoArray, vector))) / (elem_size))
800 extern LOSObject *los_object_list;
801 extern mword los_memory_usage;
802 extern mword last_los_memory_usage;
804 void mono_sgen_los_free_object (LOSObject *obj) MONO_INTERNAL;
805 void* mono_sgen_los_alloc_large_inner (MonoVTable *vtable, size_t size) MONO_INTERNAL;
806 void mono_sgen_los_sweep (void) MONO_INTERNAL;
807 gboolean mono_sgen_ptr_is_in_los (char *ptr, char **start) MONO_INTERNAL;
808 void mono_sgen_los_iterate_objects (IterateObjectCallbackFunc cb, void *user_data) MONO_INTERNAL;
809 void mono_sgen_los_iterate_live_block_ranges (sgen_cardtable_block_callback callback) MONO_INTERNAL;
810 void mono_sgen_los_scan_card_table (SgenGrayQueue *queue) MONO_INTERNAL;
811 FILE *mono_sgen_get_logfile (void) MONO_INTERNAL;
813 #endif /* HAVE_SGEN_GC */
815 #endif /* __MONO_SGENGC_H__ */