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.
11 * Copyright (C) 2012 Xamarin Inc
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Library General Public
15 * License 2.0 as published by the Free Software Foundation;
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License 2.0 along with this library; if not, write to the Free
24 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 * ######################################################################
29 * ######## Object allocation
30 * ######################################################################
31 * This section of code deals with allocating memory for objects.
32 * There are several ways:
33 * *) allocate large objects
34 * *) allocate normal objects
35 * *) fast lock-free allocation
36 * *) allocation of pinned objects
42 #include "metadata/sgen-gc.h"
43 #include "metadata/sgen-protocol.h"
44 #include "metadata/sgen-memory-governor.h"
45 #include "metadata/profiler-private.h"
46 #include "metadata/marshal.h"
47 #include "metadata/method-builder.h"
48 #include "utils/mono-memory-model.h"
49 #include "utils/mono-counters.h"
51 #define ALIGN_UP SGEN_ALIGN_UP
52 #define ALLOC_ALIGN SGEN_ALLOC_ALIGN
53 #define ALLOC_ALIGN_BITS SGEN_ALLOC_ALIGN_BITS
54 #define MAX_SMALL_OBJ_SIZE SGEN_MAX_SMALL_OBJ_SIZE
55 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
57 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
61 #include "mono/cil/opcode.def"
67 static gboolean use_managed_allocator = TRUE;
69 #ifdef HEAVY_STATISTICS
70 static long long stat_objects_alloced = 0;
71 static long long stat_bytes_alloced = 0;
72 static long long stat_bytes_alloced_los = 0;
77 * Allocation is done from a Thread Local Allocation Buffer (TLAB). TLABs are allocated
78 * from nursery fragments.
79 * tlab_next is the pointer to the space inside the TLAB where the next object will
81 * tlab_temp_end is the pointer to the end of the temporary space reserved for
82 * the allocation: it allows us to set the scan starts at reasonable intervals.
83 * tlab_real_end points to the end of the TLAB.
87 * FIXME: What is faster, a TLS variable pointing to a structure, or separate TLS
88 * variables for next+temp_end ?
91 static __thread char *tlab_start;
92 static __thread char *tlab_next;
93 static __thread char *tlab_temp_end;
94 static __thread char *tlab_real_end;
95 /* Used by the managed allocator/wbarrier */
96 static __thread char **tlab_next_addr;
100 #define TLAB_START tlab_start
101 #define TLAB_NEXT tlab_next
102 #define TLAB_TEMP_END tlab_temp_end
103 #define TLAB_REAL_END tlab_real_end
105 #define TLAB_START (__thread_info__->tlab_start)
106 #define TLAB_NEXT (__thread_info__->tlab_next)
107 #define TLAB_TEMP_END (__thread_info__->tlab_temp_end)
108 #define TLAB_REAL_END (__thread_info__->tlab_real_end)
112 alloc_degraded (MonoVTable *vtable, size_t size, gboolean for_mature)
114 static int last_major_gc_warned = -1;
115 static int num_degraded = 0;
120 if (last_major_gc_warned < stat_major_gcs) {
122 if (num_degraded == 1 || num_degraded == 3)
123 fprintf (stderr, "Warning: Degraded allocation. Consider increasing nursery-size if the warning persists.\n");
124 else if (num_degraded == 10)
125 fprintf (stderr, "Warning: Repeated degraded allocation. Consider increasing nursery-size.\n");
126 last_major_gc_warned = stat_major_gcs;
128 InterlockedExchangeAdd (°raded_mode, size);
129 sgen_ensure_free_space (size);
131 if (sgen_need_major_collection (size))
132 sgen_perform_collection (size, GENERATION_OLD, "mature allocation failure", !for_mature);
136 p = major_collector.alloc_degraded (vtable, size);
139 MONO_GC_MAJOR_OBJ_ALLOC_MATURE ((mword)p, size, vtable->klass->name_space, vtable->klass->name);
141 binary_protocol_alloc_degraded (p, vtable, size);
142 MONO_GC_MAJOR_OBJ_ALLOC_DEGRADED ((mword)p, size, vtable->klass->name_space, vtable->klass->name);
149 * Provide a variant that takes just the vtable for small fixed-size objects.
150 * The aligned size is already computed and stored in vt->gc_descr.
151 * Note: every SGEN_SCAN_START_SIZE or so we are given the chance to do some special
152 * processing. We can keep track of where objects start, for example,
153 * so when we scan the thread stacks for pinned objects, we can start
154 * a search for the pinned object in SGEN_SCAN_START_SIZE chunks.
157 mono_gc_alloc_obj_nolock (MonoVTable *vtable, size_t size)
159 /* FIXME: handle OOM */
164 HEAVY_STAT (++stat_objects_alloced);
165 if (size <= SGEN_MAX_SMALL_OBJ_SIZE)
166 HEAVY_STAT (stat_bytes_alloced += size);
168 HEAVY_STAT (stat_bytes_alloced_los += size);
170 size = ALIGN_UP (size);
172 g_assert (vtable->gc_descr);
174 if (G_UNLIKELY (has_per_allocation_action)) {
175 static int alloc_count;
176 int current_alloc = InterlockedIncrement (&alloc_count);
178 if (collect_before_allocs) {
179 if (((current_alloc % collect_before_allocs) == 0) && nursery_section) {
180 sgen_perform_collection (0, GENERATION_NURSERY, "collect-before-alloc-triggered", TRUE);
181 if (!degraded_mode && sgen_can_alloc_size (size) && size <= SGEN_MAX_SMALL_OBJ_SIZE) {
183 g_assert_not_reached ();
186 } else if (verify_before_allocs) {
187 if ((current_alloc % verify_before_allocs) == 0)
188 sgen_check_whole_heap_stw ();
193 * We must already have the lock here instead of after the
194 * fast path because we might be interrupted in the fast path
195 * (after confirming that new_next < TLAB_TEMP_END) by the GC,
196 * and we'll end up allocating an object in a fragment which
197 * no longer belongs to us.
199 * The managed allocator does not do this, but it's treated
200 * specially by the world-stopping code.
203 if (size > SGEN_MAX_SMALL_OBJ_SIZE) {
204 p = sgen_los_alloc_large_inner (vtable, size);
206 /* tlab_next and tlab_temp_end are TLS vars so accessing them might be expensive */
208 p = (void**)TLAB_NEXT;
209 /* FIXME: handle overflow */
210 new_next = (char*)p + size;
211 TLAB_NEXT = new_next;
213 if (G_LIKELY (new_next < TLAB_TEMP_END)) {
217 * FIXME: We might need a memory barrier here so the change to tlab_next is
218 * visible before the vtable store.
221 SGEN_LOG (6, "Allocated object %p, vtable: %p (%s), size: %zd", p, vtable, vtable->klass->name, size);
222 binary_protocol_alloc (p , vtable, size);
223 if (G_UNLIKELY (MONO_GC_NURSERY_OBJ_ALLOC_ENABLED ()))
224 MONO_GC_NURSERY_OBJ_ALLOC ((mword)p, size, vtable->klass->name_space, vtable->klass->name);
225 g_assert (*p == NULL);
226 mono_atomic_store_seq (p, vtable);
233 /* there are two cases: the object is too big or we run out of space in the TLAB */
234 /* we also reach here when the thread does its first allocation after a minor
235 * collection, since the tlab_ variables are initialized to NULL.
236 * there can be another case (from ORP), if we cooperate with the runtime a bit:
237 * objects that need finalizers can have the high bit set in their size
238 * so the above check fails and we can readily add the object to the queue.
239 * This avoids taking again the GC lock when registering, but this is moot when
240 * doing thread-local allocation, so it may not be a good idea.
242 if (TLAB_NEXT >= TLAB_REAL_END) {
243 int available_in_tlab;
245 * Run out of space in the TLAB. When this happens, some amount of space
246 * remains in the TLAB, but not enough to satisfy the current allocation
247 * request. Currently, we retire the TLAB in all cases, later we could
248 * keep it if the remaining space is above a treshold, and satisfy the
249 * allocation directly from the nursery.
252 /* when running in degraded mode, we continue allocing that way
253 * for a while, to decrease the number of useless nursery collections.
255 if (degraded_mode && degraded_mode < DEFAULT_NURSERY_SIZE)
256 return alloc_degraded (vtable, size, FALSE);
258 available_in_tlab = TLAB_REAL_END - TLAB_NEXT;
259 if (size > tlab_size || available_in_tlab > SGEN_MAX_NURSERY_WASTE) {
260 /* Allocate directly from the nursery */
262 p = sgen_nursery_alloc (size);
264 sgen_ensure_free_space (size);
266 return alloc_degraded (vtable, size, FALSE);
268 p = sgen_nursery_alloc (size);
276 if (nursery_clear_policy == CLEAR_AT_TLAB_CREATION) {
280 size_t alloc_size = 0;
282 SGEN_LOG (3, "Retire TLAB: %p-%p [%ld]", TLAB_START, TLAB_REAL_END, (long)(TLAB_REAL_END - TLAB_NEXT - size));
283 sgen_nursery_retire_region (p, available_in_tlab);
286 p = sgen_nursery_alloc_range (tlab_size, size, &alloc_size);
288 sgen_ensure_free_space (tlab_size);
290 return alloc_degraded (vtable, size, FALSE);
292 p = sgen_nursery_alloc_range (tlab_size, size, &alloc_size);
301 /* Allocate a new TLAB from the current nursery fragment */
302 TLAB_START = (char*)p;
303 TLAB_NEXT = TLAB_START;
304 TLAB_REAL_END = TLAB_START + alloc_size;
305 TLAB_TEMP_END = TLAB_START + MIN (SGEN_SCAN_START_SIZE, alloc_size);
307 if (nursery_clear_policy == CLEAR_AT_TLAB_CREATION) {
308 memset (TLAB_START, 0, 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 SGEN_LOG (5, "Expanding local alloc: %p-%p", TLAB_NEXT, TLAB_TEMP_END);
328 SGEN_LOG (6, "Allocated object %p, vtable: %p (%s), size: %zd", p, vtable, vtable->klass->name, size);
329 binary_protocol_alloc (p, vtable, size);
330 if (G_UNLIKELY (MONO_GC_MAJOR_OBJ_ALLOC_LARGE_ENABLED ()|| MONO_GC_NURSERY_OBJ_ALLOC_ENABLED ())) {
331 if (size > SGEN_MAX_SMALL_OBJ_SIZE)
332 MONO_GC_MAJOR_OBJ_ALLOC_LARGE ((mword)p, size, vtable->klass->name_space, vtable->klass->name);
334 MONO_GC_NURSERY_OBJ_ALLOC ((mword)p, size, vtable->klass->name_space, vtable->klass->name);
336 mono_atomic_store_seq (p, vtable);
343 mono_gc_try_alloc_obj_nolock (MonoVTable *vtable, size_t size)
349 size = ALIGN_UP (size);
350 SGEN_ASSERT (9, size >= sizeof (MonoObject), "Object too small");
352 g_assert (vtable->gc_descr);
353 if (size > SGEN_MAX_SMALL_OBJ_SIZE)
356 if (G_UNLIKELY (size > tlab_size)) {
357 /* Allocate directly from the nursery */
358 p = sgen_nursery_alloc (size);
361 sgen_set_nursery_scan_start ((char*)p);
363 /*FIXME we should use weak memory ops here. Should help specially on x86. */
364 if (nursery_clear_policy == CLEAR_AT_TLAB_CREATION)
367 int available_in_tlab;
369 /* tlab_next and tlab_temp_end are TLS vars so accessing them might be expensive */
371 p = (void**)TLAB_NEXT;
372 /* FIXME: handle overflow */
373 new_next = (char*)p + size;
375 real_end = TLAB_REAL_END;
376 available_in_tlab = real_end - (char*)p;
378 if (G_LIKELY (new_next < real_end)) {
379 TLAB_NEXT = new_next;
381 /* Second case, we overflowed temp end */
382 if (G_UNLIKELY (new_next >= TLAB_TEMP_END)) {
383 sgen_set_nursery_scan_start (new_next);
384 /* we just bump tlab_temp_end as well */
385 TLAB_TEMP_END = MIN (TLAB_REAL_END, TLAB_NEXT + SGEN_SCAN_START_SIZE);
386 SGEN_LOG (5, "Expanding local alloc: %p-%p", TLAB_NEXT, TLAB_TEMP_END);
388 } else if (available_in_tlab > SGEN_MAX_NURSERY_WASTE) {
389 /* Allocate directly from the nursery */
390 p = sgen_nursery_alloc (size);
394 if (nursery_clear_policy == CLEAR_AT_TLAB_CREATION)
397 size_t alloc_size = 0;
399 sgen_nursery_retire_region (p, available_in_tlab);
400 new_next = sgen_nursery_alloc_range (tlab_size, size, &alloc_size);
401 p = (void**)new_next;
405 TLAB_START = (char*)new_next;
406 TLAB_NEXT = new_next + size;
407 TLAB_REAL_END = new_next + alloc_size;
408 TLAB_TEMP_END = new_next + MIN (SGEN_SCAN_START_SIZE, alloc_size);
409 sgen_set_nursery_scan_start ((char*)p);
411 if (nursery_clear_policy == CLEAR_AT_TLAB_CREATION)
412 memset (new_next, 0, alloc_size);
414 MONO_GC_NURSERY_TLAB_ALLOC ((mword)new_next, alloc_size);
418 HEAVY_STAT (++stat_objects_alloced);
419 HEAVY_STAT (stat_bytes_alloced += size);
421 SGEN_LOG (6, "Allocated object %p, vtable: %p (%s), size: %zd", p, vtable, vtable->klass->name, size);
422 binary_protocol_alloc (p, vtable, size);
423 if (G_UNLIKELY (MONO_GC_NURSERY_OBJ_ALLOC_ENABLED ()))
424 MONO_GC_NURSERY_OBJ_ALLOC ((mword)p, size, vtable->klass->name_space, vtable->klass->name);
425 g_assert (*p == NULL); /* FIXME disable this in non debug builds */
427 mono_atomic_store_seq (p, vtable);
433 mono_gc_alloc_obj (MonoVTable *vtable, size_t size)
436 #ifndef DISABLE_CRITICAL_REGION
439 if (G_UNLIKELY (has_per_allocation_action)) {
440 static int alloc_count;
441 int current_alloc = InterlockedIncrement (&alloc_count);
443 if (verify_before_allocs) {
444 if ((current_alloc % verify_before_allocs) == 0)
445 sgen_check_whole_heap_stw ();
447 if (collect_before_allocs) {
448 if (((current_alloc % collect_before_allocs) == 0) && nursery_section) {
450 sgen_perform_collection (0, GENERATION_NURSERY, "collect-before-alloc-triggered", TRUE);
456 ENTER_CRITICAL_REGION;
457 res = mono_gc_try_alloc_obj_nolock (vtable, size);
459 EXIT_CRITICAL_REGION;
462 EXIT_CRITICAL_REGION;
465 res = mono_gc_alloc_obj_nolock (vtable, size);
467 if (G_UNLIKELY (!res))
468 return mono_gc_out_of_memory (size);
473 mono_gc_alloc_vector (MonoVTable *vtable, size_t size, uintptr_t max_length)
476 #ifndef DISABLE_CRITICAL_REGION
478 ENTER_CRITICAL_REGION;
479 arr = mono_gc_try_alloc_obj_nolock (vtable, size);
481 /*This doesn't require fencing since EXIT_CRITICAL_REGION already does it for us*/
482 arr->max_length = max_length;
483 EXIT_CRITICAL_REGION;
486 EXIT_CRITICAL_REGION;
491 arr = mono_gc_alloc_obj_nolock (vtable, size);
492 if (G_UNLIKELY (!arr)) {
494 return mono_gc_out_of_memory (size);
497 arr->max_length = max_length;
505 mono_gc_alloc_array (MonoVTable *vtable, size_t size, uintptr_t max_length, uintptr_t bounds_size)
508 MonoArrayBounds *bounds;
510 #ifndef DISABLE_CRITICAL_REGION
512 ENTER_CRITICAL_REGION;
513 arr = mono_gc_try_alloc_obj_nolock (vtable, size);
515 /*This doesn't require fencing since EXIT_CRITICAL_REGION already does it for us*/
516 arr->max_length = max_length;
518 bounds = (MonoArrayBounds*)((char*)arr + size - bounds_size);
519 arr->bounds = bounds;
520 EXIT_CRITICAL_REGION;
523 EXIT_CRITICAL_REGION;
528 arr = mono_gc_alloc_obj_nolock (vtable, size);
529 if (G_UNLIKELY (!arr)) {
531 return mono_gc_out_of_memory (size);
534 arr->max_length = max_length;
536 bounds = (MonoArrayBounds*)((char*)arr + size - bounds_size);
537 arr->bounds = bounds;
545 mono_gc_alloc_string (MonoVTable *vtable, size_t size, gint32 len)
548 #ifndef DISABLE_CRITICAL_REGION
550 ENTER_CRITICAL_REGION;
551 str = mono_gc_try_alloc_obj_nolock (vtable, size);
553 /*This doesn't require fencing since EXIT_CRITICAL_REGION already does it for us*/
555 EXIT_CRITICAL_REGION;
558 EXIT_CRITICAL_REGION;
563 str = mono_gc_alloc_obj_nolock (vtable, size);
564 if (G_UNLIKELY (!str)) {
566 return mono_gc_out_of_memory (size);
577 * To be used for interned strings and possibly MonoThread, reflection handles.
578 * We may want to explicitly free these objects.
581 mono_gc_alloc_pinned_obj (MonoVTable *vtable, size_t size)
584 size = ALIGN_UP (size);
587 if (size > SGEN_MAX_SMALL_OBJ_SIZE) {
588 /* large objects are always pinned anyway */
589 p = sgen_los_alloc_large_inner (vtable, size);
591 SGEN_ASSERT (9, vtable->klass->inited, "class %s:%s is not initialized", vtable->klass->name_space, vtable->klass->name);
592 p = major_collector.alloc_small_pinned_obj (vtable, size, SGEN_VTABLE_HAS_REFERENCES (vtable));
595 SGEN_LOG (6, "Allocated pinned object %p, vtable: %p (%s), size: %zd", p, vtable, vtable->klass->name, size);
596 if (size > SGEN_MAX_SMALL_OBJ_SIZE)
597 MONO_GC_MAJOR_OBJ_ALLOC_LARGE ((mword)p, size, vtable->klass->name_space, vtable->klass->name);
599 MONO_GC_MAJOR_OBJ_ALLOC_PINNED ((mword)p, size, vtable->klass->name_space, vtable->klass->name);
600 binary_protocol_alloc_pinned (p, vtable, size);
607 mono_gc_alloc_mature (MonoVTable *vtable)
610 size_t size = ALIGN_UP (vtable->klass->instance_size);
612 res = alloc_degraded (vtable, size, TRUE);
614 if (G_UNLIKELY (vtable->klass->has_finalize))
615 mono_object_register_finalizer ((MonoObject*)res);
621 mono_gc_alloc_fixed (size_t size, void *descr)
623 /* FIXME: do a single allocation */
624 void *res = calloc (1, size);
627 if (!mono_gc_register_root (res, size, descr)) {
635 mono_gc_free_fixed (void* addr)
637 mono_gc_deregister_root (addr);
642 sgen_init_tlab_info (SgenThreadInfo* info)
644 #ifndef HAVE_KW_THREAD
645 SgenThreadInfo *__thread_info__ = info;
648 info->tlab_start_addr = &TLAB_START;
649 info->tlab_next_addr = &TLAB_NEXT;
650 info->tlab_temp_end_addr = &TLAB_TEMP_END;
651 info->tlab_real_end_addr = &TLAB_REAL_END;
653 #ifdef HAVE_KW_THREAD
654 tlab_next_addr = &tlab_next;
659 * Clear the thread local TLAB variables for all threads.
662 sgen_clear_tlabs (void)
664 SgenThreadInfo *info;
666 FOREACH_THREAD (info) {
667 /* A new TLAB will be allocated when the thread does its first allocation */
668 *info->tlab_start_addr = NULL;
669 *info->tlab_next_addr = NULL;
670 *info->tlab_temp_end_addr = NULL;
671 *info->tlab_real_end_addr = NULL;
675 static MonoMethod* alloc_method_cache [ATYPE_NUM];
677 #ifdef MANAGED_ALLOCATION
678 /* FIXME: Do this in the JIT, where specialized allocation sequences can be created
679 * for each class. This is currently not easy to do, as it is hard to generate basic
680 * blocks + branches, but it is easy with the linear IL codebase.
682 * For this to work we'd need to solve the TLAB race, first. Now we
683 * require the allocator to be in a few known methods to make sure
684 * that they are executed atomically via the restart mechanism.
687 create_allocator (int atype)
690 guint32 slowpath_branch, max_size_branch;
691 MonoMethodBuilder *mb;
693 MonoMethodSignature *csig;
694 static gboolean registered = FALSE;
695 int tlab_next_addr_var, new_next_var;
697 const char *name = NULL;
698 AllocatorWrapperInfo *info;
700 #ifdef HAVE_KW_THREAD
701 int tlab_next_addr_offset = -1;
702 int tlab_temp_end_offset = -1;
704 MONO_THREAD_VAR_OFFSET (tlab_next_addr, tlab_next_addr_offset);
705 MONO_THREAD_VAR_OFFSET (tlab_temp_end, tlab_temp_end_offset);
707 mono_tls_key_set_offset (TLS_KEY_SGEN_TLAB_NEXT_ADDR, tlab_next_addr_offset);
708 mono_tls_key_set_offset (TLS_KEY_SGEN_TLAB_TEMP_END, tlab_temp_end_offset);
710 g_assert (tlab_next_addr_offset != -1);
711 g_assert (tlab_temp_end_offset != -1);
715 mono_register_jit_icall (mono_gc_alloc_obj, "mono_gc_alloc_obj", mono_create_icall_signature ("object ptr int"), FALSE);
716 mono_register_jit_icall (mono_gc_alloc_vector, "mono_gc_alloc_vector", mono_create_icall_signature ("object ptr int int"), FALSE);
717 mono_register_jit_icall (mono_gc_alloc_string, "mono_gc_alloc_string", mono_create_icall_signature ("object ptr int int32"), FALSE);
721 if (atype == ATYPE_SMALL) {
724 } else if (atype == ATYPE_NORMAL) {
727 } else if (atype == ATYPE_VECTOR) {
729 name = "AllocVector";
730 } else if (atype == ATYPE_STRING) {
732 name = "AllocString";
734 g_assert_not_reached ();
737 csig = mono_metadata_signature_alloc (mono_defaults.corlib, num_params);
738 if (atype == ATYPE_STRING) {
739 csig->ret = &mono_defaults.string_class->byval_arg;
740 csig->params [0] = &mono_defaults.int_class->byval_arg;
741 csig->params [1] = &mono_defaults.int32_class->byval_arg;
743 csig->ret = &mono_defaults.object_class->byval_arg;
744 for (i = 0; i < num_params; ++i)
745 csig->params [i] = &mono_defaults.int_class->byval_arg;
748 mb = mono_mb_new (mono_defaults.object_class, name, MONO_WRAPPER_ALLOC);
751 size_var = mono_mb_add_local (mb, &mono_defaults.int32_class->byval_arg);
752 if (atype == ATYPE_NORMAL || atype == ATYPE_SMALL) {
753 /* size = vtable->klass->instance_size; */
754 mono_mb_emit_ldarg (mb, 0);
755 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoVTable, klass));
756 mono_mb_emit_byte (mb, CEE_ADD);
757 mono_mb_emit_byte (mb, CEE_LDIND_I);
758 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoClass, instance_size));
759 mono_mb_emit_byte (mb, CEE_ADD);
760 /* FIXME: assert instance_size stays a 4 byte integer */
761 mono_mb_emit_byte (mb, CEE_LDIND_U4);
762 mono_mb_emit_stloc (mb, size_var);
763 } else if (atype == ATYPE_VECTOR) {
764 MonoExceptionClause *clause;
766 MonoClass *oom_exc_class;
769 /* n > MONO_ARRAY_MAX_INDEX -> OverflowException */
770 mono_mb_emit_ldarg (mb, 1);
771 mono_mb_emit_icon (mb, MONO_ARRAY_MAX_INDEX);
772 pos = mono_mb_emit_short_branch (mb, CEE_BLE_UN_S);
773 mono_mb_emit_exception (mb, "OverflowException", NULL);
774 mono_mb_patch_short_branch (mb, pos);
776 clause = mono_image_alloc0 (mono_defaults.corlib, sizeof (MonoExceptionClause));
777 clause->try_offset = mono_mb_get_label (mb);
779 /* vtable->klass->sizes.element_size */
780 mono_mb_emit_ldarg (mb, 0);
781 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoVTable, klass));
782 mono_mb_emit_byte (mb, CEE_ADD);
783 mono_mb_emit_byte (mb, CEE_LDIND_I);
784 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoClass, sizes.element_size));
785 mono_mb_emit_byte (mb, CEE_ADD);
786 mono_mb_emit_byte (mb, CEE_LDIND_U4);
789 mono_mb_emit_ldarg (mb, 1);
790 mono_mb_emit_byte (mb, CEE_MUL_OVF_UN);
791 /* + sizeof (MonoArray) */
792 mono_mb_emit_icon (mb, sizeof (MonoArray));
793 mono_mb_emit_byte (mb, CEE_ADD_OVF_UN);
794 mono_mb_emit_stloc (mb, size_var);
796 pos_leave = mono_mb_emit_branch (mb, CEE_LEAVE);
799 clause->flags = MONO_EXCEPTION_CLAUSE_NONE;
800 clause->try_len = mono_mb_get_pos (mb) - clause->try_offset;
801 clause->data.catch_class = mono_class_from_name (mono_defaults.corlib,
802 "System", "OverflowException");
803 g_assert (clause->data.catch_class);
804 clause->handler_offset = mono_mb_get_label (mb);
806 oom_exc_class = mono_class_from_name (mono_defaults.corlib,
807 "System", "OutOfMemoryException");
808 g_assert (oom_exc_class);
809 ctor = mono_class_get_method_from_name (oom_exc_class, ".ctor", 0);
812 mono_mb_emit_byte (mb, CEE_POP);
813 mono_mb_emit_op (mb, CEE_NEWOBJ, ctor);
814 mono_mb_emit_byte (mb, CEE_THROW);
816 clause->handler_len = mono_mb_get_pos (mb) - clause->handler_offset;
817 mono_mb_set_clauses (mb, 1, clause);
818 mono_mb_patch_branch (mb, pos_leave);
820 } else if (atype == ATYPE_STRING) {
821 /* a string allocator method takes the args: (vtable, len) */
822 /* bytes = (sizeof (MonoString) + ((len + 1) * 2)); */
823 mono_mb_emit_ldarg (mb, 1);
824 mono_mb_emit_icon (mb, 1);
825 mono_mb_emit_byte (mb, MONO_CEE_SHL);
826 //WE manually fold the above + 2 here
827 mono_mb_emit_icon (mb, sizeof (MonoString) + 2);
828 mono_mb_emit_byte (mb, CEE_ADD);
829 mono_mb_emit_stloc (mb, size_var);
831 g_assert_not_reached ();
834 /* size += ALLOC_ALIGN - 1; */
835 mono_mb_emit_ldloc (mb, size_var);
836 mono_mb_emit_icon (mb, ALLOC_ALIGN - 1);
837 mono_mb_emit_byte (mb, CEE_ADD);
838 /* size &= ~(ALLOC_ALIGN - 1); */
839 mono_mb_emit_icon (mb, ~(ALLOC_ALIGN - 1));
840 mono_mb_emit_byte (mb, CEE_AND);
841 mono_mb_emit_stloc (mb, size_var);
843 /* if (size > MAX_SMALL_OBJ_SIZE) goto slowpath */
844 if (atype != ATYPE_SMALL) {
845 mono_mb_emit_ldloc (mb, size_var);
846 mono_mb_emit_icon (mb, MAX_SMALL_OBJ_SIZE);
847 max_size_branch = mono_mb_emit_short_branch (mb, MONO_CEE_BGT_UN_S);
851 * We need to modify tlab_next, but the JIT only supports reading, so we read
852 * another tls var holding its address instead.
855 /* tlab_next_addr (local) = tlab_next_addr (TLS var) */
856 tlab_next_addr_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
857 EMIT_TLS_ACCESS (mb, tlab_next_addr, TLS_KEY_SGEN_TLAB_NEXT_ADDR);
858 mono_mb_emit_stloc (mb, tlab_next_addr_var);
860 /* p = (void**)tlab_next; */
861 p_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
862 mono_mb_emit_ldloc (mb, tlab_next_addr_var);
863 mono_mb_emit_byte (mb, CEE_LDIND_I);
864 mono_mb_emit_stloc (mb, p_var);
866 /* new_next = (char*)p + size; */
867 new_next_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
868 mono_mb_emit_ldloc (mb, p_var);
869 mono_mb_emit_ldloc (mb, size_var);
870 mono_mb_emit_byte (mb, CEE_CONV_I);
871 mono_mb_emit_byte (mb, CEE_ADD);
872 mono_mb_emit_stloc (mb, new_next_var);
874 /* if (G_LIKELY (new_next < tlab_temp_end)) */
875 mono_mb_emit_ldloc (mb, new_next_var);
876 EMIT_TLS_ACCESS (mb, tlab_temp_end, TLS_KEY_SGEN_TLAB_TEMP_END);
877 slowpath_branch = mono_mb_emit_short_branch (mb, MONO_CEE_BLT_UN_S);
880 if (atype != ATYPE_SMALL)
881 mono_mb_patch_short_branch (mb, max_size_branch);
883 mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
884 mono_mb_emit_byte (mb, CEE_MONO_NOT_TAKEN);
886 /* FIXME: mono_gc_alloc_obj takes a 'size_t' as an argument, not an int32 */
887 mono_mb_emit_ldarg (mb, 0);
888 mono_mb_emit_ldloc (mb, size_var);
889 if (atype == ATYPE_NORMAL || atype == ATYPE_SMALL) {
890 mono_mb_emit_icall (mb, mono_gc_alloc_obj);
891 } else if (atype == ATYPE_VECTOR) {
892 mono_mb_emit_ldarg (mb, 1);
893 mono_mb_emit_icall (mb, mono_gc_alloc_vector);
894 } else if (atype == ATYPE_STRING) {
895 mono_mb_emit_ldarg (mb, 1);
896 mono_mb_emit_icall (mb, mono_gc_alloc_string);
898 g_assert_not_reached ();
900 mono_mb_emit_byte (mb, CEE_RET);
903 mono_mb_patch_short_branch (mb, slowpath_branch);
905 /* FIXME: Memory barrier */
907 /* tlab_next = new_next */
908 mono_mb_emit_ldloc (mb, tlab_next_addr_var);
909 mono_mb_emit_ldloc (mb, new_next_var);
910 mono_mb_emit_byte (mb, CEE_STIND_I);
912 /*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. */
913 mono_mb_emit_byte ((mb), MONO_CUSTOM_PREFIX);
914 mono_mb_emit_op (mb, CEE_MONO_MEMORY_BARRIER, StoreStoreBarrier);
917 mono_mb_emit_ldloc (mb, p_var);
918 mono_mb_emit_ldarg (mb, 0);
919 mono_mb_emit_byte (mb, CEE_STIND_I);
921 if (atype == ATYPE_VECTOR) {
922 /* arr->max_length = max_length; */
923 mono_mb_emit_ldloc (mb, p_var);
924 mono_mb_emit_ldflda (mb, G_STRUCT_OFFSET (MonoArray, max_length));
925 mono_mb_emit_ldarg (mb, 1);
926 #ifdef MONO_BIG_ARRAYS
927 mono_mb_emit_byte (mb, CEE_STIND_I);
929 mono_mb_emit_byte (mb, CEE_STIND_I4);
931 } else if (atype == ATYPE_STRING) {
932 /* need to set length and clear the last char */
933 /* s->length = len; */
934 mono_mb_emit_ldloc (mb, p_var);
935 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoString, length));
936 mono_mb_emit_byte (mb, MONO_CEE_ADD);
937 mono_mb_emit_ldarg (mb, 1);
938 mono_mb_emit_byte (mb, MONO_CEE_STIND_I4);
939 /* s->chars [len] = 0; */
940 mono_mb_emit_ldloc (mb, p_var);
941 mono_mb_emit_ldloc (mb, size_var);
942 mono_mb_emit_icon (mb, 2);
943 mono_mb_emit_byte (mb, MONO_CEE_SUB);
944 mono_mb_emit_byte (mb, MONO_CEE_ADD);
945 mono_mb_emit_icon (mb, 0);
946 mono_mb_emit_byte (mb, MONO_CEE_STIND_I2);
950 We must make sure both vtable and max_length are globaly visible before returning to managed land.
952 mono_mb_emit_byte ((mb), MONO_CUSTOM_PREFIX);
953 mono_mb_emit_op (mb, CEE_MONO_MEMORY_BARRIER, StoreStoreBarrier);
956 mono_mb_emit_ldloc (mb, p_var);
957 mono_mb_emit_byte (mb, CEE_RET);
960 res = mono_mb_create_method (mb, csig, 8);
962 mono_method_get_header (res)->init_locals = FALSE;
964 info = mono_image_alloc0 (mono_defaults.corlib, sizeof (AllocatorWrapperInfo));
965 info->gc_name = "sgen";
966 info->alloc_type = atype;
967 mono_marshal_set_wrapper_info (res, info);
974 * Generate an allocator method implementing the fast path of mono_gc_alloc_obj ().
975 * The signature of the called method is:
976 * object allocate (MonoVTable *vtable)
979 mono_gc_get_managed_allocator (MonoClass *klass, gboolean for_box)
981 #ifdef MANAGED_ALLOCATION
983 #ifdef HAVE_KW_THREAD
984 int tlab_next_offset = -1;
985 int tlab_temp_end_offset = -1;
986 MONO_THREAD_VAR_OFFSET (tlab_next, tlab_next_offset);
987 MONO_THREAD_VAR_OFFSET (tlab_temp_end, tlab_temp_end_offset);
989 if (tlab_next_offset == -1 || tlab_temp_end_offset == -1)
992 if (collect_before_allocs)
994 if (!mono_runtime_has_tls_get ())
996 if (klass->instance_size > tlab_size)
999 if (klass->has_finalize || mono_class_is_marshalbyref (klass) || (mono_profiler_get_events () & MONO_PROFILE_ALLOCATIONS))
1003 if (klass->byval_arg.type == MONO_TYPE_STRING)
1004 return mono_gc_get_managed_allocator_by_type (ATYPE_STRING);
1005 /* Generic classes have dynamic field and can go above MAX_SMALL_OBJ_SIZE. */
1006 if (ALIGN_TO (klass->instance_size, ALLOC_ALIGN) < MAX_SMALL_OBJ_SIZE && !mono_class_is_open_constructed_type (&klass->byval_arg))
1007 return mono_gc_get_managed_allocator_by_type (ATYPE_SMALL);
1009 return mono_gc_get_managed_allocator_by_type (ATYPE_NORMAL);
1016 mono_gc_get_managed_array_allocator (MonoClass *klass)
1018 #ifdef MANAGED_ALLOCATION
1019 #ifdef HAVE_KW_THREAD
1020 int tlab_next_offset = -1;
1021 int tlab_temp_end_offset = -1;
1022 MONO_THREAD_VAR_OFFSET (tlab_next, tlab_next_offset);
1023 MONO_THREAD_VAR_OFFSET (tlab_temp_end, tlab_temp_end_offset);
1025 if (tlab_next_offset == -1 || tlab_temp_end_offset == -1)
1029 if (klass->rank != 1)
1031 if (!mono_runtime_has_tls_get ())
1033 if (mono_profiler_get_events () & MONO_PROFILE_ALLOCATIONS)
1035 if (has_per_allocation_action)
1037 g_assert (!mono_class_has_finalizer (klass) && !mono_class_is_marshalbyref (klass));
1039 return mono_gc_get_managed_allocator_by_type (ATYPE_VECTOR);
1046 sgen_set_use_managed_allocator (gboolean flag)
1048 use_managed_allocator = flag;
1052 mono_gc_get_managed_allocator_by_type (int atype)
1054 #ifdef MANAGED_ALLOCATION
1057 if (!use_managed_allocator)
1060 if (!mono_runtime_has_tls_get ())
1063 res = alloc_method_cache [atype];
1067 res = create_allocator (atype);
1069 if (alloc_method_cache [atype]) {
1070 mono_free_method (res);
1071 res = alloc_method_cache [atype];
1073 mono_memory_barrier ();
1074 alloc_method_cache [atype] = res;
1085 mono_gc_get_managed_allocator_types (void)
1091 sgen_is_managed_allocator (MonoMethod *method)
1095 for (i = 0; i < ATYPE_NUM; ++i)
1096 if (method == alloc_method_cache [i])
1102 sgen_has_managed_allocator (void)
1106 for (i = 0; i < ATYPE_NUM; ++i)
1107 if (alloc_method_cache [i])
1112 #ifdef HEAVY_STATISTICS
1114 sgen_alloc_init_heavy_stats (void)
1116 mono_counters_register ("# objects allocated", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_objects_alloced);
1117 mono_counters_register ("bytes allocated", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_bytes_alloced);
1118 mono_counters_register ("bytes allocated in LOS", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_bytes_alloced_los);
1122 #endif /*HAVE_SGEN_GC*/