do not check order sequence if option /order was not used
[mono.git] / mono / metadata / sgen-alloc.c
1 /*
2  * sgen-alloc.c: Object allocation routines + managed allocators
3  *
4  * Author:
5  *      Paolo Molaro (lupus@ximian.com)
6  *  Rodrigo Kumpera (kumpera@gmail.com)
7  *
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  *
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:
19  * 
20  * The above copyright notice and this permission notice shall be
21  * included in all copies or substantial portions of the Software.
22  * 
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.
30  */
31
32 /*
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
42  */
43
44 #include "config.h"
45 #ifdef HAVE_SGEN_GC
46
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"
55
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))
61
62 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
63         a = i,
64
65 enum {
66 #include "mono/cil/opcode.def"
67         CEE_LAST
68 };
69
70 #undef OPDEF
71
72 static gboolean use_managed_allocator = TRUE;
73
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;
78
79 #endif
80
81 /*
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 
85  * be allocated.
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.
89  */
90
91 /*
92  * FIXME: What is faster, a TLS variable pointing to a structure, or separate TLS 
93  * variables for next+temp_end ?
94  */
95 #ifdef HAVE_KW_THREAD
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;
102 #endif
103
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
109 #else
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)
114 #endif
115
116 static void*
117 alloc_degraded (MonoVTable *vtable, size_t size, gboolean for_mature)
118 {
119         static int last_major_gc_warned = -1;
120         static int num_degraded = 0;
121
122         void *p;
123
124         if (!for_mature) {
125                 if (last_major_gc_warned < stat_major_gcs) {
126                         ++num_degraded;
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;
132                 }
133                 InterlockedExchangeAdd (&degraded_mode, size);
134                 sgen_ensure_free_space (size);
135         } else {
136                 if (sgen_need_major_collection (size))
137                         sgen_perform_collection (size, GENERATION_OLD, "mature allocation failure");
138         }
139
140
141         p = major_collector.alloc_degraded (vtable, size);
142
143         if (for_mature) {
144                 MONO_GC_MAJOR_OBJ_ALLOC_MATURE ((mword)p, size, vtable->klass->name_space, vtable->klass->name);
145         } else {
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);
148         }
149
150         return p;
151 }
152
153 /*
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.
160  */
161 static void*
162 mono_gc_alloc_obj_nolock (MonoVTable *vtable, size_t size)
163 {
164         /* FIXME: handle OOM */
165         void **p;
166         char *new_next;
167         TLAB_ACCESS_INIT;
168
169         HEAVY_STAT (++stat_objects_alloced);
170         if (size <= SGEN_MAX_SMALL_OBJ_SIZE)
171                 HEAVY_STAT (stat_bytes_alloced += size);
172         else
173                 HEAVY_STAT (stat_bytes_alloced_los += size);
174
175         size = ALIGN_UP (size);
176
177         g_assert (vtable->gc_descr);
178
179         if (G_UNLIKELY (has_per_allocation_action)) {
180                 static int alloc_count;
181                 int current_alloc = InterlockedIncrement (&alloc_count);
182
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) {
187                                         // FIXME:
188                                         g_assert_not_reached ();
189                                 }
190                         }
191                 } else if (verify_before_allocs) {
192                         if ((current_alloc % verify_before_allocs) == 0)
193                                 sgen_check_whole_heap_stw ();
194                 }
195         }
196
197         /*
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.
203          *
204          * The managed allocator does not do this, but it's treated
205          * specially by the world-stopping code.
206          */
207
208         if (size > SGEN_MAX_SMALL_OBJ_SIZE) {
209                 p = sgen_los_alloc_large_inner (vtable, size);
210         } else {
211                 /* tlab_next and tlab_temp_end are TLS vars so accessing them might be expensive */
212
213                 p = (void**)TLAB_NEXT;
214                 /* FIXME: handle overflow */
215                 new_next = (char*)p + size;
216                 TLAB_NEXT = new_next;
217
218                 if (G_LIKELY (new_next < TLAB_TEMP_END)) {
219                         /* Fast path */
220
221                         /* 
222                          * FIXME: We might need a memory barrier here so the change to tlab_next is 
223                          * visible before the vtable store.
224                          */
225
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);
232
233                         return p;
234                 }
235
236                 /* Slow path */
237
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.
246                  */
247                 if (TLAB_NEXT >= TLAB_REAL_END) {
248                         int available_in_tlab;
249                         /* 
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.
255                          */
256                         TLAB_NEXT -= size;
257                         /* when running in degraded mode, we continue allocing that way
258                          * for a while, to decrease the number of useless nursery collections.
259                          */
260                         if (degraded_mode && degraded_mode < DEFAULT_NURSERY_SIZE)
261                                 return alloc_degraded (vtable, size, FALSE);
262
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 */
266                                 do {
267                                         p = sgen_nursery_alloc (size);
268                                         if (!p) {
269                                                 sgen_ensure_free_space (size);
270                                                 if (degraded_mode)
271                                                         return alloc_degraded (vtable, size, FALSE);
272                                                 else
273                                                         p = sgen_nursery_alloc (size);
274                                         }
275                                 } while (!p);
276                                 if (!p) {
277                                         // no space left
278                                         g_assert (0);
279                                 }
280
281                                 if (nursery_clear_policy == CLEAR_AT_TLAB_CREATION) {
282                                         memset (p, 0, size);
283                                 }
284                         } else {
285                                 size_t alloc_size = 0;
286                                 if (TLAB_START)
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);
289
290                                 do {
291                                         p = sgen_nursery_alloc_range (tlab_size, size, &alloc_size);
292                                         if (!p) {
293                                                 sgen_ensure_free_space (tlab_size);
294                                                 if (degraded_mode)
295                                                         return alloc_degraded (vtable, size, FALSE);
296                                                 else
297                                                         p = sgen_nursery_alloc_range (tlab_size, size, &alloc_size);
298                                         }
299                                 } while (!p);
300                                         
301                                 if (!p) {
302                                         // no space left
303                                         g_assert (0);
304                                 }
305
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);
311
312                                 if (nursery_clear_policy == CLEAR_AT_TLAB_CREATION) {
313                                         memset (TLAB_START, 0, alloc_size);
314                                 }
315
316                                 /* Allocate from the TLAB */
317                                 p = (void*)TLAB_NEXT;
318                                 TLAB_NEXT += size;
319                                 sgen_set_nursery_scan_start ((char*)p);
320                         }
321                 } else {
322                         /* Reached tlab_temp_end */
323
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));
329                 }
330         }
331
332         if (G_LIKELY (p)) {
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);
338                         else
339                                 MONO_GC_NURSERY_OBJ_ALLOC ((mword)p, size, vtable->klass->name_space, vtable->klass->name);
340                 }
341                 mono_atomic_store_seq (p, vtable);
342         }
343
344         return p;
345 }
346
347 static void*
348 mono_gc_try_alloc_obj_nolock (MonoVTable *vtable, size_t size)
349 {
350         void **p;
351         char *new_next;
352         TLAB_ACCESS_INIT;
353
354         size = ALIGN_UP (size);
355
356         g_assert (vtable->gc_descr);
357         if (size > SGEN_MAX_SMALL_OBJ_SIZE)
358                 return NULL;
359
360         if (G_UNLIKELY (size > tlab_size)) {
361                 /* Allocate directly from the nursery */
362                 p = sgen_nursery_alloc (size);
363                 if (!p)
364                         return NULL;
365                 sgen_set_nursery_scan_start ((char*)p);
366
367                 /*FIXME we should use weak memory ops here. Should help specially on x86. */
368                 if (nursery_clear_policy == CLEAR_AT_TLAB_CREATION)
369                         memset (p, 0, size);
370         } else {
371                 int available_in_tlab;
372                 char *real_end;
373                 /* tlab_next and tlab_temp_end are TLS vars so accessing them might be expensive */
374
375                 p = (void**)TLAB_NEXT;
376                 /* FIXME: handle overflow */
377                 new_next = (char*)p + size;
378
379                 real_end = TLAB_REAL_END;
380                 available_in_tlab = real_end - (char*)p;
381
382                 if (G_LIKELY (new_next < real_end)) {
383                         TLAB_NEXT = new_next;
384
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));         
391                         }
392                 } else if (available_in_tlab > SGEN_MAX_NURSERY_WASTE) {
393                         /* Allocate directly from the nursery */
394                         p = sgen_nursery_alloc (size);
395                         if (!p)
396                                 return NULL;
397
398                         if (nursery_clear_policy == CLEAR_AT_TLAB_CREATION)
399                                 memset (p, 0, size);                    
400                 } else {
401                         size_t alloc_size = 0;
402
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;
406                         if (!p)
407                                 return NULL;
408
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);
414
415                         if (nursery_clear_policy == CLEAR_AT_TLAB_CREATION)
416                                 memset (new_next, 0, alloc_size);
417
418                         MONO_GC_NURSERY_TLAB_ALLOC ((mword)new_next, alloc_size);
419                 }
420         }
421
422         HEAVY_STAT (++stat_objects_alloced);
423         HEAVY_STAT (stat_bytes_alloced += size);
424
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 */
430
431         mono_atomic_store_seq (p, vtable);
432
433         return p;
434 }
435
436 void*
437 mono_gc_alloc_obj (MonoVTable *vtable, size_t size)
438 {
439         void *res;
440 #ifndef DISABLE_CRITICAL_REGION
441         TLAB_ACCESS_INIT;
442         ENTER_CRITICAL_REGION;
443         res = mono_gc_try_alloc_obj_nolock (vtable, size);
444         if (res) {
445                 EXIT_CRITICAL_REGION;
446                 return res;
447         }
448         EXIT_CRITICAL_REGION;
449 #endif
450         LOCK_GC;
451         res = mono_gc_alloc_obj_nolock (vtable, size);
452         UNLOCK_GC;
453         if (G_UNLIKELY (!res))
454                 return mono_gc_out_of_memory (size);
455         return res;
456 }
457
458 void*
459 mono_gc_alloc_vector (MonoVTable *vtable, size_t size, uintptr_t max_length)
460 {
461         MonoArray *arr;
462 #ifndef DISABLE_CRITICAL_REGION
463         TLAB_ACCESS_INIT;
464         ENTER_CRITICAL_REGION;
465         arr = mono_gc_try_alloc_obj_nolock (vtable, size);
466         if (arr) {
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;
470                 return arr;
471         }
472         EXIT_CRITICAL_REGION;
473 #endif
474
475         LOCK_GC;
476
477         arr = mono_gc_alloc_obj_nolock (vtable, size);
478         if (G_UNLIKELY (!arr)) {
479                 UNLOCK_GC;
480                 return mono_gc_out_of_memory (size);
481         }
482
483         arr->max_length = max_length;
484
485         UNLOCK_GC;
486
487         return arr;
488 }
489
490 void*
491 mono_gc_alloc_array (MonoVTable *vtable, size_t size, uintptr_t max_length, uintptr_t bounds_size)
492 {
493         MonoArray *arr;
494         MonoArrayBounds *bounds;
495
496 #ifndef DISABLE_CRITICAL_REGION
497         TLAB_ACCESS_INIT;
498         ENTER_CRITICAL_REGION;
499         arr = mono_gc_try_alloc_obj_nolock (vtable, size);
500         if (arr) {
501                 /*This doesn't require fencing since EXIT_CRITICAL_REGION already does it for us*/
502                 arr->max_length = max_length;
503
504                 bounds = (MonoArrayBounds*)((char*)arr + size - bounds_size);
505                 arr->bounds = bounds;
506                 EXIT_CRITICAL_REGION;
507                 return arr;
508         }
509         EXIT_CRITICAL_REGION;
510 #endif
511
512         LOCK_GC;
513
514         arr = mono_gc_alloc_obj_nolock (vtable, size);
515         if (G_UNLIKELY (!arr)) {
516                 UNLOCK_GC;
517                 return mono_gc_out_of_memory (size);
518         }
519
520         arr->max_length = max_length;
521
522         bounds = (MonoArrayBounds*)((char*)arr + size - bounds_size);
523         arr->bounds = bounds;
524
525         UNLOCK_GC;
526
527         return arr;
528 }
529
530 void*
531 mono_gc_alloc_string (MonoVTable *vtable, size_t size, gint32 len)
532 {
533         MonoString *str;
534 #ifndef DISABLE_CRITICAL_REGION
535         TLAB_ACCESS_INIT;
536         ENTER_CRITICAL_REGION;
537         str = mono_gc_try_alloc_obj_nolock (vtable, size);
538         if (str) {
539                 /*This doesn't require fencing since EXIT_CRITICAL_REGION already does it for us*/
540                 str->length = len;
541                 EXIT_CRITICAL_REGION;
542                 return str;
543         }
544         EXIT_CRITICAL_REGION;
545 #endif
546
547         LOCK_GC;
548
549         str = mono_gc_alloc_obj_nolock (vtable, size);
550         if (G_UNLIKELY (!str)) {
551                 UNLOCK_GC;
552                 return mono_gc_out_of_memory (size);
553         }
554
555         str->length = len;
556
557         UNLOCK_GC;
558
559         return str;
560 }
561
562 /*
563  * To be used for interned strings and possibly MonoThread, reflection handles.
564  * We may want to explicitly free these objects.
565  */
566 void*
567 mono_gc_alloc_pinned_obj (MonoVTable *vtable, size_t size)
568 {
569         void **p;
570         size = ALIGN_UP (size);
571         LOCK_GC;
572
573         if (size > SGEN_MAX_SMALL_OBJ_SIZE) {
574                 /* large objects are always pinned anyway */
575                 p = sgen_los_alloc_large_inner (vtable, size);
576         } else {
577                 DEBUG (9, g_assert (vtable->klass->inited));
578                 p = major_collector.alloc_small_pinned_obj (size, SGEN_VTABLE_HAS_REFERENCES (vtable));
579         }
580         if (G_LIKELY (p)) {
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);
584                 else
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);
588         }
589         UNLOCK_GC;
590         return p;
591 }
592
593 void*
594 mono_gc_alloc_mature (MonoVTable *vtable)
595 {
596         void **res;
597         size_t size = ALIGN_UP (vtable->klass->instance_size);
598         LOCK_GC;
599         res = alloc_degraded (vtable, size, TRUE);
600         mono_atomic_store_seq (res, vtable);
601         UNLOCK_GC;
602         if (G_UNLIKELY (vtable->klass->has_finalize))
603                 mono_object_register_finalizer ((MonoObject*)res);
604
605         return res;
606 }
607
608 void*
609 mono_gc_alloc_fixed (size_t size, void *descr)
610 {
611         /* FIXME: do a single allocation */
612         void *res = calloc (1, size);
613         if (!res)
614                 return NULL;
615         if (!mono_gc_register_root (res, size, descr)) {
616                 free (res);
617                 res = NULL;
618         }
619         return res;
620 }
621
622 void
623 mono_gc_free_fixed (void* addr)
624 {
625         mono_gc_deregister_root (addr);
626         free (addr);
627 }
628
629 void
630 sgen_init_tlab_info (SgenThreadInfo* info)
631 {
632 #ifndef HAVE_KW_THREAD
633         SgenThreadInfo *__thread_info__ = info;
634 #endif
635
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;
640
641 #ifdef HAVE_KW_THREAD
642         tlab_next_addr = &tlab_next;
643 #endif
644 }
645
646 /*
647  * Clear the thread local TLAB variables for all threads.
648  */
649 void
650 sgen_clear_tlabs (void)
651 {
652         SgenThreadInfo *info;
653
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;
660         } END_FOREACH_THREAD
661 }
662
663 static MonoMethod* alloc_method_cache [ATYPE_NUM];
664
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.
669  *
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.
673  */
674 static MonoMethod*
675 create_allocator (int atype)
676 {
677         int p_var, size_var;
678         guint32 slowpath_branch, max_size_branch;
679         MonoMethodBuilder *mb;
680         MonoMethod *res;
681         MonoMethodSignature *csig;
682         static gboolean registered = FALSE;
683         int tlab_next_addr_var, new_next_var;
684         int num_params, i;
685         const char *name = NULL;
686         AllocatorWrapperInfo *info;
687
688 #ifdef HAVE_KW_THREAD
689         int tlab_next_addr_offset = -1;
690         int tlab_temp_end_offset = -1;
691
692         MONO_THREAD_VAR_OFFSET (tlab_next_addr, tlab_next_addr_offset);
693         MONO_THREAD_VAR_OFFSET (tlab_temp_end, tlab_temp_end_offset);
694
695         g_assert (tlab_next_addr_offset != -1);
696         g_assert (tlab_temp_end_offset != -1);
697 #endif
698
699         if (!registered) {
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);
702                 registered = TRUE;
703         }
704
705         if (atype == ATYPE_SMALL) {
706                 num_params = 1;
707                 name = "AllocSmall";
708         } else if (atype == ATYPE_NORMAL) {
709                 num_params = 1;
710                 name = "Alloc";
711         } else if (atype == ATYPE_VECTOR) {
712                 num_params = 2;
713                 name = "AllocVector";
714         } else {
715                 g_assert_not_reached ();
716         }
717
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;
722
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;
738                 int pos, pos_leave;
739                 MonoClass *oom_exc_class;
740                 MonoMethod *ctor;
741
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);
748
749                 clause = mono_image_alloc0 (mono_defaults.corlib, sizeof (MonoExceptionClause));
750                 clause->try_offset = mono_mb_get_label (mb);
751
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);
760
761                 /* * n */
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);
768
769                 pos_leave = mono_mb_emit_branch (mb, CEE_LEAVE);
770
771                 /* catch */
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);
778
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);
783                 g_assert (ctor);
784
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);
788
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);
792                 /* end catch */
793         } else {
794                 g_assert_not_reached ();
795         }
796
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);
805
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);
811         }
812
813         /*
814          * We need to modify tlab_next, but the JIT only supports reading, so we read
815          * another tls var holding its address instead.
816          */
817
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);
822
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);
828         
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);
836
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);
841
842         /* Slowpath */
843         if (atype != ATYPE_SMALL)
844                 mono_mb_patch_short_branch (mb, max_size_branch);
845
846         mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
847         mono_mb_emit_byte (mb, CEE_MONO_NOT_TAKEN);
848
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);
857         } else {
858                 g_assert_not_reached ();
859         }
860         mono_mb_emit_byte (mb, CEE_RET);
861
862         /* Fastpath */
863         mono_mb_patch_short_branch (mb, slowpath_branch);
864
865         /* FIXME: Memory barrier */
866
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);
871
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);
875
876         /* *p = vtable; */
877         mono_mb_emit_ldloc (mb, p_var);
878         mono_mb_emit_ldarg (mb, 0);
879         mono_mb_emit_byte (mb, CEE_STIND_I);
880
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);
888 #else
889                 mono_mb_emit_byte (mb, CEE_STIND_I4);
890 #endif
891         }
892
893         /*
894         We must make sure both vtable and max_length are globaly visible before returning to managed land.
895         */
896         mono_mb_emit_byte ((mb), MONO_CUSTOM_PREFIX);
897         mono_mb_emit_op (mb, CEE_MONO_MEMORY_BARRIER, StoreStoreBarrier);
898
899         /* return p */
900         mono_mb_emit_ldloc (mb, p_var);
901         mono_mb_emit_byte (mb, CEE_RET);
902
903         res = mono_mb_create_method (mb, csig, 8);
904         mono_mb_free (mb);
905         mono_method_get_header (res)->init_locals = FALSE;
906
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);
911
912         return res;
913 }
914 #endif
915
916 /*
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)
920  */
921 MonoMethod*
922 mono_gc_get_managed_allocator (MonoVTable *vtable, gboolean for_box)
923 {
924 #ifdef MANAGED_ALLOCATION
925         MonoClass *klass = vtable->klass;
926
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);
932
933         if (tlab_next_offset == -1 || tlab_temp_end_offset == -1)
934                 return NULL;
935 #endif
936
937         if (!mono_runtime_has_tls_get ())
938                 return NULL;
939         if (klass->instance_size > tlab_size)
940                 return NULL;
941         if (klass->has_finalize || klass->marshalbyref || (mono_profiler_get_events () & MONO_PROFILE_ALLOCATIONS))
942                 return NULL;
943         if (klass->rank)
944                 return NULL;
945         if (klass->byval_arg.type == MONO_TYPE_STRING)
946                 return NULL;
947         if (collect_before_allocs)
948                 return NULL;
949
950         if (ALIGN_TO (klass->instance_size, ALLOC_ALIGN) < MAX_SMALL_OBJ_SIZE)
951                 return mono_gc_get_managed_allocator_by_type (ATYPE_SMALL);
952         else
953                 return mono_gc_get_managed_allocator_by_type (ATYPE_NORMAL);
954 #else
955         return NULL;
956 #endif
957 }
958
959 MonoMethod*
960 mono_gc_get_managed_array_allocator (MonoVTable *vtable, int rank)
961 {
962 #ifdef MANAGED_ALLOCATION
963         MonoClass *klass = vtable->klass;
964
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);
970
971         if (tlab_next_offset == -1 || tlab_temp_end_offset == -1)
972                 return NULL;
973 #endif
974
975         if (rank != 1)
976                 return NULL;
977         if (!mono_runtime_has_tls_get ())
978                 return NULL;
979         if (mono_profiler_get_events () & MONO_PROFILE_ALLOCATIONS)
980                 return NULL;
981         if (has_per_allocation_action)
982                 return NULL;
983         g_assert (!mono_class_has_finalizer (klass) && !klass->marshalbyref);
984
985         return mono_gc_get_managed_allocator_by_type (ATYPE_VECTOR);
986 #else
987         return NULL;
988 #endif
989 }
990
991 void
992 sgen_set_use_managed_allocator (gboolean flag)
993 {
994         use_managed_allocator = flag;
995 }
996
997 MonoMethod*
998 mono_gc_get_managed_allocator_by_type (int atype)
999 {
1000 #ifdef MANAGED_ALLOCATION
1001         MonoMethod *res;
1002
1003         if (!use_managed_allocator)
1004                 return NULL;
1005
1006         if (!mono_runtime_has_tls_get ())
1007                 return NULL;
1008
1009         mono_loader_lock ();
1010         res = alloc_method_cache [atype];
1011         if (!res)
1012                 res = alloc_method_cache [atype] = create_allocator (atype);
1013         mono_loader_unlock ();
1014         return res;
1015 #else
1016         return NULL;
1017 #endif
1018 }
1019
1020 guint32
1021 mono_gc_get_managed_allocator_types (void)
1022 {
1023         return ATYPE_NUM;
1024 }
1025
1026 gboolean
1027 sgen_is_managed_allocator (MonoMethod *method)
1028 {
1029         int i;
1030
1031         for (i = 0; i < ATYPE_NUM; ++i)
1032                 if (method == alloc_method_cache [i])
1033                         return TRUE;
1034         return FALSE;
1035 }
1036
1037 gboolean
1038 sgen_has_managed_allocator (void)
1039 {
1040         int i;
1041
1042         for (i = 0; i < ATYPE_NUM; ++i)
1043                 if (alloc_method_cache [i])
1044                         return TRUE;
1045         return FALSE;
1046 }       
1047
1048 #ifdef HEAVY_STATISTICS
1049 void
1050 sgen_alloc_init_heavy_stats (void)
1051 {
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);
1055 }
1056 #endif
1057
1058 #endif /*HAVE_SGEN_GC*/