Merge pull request #820 from brendanzagaeski/master
[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  * Copyright (C) 2012 Xamarin Inc
12  *
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;
16  *
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.
21  *
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.
25  */
26
27 /*
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
37  */
38
39 #include "config.h"
40 #ifdef HAVE_SGEN_GC
41
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"
50
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))
56
57 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
58         a = i,
59
60 enum {
61 #include "mono/cil/opcode.def"
62         CEE_LAST
63 };
64
65 #undef OPDEF
66
67 static gboolean use_managed_allocator = TRUE;
68
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;
73
74 #endif
75
76 /*
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 
80  * be allocated.
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.
84  */
85
86 /*
87  * FIXME: What is faster, a TLS variable pointing to a structure, or separate TLS 
88  * variables for next+temp_end ?
89  */
90 #ifdef HAVE_KW_THREAD
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;
97 #endif
98
99 #ifdef HAVE_KW_THREAD
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
104 #else
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)
109 #endif
110
111 static void*
112 alloc_degraded (MonoVTable *vtable, size_t size, gboolean for_mature)
113 {
114         static int last_major_gc_warned = -1;
115         static int num_degraded = 0;
116
117         void *p;
118
119         if (!for_mature) {
120                 if (last_major_gc_warned < stat_major_gcs) {
121                         ++num_degraded;
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;
127                 }
128                 InterlockedExchangeAdd (&degraded_mode, size);
129                 sgen_ensure_free_space (size);
130         } else {
131                 if (sgen_need_major_collection (size))
132                         sgen_perform_collection (size, GENERATION_OLD, "mature allocation failure", !for_mature);
133         }
134
135
136         p = major_collector.alloc_degraded (vtable, size);
137
138         if (for_mature) {
139                 MONO_GC_MAJOR_OBJ_ALLOC_MATURE ((mword)p, size, vtable->klass->name_space, vtable->klass->name);
140         } else {
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);
143         }
144
145         return p;
146 }
147
148 /*
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.
155  */
156 static void*
157 mono_gc_alloc_obj_nolock (MonoVTable *vtable, size_t size)
158 {
159         /* FIXME: handle OOM */
160         void **p;
161         char *new_next;
162         TLAB_ACCESS_INIT;
163
164         HEAVY_STAT (++stat_objects_alloced);
165         if (size <= SGEN_MAX_SMALL_OBJ_SIZE)
166                 HEAVY_STAT (stat_bytes_alloced += size);
167         else
168                 HEAVY_STAT (stat_bytes_alloced_los += size);
169
170         size = ALIGN_UP (size);
171
172         g_assert (vtable->gc_descr);
173
174         if (G_UNLIKELY (has_per_allocation_action)) {
175                 static int alloc_count;
176                 int current_alloc = InterlockedIncrement (&alloc_count);
177
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) {
182                                         // FIXME:
183                                         g_assert_not_reached ();
184                                 }
185                         }
186                 } else if (verify_before_allocs) {
187                         if ((current_alloc % verify_before_allocs) == 0)
188                                 sgen_check_whole_heap_stw ();
189                 }
190         }
191
192         /*
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.
198          *
199          * The managed allocator does not do this, but it's treated
200          * specially by the world-stopping code.
201          */
202
203         if (size > SGEN_MAX_SMALL_OBJ_SIZE) {
204                 p = sgen_los_alloc_large_inner (vtable, size);
205         } else {
206                 /* tlab_next and tlab_temp_end are TLS vars so accessing them might be expensive */
207
208                 p = (void**)TLAB_NEXT;
209                 /* FIXME: handle overflow */
210                 new_next = (char*)p + size;
211                 TLAB_NEXT = new_next;
212
213                 if (G_LIKELY (new_next < TLAB_TEMP_END)) {
214                         /* Fast path */
215
216                         /* 
217                          * FIXME: We might need a memory barrier here so the change to tlab_next is 
218                          * visible before the vtable store.
219                          */
220
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);
227
228                         return p;
229                 }
230
231                 /* Slow path */
232
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.
241                  */
242                 if (TLAB_NEXT >= TLAB_REAL_END) {
243                         int available_in_tlab;
244                         /* 
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.
250                          */
251                         TLAB_NEXT -= size;
252                         /* when running in degraded mode, we continue allocing that way
253                          * for a while, to decrease the number of useless nursery collections.
254                          */
255                         if (degraded_mode && degraded_mode < DEFAULT_NURSERY_SIZE)
256                                 return alloc_degraded (vtable, size, FALSE);
257
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 */
261                                 do {
262                                         p = sgen_nursery_alloc (size);
263                                         if (!p) {
264                                                 sgen_ensure_free_space (size);
265                                                 if (degraded_mode)
266                                                         return alloc_degraded (vtable, size, FALSE);
267                                                 else
268                                                         p = sgen_nursery_alloc (size);
269                                         }
270                                 } while (!p);
271                                 if (!p) {
272                                         // no space left
273                                         g_assert (0);
274                                 }
275
276                                 if (nursery_clear_policy == CLEAR_AT_TLAB_CREATION) {
277                                         memset (p, 0, size);
278                                 }
279                         } else {
280                                 size_t alloc_size = 0;
281                                 if (TLAB_START)
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);
284
285                                 do {
286                                         p = sgen_nursery_alloc_range (tlab_size, size, &alloc_size);
287                                         if (!p) {
288                                                 sgen_ensure_free_space (tlab_size);
289                                                 if (degraded_mode)
290                                                         return alloc_degraded (vtable, size, FALSE);
291                                                 else
292                                                         p = sgen_nursery_alloc_range (tlab_size, size, &alloc_size);
293                                         }
294                                 } while (!p);
295                                         
296                                 if (!p) {
297                                         // no space left
298                                         g_assert (0);
299                                 }
300
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);
306
307                                 if (nursery_clear_policy == CLEAR_AT_TLAB_CREATION) {
308                                         memset (TLAB_START, 0, alloc_size);
309                                 }
310
311                                 /* Allocate from the TLAB */
312                                 p = (void*)TLAB_NEXT;
313                                 TLAB_NEXT += size;
314                                 sgen_set_nursery_scan_start ((char*)p);
315                         }
316                 } else {
317                         /* Reached tlab_temp_end */
318
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);
324                 }
325         }
326
327         if (G_LIKELY (p)) {
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);
333                         else
334                                 MONO_GC_NURSERY_OBJ_ALLOC ((mword)p, size, vtable->klass->name_space, vtable->klass->name);
335                 }
336                 mono_atomic_store_seq (p, vtable);
337         }
338
339         return p;
340 }
341
342 static void*
343 mono_gc_try_alloc_obj_nolock (MonoVTable *vtable, size_t size)
344 {
345         void **p;
346         char *new_next;
347         TLAB_ACCESS_INIT;
348
349         size = ALIGN_UP (size);
350         SGEN_ASSERT (9, size >= sizeof (MonoObject), "Object too small");
351
352         g_assert (vtable->gc_descr);
353         if (size > SGEN_MAX_SMALL_OBJ_SIZE)
354                 return NULL;
355
356         if (G_UNLIKELY (size > tlab_size)) {
357                 /* Allocate directly from the nursery */
358                 p = sgen_nursery_alloc (size);
359                 if (!p)
360                         return NULL;
361                 sgen_set_nursery_scan_start ((char*)p);
362
363                 /*FIXME we should use weak memory ops here. Should help specially on x86. */
364                 if (nursery_clear_policy == CLEAR_AT_TLAB_CREATION)
365                         memset (p, 0, size);
366         } else {
367                 int available_in_tlab;
368                 char *real_end;
369                 /* tlab_next and tlab_temp_end are TLS vars so accessing them might be expensive */
370
371                 p = (void**)TLAB_NEXT;
372                 /* FIXME: handle overflow */
373                 new_next = (char*)p + size;
374
375                 real_end = TLAB_REAL_END;
376                 available_in_tlab = real_end - (char*)p;
377
378                 if (G_LIKELY (new_next < real_end)) {
379                         TLAB_NEXT = new_next;
380
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);
387                         }
388                 } else if (available_in_tlab > SGEN_MAX_NURSERY_WASTE) {
389                         /* Allocate directly from the nursery */
390                         p = sgen_nursery_alloc (size);
391                         if (!p)
392                                 return NULL;
393
394                         if (nursery_clear_policy == CLEAR_AT_TLAB_CREATION)
395                                 memset (p, 0, size);                    
396                 } else {
397                         size_t alloc_size = 0;
398
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;
402                         if (!p)
403                                 return NULL;
404
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);
410
411                         if (nursery_clear_policy == CLEAR_AT_TLAB_CREATION)
412                                 memset (new_next, 0, alloc_size);
413
414                         MONO_GC_NURSERY_TLAB_ALLOC ((mword)new_next, alloc_size);
415                 }
416         }
417
418         HEAVY_STAT (++stat_objects_alloced);
419         HEAVY_STAT (stat_bytes_alloced += size);
420
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 */
426
427         mono_atomic_store_seq (p, vtable);
428
429         return p;
430 }
431
432 void*
433 mono_gc_alloc_obj (MonoVTable *vtable, size_t size)
434 {
435         void *res;
436
437         if (!SGEN_CAN_ALIGN_UP (size))
438                 return NULL;
439
440 #ifndef DISABLE_CRITICAL_REGION
441         TLAB_ACCESS_INIT;
442
443         if (G_UNLIKELY (has_per_allocation_action)) {
444                 static int alloc_count;
445                 int current_alloc = InterlockedIncrement (&alloc_count);
446
447                 if (verify_before_allocs) {
448                         if ((current_alloc % verify_before_allocs) == 0)
449                                 sgen_check_whole_heap_stw ();
450                 }
451                 if (collect_before_allocs) {
452                         if (((current_alloc % collect_before_allocs) == 0) && nursery_section) {
453                                 LOCK_GC;
454                                 sgen_perform_collection (0, GENERATION_NURSERY, "collect-before-alloc-triggered", TRUE);
455                                 UNLOCK_GC;
456                         }
457                 }
458         }
459
460         ENTER_CRITICAL_REGION;
461         res = mono_gc_try_alloc_obj_nolock (vtable, size);
462         if (res) {
463                 EXIT_CRITICAL_REGION;
464                 return res;
465         }
466         EXIT_CRITICAL_REGION;
467 #endif
468         LOCK_GC;
469         res = mono_gc_alloc_obj_nolock (vtable, size);
470         UNLOCK_GC;
471         if (G_UNLIKELY (!res))
472                 return mono_gc_out_of_memory (size);
473         return res;
474 }
475
476 void*
477 mono_gc_alloc_vector (MonoVTable *vtable, size_t size, uintptr_t max_length)
478 {
479         MonoArray *arr;
480
481         if (!SGEN_CAN_ALIGN_UP (size))
482                 return NULL;
483
484 #ifndef DISABLE_CRITICAL_REGION
485         TLAB_ACCESS_INIT;
486         ENTER_CRITICAL_REGION;
487         arr = mono_gc_try_alloc_obj_nolock (vtable, size);
488         if (arr) {
489                 /*This doesn't require fencing since EXIT_CRITICAL_REGION already does it for us*/
490                 arr->max_length = max_length;
491                 EXIT_CRITICAL_REGION;
492                 return arr;
493         }
494         EXIT_CRITICAL_REGION;
495 #endif
496
497         LOCK_GC;
498
499         arr = mono_gc_alloc_obj_nolock (vtable, size);
500         if (G_UNLIKELY (!arr)) {
501                 UNLOCK_GC;
502                 return mono_gc_out_of_memory (size);
503         }
504
505         arr->max_length = max_length;
506
507         UNLOCK_GC;
508
509         return arr;
510 }
511
512 void*
513 mono_gc_alloc_array (MonoVTable *vtable, size_t size, uintptr_t max_length, uintptr_t bounds_size)
514 {
515         MonoArray *arr;
516         MonoArrayBounds *bounds;
517
518         if (!SGEN_CAN_ALIGN_UP (size))
519                 return NULL;
520
521 #ifndef DISABLE_CRITICAL_REGION
522         TLAB_ACCESS_INIT;
523         ENTER_CRITICAL_REGION;
524         arr = mono_gc_try_alloc_obj_nolock (vtable, size);
525         if (arr) {
526                 /*This doesn't require fencing since EXIT_CRITICAL_REGION already does it for us*/
527                 arr->max_length = max_length;
528
529                 bounds = (MonoArrayBounds*)((char*)arr + size - bounds_size);
530                 arr->bounds = bounds;
531                 EXIT_CRITICAL_REGION;
532                 return arr;
533         }
534         EXIT_CRITICAL_REGION;
535 #endif
536
537         LOCK_GC;
538
539         arr = mono_gc_alloc_obj_nolock (vtable, size);
540         if (G_UNLIKELY (!arr)) {
541                 UNLOCK_GC;
542                 return mono_gc_out_of_memory (size);
543         }
544
545         arr->max_length = max_length;
546
547         bounds = (MonoArrayBounds*)((char*)arr + size - bounds_size);
548         arr->bounds = bounds;
549
550         UNLOCK_GC;
551
552         return arr;
553 }
554
555 void*
556 mono_gc_alloc_string (MonoVTable *vtable, size_t size, gint32 len)
557 {
558         MonoString *str;
559
560         if (!SGEN_CAN_ALIGN_UP (size))
561                 return NULL;
562
563 #ifndef DISABLE_CRITICAL_REGION
564         TLAB_ACCESS_INIT;
565         ENTER_CRITICAL_REGION;
566         str = mono_gc_try_alloc_obj_nolock (vtable, size);
567         if (str) {
568                 /*This doesn't require fencing since EXIT_CRITICAL_REGION already does it for us*/
569                 str->length = len;
570                 EXIT_CRITICAL_REGION;
571                 return str;
572         }
573         EXIT_CRITICAL_REGION;
574 #endif
575
576         LOCK_GC;
577
578         str = mono_gc_alloc_obj_nolock (vtable, size);
579         if (G_UNLIKELY (!str)) {
580                 UNLOCK_GC;
581                 return mono_gc_out_of_memory (size);
582         }
583
584         str->length = len;
585
586         UNLOCK_GC;
587
588         return str;
589 }
590
591 /*
592  * To be used for interned strings and possibly MonoThread, reflection handles.
593  * We may want to explicitly free these objects.
594  */
595 void*
596 mono_gc_alloc_pinned_obj (MonoVTable *vtable, size_t size)
597 {
598         void **p;
599
600         if (!SGEN_CAN_ALIGN_UP (size))
601                 return NULL;
602         size = ALIGN_UP (size);
603
604         LOCK_GC;
605
606         if (size > SGEN_MAX_SMALL_OBJ_SIZE) {
607                 /* large objects are always pinned anyway */
608                 p = sgen_los_alloc_large_inner (vtable, size);
609         } else {
610                 SGEN_ASSERT (9, vtable->klass->inited, "class %s:%s is not initialized", vtable->klass->name_space, vtable->klass->name);
611                 p = major_collector.alloc_small_pinned_obj (vtable, size, SGEN_VTABLE_HAS_REFERENCES (vtable));
612         }
613         if (G_LIKELY (p)) {
614                 SGEN_LOG (6, "Allocated pinned object %p, vtable: %p (%s), size: %zd", p, vtable, vtable->klass->name, size);
615                 if (size > SGEN_MAX_SMALL_OBJ_SIZE)
616                         MONO_GC_MAJOR_OBJ_ALLOC_LARGE ((mword)p, size, vtable->klass->name_space, vtable->klass->name);
617                 else
618                         MONO_GC_MAJOR_OBJ_ALLOC_PINNED ((mword)p, size, vtable->klass->name_space, vtable->klass->name);
619                 binary_protocol_alloc_pinned (p, vtable, size);
620         }
621         UNLOCK_GC;
622         return p;
623 }
624
625 void*
626 mono_gc_alloc_mature (MonoVTable *vtable)
627 {
628         void **res;
629         size_t size = vtable->klass->instance_size;
630
631         if (!SGEN_CAN_ALIGN_UP (size))
632                 return NULL;
633         size = ALIGN_UP (size);
634
635         LOCK_GC;
636         res = alloc_degraded (vtable, size, TRUE);
637         UNLOCK_GC;
638         if (G_UNLIKELY (vtable->klass->has_finalize))
639                 mono_object_register_finalizer ((MonoObject*)res);
640
641         return res;
642 }
643
644 void*
645 mono_gc_alloc_fixed (size_t size, void *descr)
646 {
647         /* FIXME: do a single allocation */
648         void *res = calloc (1, size);
649         if (!res)
650                 return NULL;
651         if (!mono_gc_register_root (res, size, descr)) {
652                 free (res);
653                 res = NULL;
654         }
655         return res;
656 }
657
658 void
659 mono_gc_free_fixed (void* addr)
660 {
661         mono_gc_deregister_root (addr);
662         free (addr);
663 }
664
665 void
666 sgen_init_tlab_info (SgenThreadInfo* info)
667 {
668 #ifndef HAVE_KW_THREAD
669         SgenThreadInfo *__thread_info__ = info;
670 #endif
671
672         info->tlab_start_addr = &TLAB_START;
673         info->tlab_next_addr = &TLAB_NEXT;
674         info->tlab_temp_end_addr = &TLAB_TEMP_END;
675         info->tlab_real_end_addr = &TLAB_REAL_END;
676
677 #ifdef HAVE_KW_THREAD
678         tlab_next_addr = &tlab_next;
679 #endif
680 }
681
682 /*
683  * Clear the thread local TLAB variables for all threads.
684  */
685 void
686 sgen_clear_tlabs (void)
687 {
688         SgenThreadInfo *info;
689
690         FOREACH_THREAD (info) {
691                 /* A new TLAB will be allocated when the thread does its first allocation */
692                 *info->tlab_start_addr = NULL;
693                 *info->tlab_next_addr = NULL;
694                 *info->tlab_temp_end_addr = NULL;
695                 *info->tlab_real_end_addr = NULL;
696         } END_FOREACH_THREAD
697 }
698
699 static MonoMethod* alloc_method_cache [ATYPE_NUM];
700
701 #ifdef MANAGED_ALLOCATION
702 /* FIXME: Do this in the JIT, where specialized allocation sequences can be created
703  * for each class. This is currently not easy to do, as it is hard to generate basic 
704  * blocks + branches, but it is easy with the linear IL codebase.
705  *
706  * For this to work we'd need to solve the TLAB race, first.  Now we
707  * require the allocator to be in a few known methods to make sure
708  * that they are executed atomically via the restart mechanism.
709  */
710 static MonoMethod*
711 create_allocator (int atype)
712 {
713         int p_var, size_var;
714         guint32 slowpath_branch, max_size_branch;
715         MonoMethodBuilder *mb;
716         MonoMethod *res;
717         MonoMethodSignature *csig;
718         static gboolean registered = FALSE;
719         int tlab_next_addr_var, new_next_var;
720         int num_params, i;
721         const char *name = NULL;
722         AllocatorWrapperInfo *info;
723
724 #ifdef HAVE_KW_THREAD
725         int tlab_next_addr_offset = -1;
726         int tlab_temp_end_offset = -1;
727
728         MONO_THREAD_VAR_OFFSET (tlab_next_addr, tlab_next_addr_offset);
729         MONO_THREAD_VAR_OFFSET (tlab_temp_end, tlab_temp_end_offset);
730
731         mono_tls_key_set_offset (TLS_KEY_SGEN_TLAB_NEXT_ADDR, tlab_next_addr_offset);
732         mono_tls_key_set_offset (TLS_KEY_SGEN_TLAB_TEMP_END, tlab_temp_end_offset);
733
734         g_assert (tlab_next_addr_offset != -1);
735         g_assert (tlab_temp_end_offset != -1);
736 #endif
737
738         if (!registered) {
739                 mono_register_jit_icall (mono_gc_alloc_obj, "mono_gc_alloc_obj", mono_create_icall_signature ("object ptr int"), FALSE);
740                 mono_register_jit_icall (mono_gc_alloc_vector, "mono_gc_alloc_vector", mono_create_icall_signature ("object ptr int int"), FALSE);
741                 mono_register_jit_icall (mono_gc_alloc_string, "mono_gc_alloc_string", mono_create_icall_signature ("object ptr int int32"), FALSE);
742                 registered = TRUE;
743         }
744
745         if (atype == ATYPE_SMALL) {
746                 num_params = 1;
747                 name = "AllocSmall";
748         } else if (atype == ATYPE_NORMAL) {
749                 num_params = 1;
750                 name = "Alloc";
751         } else if (atype == ATYPE_VECTOR) {
752                 num_params = 2;
753                 name = "AllocVector";
754         } else if (atype == ATYPE_STRING) {
755                 num_params = 2;
756                 name = "AllocString";
757         } else {
758                 g_assert_not_reached ();
759         }
760
761         csig = mono_metadata_signature_alloc (mono_defaults.corlib, num_params);
762         if (atype == ATYPE_STRING) {
763                 csig->ret = &mono_defaults.string_class->byval_arg;
764                 csig->params [0] = &mono_defaults.int_class->byval_arg;
765                 csig->params [1] = &mono_defaults.int32_class->byval_arg;
766         } else {
767                 csig->ret = &mono_defaults.object_class->byval_arg;
768                 for (i = 0; i < num_params; ++i)
769                         csig->params [i] = &mono_defaults.int_class->byval_arg;
770         }
771
772         mb = mono_mb_new (mono_defaults.object_class, name, MONO_WRAPPER_ALLOC);
773
774 #ifndef DISABLE_JIT
775         size_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
776         if (atype == ATYPE_NORMAL || atype == ATYPE_SMALL) {
777                 /* size = vtable->klass->instance_size; */
778                 mono_mb_emit_ldarg (mb, 0);
779                 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoVTable, klass));
780                 mono_mb_emit_byte (mb, CEE_ADD);
781                 mono_mb_emit_byte (mb, CEE_LDIND_I);
782                 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoClass, instance_size));
783                 mono_mb_emit_byte (mb, CEE_ADD);
784                 /* FIXME: assert instance_size stays a 4 byte integer */
785                 mono_mb_emit_byte (mb, CEE_LDIND_U4);
786                 mono_mb_emit_byte (mb, CEE_CONV_I);
787                 mono_mb_emit_stloc (mb, size_var);
788         } else if (atype == ATYPE_VECTOR) {
789                 MonoExceptionClause *clause;
790                 int pos, pos_leave, pos_error;
791                 MonoClass *oom_exc_class;
792                 MonoMethod *ctor;
793
794                 /*
795                  * n > MONO_ARRAY_MAX_INDEX => OutOfMemoryException
796                  * n < 0                    => OverflowException
797                  *
798                  * We can do an unsigned comparison to catch both cases, then in the error
799                  * case compare signed to distinguish between them.
800                  */
801                 mono_mb_emit_ldarg (mb, 1);
802                 mono_mb_emit_ptr (mb, (gpointer) MONO_ARRAY_MAX_INDEX);
803                 pos = mono_mb_emit_short_branch (mb, CEE_BLE_UN_S);
804
805                 mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
806                 mono_mb_emit_byte (mb, CEE_MONO_NOT_TAKEN);
807                 mono_mb_emit_ldarg (mb, 1);
808                 mono_mb_emit_icon (mb, 0);
809                 pos_error = mono_mb_emit_short_branch (mb, CEE_BLT_S);
810                 mono_mb_emit_exception (mb, "OutOfMemoryException", NULL);
811                 mono_mb_patch_short_branch (mb, pos_error);
812                 mono_mb_emit_exception (mb, "OverflowException", NULL);
813
814                 mono_mb_patch_short_branch (mb, pos);
815
816                 clause = mono_image_alloc0 (mono_defaults.corlib, sizeof (MonoExceptionClause));
817                 clause->try_offset = mono_mb_get_label (mb);
818
819                 /* vtable->klass->sizes.element_size */
820                 mono_mb_emit_ldarg (mb, 0);
821                 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoVTable, klass));
822                 mono_mb_emit_byte (mb, CEE_ADD);
823                 mono_mb_emit_byte (mb, CEE_LDIND_I);
824                 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoClass, sizes.element_size));
825                 mono_mb_emit_byte (mb, CEE_ADD);
826                 mono_mb_emit_byte (mb, CEE_LDIND_U4);
827                 mono_mb_emit_byte (mb, CEE_CONV_I);
828
829                 /* * n */
830                 mono_mb_emit_ldarg (mb, 1);
831                 mono_mb_emit_byte (mb, CEE_MUL_OVF_UN);
832                 /* + sizeof (MonoArray) */
833                 mono_mb_emit_icon (mb, sizeof (MonoArray));
834                 mono_mb_emit_byte (mb, CEE_ADD_OVF_UN);
835                 mono_mb_emit_stloc (mb, size_var);
836
837                 pos_leave = mono_mb_emit_branch (mb, CEE_LEAVE);
838
839                 /* catch */
840                 clause->flags = MONO_EXCEPTION_CLAUSE_NONE;
841                 clause->try_len = mono_mb_get_pos (mb) - clause->try_offset;
842                 clause->data.catch_class = mono_class_from_name (mono_defaults.corlib,
843                                 "System", "OverflowException");
844                 g_assert (clause->data.catch_class);
845                 clause->handler_offset = mono_mb_get_label (mb);
846
847                 oom_exc_class = mono_class_from_name (mono_defaults.corlib,
848                                 "System", "OutOfMemoryException");
849                 g_assert (oom_exc_class);
850                 ctor = mono_class_get_method_from_name (oom_exc_class, ".ctor", 0);
851                 g_assert (ctor);
852
853                 mono_mb_emit_byte (mb, CEE_POP);
854                 mono_mb_emit_op (mb, CEE_NEWOBJ, ctor);
855                 mono_mb_emit_byte (mb, CEE_THROW);
856
857                 clause->handler_len = mono_mb_get_pos (mb) - clause->handler_offset;
858                 mono_mb_set_clauses (mb, 1, clause);
859                 mono_mb_patch_branch (mb, pos_leave);
860                 /* end catch */
861         } else if (atype == ATYPE_STRING) {
862                 int pos;
863
864                 /*
865                  * a string allocator method takes the args: (vtable, len)
866                  *
867                  * bytes = sizeof (MonoString) + ((len + 1) * 2)
868                  *
869                  * condition:
870                  *
871                  * bytes <= INT32_MAX - (SGEN_ALLOC_ALIGN - 1)
872                  *
873                  * therefore:
874                  *
875                  * sizeof (MonoString) + ((len + 1) * 2) <= INT32_MAX - (SGEN_ALLOC_ALIGN - 1)
876                  * len <= (INT32_MAX - (SGEN_ALLOC_ALIGN - 1) - sizeof (MonoString)) / 2 - 1
877                  */
878                 mono_mb_emit_ldarg (mb, 1);
879                 mono_mb_emit_icon (mb, (INT32_MAX - (SGEN_ALLOC_ALIGN - 1) - sizeof (MonoString)) / 2 - 1);
880                 pos = mono_mb_emit_short_branch (mb, MONO_CEE_BLE_UN_S);
881
882                 mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
883                 mono_mb_emit_byte (mb, CEE_MONO_NOT_TAKEN);
884                 mono_mb_emit_exception (mb, "OutOfMemoryException", NULL);
885                 mono_mb_patch_short_branch (mb, pos);
886
887                 mono_mb_emit_ldarg (mb, 1);
888                 mono_mb_emit_icon (mb, 1);
889                 mono_mb_emit_byte (mb, MONO_CEE_SHL);
890                 //WE manually fold the above + 2 here
891                 mono_mb_emit_icon (mb, sizeof (MonoString) + 2);
892                 mono_mb_emit_byte (mb, CEE_ADD);
893                 mono_mb_emit_stloc (mb, size_var);
894         } else {
895                 g_assert_not_reached ();
896         }
897
898         /* size += ALLOC_ALIGN - 1; */
899         mono_mb_emit_ldloc (mb, size_var);
900         mono_mb_emit_icon (mb, ALLOC_ALIGN - 1);
901         mono_mb_emit_byte (mb, CEE_ADD);
902         /* size &= ~(ALLOC_ALIGN - 1); */
903         mono_mb_emit_icon (mb, ~(ALLOC_ALIGN - 1));
904         mono_mb_emit_byte (mb, CEE_AND);
905         mono_mb_emit_stloc (mb, size_var);
906
907         /* if (size > MAX_SMALL_OBJ_SIZE) goto slowpath */
908         if (atype != ATYPE_SMALL) {
909                 mono_mb_emit_ldloc (mb, size_var);
910                 mono_mb_emit_icon (mb, MAX_SMALL_OBJ_SIZE);
911                 max_size_branch = mono_mb_emit_short_branch (mb, MONO_CEE_BGT_UN_S);
912         }
913
914         /*
915          * We need to modify tlab_next, but the JIT only supports reading, so we read
916          * another tls var holding its address instead.
917          */
918
919         /* tlab_next_addr (local) = tlab_next_addr (TLS var) */
920         tlab_next_addr_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
921         EMIT_TLS_ACCESS (mb, tlab_next_addr, TLS_KEY_SGEN_TLAB_NEXT_ADDR);
922         mono_mb_emit_stloc (mb, tlab_next_addr_var);
923
924         /* p = (void**)tlab_next; */
925         p_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
926         mono_mb_emit_ldloc (mb, tlab_next_addr_var);
927         mono_mb_emit_byte (mb, CEE_LDIND_I);
928         mono_mb_emit_stloc (mb, p_var);
929         
930         /* new_next = (char*)p + size; */
931         new_next_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
932         mono_mb_emit_ldloc (mb, p_var);
933         mono_mb_emit_ldloc (mb, size_var);
934         mono_mb_emit_byte (mb, CEE_CONV_I);
935         mono_mb_emit_byte (mb, CEE_ADD);
936         mono_mb_emit_stloc (mb, new_next_var);
937
938         /* if (G_LIKELY (new_next < tlab_temp_end)) */
939         mono_mb_emit_ldloc (mb, new_next_var);
940         EMIT_TLS_ACCESS (mb, tlab_temp_end, TLS_KEY_SGEN_TLAB_TEMP_END);
941         slowpath_branch = mono_mb_emit_short_branch (mb, MONO_CEE_BLT_UN_S);
942
943         /* Slowpath */
944         if (atype != ATYPE_SMALL)
945                 mono_mb_patch_short_branch (mb, max_size_branch);
946
947         mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
948         mono_mb_emit_byte (mb, CEE_MONO_NOT_TAKEN);
949
950         /* FIXME: mono_gc_alloc_obj takes a 'size_t' as an argument, not an int32 */
951         mono_mb_emit_ldarg (mb, 0);
952         mono_mb_emit_ldloc (mb, size_var);
953         if (atype == ATYPE_NORMAL || atype == ATYPE_SMALL) {
954                 mono_mb_emit_icall (mb, mono_gc_alloc_obj);
955         } else if (atype == ATYPE_VECTOR) {
956                 mono_mb_emit_ldarg (mb, 1);
957                 mono_mb_emit_icall (mb, mono_gc_alloc_vector);
958         } else if (atype == ATYPE_STRING) {
959                 mono_mb_emit_ldarg (mb, 1);
960                 mono_mb_emit_icall (mb, mono_gc_alloc_string);
961         } else {
962                 g_assert_not_reached ();
963         }
964         mono_mb_emit_byte (mb, CEE_RET);
965
966         /* Fastpath */
967         mono_mb_patch_short_branch (mb, slowpath_branch);
968
969         /* FIXME: Memory barrier */
970
971         /* tlab_next = new_next */
972         mono_mb_emit_ldloc (mb, tlab_next_addr_var);
973         mono_mb_emit_ldloc (mb, new_next_var);
974         mono_mb_emit_byte (mb, CEE_STIND_I);
975
976         /*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. */
977         mono_mb_emit_byte ((mb), MONO_CUSTOM_PREFIX);
978         mono_mb_emit_op (mb, CEE_MONO_MEMORY_BARRIER, StoreStoreBarrier);
979
980         /* *p = vtable; */
981         mono_mb_emit_ldloc (mb, p_var);
982         mono_mb_emit_ldarg (mb, 0);
983         mono_mb_emit_byte (mb, CEE_STIND_I);
984
985         if (atype == ATYPE_VECTOR) {
986                 /* arr->max_length = max_length; */
987                 mono_mb_emit_ldloc (mb, p_var);
988                 mono_mb_emit_ldflda (mb, G_STRUCT_OFFSET (MonoArray, max_length));
989                 mono_mb_emit_ldarg (mb, 1);
990 #ifdef MONO_BIG_ARRAYS
991                 mono_mb_emit_byte (mb, CEE_STIND_I);
992 #else
993                 mono_mb_emit_byte (mb, CEE_STIND_I4);
994 #endif
995         } else  if (atype == ATYPE_STRING) {
996                 /* need to set length and clear the last char */
997                 /* s->length = len; */
998                 mono_mb_emit_ldloc (mb, p_var);
999                 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoString, length));
1000                 mono_mb_emit_byte (mb, MONO_CEE_ADD);
1001                 mono_mb_emit_ldarg (mb, 1);
1002                 mono_mb_emit_byte (mb, MONO_CEE_STIND_I4);
1003                 /* s->chars [len] = 0; */
1004                 mono_mb_emit_ldloc (mb, p_var);
1005                 mono_mb_emit_ldloc (mb, size_var);
1006                 mono_mb_emit_icon (mb, 2);
1007                 mono_mb_emit_byte (mb, MONO_CEE_SUB);
1008                 mono_mb_emit_byte (mb, MONO_CEE_ADD);
1009                 mono_mb_emit_icon (mb, 0);
1010                 mono_mb_emit_byte (mb, MONO_CEE_STIND_I2);
1011         }
1012
1013         /*
1014         We must make sure both vtable and max_length are globaly visible before returning to managed land.
1015         */
1016         mono_mb_emit_byte ((mb), MONO_CUSTOM_PREFIX);
1017         mono_mb_emit_op (mb, CEE_MONO_MEMORY_BARRIER, StoreStoreBarrier);
1018
1019         /* return p */
1020         mono_mb_emit_ldloc (mb, p_var);
1021         mono_mb_emit_byte (mb, CEE_RET);
1022 #endif
1023
1024         res = mono_mb_create_method (mb, csig, 8);
1025         mono_mb_free (mb);
1026         mono_method_get_header (res)->init_locals = FALSE;
1027
1028         info = mono_image_alloc0 (mono_defaults.corlib, sizeof (AllocatorWrapperInfo));
1029         info->gc_name = "sgen";
1030         info->alloc_type = atype;
1031         mono_marshal_set_wrapper_info (res, info);
1032
1033         return res;
1034 }
1035 #endif
1036
1037 /*
1038  * Generate an allocator method implementing the fast path of mono_gc_alloc_obj ().
1039  * The signature of the called method is:
1040  *      object allocate (MonoVTable *vtable)
1041  */
1042 MonoMethod*
1043 mono_gc_get_managed_allocator (MonoClass *klass, gboolean for_box)
1044 {
1045 #ifdef MANAGED_ALLOCATION
1046
1047 #ifdef HAVE_KW_THREAD
1048         int tlab_next_offset = -1;
1049         int tlab_temp_end_offset = -1;
1050         MONO_THREAD_VAR_OFFSET (tlab_next, tlab_next_offset);
1051         MONO_THREAD_VAR_OFFSET (tlab_temp_end, tlab_temp_end_offset);
1052
1053         if (tlab_next_offset == -1 || tlab_temp_end_offset == -1)
1054                 return NULL;
1055 #endif
1056         if (collect_before_allocs)
1057                 return NULL;
1058         if (!mono_runtime_has_tls_get ())
1059                 return NULL;
1060         if (klass->instance_size > tlab_size)
1061                 return NULL;
1062
1063         if (klass->has_finalize || mono_class_is_marshalbyref (klass) || (mono_profiler_get_events () & MONO_PROFILE_ALLOCATIONS))
1064                 return NULL;
1065         if (klass->rank)
1066                 return NULL;
1067         if (klass->byval_arg.type == MONO_TYPE_STRING)
1068                 return mono_gc_get_managed_allocator_by_type (ATYPE_STRING);
1069         /* Generic classes have dynamic field and can go above MAX_SMALL_OBJ_SIZE. */
1070         if (ALIGN_TO (klass->instance_size, ALLOC_ALIGN) < MAX_SMALL_OBJ_SIZE && !mono_class_is_open_constructed_type (&klass->byval_arg))
1071                 return mono_gc_get_managed_allocator_by_type (ATYPE_SMALL);
1072         else
1073                 return mono_gc_get_managed_allocator_by_type (ATYPE_NORMAL);
1074 #else
1075         return NULL;
1076 #endif
1077 }
1078
1079 MonoMethod*
1080 mono_gc_get_managed_array_allocator (MonoClass *klass)
1081 {
1082 #ifdef MANAGED_ALLOCATION
1083 #ifdef HAVE_KW_THREAD
1084         int tlab_next_offset = -1;
1085         int tlab_temp_end_offset = -1;
1086         MONO_THREAD_VAR_OFFSET (tlab_next, tlab_next_offset);
1087         MONO_THREAD_VAR_OFFSET (tlab_temp_end, tlab_temp_end_offset);
1088
1089         if (tlab_next_offset == -1 || tlab_temp_end_offset == -1)
1090                 return NULL;
1091 #endif
1092
1093         if (klass->rank != 1)
1094                 return NULL;
1095         if (!mono_runtime_has_tls_get ())
1096                 return NULL;
1097         if (mono_profiler_get_events () & MONO_PROFILE_ALLOCATIONS)
1098                 return NULL;
1099         if (has_per_allocation_action)
1100                 return NULL;
1101         g_assert (!mono_class_has_finalizer (klass) && !mono_class_is_marshalbyref (klass));
1102
1103         return mono_gc_get_managed_allocator_by_type (ATYPE_VECTOR);
1104 #else
1105         return NULL;
1106 #endif
1107 }
1108
1109 void
1110 sgen_set_use_managed_allocator (gboolean flag)
1111 {
1112         use_managed_allocator = flag;
1113 }
1114
1115 MonoMethod*
1116 mono_gc_get_managed_allocator_by_type (int atype)
1117 {
1118 #ifdef MANAGED_ALLOCATION
1119         MonoMethod *res;
1120
1121         if (!use_managed_allocator)
1122                 return NULL;
1123
1124         if (!mono_runtime_has_tls_get ())
1125                 return NULL;
1126
1127         res = alloc_method_cache [atype];
1128         if (res)
1129                 return res;
1130
1131         res = create_allocator (atype);
1132         LOCK_GC;
1133         if (alloc_method_cache [atype]) {
1134                 mono_free_method (res);
1135                 res = alloc_method_cache [atype];
1136         } else {
1137                 mono_memory_barrier ();
1138                 alloc_method_cache [atype] = res;
1139         }
1140         UNLOCK_GC;
1141
1142         return res;
1143 #else
1144         return NULL;
1145 #endif
1146 }
1147
1148 guint32
1149 mono_gc_get_managed_allocator_types (void)
1150 {
1151         return ATYPE_NUM;
1152 }
1153
1154 gboolean
1155 sgen_is_managed_allocator (MonoMethod *method)
1156 {
1157         int i;
1158
1159         for (i = 0; i < ATYPE_NUM; ++i)
1160                 if (method == alloc_method_cache [i])
1161                         return TRUE;
1162         return FALSE;
1163 }
1164
1165 gboolean
1166 sgen_has_managed_allocator (void)
1167 {
1168         int i;
1169
1170         for (i = 0; i < ATYPE_NUM; ++i)
1171                 if (alloc_method_cache [i])
1172                         return TRUE;
1173         return FALSE;
1174 }       
1175
1176 #ifdef HEAVY_STATISTICS
1177 void
1178 sgen_alloc_init_heavy_stats (void)
1179 {
1180         mono_counters_register ("# objects allocated", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_objects_alloced);     
1181         mono_counters_register ("bytes allocated", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_bytes_alloced);
1182         mono_counters_register ("bytes allocated in LOS", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_bytes_alloced_los);
1183 }
1184 #endif
1185
1186 #endif /*HAVE_SGEN_GC*/