2 * sgen-alloc.c: Object allocation routines + managed allocators
5 * Paolo Molaro (lupus@ximian.com)
6 * Rodrigo Kumpera (kumpera@gmail.com)
8 * Copyright 2005-2011 Novell, Inc (http://www.novell.com)
9 * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
10 * Copyright 2011 Xamarin, Inc.
12 * Permission is hereby granted, free of charge, to any person obtaining
13 * a copy of this software and associated documentation files (the
14 * "Software"), to deal in the Software without restriction, including
15 * without limitation the rights to use, copy, modify, merge, publish,
16 * distribute, sublicense, and/or sell copies of the Software, and to
17 * permit persons to whom the Software is furnished to do so, subject to
18 * the following conditions:
20 * The above copyright notice and this permission notice shall be
21 * included in all copies or substantial portions of the Software.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 * ######################################################################
34 * ######## Object allocation
35 * ######################################################################
36 * This section of code deals with allocating memory for objects.
37 * There are several ways:
38 * *) allocate large objects
39 * *) allocate normal objects
40 * *) fast lock-free allocation
41 * *) allocation of pinned objects
47 #include "metadata/sgen-gc.h"
48 #include "metadata/sgen-protocol.h"
49 #include "metadata/sgen-memory-governor.h"
50 #include "metadata/profiler-private.h"
51 #include "metadata/marshal.h"
52 #include "metadata/method-builder.h"
53 #include "utils/mono-memory-model.h"
54 #include "utils/mono-counters.h"
56 #define ALIGN_UP SGEN_ALIGN_UP
57 #define ALLOC_ALIGN SGEN_ALLOC_ALIGN
58 #define ALLOC_ALIGN_BITS SGEN_ALLOC_ALIGN_BITS
59 #define MAX_SMALL_OBJ_SIZE SGEN_MAX_SMALL_OBJ_SIZE
60 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
62 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
66 #include "mono/cil/opcode.def"
72 static gboolean use_managed_allocator = TRUE;
74 #ifdef HEAVY_STATISTICS
75 static long long stat_objects_alloced = 0;
76 static long long stat_bytes_alloced = 0;
77 static long long stat_bytes_alloced_los = 0;
82 * Allocation is done from a Thread Local Allocation Buffer (TLAB). TLABs are allocated
83 * from nursery fragments.
84 * tlab_next is the pointer to the space inside the TLAB where the next object will
86 * tlab_temp_end is the pointer to the end of the temporary space reserved for
87 * the allocation: it allows us to set the scan starts at reasonable intervals.
88 * tlab_real_end points to the end of the TLAB.
92 * FIXME: What is faster, a TLS variable pointing to a structure, or separate TLS
93 * variables for next+temp_end ?
96 static __thread char *tlab_start;
97 static __thread char *tlab_next;
98 static __thread char *tlab_temp_end;
99 static __thread char *tlab_real_end;
100 /* Used by the managed allocator/wbarrier */
101 static __thread char **tlab_next_addr;
104 #ifdef HAVE_KW_THREAD
105 #define TLAB_START tlab_start
106 #define TLAB_NEXT tlab_next
107 #define TLAB_TEMP_END tlab_temp_end
108 #define TLAB_REAL_END tlab_real_end
110 #define TLAB_START (__thread_info__->tlab_start)
111 #define TLAB_NEXT (__thread_info__->tlab_next)
112 #define TLAB_TEMP_END (__thread_info__->tlab_temp_end)
113 #define TLAB_REAL_END (__thread_info__->tlab_real_end)
117 alloc_degraded (MonoVTable *vtable, size_t size, gboolean for_mature)
119 static int last_major_gc_warned = -1;
120 static int num_degraded = 0;
125 if (last_major_gc_warned < stat_major_gcs) {
127 if (num_degraded == 1 || num_degraded == 3)
128 fprintf (stderr, "Warning: Degraded allocation. Consider increasing nursery-size if the warning persists.\n");
129 else if (num_degraded == 10)
130 fprintf (stderr, "Warning: Repeated degraded allocation. Consider increasing nursery-size.\n");
131 last_major_gc_warned = stat_major_gcs;
133 InterlockedExchangeAdd (°raded_mode, size);
134 sgen_ensure_free_space (size);
136 if (sgen_need_major_collection (size))
137 sgen_perform_collection (size, GENERATION_OLD, "mature allocation failure");
141 p = major_collector.alloc_degraded (vtable, size);
144 MONO_GC_MAJOR_OBJ_ALLOC_MATURE ((mword)p, size, vtable->klass->name_space, vtable->klass->name);
146 binary_protocol_alloc_degraded (p, vtable, size);
147 MONO_GC_MAJOR_OBJ_ALLOC_DEGRADED ((mword)p, size, vtable->klass->name_space, vtable->klass->name);
154 * Provide a variant that takes just the vtable for small fixed-size objects.
155 * The aligned size is already computed and stored in vt->gc_descr.
156 * Note: every SGEN_SCAN_START_SIZE or so we are given the chance to do some special
157 * processing. We can keep track of where objects start, for example,
158 * so when we scan the thread stacks for pinned objects, we can start
159 * a search for the pinned object in SGEN_SCAN_START_SIZE chunks.
162 mono_gc_alloc_obj_nolock (MonoVTable *vtable, size_t size)
164 /* FIXME: handle OOM */
169 HEAVY_STAT (++stat_objects_alloced);
170 if (size <= SGEN_MAX_SMALL_OBJ_SIZE)
171 HEAVY_STAT (stat_bytes_alloced += size);
173 HEAVY_STAT (stat_bytes_alloced_los += size);
175 size = ALIGN_UP (size);
177 g_assert (vtable->gc_descr);
179 if (G_UNLIKELY (has_per_allocation_action)) {
180 static int alloc_count;
181 int current_alloc = InterlockedIncrement (&alloc_count);
183 if (collect_before_allocs) {
184 if (((current_alloc % collect_before_allocs) == 0) && nursery_section) {
185 sgen_perform_collection (0, GENERATION_NURSERY, "collect-before-alloc-triggered");
186 if (!degraded_mode && sgen_can_alloc_size (size) && size <= SGEN_MAX_SMALL_OBJ_SIZE) {
188 g_assert_not_reached ();
191 } else if (verify_before_allocs) {
192 if ((current_alloc % verify_before_allocs) == 0)
193 sgen_check_whole_heap_stw ();
198 * We must already have the lock here instead of after the
199 * fast path because we might be interrupted in the fast path
200 * (after confirming that new_next < TLAB_TEMP_END) by the GC,
201 * and we'll end up allocating an object in a fragment which
202 * no longer belongs to us.
204 * The managed allocator does not do this, but it's treated
205 * specially by the world-stopping code.
208 if (size > SGEN_MAX_SMALL_OBJ_SIZE) {
209 p = sgen_los_alloc_large_inner (vtable, size);
211 /* tlab_next and tlab_temp_end are TLS vars so accessing them might be expensive */
213 p = (void**)TLAB_NEXT;
214 /* FIXME: handle overflow */
215 new_next = (char*)p + size;
216 TLAB_NEXT = new_next;
218 if (G_LIKELY (new_next < TLAB_TEMP_END)) {
222 * FIXME: We might need a memory barrier here so the change to tlab_next is
223 * visible before the vtable store.
226 DEBUG (6, fprintf (gc_debug_file, "Allocated object %p, vtable: %p (%s), size: %zd\n", p, vtable, vtable->klass->name, size));
227 binary_protocol_alloc (p , vtable, size);
228 if (G_UNLIKELY (MONO_GC_NURSERY_OBJ_ALLOC_ENABLED ()))
229 MONO_GC_NURSERY_OBJ_ALLOC ((mword)p, size, vtable->klass->name_space, vtable->klass->name);
230 g_assert (*p == NULL);
231 mono_atomic_store_seq (p, vtable);
238 /* there are two cases: the object is too big or we run out of space in the TLAB */
239 /* we also reach here when the thread does its first allocation after a minor
240 * collection, since the tlab_ variables are initialized to NULL.
241 * there can be another case (from ORP), if we cooperate with the runtime a bit:
242 * objects that need finalizers can have the high bit set in their size
243 * so the above check fails and we can readily add the object to the queue.
244 * This avoids taking again the GC lock when registering, but this is moot when
245 * doing thread-local allocation, so it may not be a good idea.
247 if (TLAB_NEXT >= TLAB_REAL_END) {
248 int available_in_tlab;
250 * Run out of space in the TLAB. When this happens, some amount of space
251 * remains in the TLAB, but not enough to satisfy the current allocation
252 * request. Currently, we retire the TLAB in all cases, later we could
253 * keep it if the remaining space is above a treshold, and satisfy the
254 * allocation directly from the nursery.
257 /* when running in degraded mode, we continue allocing that way
258 * for a while, to decrease the number of useless nursery collections.
260 if (degraded_mode && degraded_mode < DEFAULT_NURSERY_SIZE)
261 return alloc_degraded (vtable, size, FALSE);
263 available_in_tlab = TLAB_REAL_END - TLAB_NEXT;
264 if (size > tlab_size || available_in_tlab > SGEN_MAX_NURSERY_WASTE) {
265 /* Allocate directly from the nursery */
267 p = sgen_nursery_alloc (size);
269 sgen_ensure_free_space (size);
271 return alloc_degraded (vtable, size, FALSE);
273 p = sgen_nursery_alloc (size);
281 if (nursery_clear_policy == CLEAR_AT_TLAB_CREATION) {
285 size_t alloc_size = 0;
287 DEBUG (3, fprintf (gc_debug_file, "Retire TLAB: %p-%p [%ld]\n", TLAB_START, TLAB_REAL_END, (long)(TLAB_REAL_END - TLAB_NEXT - size)));
288 sgen_nursery_retire_region (p, available_in_tlab);
291 p = sgen_nursery_alloc_range (tlab_size, size, &alloc_size);
293 sgen_ensure_free_space (tlab_size);
295 return alloc_degraded (vtable, size, FALSE);
297 p = sgen_nursery_alloc_range (tlab_size, size, &alloc_size);
306 /* Allocate a new TLAB from the current nursery fragment */
307 TLAB_START = (char*)p;
308 TLAB_NEXT = TLAB_START;
309 TLAB_REAL_END = TLAB_START + alloc_size;
310 TLAB_TEMP_END = TLAB_START + MIN (SGEN_SCAN_START_SIZE, alloc_size);
312 if (nursery_clear_policy == CLEAR_AT_TLAB_CREATION) {
313 memset (TLAB_START, 0, alloc_size);
316 /* Allocate from the TLAB */
317 p = (void*)TLAB_NEXT;
319 sgen_set_nursery_scan_start ((char*)p);
322 /* Reached tlab_temp_end */
324 /* record the scan start so we can find pinned objects more easily */
325 sgen_set_nursery_scan_start ((char*)p);
326 /* we just bump tlab_temp_end as well */
327 TLAB_TEMP_END = MIN (TLAB_REAL_END, TLAB_NEXT + SGEN_SCAN_START_SIZE);
328 DEBUG (5, fprintf (gc_debug_file, "Expanding local alloc: %p-%p\n", TLAB_NEXT, TLAB_TEMP_END));
333 DEBUG (6, fprintf (gc_debug_file, "Allocated object %p, vtable: %p (%s), size: %zd\n", p, vtable, vtable->klass->name, size));
334 binary_protocol_alloc (p, vtable, size);
335 if (G_UNLIKELY (MONO_GC_MAJOR_OBJ_ALLOC_LARGE_ENABLED ()|| MONO_GC_NURSERY_OBJ_ALLOC_ENABLED ())) {
336 if (size > SGEN_MAX_SMALL_OBJ_SIZE)
337 MONO_GC_MAJOR_OBJ_ALLOC_LARGE ((mword)p, size, vtable->klass->name_space, vtable->klass->name);
339 MONO_GC_NURSERY_OBJ_ALLOC ((mword)p, size, vtable->klass->name_space, vtable->klass->name);
341 mono_atomic_store_seq (p, vtable);
348 mono_gc_try_alloc_obj_nolock (MonoVTable *vtable, size_t size)
354 size = ALIGN_UP (size);
356 g_assert (vtable->gc_descr);
357 if (size > SGEN_MAX_SMALL_OBJ_SIZE)
360 if (G_UNLIKELY (size > tlab_size)) {
361 /* Allocate directly from the nursery */
362 p = sgen_nursery_alloc (size);
365 sgen_set_nursery_scan_start ((char*)p);
367 /*FIXME we should use weak memory ops here. Should help specially on x86. */
368 if (nursery_clear_policy == CLEAR_AT_TLAB_CREATION)
371 int available_in_tlab;
373 /* tlab_next and tlab_temp_end are TLS vars so accessing them might be expensive */
375 p = (void**)TLAB_NEXT;
376 /* FIXME: handle overflow */
377 new_next = (char*)p + size;
379 real_end = TLAB_REAL_END;
380 available_in_tlab = real_end - (char*)p;
382 if (G_LIKELY (new_next < real_end)) {
383 TLAB_NEXT = new_next;
385 /* Second case, we overflowed temp end */
386 if (G_UNLIKELY (new_next >= TLAB_TEMP_END)) {
387 sgen_set_nursery_scan_start (new_next);
388 /* we just bump tlab_temp_end as well */
389 TLAB_TEMP_END = MIN (TLAB_REAL_END, TLAB_NEXT + SGEN_SCAN_START_SIZE);
390 DEBUG (5, fprintf (gc_debug_file, "Expanding local alloc: %p-%p\n", TLAB_NEXT, TLAB_TEMP_END));
392 } else if (available_in_tlab > SGEN_MAX_NURSERY_WASTE) {
393 /* Allocate directly from the nursery */
394 p = sgen_nursery_alloc (size);
398 if (nursery_clear_policy == CLEAR_AT_TLAB_CREATION)
401 size_t alloc_size = 0;
403 sgen_nursery_retire_region (p, available_in_tlab);
404 new_next = sgen_nursery_alloc_range (tlab_size, size, &alloc_size);
405 p = (void**)new_next;
409 TLAB_START = (char*)new_next;
410 TLAB_NEXT = new_next + size;
411 TLAB_REAL_END = new_next + alloc_size;
412 TLAB_TEMP_END = new_next + MIN (SGEN_SCAN_START_SIZE, alloc_size);
413 sgen_set_nursery_scan_start ((char*)p);
415 if (nursery_clear_policy == CLEAR_AT_TLAB_CREATION)
416 memset (new_next, 0, alloc_size);
418 MONO_GC_NURSERY_TLAB_ALLOC ((mword)new_next, alloc_size);
422 HEAVY_STAT (++stat_objects_alloced);
423 HEAVY_STAT (stat_bytes_alloced += size);
425 DEBUG (6, fprintf (gc_debug_file, "Allocated object %p, vtable: %p (%s), size: %zd\n", p, vtable, vtable->klass->name, size));
426 binary_protocol_alloc (p, vtable, size);
427 if (G_UNLIKELY (MONO_GC_NURSERY_OBJ_ALLOC_ENABLED ()))
428 MONO_GC_NURSERY_OBJ_ALLOC ((mword)p, size, vtable->klass->name_space, vtable->klass->name);
429 g_assert (*p == NULL); /* FIXME disable this in non debug builds */
431 mono_atomic_store_seq (p, vtable);
437 mono_gc_alloc_obj (MonoVTable *vtable, size_t size)
440 #ifndef DISABLE_CRITICAL_REGION
442 ENTER_CRITICAL_REGION;
443 res = mono_gc_try_alloc_obj_nolock (vtable, size);
445 EXIT_CRITICAL_REGION;
448 EXIT_CRITICAL_REGION;
451 res = mono_gc_alloc_obj_nolock (vtable, size);
453 if (G_UNLIKELY (!res))
454 return mono_gc_out_of_memory (size);
459 mono_gc_alloc_vector (MonoVTable *vtable, size_t size, uintptr_t max_length)
462 #ifndef DISABLE_CRITICAL_REGION
464 ENTER_CRITICAL_REGION;
465 arr = mono_gc_try_alloc_obj_nolock (vtable, size);
467 /*This doesn't require fencing since EXIT_CRITICAL_REGION already does it for us*/
468 arr->max_length = max_length;
469 EXIT_CRITICAL_REGION;
472 EXIT_CRITICAL_REGION;
477 arr = mono_gc_alloc_obj_nolock (vtable, size);
478 if (G_UNLIKELY (!arr)) {
480 return mono_gc_out_of_memory (size);
483 arr->max_length = max_length;
491 mono_gc_alloc_array (MonoVTable *vtable, size_t size, uintptr_t max_length, uintptr_t bounds_size)
494 MonoArrayBounds *bounds;
496 #ifndef DISABLE_CRITICAL_REGION
498 ENTER_CRITICAL_REGION;
499 arr = mono_gc_try_alloc_obj_nolock (vtable, size);
501 /*This doesn't require fencing since EXIT_CRITICAL_REGION already does it for us*/
502 arr->max_length = max_length;
504 bounds = (MonoArrayBounds*)((char*)arr + size - bounds_size);
505 arr->bounds = bounds;
506 EXIT_CRITICAL_REGION;
509 EXIT_CRITICAL_REGION;
514 arr = mono_gc_alloc_obj_nolock (vtable, size);
515 if (G_UNLIKELY (!arr)) {
517 return mono_gc_out_of_memory (size);
520 arr->max_length = max_length;
522 bounds = (MonoArrayBounds*)((char*)arr + size - bounds_size);
523 arr->bounds = bounds;
531 mono_gc_alloc_string (MonoVTable *vtable, size_t size, gint32 len)
534 #ifndef DISABLE_CRITICAL_REGION
536 ENTER_CRITICAL_REGION;
537 str = mono_gc_try_alloc_obj_nolock (vtable, size);
539 /*This doesn't require fencing since EXIT_CRITICAL_REGION already does it for us*/
541 EXIT_CRITICAL_REGION;
544 EXIT_CRITICAL_REGION;
549 str = mono_gc_alloc_obj_nolock (vtable, size);
550 if (G_UNLIKELY (!str)) {
552 return mono_gc_out_of_memory (size);
563 * To be used for interned strings and possibly MonoThread, reflection handles.
564 * We may want to explicitly free these objects.
567 mono_gc_alloc_pinned_obj (MonoVTable *vtable, size_t size)
570 size = ALIGN_UP (size);
573 if (size > SGEN_MAX_SMALL_OBJ_SIZE) {
574 /* large objects are always pinned anyway */
575 p = sgen_los_alloc_large_inner (vtable, size);
577 DEBUG (9, g_assert (vtable->klass->inited));
578 p = major_collector.alloc_small_pinned_obj (size, SGEN_VTABLE_HAS_REFERENCES (vtable));
581 DEBUG (6, fprintf (gc_debug_file, "Allocated pinned object %p, vtable: %p (%s), size: %zd\n", p, vtable, vtable->klass->name, size));
582 if (size > SGEN_MAX_SMALL_OBJ_SIZE)
583 MONO_GC_MAJOR_OBJ_ALLOC_LARGE ((mword)p, size, vtable->klass->name_space, vtable->klass->name);
585 MONO_GC_MAJOR_OBJ_ALLOC_PINNED ((mword)p, size, vtable->klass->name_space, vtable->klass->name);
586 binary_protocol_alloc_pinned (p, vtable, size);
587 mono_atomic_store_seq (p, vtable);
594 mono_gc_alloc_mature (MonoVTable *vtable)
597 size_t size = ALIGN_UP (vtable->klass->instance_size);
599 res = alloc_degraded (vtable, size, TRUE);
600 mono_atomic_store_seq (res, vtable);
602 if (G_UNLIKELY (vtable->klass->has_finalize))
603 mono_object_register_finalizer ((MonoObject*)res);
609 mono_gc_alloc_fixed (size_t size, void *descr)
611 /* FIXME: do a single allocation */
612 void *res = calloc (1, size);
615 if (!mono_gc_register_root (res, size, descr)) {
623 mono_gc_free_fixed (void* addr)
625 mono_gc_deregister_root (addr);
630 sgen_init_tlab_info (SgenThreadInfo* info)
632 #ifndef HAVE_KW_THREAD
633 SgenThreadInfo *__thread_info__ = info;
636 info->tlab_start_addr = &TLAB_START;
637 info->tlab_next_addr = &TLAB_NEXT;
638 info->tlab_temp_end_addr = &TLAB_TEMP_END;
639 info->tlab_real_end_addr = &TLAB_REAL_END;
641 #ifdef HAVE_KW_THREAD
642 tlab_next_addr = &tlab_next;
647 * Clear the thread local TLAB variables for all threads.
650 sgen_clear_tlabs (void)
652 SgenThreadInfo *info;
654 FOREACH_THREAD (info) {
655 /* A new TLAB will be allocated when the thread does its first allocation */
656 *info->tlab_start_addr = NULL;
657 *info->tlab_next_addr = NULL;
658 *info->tlab_temp_end_addr = NULL;
659 *info->tlab_real_end_addr = NULL;
663 static MonoMethod* alloc_method_cache [ATYPE_NUM];
665 #ifdef MANAGED_ALLOCATION
666 /* FIXME: Do this in the JIT, where specialized allocation sequences can be created
667 * for each class. This is currently not easy to do, as it is hard to generate basic
668 * blocks + branches, but it is easy with the linear IL codebase.
670 * For this to work we'd need to solve the TLAB race, first. Now we
671 * require the allocator to be in a few known methods to make sure
672 * that they are executed atomically via the restart mechanism.
675 create_allocator (int atype)
678 guint32 slowpath_branch, max_size_branch;
679 MonoMethodBuilder *mb;
681 MonoMethodSignature *csig;
682 static gboolean registered = FALSE;
683 int tlab_next_addr_var, new_next_var;
685 const char *name = NULL;
686 AllocatorWrapperInfo *info;
688 #ifdef HAVE_KW_THREAD
689 int tlab_next_addr_offset = -1;
690 int tlab_temp_end_offset = -1;
692 MONO_THREAD_VAR_OFFSET (tlab_next_addr, tlab_next_addr_offset);
693 MONO_THREAD_VAR_OFFSET (tlab_temp_end, tlab_temp_end_offset);
695 g_assert (tlab_next_addr_offset != -1);
696 g_assert (tlab_temp_end_offset != -1);
700 mono_register_jit_icall (mono_gc_alloc_obj, "mono_gc_alloc_obj", mono_create_icall_signature ("object ptr int"), FALSE);
701 mono_register_jit_icall (mono_gc_alloc_vector, "mono_gc_alloc_vector", mono_create_icall_signature ("object ptr int int"), FALSE);
705 if (atype == ATYPE_SMALL) {
708 } else if (atype == ATYPE_NORMAL) {
711 } else if (atype == ATYPE_VECTOR) {
713 name = "AllocVector";
715 g_assert_not_reached ();
718 csig = mono_metadata_signature_alloc (mono_defaults.corlib, num_params);
719 csig->ret = &mono_defaults.object_class->byval_arg;
720 for (i = 0; i < num_params; ++i)
721 csig->params [i] = &mono_defaults.int_class->byval_arg;
723 mb = mono_mb_new (mono_defaults.object_class, name, MONO_WRAPPER_ALLOC);
724 size_var = mono_mb_add_local (mb, &mono_defaults.int32_class->byval_arg);
725 if (atype == ATYPE_NORMAL || atype == ATYPE_SMALL) {
726 /* size = vtable->klass->instance_size; */
727 mono_mb_emit_ldarg (mb, 0);
728 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoVTable, klass));
729 mono_mb_emit_byte (mb, CEE_ADD);
730 mono_mb_emit_byte (mb, CEE_LDIND_I);
731 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoClass, instance_size));
732 mono_mb_emit_byte (mb, CEE_ADD);
733 /* FIXME: assert instance_size stays a 4 byte integer */
734 mono_mb_emit_byte (mb, CEE_LDIND_U4);
735 mono_mb_emit_stloc (mb, size_var);
736 } else if (atype == ATYPE_VECTOR) {
737 MonoExceptionClause *clause;
739 MonoClass *oom_exc_class;
742 /* n > MONO_ARRAY_MAX_INDEX -> OverflowException */
743 mono_mb_emit_ldarg (mb, 1);
744 mono_mb_emit_icon (mb, MONO_ARRAY_MAX_INDEX);
745 pos = mono_mb_emit_short_branch (mb, CEE_BLE_UN_S);
746 mono_mb_emit_exception (mb, "OverflowException", NULL);
747 mono_mb_patch_short_branch (mb, pos);
749 clause = mono_image_alloc0 (mono_defaults.corlib, sizeof (MonoExceptionClause));
750 clause->try_offset = mono_mb_get_label (mb);
752 /* vtable->klass->sizes.element_size */
753 mono_mb_emit_ldarg (mb, 0);
754 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoVTable, klass));
755 mono_mb_emit_byte (mb, CEE_ADD);
756 mono_mb_emit_byte (mb, CEE_LDIND_I);
757 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoClass, sizes.element_size));
758 mono_mb_emit_byte (mb, CEE_ADD);
759 mono_mb_emit_byte (mb, CEE_LDIND_U4);
762 mono_mb_emit_ldarg (mb, 1);
763 mono_mb_emit_byte (mb, CEE_MUL_OVF_UN);
764 /* + sizeof (MonoArray) */
765 mono_mb_emit_icon (mb, sizeof (MonoArray));
766 mono_mb_emit_byte (mb, CEE_ADD_OVF_UN);
767 mono_mb_emit_stloc (mb, size_var);
769 pos_leave = mono_mb_emit_branch (mb, CEE_LEAVE);
772 clause->flags = MONO_EXCEPTION_CLAUSE_NONE;
773 clause->try_len = mono_mb_get_pos (mb) - clause->try_offset;
774 clause->data.catch_class = mono_class_from_name (mono_defaults.corlib,
775 "System", "OverflowException");
776 g_assert (clause->data.catch_class);
777 clause->handler_offset = mono_mb_get_label (mb);
779 oom_exc_class = mono_class_from_name (mono_defaults.corlib,
780 "System", "OutOfMemoryException");
781 g_assert (oom_exc_class);
782 ctor = mono_class_get_method_from_name (oom_exc_class, ".ctor", 0);
785 mono_mb_emit_byte (mb, CEE_POP);
786 mono_mb_emit_op (mb, CEE_NEWOBJ, ctor);
787 mono_mb_emit_byte (mb, CEE_THROW);
789 clause->handler_len = mono_mb_get_pos (mb) - clause->handler_offset;
790 mono_mb_set_clauses (mb, 1, clause);
791 mono_mb_patch_branch (mb, pos_leave);
794 g_assert_not_reached ();
797 /* size += ALLOC_ALIGN - 1; */
798 mono_mb_emit_ldloc (mb, size_var);
799 mono_mb_emit_icon (mb, ALLOC_ALIGN - 1);
800 mono_mb_emit_byte (mb, CEE_ADD);
801 /* size &= ~(ALLOC_ALIGN - 1); */
802 mono_mb_emit_icon (mb, ~(ALLOC_ALIGN - 1));
803 mono_mb_emit_byte (mb, CEE_AND);
804 mono_mb_emit_stloc (mb, size_var);
806 /* if (size > MAX_SMALL_OBJ_SIZE) goto slowpath */
807 if (atype != ATYPE_SMALL) {
808 mono_mb_emit_ldloc (mb, size_var);
809 mono_mb_emit_icon (mb, MAX_SMALL_OBJ_SIZE);
810 max_size_branch = mono_mb_emit_short_branch (mb, MONO_CEE_BGT_UN_S);
814 * We need to modify tlab_next, but the JIT only supports reading, so we read
815 * another tls var holding its address instead.
818 /* tlab_next_addr (local) = tlab_next_addr (TLS var) */
819 tlab_next_addr_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
820 EMIT_TLS_ACCESS (mb, tlab_next_addr, tlab_next_addr_offset);
821 mono_mb_emit_stloc (mb, tlab_next_addr_var);
823 /* p = (void**)tlab_next; */
824 p_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
825 mono_mb_emit_ldloc (mb, tlab_next_addr_var);
826 mono_mb_emit_byte (mb, CEE_LDIND_I);
827 mono_mb_emit_stloc (mb, p_var);
829 /* new_next = (char*)p + size; */
830 new_next_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
831 mono_mb_emit_ldloc (mb, p_var);
832 mono_mb_emit_ldloc (mb, size_var);
833 mono_mb_emit_byte (mb, CEE_CONV_I);
834 mono_mb_emit_byte (mb, CEE_ADD);
835 mono_mb_emit_stloc (mb, new_next_var);
837 /* if (G_LIKELY (new_next < tlab_temp_end)) */
838 mono_mb_emit_ldloc (mb, new_next_var);
839 EMIT_TLS_ACCESS (mb, tlab_temp_end, tlab_temp_end_offset);
840 slowpath_branch = mono_mb_emit_short_branch (mb, MONO_CEE_BLT_UN_S);
843 if (atype != ATYPE_SMALL)
844 mono_mb_patch_short_branch (mb, max_size_branch);
846 mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
847 mono_mb_emit_byte (mb, CEE_MONO_NOT_TAKEN);
849 /* FIXME: mono_gc_alloc_obj takes a 'size_t' as an argument, not an int32 */
850 mono_mb_emit_ldarg (mb, 0);
851 mono_mb_emit_ldloc (mb, size_var);
852 if (atype == ATYPE_NORMAL || atype == ATYPE_SMALL) {
853 mono_mb_emit_icall (mb, mono_gc_alloc_obj);
854 } else if (atype == ATYPE_VECTOR) {
855 mono_mb_emit_ldarg (mb, 1);
856 mono_mb_emit_icall (mb, mono_gc_alloc_vector);
858 g_assert_not_reached ();
860 mono_mb_emit_byte (mb, CEE_RET);
863 mono_mb_patch_short_branch (mb, slowpath_branch);
865 /* FIXME: Memory barrier */
867 /* tlab_next = new_next */
868 mono_mb_emit_ldloc (mb, tlab_next_addr_var);
869 mono_mb_emit_ldloc (mb, new_next_var);
870 mono_mb_emit_byte (mb, CEE_STIND_I);
872 /*The tlab store must be visible before the the vtable store. This could be replaced with a DDS but doing it with IL would be tricky. */
873 mono_mb_emit_byte ((mb), MONO_CUSTOM_PREFIX);
874 mono_mb_emit_op (mb, CEE_MONO_MEMORY_BARRIER, StoreStoreBarrier);
877 mono_mb_emit_ldloc (mb, p_var);
878 mono_mb_emit_ldarg (mb, 0);
879 mono_mb_emit_byte (mb, CEE_STIND_I);
881 if (atype == ATYPE_VECTOR) {
882 /* arr->max_length = max_length; */
883 mono_mb_emit_ldloc (mb, p_var);
884 mono_mb_emit_ldflda (mb, G_STRUCT_OFFSET (MonoArray, max_length));
885 mono_mb_emit_ldarg (mb, 1);
886 #ifdef MONO_BIG_ARRAYS
887 mono_mb_emit_byte (mb, CEE_STIND_I);
889 mono_mb_emit_byte (mb, CEE_STIND_I4);
894 We must make sure both vtable and max_length are globaly visible before returning to managed land.
896 mono_mb_emit_byte ((mb), MONO_CUSTOM_PREFIX);
897 mono_mb_emit_op (mb, CEE_MONO_MEMORY_BARRIER, StoreStoreBarrier);
900 mono_mb_emit_ldloc (mb, p_var);
901 mono_mb_emit_byte (mb, CEE_RET);
903 res = mono_mb_create_method (mb, csig, 8);
905 mono_method_get_header (res)->init_locals = FALSE;
907 info = mono_image_alloc0 (mono_defaults.corlib, sizeof (AllocatorWrapperInfo));
908 info->gc_name = "sgen";
909 info->alloc_type = atype;
910 mono_marshal_set_wrapper_info (res, info);
917 * Generate an allocator method implementing the fast path of mono_gc_alloc_obj ().
918 * The signature of the called method is:
919 * object allocate (MonoVTable *vtable)
922 mono_gc_get_managed_allocator (MonoVTable *vtable, gboolean for_box)
924 #ifdef MANAGED_ALLOCATION
925 MonoClass *klass = vtable->klass;
927 #ifdef HAVE_KW_THREAD
928 int tlab_next_offset = -1;
929 int tlab_temp_end_offset = -1;
930 MONO_THREAD_VAR_OFFSET (tlab_next, tlab_next_offset);
931 MONO_THREAD_VAR_OFFSET (tlab_temp_end, tlab_temp_end_offset);
933 if (tlab_next_offset == -1 || tlab_temp_end_offset == -1)
937 if (!mono_runtime_has_tls_get ())
939 if (klass->instance_size > tlab_size)
941 if (klass->has_finalize || klass->marshalbyref || (mono_profiler_get_events () & MONO_PROFILE_ALLOCATIONS))
945 if (klass->byval_arg.type == MONO_TYPE_STRING)
947 if (collect_before_allocs)
950 if (ALIGN_TO (klass->instance_size, ALLOC_ALIGN) < MAX_SMALL_OBJ_SIZE)
951 return mono_gc_get_managed_allocator_by_type (ATYPE_SMALL);
953 return mono_gc_get_managed_allocator_by_type (ATYPE_NORMAL);
960 mono_gc_get_managed_array_allocator (MonoVTable *vtable, int rank)
962 #ifdef MANAGED_ALLOCATION
963 MonoClass *klass = vtable->klass;
965 #ifdef HAVE_KW_THREAD
966 int tlab_next_offset = -1;
967 int tlab_temp_end_offset = -1;
968 MONO_THREAD_VAR_OFFSET (tlab_next, tlab_next_offset);
969 MONO_THREAD_VAR_OFFSET (tlab_temp_end, tlab_temp_end_offset);
971 if (tlab_next_offset == -1 || tlab_temp_end_offset == -1)
977 if (!mono_runtime_has_tls_get ())
979 if (mono_profiler_get_events () & MONO_PROFILE_ALLOCATIONS)
981 if (has_per_allocation_action)
983 g_assert (!mono_class_has_finalizer (klass) && !klass->marshalbyref);
985 return mono_gc_get_managed_allocator_by_type (ATYPE_VECTOR);
992 sgen_set_use_managed_allocator (gboolean flag)
994 use_managed_allocator = flag;
998 mono_gc_get_managed_allocator_by_type (int atype)
1000 #ifdef MANAGED_ALLOCATION
1003 if (!use_managed_allocator)
1006 if (!mono_runtime_has_tls_get ())
1009 mono_loader_lock ();
1010 res = alloc_method_cache [atype];
1012 res = alloc_method_cache [atype] = create_allocator (atype);
1013 mono_loader_unlock ();
1021 mono_gc_get_managed_allocator_types (void)
1027 sgen_is_managed_allocator (MonoMethod *method)
1031 for (i = 0; i < ATYPE_NUM; ++i)
1032 if (method == alloc_method_cache [i])
1038 sgen_has_managed_allocator (void)
1042 for (i = 0; i < ATYPE_NUM; ++i)
1043 if (alloc_method_cache [i])
1048 #ifdef HEAVY_STATISTICS
1050 sgen_alloc_init_heavy_stats (void)
1052 mono_counters_register ("# objects allocated", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_objects_alloced);
1053 mono_counters_register ("bytes allocated", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_bytes_alloced);
1054 mono_counters_register ("bytes allocated in LOS", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_bytes_alloced_los);
1058 #endif /*HAVE_SGEN_GC*/