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