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 #ifdef HEAVY_STATISTICS
73 static long long stat_objects_alloced = 0;
74 static long long stat_bytes_alloced = 0;
75 static long long stat_bytes_alloced_los = 0;
80 * Allocation is done from a Thread Local Allocation Buffer (TLAB). TLABs are allocated
81 * from nursery fragments.
82 * tlab_next is the pointer to the space inside the TLAB where the next object will
84 * tlab_temp_end is the pointer to the end of the temporary space reserved for
85 * the allocation: it allows us to set the scan starts at reasonable intervals.
86 * tlab_real_end points to the end of the TLAB.
90 * FIXME: What is faster, a TLS variable pointing to a structure, or separate TLS
91 * variables for next+temp_end ?
94 static __thread char *tlab_start;
95 static __thread char *tlab_next;
96 static __thread char *tlab_temp_end;
97 static __thread char *tlab_real_end;
98 /* Used by the managed allocator/wbarrier */
99 static __thread char **tlab_next_addr;
102 #ifdef HAVE_KW_THREAD
103 #define TLAB_START tlab_start
104 #define TLAB_NEXT tlab_next
105 #define TLAB_TEMP_END tlab_temp_end
106 #define TLAB_REAL_END tlab_real_end
108 #define TLAB_START (__thread_info__->tlab_start)
109 #define TLAB_NEXT (__thread_info__->tlab_next)
110 #define TLAB_TEMP_END (__thread_info__->tlab_temp_end)
111 #define TLAB_REAL_END (__thread_info__->tlab_real_end)
115 alloc_degraded (MonoVTable *vtable, size_t size, gboolean for_mature)
117 static int last_major_gc_warned = -1;
118 static int num_degraded = 0;
121 if (last_major_gc_warned < stat_major_gcs) {
123 if (num_degraded == 1 || num_degraded == 3)
124 fprintf (stderr, "Warning: Degraded allocation. Consider increasing nursery-size if the warning persists.\n");
125 else if (num_degraded == 10)
126 fprintf (stderr, "Warning: Repeated degraded allocation. Consider increasing nursery-size.\n");
127 last_major_gc_warned = stat_major_gcs;
129 InterlockedExchangeAdd (°raded_mode, size);
132 sgen_ensure_free_space (size);
134 return major_collector.alloc_degraded (vtable, size);
138 * Provide a variant that takes just the vtable for small fixed-size objects.
139 * The aligned size is already computed and stored in vt->gc_descr.
140 * Note: every SGEN_SCAN_START_SIZE or so we are given the chance to do some special
141 * processing. We can keep track of where objects start, for example,
142 * so when we scan the thread stacks for pinned objects, we can start
143 * a search for the pinned object in SGEN_SCAN_START_SIZE chunks.
146 mono_gc_alloc_obj_nolock (MonoVTable *vtable, size_t size)
148 /* FIXME: handle OOM */
153 HEAVY_STAT (++stat_objects_alloced);
154 if (size <= SGEN_MAX_SMALL_OBJ_SIZE)
155 HEAVY_STAT (stat_bytes_alloced += size);
157 HEAVY_STAT (stat_bytes_alloced_los += size);
159 size = ALIGN_UP (size);
161 g_assert (vtable->gc_descr);
163 if (G_UNLIKELY (has_per_allocation_action)) {
164 static int alloc_count;
165 int current_alloc = InterlockedIncrement (&alloc_count);
167 if (collect_before_allocs) {
168 if (((current_alloc % collect_before_allocs) == 0) && nursery_section) {
169 sgen_perform_collection (0, GENERATION_NURSERY, "collect-before-alloc-triggered");
170 if (!degraded_mode && sgen_can_alloc_size (size) && size <= SGEN_MAX_SMALL_OBJ_SIZE) {
172 g_assert_not_reached ();
175 } else if (verify_before_allocs) {
176 if ((current_alloc % verify_before_allocs) == 0)
177 sgen_check_whole_heap_stw ();
182 * We must already have the lock here instead of after the
183 * fast path because we might be interrupted in the fast path
184 * (after confirming that new_next < TLAB_TEMP_END) by the GC,
185 * and we'll end up allocating an object in a fragment which
186 * no longer belongs to us.
188 * The managed allocator does not do this, but it's treated
189 * specially by the world-stopping code.
192 if (size > SGEN_MAX_SMALL_OBJ_SIZE) {
193 p = sgen_los_alloc_large_inner (vtable, size);
195 /* tlab_next and tlab_temp_end are TLS vars so accessing them might be expensive */
197 p = (void**)TLAB_NEXT;
198 /* FIXME: handle overflow */
199 new_next = (char*)p + size;
200 TLAB_NEXT = new_next;
202 if (G_LIKELY (new_next < TLAB_TEMP_END)) {
206 * FIXME: We might need a memory barrier here so the change to tlab_next is
207 * visible before the vtable store.
210 DEBUG (6, fprintf (gc_debug_file, "Allocated object %p, vtable: %p (%s), size: %zd\n", p, vtable, vtable->klass->name, size));
211 binary_protocol_alloc (p , vtable, size);
212 g_assert (*p == NULL);
213 mono_atomic_store_seq (p, vtable);
220 /* there are two cases: the object is too big or we run out of space in the TLAB */
221 /* we also reach here when the thread does its first allocation after a minor
222 * collection, since the tlab_ variables are initialized to NULL.
223 * there can be another case (from ORP), if we cooperate with the runtime a bit:
224 * objects that need finalizers can have the high bit set in their size
225 * so the above check fails and we can readily add the object to the queue.
226 * This avoids taking again the GC lock when registering, but this is moot when
227 * doing thread-local allocation, so it may not be a good idea.
229 if (TLAB_NEXT >= TLAB_REAL_END) {
230 int available_in_tlab;
232 * Run out of space in the TLAB. When this happens, some amount of space
233 * remains in the TLAB, but not enough to satisfy the current allocation
234 * request. Currently, we retire the TLAB in all cases, later we could
235 * keep it if the remaining space is above a treshold, and satisfy the
236 * allocation directly from the nursery.
239 /* when running in degraded mode, we continue allocing that way
240 * for a while, to decrease the number of useless nursery collections.
242 if (degraded_mode && degraded_mode < DEFAULT_NURSERY_SIZE) {
243 p = alloc_degraded (vtable, size, FALSE);
244 binary_protocol_alloc_degraded (p, vtable, size);
248 available_in_tlab = TLAB_REAL_END - TLAB_NEXT;
249 if (size > tlab_size || available_in_tlab > SGEN_MAX_NURSERY_WASTE) {
250 /* Allocate directly from the nursery */
252 p = sgen_nursery_alloc (size);
254 sgen_ensure_free_space (size);
256 p = alloc_degraded (vtable, size, FALSE);
257 binary_protocol_alloc_degraded (p, vtable, size);
260 p = sgen_nursery_alloc (size);
269 MONO_GC_NURSERY_OBJ_ALLOC (p, size, NULL);
271 if (nursery_clear_policy == CLEAR_AT_TLAB_CREATION) {
275 size_t alloc_size = 0;
277 DEBUG (3, fprintf (gc_debug_file, "Retire TLAB: %p-%p [%ld]\n", TLAB_START, TLAB_REAL_END, (long)(TLAB_REAL_END - TLAB_NEXT - size)));
278 sgen_nursery_retire_region (p, available_in_tlab);
281 p = sgen_nursery_alloc_range (tlab_size, size, &alloc_size);
283 sgen_ensure_free_space (tlab_size);
285 p = alloc_degraded (vtable, size, FALSE);
286 binary_protocol_alloc_degraded (p, vtable, size);
289 p = sgen_nursery_alloc_range (tlab_size, size, &alloc_size);
299 /* Allocate a new TLAB from the current nursery fragment */
300 TLAB_START = (char*)p;
301 TLAB_NEXT = TLAB_START;
302 TLAB_REAL_END = TLAB_START + alloc_size;
303 TLAB_TEMP_END = TLAB_START + MIN (SGEN_SCAN_START_SIZE, alloc_size);
305 if (nursery_clear_policy == CLEAR_AT_TLAB_CREATION) {
306 memset (TLAB_START, 0, alloc_size);
309 MONO_GC_NURSERY_TLAB_ALLOC (p, alloc_size);
311 /* Allocate from the TLAB */
312 p = (void*)TLAB_NEXT;
314 sgen_set_nursery_scan_start ((char*)p);
317 /* Reached tlab_temp_end */
319 /* record the scan start so we can find pinned objects more easily */
320 sgen_set_nursery_scan_start ((char*)p);
321 /* we just bump tlab_temp_end as well */
322 TLAB_TEMP_END = MIN (TLAB_REAL_END, TLAB_NEXT + SGEN_SCAN_START_SIZE);
323 DEBUG (5, fprintf (gc_debug_file, "Expanding local alloc: %p-%p\n", TLAB_NEXT, TLAB_TEMP_END));
328 DEBUG (6, fprintf (gc_debug_file, "Allocated object %p, vtable: %p (%s), size: %zd\n", p, vtable, vtable->klass->name, size));
329 binary_protocol_alloc (p, vtable, size);
330 mono_atomic_store_seq (p, vtable);
337 mono_gc_try_alloc_obj_nolock (MonoVTable *vtable, size_t size)
343 size = ALIGN_UP (size);
345 g_assert (vtable->gc_descr);
346 if (size > SGEN_MAX_SMALL_OBJ_SIZE)
349 if (G_UNLIKELY (size > tlab_size)) {
350 /* Allocate directly from the nursery */
351 p = sgen_nursery_alloc (size);
354 MONO_GC_NURSERY_OBJ_ALLOC (p, size, NULL);
355 sgen_set_nursery_scan_start ((char*)p);
357 /*FIXME we should use weak memory ops here. Should help specially on x86. */
358 if (nursery_clear_policy == CLEAR_AT_TLAB_CREATION)
361 int available_in_tlab;
363 /* tlab_next and tlab_temp_end are TLS vars so accessing them might be expensive */
365 p = (void**)TLAB_NEXT;
366 /* FIXME: handle overflow */
367 new_next = (char*)p + size;
369 real_end = TLAB_REAL_END;
370 available_in_tlab = real_end - (char*)p;
372 if (G_LIKELY (new_next < real_end)) {
373 TLAB_NEXT = new_next;
375 /* Second case, we overflowed temp end */
376 if (G_UNLIKELY (new_next >= TLAB_TEMP_END)) {
377 sgen_set_nursery_scan_start (new_next);
378 /* we just bump tlab_temp_end as well */
379 TLAB_TEMP_END = MIN (TLAB_REAL_END, TLAB_NEXT + SGEN_SCAN_START_SIZE);
380 DEBUG (5, fprintf (gc_debug_file, "Expanding local alloc: %p-%p\n", TLAB_NEXT, TLAB_TEMP_END));
382 } else if (available_in_tlab > SGEN_MAX_NURSERY_WASTE) {
383 /* Allocate directly from the nursery */
384 p = sgen_nursery_alloc (size);
387 MONO_GC_NURSERY_OBJ_ALLOC (p, size, NULL);
389 if (nursery_clear_policy == CLEAR_AT_TLAB_CREATION)
392 size_t alloc_size = 0;
394 sgen_nursery_retire_region (p, available_in_tlab);
395 new_next = sgen_nursery_alloc_range (tlab_size, size, &alloc_size);
396 p = (void**)new_next;
400 TLAB_START = (char*)new_next;
401 TLAB_NEXT = new_next + size;
402 TLAB_REAL_END = new_next + alloc_size;
403 TLAB_TEMP_END = new_next + MIN (SGEN_SCAN_START_SIZE, alloc_size);
404 sgen_set_nursery_scan_start ((char*)p);
406 if (nursery_clear_policy == CLEAR_AT_TLAB_CREATION)
407 memset (new_next, 0, alloc_size);
409 MONO_GC_NURSERY_TLAB_ALLOC (new_next, alloc_size);
413 HEAVY_STAT (++stat_objects_alloced);
414 HEAVY_STAT (stat_bytes_alloced += size);
416 DEBUG (6, fprintf (gc_debug_file, "Allocated object %p, vtable: %p (%s), size: %zd\n", p, vtable, vtable->klass->name, size));
417 binary_protocol_alloc (p, vtable, size);
418 g_assert (*p == NULL); /* FIXME disable this in non debug builds */
420 mono_atomic_store_seq (p, vtable);
426 mono_gc_alloc_obj (MonoVTable *vtable, size_t size)
429 #ifndef DISABLE_CRITICAL_REGION
431 ENTER_CRITICAL_REGION;
432 res = mono_gc_try_alloc_obj_nolock (vtable, size);
434 EXIT_CRITICAL_REGION;
437 EXIT_CRITICAL_REGION;
440 res = mono_gc_alloc_obj_nolock (vtable, size);
442 if (G_UNLIKELY (!res))
443 return mono_gc_out_of_memory (size);
448 mono_gc_alloc_vector (MonoVTable *vtable, size_t size, uintptr_t max_length)
451 #ifndef DISABLE_CRITICAL_REGION
453 ENTER_CRITICAL_REGION;
454 arr = mono_gc_try_alloc_obj_nolock (vtable, size);
456 /*This doesn't require fencing since EXIT_CRITICAL_REGION already does it for us*/
457 arr->max_length = max_length;
458 EXIT_CRITICAL_REGION;
461 EXIT_CRITICAL_REGION;
466 arr = mono_gc_alloc_obj_nolock (vtable, size);
467 if (G_UNLIKELY (!arr)) {
469 return mono_gc_out_of_memory (size);
472 arr->max_length = max_length;
480 mono_gc_alloc_array (MonoVTable *vtable, size_t size, uintptr_t max_length, uintptr_t bounds_size)
483 MonoArrayBounds *bounds;
485 #ifndef DISABLE_CRITICAL_REGION
487 ENTER_CRITICAL_REGION;
488 arr = mono_gc_try_alloc_obj_nolock (vtable, size);
490 /*This doesn't require fencing since EXIT_CRITICAL_REGION already does it for us*/
491 arr->max_length = max_length;
493 bounds = (MonoArrayBounds*)((char*)arr + size - bounds_size);
494 arr->bounds = bounds;
495 EXIT_CRITICAL_REGION;
498 EXIT_CRITICAL_REGION;
503 arr = mono_gc_alloc_obj_nolock (vtable, size);
504 if (G_UNLIKELY (!arr)) {
506 return mono_gc_out_of_memory (size);
509 arr->max_length = max_length;
511 bounds = (MonoArrayBounds*)((char*)arr + size - bounds_size);
512 arr->bounds = bounds;
520 mono_gc_alloc_string (MonoVTable *vtable, size_t size, gint32 len)
523 #ifndef DISABLE_CRITICAL_REGION
525 ENTER_CRITICAL_REGION;
526 str = mono_gc_try_alloc_obj_nolock (vtable, size);
528 /*This doesn't require fencing since EXIT_CRITICAL_REGION already does it for us*/
530 EXIT_CRITICAL_REGION;
533 EXIT_CRITICAL_REGION;
538 str = mono_gc_alloc_obj_nolock (vtable, size);
539 if (G_UNLIKELY (!str)) {
541 return mono_gc_out_of_memory (size);
552 * To be used for interned strings and possibly MonoThread, reflection handles.
553 * We may want to explicitly free these objects.
556 mono_gc_alloc_pinned_obj (MonoVTable *vtable, size_t size)
559 size = ALIGN_UP (size);
562 if (size > SGEN_MAX_SMALL_OBJ_SIZE) {
563 /* large objects are always pinned anyway */
564 p = sgen_los_alloc_large_inner (vtable, size);
566 DEBUG (9, g_assert (vtable->klass->inited));
567 p = major_collector.alloc_small_pinned_obj (size, SGEN_VTABLE_HAS_REFERENCES (vtable));
570 DEBUG (6, fprintf (gc_debug_file, "Allocated pinned object %p, vtable: %p (%s), size: %zd\n", p, vtable, vtable->klass->name, size));
571 binary_protocol_alloc_pinned (p, vtable, size);
572 mono_atomic_store_seq (p, vtable);
579 mono_gc_alloc_mature (MonoVTable *vtable)
582 size_t size = ALIGN_UP (vtable->klass->instance_size);
584 res = alloc_degraded (vtable, size, TRUE);
585 mono_atomic_store_seq (res, vtable);
587 if (G_UNLIKELY (vtable->klass->has_finalize))
588 mono_object_register_finalizer ((MonoObject*)res);
594 mono_gc_alloc_fixed (size_t size, void *descr)
596 /* FIXME: do a single allocation */
597 void *res = calloc (1, size);
600 if (!mono_gc_register_root (res, size, descr)) {
608 mono_gc_free_fixed (void* addr)
610 mono_gc_deregister_root (addr);
615 sgen_init_tlab_info (SgenThreadInfo* info)
617 #ifndef HAVE_KW_THREAD
618 SgenThreadInfo *__thread_info__ = info;
621 info->tlab_start_addr = &TLAB_START;
622 info->tlab_next_addr = &TLAB_NEXT;
623 info->tlab_temp_end_addr = &TLAB_TEMP_END;
624 info->tlab_real_end_addr = &TLAB_REAL_END;
626 #ifdef HAVE_KW_THREAD
627 tlab_next_addr = &tlab_next;
632 * Clear the thread local TLAB variables for all threads.
635 sgen_clear_tlabs (void)
637 SgenThreadInfo *info;
639 FOREACH_THREAD (info) {
640 /* A new TLAB will be allocated when the thread does its first allocation */
641 *info->tlab_start_addr = NULL;
642 *info->tlab_next_addr = NULL;
643 *info->tlab_temp_end_addr = NULL;
644 *info->tlab_real_end_addr = NULL;
648 static MonoMethod* alloc_method_cache [ATYPE_NUM];
650 #ifdef MANAGED_ALLOCATION
651 /* FIXME: Do this in the JIT, where specialized allocation sequences can be created
652 * for each class. This is currently not easy to do, as it is hard to generate basic
653 * blocks + branches, but it is easy with the linear IL codebase.
655 * For this to work we'd need to solve the TLAB race, first. Now we
656 * require the allocator to be in a few known methods to make sure
657 * that they are executed atomically via the restart mechanism.
660 create_allocator (int atype)
663 guint32 slowpath_branch, max_size_branch;
664 MonoMethodBuilder *mb;
666 MonoMethodSignature *csig;
667 static gboolean registered = FALSE;
668 int tlab_next_addr_var, new_next_var;
670 const char *name = NULL;
671 AllocatorWrapperInfo *info;
673 #ifdef HAVE_KW_THREAD
674 int tlab_next_addr_offset = -1;
675 int tlab_temp_end_offset = -1;
677 MONO_THREAD_VAR_OFFSET (tlab_next_addr, tlab_next_addr_offset);
678 MONO_THREAD_VAR_OFFSET (tlab_temp_end, tlab_temp_end_offset);
680 g_assert (tlab_next_addr_offset != -1);
681 g_assert (tlab_temp_end_offset != -1);
685 mono_register_jit_icall (mono_gc_alloc_obj, "mono_gc_alloc_obj", mono_create_icall_signature ("object ptr int"), FALSE);
686 mono_register_jit_icall (mono_gc_alloc_vector, "mono_gc_alloc_vector", mono_create_icall_signature ("object ptr int int"), FALSE);
690 if (atype == ATYPE_SMALL) {
693 } else if (atype == ATYPE_NORMAL) {
696 } else if (atype == ATYPE_VECTOR) {
698 name = "AllocVector";
700 g_assert_not_reached ();
703 csig = mono_metadata_signature_alloc (mono_defaults.corlib, num_params);
704 csig->ret = &mono_defaults.object_class->byval_arg;
705 for (i = 0; i < num_params; ++i)
706 csig->params [i] = &mono_defaults.int_class->byval_arg;
708 mb = mono_mb_new (mono_defaults.object_class, name, MONO_WRAPPER_ALLOC);
709 size_var = mono_mb_add_local (mb, &mono_defaults.int32_class->byval_arg);
710 if (atype == ATYPE_NORMAL || atype == ATYPE_SMALL) {
711 /* size = vtable->klass->instance_size; */
712 mono_mb_emit_ldarg (mb, 0);
713 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoVTable, klass));
714 mono_mb_emit_byte (mb, CEE_ADD);
715 mono_mb_emit_byte (mb, CEE_LDIND_I);
716 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoClass, instance_size));
717 mono_mb_emit_byte (mb, CEE_ADD);
718 /* FIXME: assert instance_size stays a 4 byte integer */
719 mono_mb_emit_byte (mb, CEE_LDIND_U4);
720 mono_mb_emit_stloc (mb, size_var);
721 } else if (atype == ATYPE_VECTOR) {
722 MonoExceptionClause *clause;
724 MonoClass *oom_exc_class;
727 /* n > MONO_ARRAY_MAX_INDEX -> OverflowException */
728 mono_mb_emit_ldarg (mb, 1);
729 mono_mb_emit_icon (mb, MONO_ARRAY_MAX_INDEX);
730 pos = mono_mb_emit_short_branch (mb, CEE_BLE_UN_S);
731 mono_mb_emit_exception (mb, "OverflowException", NULL);
732 mono_mb_patch_short_branch (mb, pos);
734 clause = mono_image_alloc0 (mono_defaults.corlib, sizeof (MonoExceptionClause));
735 clause->try_offset = mono_mb_get_label (mb);
737 /* vtable->klass->sizes.element_size */
738 mono_mb_emit_ldarg (mb, 0);
739 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoVTable, klass));
740 mono_mb_emit_byte (mb, CEE_ADD);
741 mono_mb_emit_byte (mb, CEE_LDIND_I);
742 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoClass, sizes.element_size));
743 mono_mb_emit_byte (mb, CEE_ADD);
744 mono_mb_emit_byte (mb, CEE_LDIND_U4);
747 mono_mb_emit_ldarg (mb, 1);
748 mono_mb_emit_byte (mb, CEE_MUL_OVF_UN);
749 /* + sizeof (MonoArray) */
750 mono_mb_emit_icon (mb, sizeof (MonoArray));
751 mono_mb_emit_byte (mb, CEE_ADD_OVF_UN);
752 mono_mb_emit_stloc (mb, size_var);
754 pos_leave = mono_mb_emit_branch (mb, CEE_LEAVE);
757 clause->flags = MONO_EXCEPTION_CLAUSE_NONE;
758 clause->try_len = mono_mb_get_pos (mb) - clause->try_offset;
759 clause->data.catch_class = mono_class_from_name (mono_defaults.corlib,
760 "System", "OverflowException");
761 g_assert (clause->data.catch_class);
762 clause->handler_offset = mono_mb_get_label (mb);
764 oom_exc_class = mono_class_from_name (mono_defaults.corlib,
765 "System", "OutOfMemoryException");
766 g_assert (oom_exc_class);
767 ctor = mono_class_get_method_from_name (oom_exc_class, ".ctor", 0);
770 mono_mb_emit_byte (mb, CEE_POP);
771 mono_mb_emit_op (mb, CEE_NEWOBJ, ctor);
772 mono_mb_emit_byte (mb, CEE_THROW);
774 clause->handler_len = mono_mb_get_pos (mb) - clause->handler_offset;
775 mono_mb_set_clauses (mb, 1, clause);
776 mono_mb_patch_branch (mb, pos_leave);
779 g_assert_not_reached ();
782 /* size += ALLOC_ALIGN - 1; */
783 mono_mb_emit_ldloc (mb, size_var);
784 mono_mb_emit_icon (mb, ALLOC_ALIGN - 1);
785 mono_mb_emit_byte (mb, CEE_ADD);
786 /* size &= ~(ALLOC_ALIGN - 1); */
787 mono_mb_emit_icon (mb, ~(ALLOC_ALIGN - 1));
788 mono_mb_emit_byte (mb, CEE_AND);
789 mono_mb_emit_stloc (mb, size_var);
791 /* if (size > MAX_SMALL_OBJ_SIZE) goto slowpath */
792 if (atype != ATYPE_SMALL) {
793 mono_mb_emit_ldloc (mb, size_var);
794 mono_mb_emit_icon (mb, MAX_SMALL_OBJ_SIZE);
795 max_size_branch = mono_mb_emit_short_branch (mb, MONO_CEE_BGT_UN_S);
799 * We need to modify tlab_next, but the JIT only supports reading, so we read
800 * another tls var holding its address instead.
803 /* tlab_next_addr (local) = tlab_next_addr (TLS var) */
804 tlab_next_addr_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
805 EMIT_TLS_ACCESS (mb, tlab_next_addr, tlab_next_addr_offset);
806 mono_mb_emit_stloc (mb, tlab_next_addr_var);
808 /* p = (void**)tlab_next; */
809 p_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
810 mono_mb_emit_ldloc (mb, tlab_next_addr_var);
811 mono_mb_emit_byte (mb, CEE_LDIND_I);
812 mono_mb_emit_stloc (mb, p_var);
814 /* new_next = (char*)p + size; */
815 new_next_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
816 mono_mb_emit_ldloc (mb, p_var);
817 mono_mb_emit_ldloc (mb, size_var);
818 mono_mb_emit_byte (mb, CEE_CONV_I);
819 mono_mb_emit_byte (mb, CEE_ADD);
820 mono_mb_emit_stloc (mb, new_next_var);
822 /* if (G_LIKELY (new_next < tlab_temp_end)) */
823 mono_mb_emit_ldloc (mb, new_next_var);
824 EMIT_TLS_ACCESS (mb, tlab_temp_end, tlab_temp_end_offset);
825 slowpath_branch = mono_mb_emit_short_branch (mb, MONO_CEE_BLT_UN_S);
828 if (atype != ATYPE_SMALL)
829 mono_mb_patch_short_branch (mb, max_size_branch);
831 mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
832 mono_mb_emit_byte (mb, CEE_MONO_NOT_TAKEN);
834 /* FIXME: mono_gc_alloc_obj takes a 'size_t' as an argument, not an int32 */
835 mono_mb_emit_ldarg (mb, 0);
836 mono_mb_emit_ldloc (mb, size_var);
837 if (atype == ATYPE_NORMAL || atype == ATYPE_SMALL) {
838 mono_mb_emit_icall (mb, mono_gc_alloc_obj);
839 } else if (atype == ATYPE_VECTOR) {
840 mono_mb_emit_ldarg (mb, 1);
841 mono_mb_emit_icall (mb, mono_gc_alloc_vector);
843 g_assert_not_reached ();
845 mono_mb_emit_byte (mb, CEE_RET);
848 mono_mb_patch_short_branch (mb, slowpath_branch);
850 /* FIXME: Memory barrier */
852 /* tlab_next = new_next */
853 mono_mb_emit_ldloc (mb, tlab_next_addr_var);
854 mono_mb_emit_ldloc (mb, new_next_var);
855 mono_mb_emit_byte (mb, CEE_STIND_I);
857 /*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. */
858 mono_mb_emit_byte ((mb), MONO_CUSTOM_PREFIX);
859 mono_mb_emit_op (mb, CEE_MONO_MEMORY_BARRIER, StoreStoreBarrier);
862 mono_mb_emit_ldloc (mb, p_var);
863 mono_mb_emit_ldarg (mb, 0);
864 mono_mb_emit_byte (mb, CEE_STIND_I);
866 if (atype == ATYPE_VECTOR) {
867 /* arr->max_length = max_length; */
868 mono_mb_emit_ldloc (mb, p_var);
869 mono_mb_emit_ldflda (mb, G_STRUCT_OFFSET (MonoArray, max_length));
870 mono_mb_emit_ldarg (mb, 1);
871 #ifdef MONO_BIG_ARRAYS
872 mono_mb_emit_byte (mb, CEE_STIND_I);
874 mono_mb_emit_byte (mb, CEE_STIND_I4);
879 We must make sure both vtable and max_length are globaly visible before returning to managed land.
881 mono_mb_emit_byte ((mb), MONO_CUSTOM_PREFIX);
882 mono_mb_emit_op (mb, CEE_MONO_MEMORY_BARRIER, StoreStoreBarrier);
885 mono_mb_emit_ldloc (mb, p_var);
886 mono_mb_emit_byte (mb, CEE_RET);
888 res = mono_mb_create_method (mb, csig, 8);
890 mono_method_get_header (res)->init_locals = FALSE;
892 info = mono_image_alloc0 (mono_defaults.corlib, sizeof (AllocatorWrapperInfo));
893 info->gc_name = "sgen";
894 info->alloc_type = atype;
895 mono_marshal_set_wrapper_info (res, info);
902 * Generate an allocator method implementing the fast path of mono_gc_alloc_obj ().
903 * The signature of the called method is:
904 * object allocate (MonoVTable *vtable)
907 mono_gc_get_managed_allocator (MonoVTable *vtable, gboolean for_box)
909 #ifdef MANAGED_ALLOCATION
910 MonoClass *klass = vtable->klass;
912 #ifdef HAVE_KW_THREAD
913 int tlab_next_offset = -1;
914 int tlab_temp_end_offset = -1;
915 MONO_THREAD_VAR_OFFSET (tlab_next, tlab_next_offset);
916 MONO_THREAD_VAR_OFFSET (tlab_temp_end, tlab_temp_end_offset);
918 if (tlab_next_offset == -1 || tlab_temp_end_offset == -1)
922 if (!mono_runtime_has_tls_get ())
924 if (klass->instance_size > tlab_size)
926 if (klass->has_finalize || klass->marshalbyref || (mono_profiler_get_events () & MONO_PROFILE_ALLOCATIONS))
930 if (klass->byval_arg.type == MONO_TYPE_STRING)
932 if (collect_before_allocs)
935 if (ALIGN_TO (klass->instance_size, ALLOC_ALIGN) < MAX_SMALL_OBJ_SIZE)
936 return mono_gc_get_managed_allocator_by_type (ATYPE_SMALL);
938 return mono_gc_get_managed_allocator_by_type (ATYPE_NORMAL);
945 mono_gc_get_managed_array_allocator (MonoVTable *vtable, int rank)
947 #ifdef MANAGED_ALLOCATION
948 MonoClass *klass = vtable->klass;
950 #ifdef HAVE_KW_THREAD
951 int tlab_next_offset = -1;
952 int tlab_temp_end_offset = -1;
953 MONO_THREAD_VAR_OFFSET (tlab_next, tlab_next_offset);
954 MONO_THREAD_VAR_OFFSET (tlab_temp_end, tlab_temp_end_offset);
956 if (tlab_next_offset == -1 || tlab_temp_end_offset == -1)
962 if (!mono_runtime_has_tls_get ())
964 if (mono_profiler_get_events () & MONO_PROFILE_ALLOCATIONS)
966 if (has_per_allocation_action)
968 g_assert (!mono_class_has_finalizer (klass) && !klass->marshalbyref);
970 return mono_gc_get_managed_allocator_by_type (ATYPE_VECTOR);
977 mono_gc_get_managed_allocator_by_type (int atype)
979 #ifdef MANAGED_ALLOCATION
982 if (!mono_runtime_has_tls_get ())
986 res = alloc_method_cache [atype];
988 res = alloc_method_cache [atype] = create_allocator (atype);
989 mono_loader_unlock ();
997 mono_gc_get_managed_allocator_types (void)
1003 sgen_is_managed_allocator (MonoMethod *method)
1007 for (i = 0; i < ATYPE_NUM; ++i)
1008 if (method == alloc_method_cache [i])
1013 #ifdef HEAVY_STATISTICS
1015 sgen_alloc_init_heavy_stats (void)
1017 mono_counters_register ("# objects allocated", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_objects_alloced);
1018 mono_counters_register ("bytes allocated", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_bytes_alloced);
1019 mono_counters_register ("bytes allocated in LOS", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_bytes_alloced_los);
1023 #endif /*HAVE_SGEN_GC*/