[sgen] One internal allocator per worker thread, to get rid of locking.
[mono-project/dkf.git] / mono / metadata / sgen-gc.h
blob880aeae5ce7d67e259d4154e41298820040338b8
1 /*
2 * Copyright 2001-2003 Ximian, Inc
3 * Copyright 2003-2010 Novell, Inc.
4 *
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__
27 /* pthread impl */
28 #include "config.h"
29 #include <glib.h>
30 #include <pthread.h>
31 #include <signal.h>
32 #include <mono/utils/mono-compiler.h>
33 #include <mono/metadata/class-internals.h>
34 #include <mono/metadata/object-internals.h>
36 #define SGEN_PARALLEL_MARK
39 * Turning on heavy statistics will turn off the managed allocator and
40 * the managed write barrier.
42 //#define HEAVY_STATISTICS
45 * If this is set, the nursery is aligned to an address aligned to its size, ie.
46 * a 1MB nursery will be aligned to an address divisible by 1MB. This allows us to
47 * speed up ptr_in_nursery () checks which are very frequent. This requires the
48 * nursery size to be a compile time constant.
50 #define SGEN_ALIGN_NURSERY 1
52 //#define SGEN_BINARY_PROTOCOL
54 #define SGEN_MAX_DEBUG_LEVEL 2
56 #define THREAD_HASH_SIZE 11
58 #define GC_BITS_PER_WORD (sizeof (mword) * 8)
60 #define ARCH_THREAD_TYPE pthread_t
61 #define ARCH_GET_THREAD pthread_self
62 #define ARCH_THREAD_EQUALS(a,b) pthread_equal (a, b)
64 #if SIZEOF_VOID_P == 4
65 typedef guint32 mword;
66 #else
67 typedef guint64 mword;
68 #endif
70 /* for use with write barriers */
71 typedef struct _RememberedSet RememberedSet;
72 struct _RememberedSet {
73 mword *store_next;
74 mword *end_set;
75 RememberedSet *next;
76 mword data [MONO_ZERO_LEN_ARRAY];
79 /* eventually share with MonoThread? */
80 typedef struct _SgenThreadInfo SgenThreadInfo;
82 struct _SgenThreadInfo {
83 SgenThreadInfo *next;
84 ARCH_THREAD_TYPE id;
85 unsigned int stop_count; /* to catch duplicate signals */
86 int signal;
87 int skip;
88 volatile int in_critical_region;
89 void *stack_end;
90 void *stack_start;
91 void *stack_start_limit;
92 char **tlab_next_addr;
93 char **tlab_start_addr;
94 char **tlab_temp_end_addr;
95 char **tlab_real_end_addr;
96 gpointer **store_remset_buffer_addr;
97 long *store_remset_buffer_index_addr;
98 RememberedSet *remset;
99 gpointer runtime_data;
100 gpointer stopped_ip; /* only valid if the thread is stopped */
101 MonoDomain *stopped_domain; /* ditto */
102 gpointer *stopped_regs; /* ditto */
103 #ifndef HAVE_KW_THREAD
104 char *tlab_start;
105 char *tlab_next;
106 char *tlab_temp_end;
107 char *tlab_real_end;
108 gpointer *store_remset_buffer;
109 long store_remset_buffer_index;
110 #endif
113 enum {
114 MEMORY_ROLE_GEN0,
115 MEMORY_ROLE_GEN1,
116 MEMORY_ROLE_PINNED,
117 MEMORY_ROLE_INTERNAL
120 typedef struct _SgenBlock SgenBlock;
121 struct _SgenBlock {
122 void *next;
123 unsigned char role;
127 * The nursery section and the major copying collector's sections use
128 * this struct.
130 typedef struct _GCMemSection GCMemSection;
131 struct _GCMemSection {
132 SgenBlock block;
133 char *data;
134 mword size;
135 /* pointer where more data could be allocated if it fits */
136 char *next_data;
137 char *end_data;
139 * scan starts is an array of pointers to objects equally spaced in the allocation area
140 * They let use quickly find pinned objects from pinning pointers.
142 char **scan_starts;
143 /* in major collections indexes in the pin_queue for objects that pin this section */
144 void **pin_queue_start;
145 int pin_queue_num_entries;
146 unsigned short num_scan_start;
147 gboolean is_to_space;
150 #define SGEN_SIZEOF_GC_MEM_SECTION ((sizeof (GCMemSection) + 7) & ~7)
153 * to quickly find the head of an object pinned by a conservative
154 * address we keep track of the objects allocated for each
155 * SGEN_SCAN_START_SIZE memory chunk in the nursery or other memory
156 * sections. Larger values have less memory overhead and bigger
157 * runtime cost. 4-8 KB are reasonable values.
159 #define SGEN_SCAN_START_SIZE (4096*2)
162 * Objects bigger then this go into the large object space. This size
163 * has a few constraints. It must fit into the major heap, which in
164 * the case of the copying collector means that it must fit into a
165 * pinned chunk. It must also play well with the GC descriptors, some
166 * of which (DESC_TYPE_RUN_LENGTH, DESC_TYPE_SMALL_BITMAP) encode the
167 * object size.
169 #define SGEN_MAX_SMALL_OBJ_SIZE 8000
171 /* This is also the MAJOR_SECTION_SIZE for the copying major
172 collector */
173 #define SGEN_PINNED_CHUNK_SIZE (128 * 1024)
175 #define SGEN_PINNED_CHUNK_FOR_PTR(o) ((SgenBlock*)(((mword)(o)) & ~(SGEN_PINNED_CHUNK_SIZE - 1)))
177 typedef struct _SgenPinnedChunk SgenPinnedChunk;
179 #ifdef __APPLE__
180 const static int suspend_signal_num = SIGXFSZ;
181 #else
182 const static int suspend_signal_num = SIGPWR;
183 #endif
184 const static int restart_signal_num = SIGXCPU;
187 * Recursion is not allowed for the thread lock.
189 #define LOCK_DECLARE(name) pthread_mutex_t name = PTHREAD_MUTEX_INITIALIZER
190 #define LOCK_INIT(name)
191 #define LOCK_GC pthread_mutex_lock (&gc_mutex)
192 #define UNLOCK_GC pthread_mutex_unlock (&gc_mutex)
193 #define LOCK_INTERRUPTION pthread_mutex_lock (&interruption_mutex)
194 #define UNLOCK_INTERRUPTION pthread_mutex_unlock (&interruption_mutex)
196 #ifdef SGEN_PARALLEL_MARK
197 #define LOCK_GLOBAL_REMSET pthread_mutex_lock (&global_remset_mutex)
198 #define UNLOCK_GLOBAL_REMSET pthread_mutex_unlock (&global_remset_mutex)
199 #else
200 #define LOCK_GLOBAL_REMSET
201 #define UNLOCK_GLOBAL_REMSET
202 #endif
204 #ifdef SGEN_PARALLEL_MARK
205 #define SGEN_CAS_PTR InterlockedCompareExchangePointer
206 #define SGEN_ATOMIC_ADD(x,i) do { \
207 int __old_x; \
208 do { \
209 __old_x = (x); \
210 } while (InterlockedCompareExchange (&(x), __old_x + (i), __old_x) != __old_x); \
211 } while (0)
212 #else
213 #define SGEN_CAS_PTR(p,n,c) ((*(void**)(p) == (void*)(c)) ? (*(void**)(p) = (void*)(n), (void*)(c)) : (*(void**)(p)))
214 #define SGEN_ATOMIC_ADD(x,i) ((x) += (i))
215 #endif
217 /* non-pthread will need to provide their own version of start/stop */
218 #define USE_SIGNAL_BASED_START_STOP_WORLD 1
219 /* we intercept pthread_create calls to know which threads exist */
220 #define USE_PTHREAD_INTERCEPT 1
222 #ifdef HEAVY_STATISTICS
223 #define HEAVY_STAT(x) x
225 extern long long stat_objects_alloced_degraded;
226 extern long long stat_bytes_alloced_degraded;
227 extern long long stat_copy_object_called_major;
228 extern long long stat_objects_copied_major;
229 #else
230 #define HEAVY_STAT(x)
231 #endif
233 #define SGEN_ALLOC_ALIGN 8
234 #define SGEN_ALLOC_ALIGN_BITS 3
236 #define SGEN_ALIGN_UP(s) (((s)+(SGEN_ALLOC_ALIGN-1)) & ~(SGEN_ALLOC_ALIGN-1))
238 #ifdef SGEN_ALIGN_NURSERY
239 #define SGEN_PTR_IN_NURSERY(p,bits,start,end) (((mword)(p) & ~((1 << (bits)) - 1)) == (mword)(start))
240 #else
241 #define SGEN_PTR_IN_NURSERY(p,bits,start,end) ((char*)(p) >= (start) && (char*)(p) < (end))
242 #endif
244 /* Structure that corresponds to a MonoVTable: desc is a mword so requires
245 * no cast from a pointer to an integer
247 typedef struct {
248 MonoClass *klass;
249 mword desc;
250 } GCVTable;
252 /* these bits are set in the object vtable: we could merge them since an object can be
253 * either pinned or forwarded but not both.
254 * We store them in the vtable slot because the bits are used in the sync block for
255 * other purposes: if we merge them and alloc the sync blocks aligned to 8 bytes, we can change
256 * this and use bit 3 in the syncblock (with the lower two bits both set for forwarded, that
257 * would be an invalid combination for the monitor and hash code).
258 * The values are already shifted.
259 * The forwarding address is stored in the sync block.
261 #define SGEN_FORWARDED_BIT 1
262 #define SGEN_PINNED_BIT 2
263 #define SGEN_VTABLE_BITS_MASK 0x3
265 /* returns NULL if not forwarded, or the forwarded address */
266 #define SGEN_OBJECT_IS_FORWARDED(obj) (((mword*)(obj))[0] & SGEN_FORWARDED_BIT ? (void*)(((mword*)(obj))[0] & ~SGEN_VTABLE_BITS_MASK) : NULL)
267 #define SGEN_OBJECT_IS_PINNED(obj) (((mword*)(obj))[0] & SGEN_PINNED_BIT)
269 /* set the forwarded address fw_addr for object obj */
270 #define SGEN_FORWARD_OBJECT(obj,fw_addr) do { \
271 ((mword*)(obj))[0] = (mword)(fw_addr) | SGEN_FORWARDED_BIT; \
272 } while (0)
273 #define SGEN_PIN_OBJECT(obj) do { \
274 ((mword*)(obj))[0] |= SGEN_PINNED_BIT; \
275 } while (0)
276 #define SGEN_UNPIN_OBJECT(obj) do { \
277 ((mword*)(obj))[0] &= ~SGEN_PINNED_BIT; \
278 } while (0)
281 * Since we set bits in the vtable, use the macro to load it from the pointer to
282 * an object that is potentially pinned.
284 #define SGEN_LOAD_VTABLE(addr) ((*(mword*)(addr)) & ~SGEN_VTABLE_BITS_MASK)
287 * ######################################################################
288 * ######## GC descriptors
289 * ######################################################################
290 * Used to quickly get the info the GC needs about an object: size and
291 * where the references are held.
293 #define OBJECT_HEADER_WORDS (sizeof(MonoObject)/sizeof(gpointer))
294 #define LOW_TYPE_BITS 3
295 #define SMALL_BITMAP_SHIFT 16
296 #define SMALL_BITMAP_SIZE (GC_BITS_PER_WORD - SMALL_BITMAP_SHIFT)
297 #define VECTOR_INFO_SHIFT 14
298 #define VECTOR_ELSIZE_SHIFT 3
299 #define LARGE_BITMAP_SIZE (GC_BITS_PER_WORD - LOW_TYPE_BITS)
300 #define MAX_ELEMENT_SIZE 0x3ff
301 #define VECTOR_SUBTYPE_PTRFREE (DESC_TYPE_V_PTRFREE << VECTOR_INFO_SHIFT)
302 #define VECTOR_SUBTYPE_REFS (DESC_TYPE_V_REFS << VECTOR_INFO_SHIFT)
303 #define VECTOR_SUBTYPE_RUN_LEN (DESC_TYPE_V_RUN_LEN << VECTOR_INFO_SHIFT)
304 #define VECTOR_SUBTYPE_BITMAP (DESC_TYPE_V_BITMAP << VECTOR_INFO_SHIFT)
306 /* objects are aligned to 8 bytes boundaries
307 * A descriptor is a pointer in MonoVTable, so 32 or 64 bits of size.
308 * The low 3 bits define the type of the descriptor. The other bits
309 * depend on the type.
310 * As a general rule the 13 remaining low bits define the size, either
311 * of the whole object or of the elements in the arrays. While for objects
312 * the size is already in bytes, for arrays we need to shift, because
313 * array elements might be smaller than 8 bytes. In case of arrays, we
314 * use two bits to describe what the additional high bits represents,
315 * so the default behaviour can handle element sizes less than 2048 bytes.
316 * The high 16 bits, if 0 it means the object is pointer-free.
317 * This design should make it easy and fast to skip over ptr-free data.
318 * The first 4 types should cover >95% of the objects.
319 * Note that since the size of objects is limited to 64K, larger objects
320 * will be allocated in the large object heap.
321 * If we want 4-bytes alignment, we need to put vector and small bitmap
322 * inside complex.
324 enum {
326 * We don't use 0 so that 0 isn't a valid GC descriptor. No
327 * deep reason for this other than to be able to identify a
328 * non-inited descriptor for debugging.
330 * If an object contains no references, its GC descriptor is
331 * always DESC_TYPE_RUN_LENGTH, without a size, no exceptions.
332 * This is so that we can quickly check for that in
333 * copy_object_no_checks(), without having to fetch the
334 * object's class.
336 DESC_TYPE_RUN_LENGTH = 1, /* 15 bits aligned byte size | 1-3 (offset, numptr) bytes tuples */
337 DESC_TYPE_SMALL_BITMAP, /* 15 bits aligned byte size | 16-48 bit bitmap */
338 DESC_TYPE_COMPLEX, /* index for bitmap into complex_descriptors */
339 DESC_TYPE_VECTOR, /* 10 bits element size | 1 bit array | 2 bits desc | element desc */
340 DESC_TYPE_ARRAY, /* 10 bits element size | 1 bit array | 2 bits desc | element desc */
341 DESC_TYPE_LARGE_BITMAP, /* | 29-61 bitmap bits */
342 DESC_TYPE_COMPLEX_ARR, /* index for bitmap into complex_descriptors */
343 /* subtypes for arrays and vectors */
344 DESC_TYPE_V_PTRFREE = 0,/* there are no refs: keep first so it has a zero value */
345 DESC_TYPE_V_REFS, /* all the array elements are refs */
346 DESC_TYPE_V_RUN_LEN, /* elements are run-length encoded as DESC_TYPE_RUN_LENGTH */
347 DESC_TYPE_V_BITMAP /* elements are as the bitmap in DESC_TYPE_SMALL_BITMAP */
350 #define SGEN_VTABLE_HAS_REFERENCES(vt) (((MonoVTable*)(vt))->gc_descr != (void*)DESC_TYPE_RUN_LENGTH)
352 /* helper macros to scan and traverse objects, macros because we resue them in many functions */
353 #define OBJ_RUN_LEN_SIZE(size,desc,obj) do { \
354 (size) = ((desc) & 0xfff8) >> 1; \
355 } while (0)
357 #define OBJ_BITMAP_SIZE(size,desc,obj) do { \
358 (size) = ((desc) & 0xfff8) >> 1; \
359 } while (0)
361 //#define PREFETCH(addr) __asm__ __volatile__ (" prefetchnta %0": : "m"(*(char *)(addr)))
362 #define PREFETCH(addr)
364 /* code using these macros must define a HANDLE_PTR(ptr) macro that does the work */
365 #define OBJ_RUN_LEN_FOREACH_PTR(desc,obj) do { \
366 if ((desc) & 0xffff0000) { \
367 /* there are pointers */ \
368 void **_objptr_end; \
369 void **_objptr = (void**)(obj); \
370 _objptr += ((desc) >> 16) & 0xff; \
371 _objptr_end = _objptr + (((desc) >> 24) & 0xff); \
372 while (_objptr < _objptr_end) { \
373 HANDLE_PTR (_objptr, (obj)); \
374 _objptr++; \
377 } while (0)
379 /* a bitmap desc means that there are pointer references or we'd have
380 * choosen run-length, instead: add an assert to check.
382 #define OBJ_BITMAP_FOREACH_PTR(desc,obj) do { \
383 /* there are pointers */ \
384 void **_objptr = (void**)(obj); \
385 gsize _bmap = (desc) >> 16; \
386 _objptr += OBJECT_HEADER_WORDS; \
387 while (_bmap) { \
388 if ((_bmap & 1)) { \
389 HANDLE_PTR (_objptr, (obj)); \
391 _bmap >>= 1; \
392 ++_objptr; \
394 } while (0)
396 #define OBJ_LARGE_BITMAP_FOREACH_PTR(vt,obj) do { \
397 /* there are pointers */ \
398 void **_objptr = (void**)(obj); \
399 gsize _bmap = (vt)->desc >> LOW_TYPE_BITS; \
400 _objptr += OBJECT_HEADER_WORDS; \
401 while (_bmap) { \
402 if ((_bmap & 1)) { \
403 HANDLE_PTR (_objptr, (obj)); \
405 _bmap >>= 1; \
406 ++_objptr; \
408 } while (0)
410 gsize* mono_sgen_get_complex_descriptor (GCVTable *vt) MONO_INTERNAL;
412 #define OBJ_COMPLEX_FOREACH_PTR(vt,obj) do { \
413 /* there are pointers */ \
414 void **_objptr = (void**)(obj); \
415 gsize *bitmap_data = mono_sgen_get_complex_descriptor ((vt)); \
416 int bwords = (*bitmap_data) - 1; \
417 void **start_run = _objptr; \
418 bitmap_data++; \
419 if (0) { \
420 MonoObject *myobj = (MonoObject*)obj; \
421 g_print ("found %d at %p (0x%zx): %s.%s\n", bwords, (obj), (vt)->desc, myobj->vtable->klass->name_space, myobj->vtable->klass->name); \
423 while (bwords-- > 0) { \
424 gsize _bmap = *bitmap_data++; \
425 _objptr = start_run; \
426 /*g_print ("bitmap: 0x%x/%d at %p\n", _bmap, bwords, _objptr);*/ \
427 while (_bmap) { \
428 if ((_bmap & 1)) { \
429 HANDLE_PTR (_objptr, (obj)); \
431 _bmap >>= 1; \
432 ++_objptr; \
434 start_run += GC_BITS_PER_WORD; \
436 } while (0)
438 /* this one is untested */
439 #define OBJ_COMPLEX_ARR_FOREACH_PTR(vt,obj) do { \
440 /* there are pointers */ \
441 gsize *mbitmap_data = mono_sgen_get_complex_descriptor ((vt)); \
442 int mbwords = (*mbitmap_data++) - 1; \
443 int el_size = mono_array_element_size (vt->klass); \
444 char *e_start = (char*)(obj) + G_STRUCT_OFFSET (MonoArray, vector); \
445 char *e_end = e_start + el_size * mono_array_length_fast ((MonoArray*)(obj)); \
446 if (0) \
447 g_print ("found %d at %p (0x%zx): %s.%s\n", mbwords, (obj), (vt)->desc, vt->klass->name_space, vt->klass->name); \
448 while (e_start < e_end) { \
449 void **_objptr = (void**)e_start; \
450 gsize *bitmap_data = mbitmap_data; \
451 unsigned int bwords = mbwords; \
452 while (bwords-- > 0) { \
453 gsize _bmap = *bitmap_data++; \
454 void **start_run = _objptr; \
455 /*g_print ("bitmap: 0x%x\n", _bmap);*/ \
456 while (_bmap) { \
457 if ((_bmap & 1)) { \
458 HANDLE_PTR (_objptr, (obj)); \
460 _bmap >>= 1; \
461 ++_objptr; \
463 _objptr = start_run + GC_BITS_PER_WORD; \
465 e_start += el_size; \
467 } while (0)
469 #define OBJ_VECTOR_FOREACH_PTR(vt,obj) do { \
470 /* note: 0xffffc000 excludes DESC_TYPE_V_PTRFREE */ \
471 if ((vt)->desc & 0xffffc000) { \
472 int el_size = ((vt)->desc >> 3) & MAX_ELEMENT_SIZE; \
473 /* there are pointers */ \
474 int etype = (vt)->desc & 0xc000; \
475 if (etype == (DESC_TYPE_V_REFS << 14)) { \
476 void **p = (void**)((char*)(obj) + G_STRUCT_OFFSET (MonoArray, vector)); \
477 void **end_refs = (void**)((char*)p + el_size * mono_array_length_fast ((MonoArray*)(obj))); \
478 /* Note: this code can handle also arrays of struct with only references in them */ \
479 while (p < end_refs) { \
480 HANDLE_PTR (p, (obj)); \
481 ++p; \
483 } else if (etype == DESC_TYPE_V_RUN_LEN << 14) { \
484 int offset = ((vt)->desc >> 16) & 0xff; \
485 int num_refs = ((vt)->desc >> 24) & 0xff; \
486 char *e_start = (char*)(obj) + G_STRUCT_OFFSET (MonoArray, vector); \
487 char *e_end = e_start + el_size * mono_array_length_fast ((MonoArray*)(obj)); \
488 while (e_start < e_end) { \
489 void **p = (void**)e_start; \
490 int i; \
491 p += offset; \
492 for (i = 0; i < num_refs; ++i) { \
493 HANDLE_PTR (p + i, (obj)); \
495 e_start += el_size; \
497 } else if (etype == DESC_TYPE_V_BITMAP << 14) { \
498 char *e_start = (char*)(obj) + G_STRUCT_OFFSET (MonoArray, vector); \
499 char *e_end = e_start + el_size * mono_array_length_fast ((MonoArray*)(obj)); \
500 while (e_start < e_end) { \
501 void **p = (void**)e_start; \
502 gsize _bmap = (vt)->desc >> 16; \
503 /* Note: there is no object header here to skip */ \
504 while (_bmap) { \
505 if ((_bmap & 1)) { \
506 HANDLE_PTR (p, (obj)); \
508 _bmap >>= 1; \
509 ++p; \
511 e_start += el_size; \
515 } while (0)
517 typedef struct _SgenInternalAllocator SgenInternalAllocator;
519 #define SGEN_GRAY_QUEUE_SECTION_SIZE (128 - 3)
522 * This is a stack now instead of a queue, so the most recently added items are removed
523 * first, improving cache locality, and keeping the stack size manageable.
525 typedef struct _GrayQueueSection GrayQueueSection;
526 struct _GrayQueueSection {
527 int end;
528 GrayQueueSection *next;
529 char *objects [SGEN_GRAY_QUEUE_SECTION_SIZE];
532 typedef struct _SgenGrayQueue SgenGrayQueue;
534 typedef void (*GrayQueueAllocPrepareFunc) (SgenGrayQueue*);
536 struct _SgenGrayQueue {
537 SgenInternalAllocator *allocator;
538 GrayQueueSection *first;
539 GrayQueueSection *free_list;
540 int balance;
541 GrayQueueAllocPrepareFunc alloc_prepare_func;
542 void *alloc_prepare_data;
545 #if SGEN_MAX_DEBUG_LEVEL >= 9
546 #define GRAY_OBJECT_ENQUEUE gray_object_enqueue
547 #define GRAY_OBJECT_DEQUEUE(queue,o) ((o) = gray_object_dequeue ((queue)))
548 #else
549 #define GRAY_OBJECT_ENQUEUE(queue,o) do { \
550 if (G_UNLIKELY (!(queue)->first || (queue)->first->end == SGEN_GRAY_QUEUE_SECTION_SIZE)) \
551 mono_sgen_gray_object_enqueue ((queue), (o)); \
552 else \
553 (queue)->first->objects [(queue)->first->end++] = (o); \
554 } while (0)
555 #define GRAY_OBJECT_DEQUEUE(queue,o) do { \
556 if (!(queue)->first) \
557 (o) = NULL; \
558 else if (G_UNLIKELY ((queue)->first->end == 1)) \
559 (o) = mono_sgen_gray_object_dequeue ((queue)); \
560 else \
561 (o) = (queue)->first->objects [--(queue)->first->end]; \
562 } while (0)
563 #endif
565 void mono_sgen_gray_object_enqueue (SgenGrayQueue *queue, char *obj) MONO_INTERNAL;
566 char* mono_sgen_gray_object_dequeue (SgenGrayQueue *queue) MONO_INTERNAL;
568 typedef void (*IterateObjectCallbackFunc) (char*, size_t, void*);
570 void* mono_sgen_alloc_os_memory (size_t size, int activate) MONO_INTERNAL;
571 void* mono_sgen_alloc_os_memory_aligned (mword size, mword alignment, gboolean activate) MONO_INTERNAL;
572 void mono_sgen_free_os_memory (void *addr, size_t size) MONO_INTERNAL;
574 int mono_sgen_thread_handshake (int signum) MONO_INTERNAL;
575 SgenThreadInfo* mono_sgen_thread_info_lookup (ARCH_THREAD_TYPE id) MONO_INTERNAL;
576 SgenThreadInfo** mono_sgen_get_thread_table (void) MONO_INTERNAL;
577 void mono_sgen_wait_for_suspend_ack (int count) MONO_INTERNAL;
579 gboolean mono_sgen_is_worker_thread (pthread_t thread) MONO_INTERNAL;
581 void mono_sgen_update_heap_boundaries (mword low, mword high) MONO_INTERNAL;
583 void mono_sgen_register_major_sections_alloced (int num_sections) MONO_INTERNAL;
584 mword mono_sgen_get_minor_collection_allowance (void) MONO_INTERNAL;
586 void mono_sgen_scan_area_with_callback (char *start, char *end, IterateObjectCallbackFunc callback, void *data) MONO_INTERNAL;
587 void mono_sgen_check_section_scan_starts (GCMemSection *section) MONO_INTERNAL;
589 /* Keep in sync with mono_sgen_dump_internal_mem_usage() in dump_heap()! */
590 enum {
591 INTERNAL_MEM_MANAGED,
592 INTERNAL_MEM_PIN_QUEUE,
593 INTERNAL_MEM_FRAGMENT,
594 INTERNAL_MEM_SECTION,
595 INTERNAL_MEM_SCAN_STARTS,
596 INTERNAL_MEM_FIN_TABLE,
597 INTERNAL_MEM_FINALIZE_ENTRY,
598 INTERNAL_MEM_DISLINK_TABLE,
599 INTERNAL_MEM_DISLINK,
600 INTERNAL_MEM_ROOTS_TABLE,
601 INTERNAL_MEM_ROOT_RECORD,
602 INTERNAL_MEM_STATISTICS,
603 INTERNAL_MEM_REMSET,
604 INTERNAL_MEM_GRAY_QUEUE,
605 INTERNAL_MEM_STORE_REMSET,
606 INTERNAL_MEM_MS_TABLES,
607 INTERNAL_MEM_MS_BLOCK_INFO,
608 INTERNAL_MEM_EPHEMERON_LINK,
609 INTERNAL_MEM_WORKER_DATA,
610 INTERNAL_MEM_MAX
613 #define SGEN_INTERNAL_FREELIST_NUM_SLOTS 30
615 struct _SgenInternalAllocator {
616 SgenPinnedChunk *chunk_list;
617 SgenPinnedChunk *free_lists [SGEN_INTERNAL_FREELIST_NUM_SLOTS];
618 void *delayed_free_lists [SGEN_INTERNAL_FREELIST_NUM_SLOTS];
619 long small_internal_mem_bytes [INTERNAL_MEM_MAX];
622 void mono_sgen_init_internal_allocator (void) MONO_INTERNAL;
624 SgenInternalAllocator* mono_sgen_get_unmanaged_allocator (void) MONO_INTERNAL;
626 const char* mono_sgen_internal_mem_type_name (int type) MONO_INTERNAL;
627 void mono_sgen_report_internal_mem_usage (void) MONO_INTERNAL;
628 void mono_sgen_report_internal_mem_usage_full (SgenInternalAllocator *alc) MONO_INTERNAL;
629 void mono_sgen_dump_internal_mem_usage (FILE *heap_dump_file) MONO_INTERNAL;
630 void mono_sgen_dump_section (GCMemSection *section, const char *type) MONO_INTERNAL;
631 void mono_sgen_dump_occupied (char *start, char *end, char *section_start) MONO_INTERNAL;
633 void mono_sgen_register_moved_object (void *obj, void *destination) MONO_INTERNAL;
635 void mono_sgen_register_fixed_internal_mem_type (int type, size_t size) MONO_INTERNAL;
637 void* mono_sgen_alloc_internal (int type) MONO_INTERNAL;
638 void mono_sgen_free_internal (void *addr, int type) MONO_INTERNAL;
640 void* mono_sgen_alloc_internal_dynamic (size_t size, int type) MONO_INTERNAL;
641 void mono_sgen_free_internal_dynamic (void *addr, size_t size, int type) MONO_INTERNAL;
643 void* mono_sgen_alloc_internal_fixed (SgenInternalAllocator *allocator, int type) MONO_INTERNAL;
644 void mono_sgen_free_internal_fixed (SgenInternalAllocator *allocator, void *addr, int type) MONO_INTERNAL;
646 void* mono_sgen_alloc_internal_full (SgenInternalAllocator *allocator, size_t size, int type) MONO_INTERNAL;
647 void mono_sgen_free_internal_full (SgenInternalAllocator *allocator, void *addr, size_t size, int type) MONO_INTERNAL;
649 void mono_sgen_free_internal_delayed (void *addr, int type, SgenInternalAllocator *thread_allocator) MONO_INTERNAL;
651 void mono_sgen_debug_printf (int level, const char *format, ...) MONO_INTERNAL;
653 void mono_sgen_internal_scan_objects (SgenInternalAllocator *alc, IterateObjectCallbackFunc callback, void *callback_data) MONO_INTERNAL;
654 void mono_sgen_internal_scan_pinned_objects (SgenInternalAllocator *alc, IterateObjectCallbackFunc callback, void *callback_data) MONO_INTERNAL;
656 void** mono_sgen_find_optimized_pin_queue_area (void *start, void *end, int *num) MONO_INTERNAL;
657 void mono_sgen_find_section_pin_queue_start_end (GCMemSection *section) MONO_INTERNAL;
658 void mono_sgen_pin_objects_in_section (GCMemSection *section, SgenGrayQueue *queue) MONO_INTERNAL;
660 void mono_sgen_pin_stats_register_object (char *obj, size_t size);
662 void mono_sgen_add_to_global_remset (gpointer ptr) MONO_INTERNAL;
664 typedef struct _SgenMajorCollector SgenMajorCollector;
665 struct _SgenMajorCollector {
666 size_t section_size;
668 gboolean (*is_object_live) (char *obj);
669 void* (*alloc_small_pinned_obj) (size_t size, gboolean has_references);
670 void* (*alloc_degraded) (MonoVTable *vtable, size_t size);
671 void (*copy_or_mark_object) (void **obj_slot, SgenGrayQueue *queue);
672 void (*minor_scan_object) (char *start, SgenGrayQueue *queue);
673 char* (*minor_scan_vtype) (char *start, mword desc, char* from_start, char* from_end, SgenGrayQueue *queue);
674 void (*major_scan_object) (char *start, SgenGrayQueue *queue);
675 void (*copy_object) (void **obj_slot, SgenGrayQueue *queue);
676 void* (*alloc_object) (int size, gboolean has_references);
677 void (*free_pinned_object) (char *obj, size_t size);
678 void (*iterate_objects) (gboolean non_pinned, gboolean pinned, IterateObjectCallbackFunc callback, void *data);
679 void (*free_non_pinned_object) (char *obj, size_t size);
680 void (*find_pin_queue_start_ends) (SgenGrayQueue *queue);
681 void (*pin_objects) (SgenGrayQueue *queue);
682 void (*init_to_space) (void);
683 void (*sweep) (void);
684 void (*check_scan_starts) (void);
685 void (*dump_heap) (FILE *heap_dump_file);
686 gint64 (*get_used_size) (void);
687 void (*start_nursery_collection) (void);
688 void (*finish_nursery_collection) (void);
689 void (*finish_major_collection) (void);
690 gboolean (*ptr_is_in_non_pinned_space) (char *ptr);
691 gboolean (*obj_is_from_pinned_alloc) (char *obj);
692 void (*report_pinned_memory_usage) (void);
693 int (*get_num_major_sections) (void);
696 void mono_sgen_marksweep_init (SgenMajorCollector *collector, int nursery_bits, char *nursery_start, char *nursery_end) MONO_INTERNAL;
697 void mono_sgen_copying_init (SgenMajorCollector *collector, int the_nursery_bits, char *the_nursery_start, char *the_nursery_end) MONO_INTERNAL;
700 * This function can be called on an object whose first word, the
701 * vtable field, is not intact. This is necessary for the parallel
702 * collector.
704 static inline guint
705 mono_sgen_par_object_get_size (MonoVTable *vtable, MonoObject* o)
707 MonoClass *klass = vtable->klass;
709 * We depend on mono_string_length_fast and
710 * mono_array_length_fast not using the object's vtable.
712 if (klass == mono_defaults.string_class) {
713 return sizeof (MonoString) + 2 * mono_string_length_fast ((MonoString*) o) + 2;
714 } else if (klass->rank) {
715 MonoArray *array = (MonoArray*)o;
716 size_t size = sizeof (MonoArray) + klass->sizes.element_size * mono_array_length_fast (array);
717 if (G_UNLIKELY (array->bounds)) {
718 size += sizeof (mono_array_size_t) - 1;
719 size &= ~(sizeof (mono_array_size_t) - 1);
720 size += sizeof (MonoArrayBounds) * klass->rank;
722 return size;
723 } else {
724 /* from a created object: the class must be inited already */
725 return klass->instance_size;
729 #define mono_sgen_safe_object_get_size(o) mono_sgen_par_object_get_size ((MonoVTable*)SGEN_LOAD_VTABLE ((o)), (o))
731 #endif /* __MONO_SGENGC_H__ */