Merged into single file, added assertions
[mono.git] / mono / metadata / sgen-los.c
1 /*
2  * sgen-los.c: Simple generational GC.
3  *
4  * Author:
5  *      Paolo Molaro (lupus@ximian.com)
6  *
7  * Copyright 2005-2010 Novell, Inc (http://www.novell.com)
8  *
9  * Thread start/stop adapted from Boehm's GC:
10  * Copyright (c) 1994 by Xerox Corporation.  All rights reserved.
11  * Copyright (c) 1996 by Silicon Graphics.  All rights reserved.
12  * Copyright (c) 1998 by Fergus Henderson.  All rights reserved.
13  * Copyright (c) 2000-2004 by Hewlett-Packard Company.  All rights reserved.
14  *
15  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
16  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
17  *
18  * Permission is hereby granted to use or copy this program
19  * for any purpose,  provided the above notices are retained on all copies.
20  * Permission to modify the code and to distribute modified code is granted,
21  * provided the above notices are retained, and a notice that the code was
22  * modified is included with the above copyright notice.
23  *
24  *
25  * Copyright 2001-2003 Ximian, Inc
26  * Copyright 2003-2010 Novell, Inc.
27  *
28  * Permission is hereby granted, free of charge, to any person obtaining
29  * a copy of this software and associated documentation files (the
30  * "Software"), to deal in the Software without restriction, including
31  * without limitation the rights to use, copy, modify, merge, publish,
32  * distribute, sublicense, and/or sell copies of the Software, and to
33  * permit persons to whom the Software is furnished to do so, subject to
34  * the following conditions:
35  *
36  * The above copyright notice and this permission notice shall be
37  * included in all copies or substantial portions of the Software.
38  *
39  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
40  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
41  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
42  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
43  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
44  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
45  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
46  */
47
48 #include "config.h"
49
50 #ifdef HAVE_SGEN_GC
51
52 #include "metadata/sgen-gc.h"
53 #include "metadata/sgen-protocol.h"
54 #include "metadata/sgen-cardtable.h"
55 #include "metadata/sgen-memory-governor.h"
56 #include "utils/mono-mmap.h"
57
58 #define LOS_SECTION_SIZE        (1024 * 1024)
59
60 /*
61  * This shouldn't be much smaller or larger than MAX_SMALL_OBJ_SIZE.
62  * Must be at least sizeof (LOSSection).
63  */
64 #define LOS_CHUNK_SIZE          4096
65 #define LOS_CHUNK_BITS          12
66
67 /* Largest object that can be allocated in a section. */
68 #define LOS_SECTION_OBJECT_LIMIT        (LOS_SECTION_SIZE - LOS_CHUNK_SIZE - sizeof (LOSObject))
69 //#define LOS_SECTION_OBJECT_LIMIT      0
70 #define LOS_SECTION_NUM_CHUNKS          ((LOS_SECTION_SIZE >> LOS_CHUNK_BITS) - 1)
71
72 #define LOS_SECTION_FOR_OBJ(obj)        ((LOSSection*)((mword)(obj) & ~(mword)(LOS_SECTION_SIZE - 1)))
73 #define LOS_CHUNK_INDEX(obj,section)    (((char*)(obj) - (char*)(section)) >> LOS_CHUNK_BITS)
74
75 #define LOS_NUM_FAST_SIZES              32
76
77 typedef struct _LOSFreeChunks LOSFreeChunks;
78 struct _LOSFreeChunks {
79         LOSFreeChunks *next_size;
80         size_t size;
81 };
82
83 typedef struct _LOSSection LOSSection;
84 struct _LOSSection {
85         LOSSection *next;
86         int num_free_chunks;
87         unsigned char *free_chunk_map;
88 };
89
90 LOSObject *los_object_list = NULL;
91 mword los_memory_usage = 0;
92
93 static LOSSection *los_sections = NULL;
94 static LOSFreeChunks *los_fast_free_lists [LOS_NUM_FAST_SIZES]; /* 0 is for larger sizes */
95 static mword los_num_objects = 0;
96 static int los_num_sections = 0;
97
98 //#define USE_MALLOC
99 //#define LOS_CONSISTENCY_CHECK
100 //#define LOS_DUMMY
101
102 #ifdef LOS_DUMMY
103 #define LOS_SEGMENT_SIZE        (4096 * 1024)
104
105 static char *los_segment = NULL;
106 static int los_segment_index = 0;
107 #endif
108
109 #ifdef LOS_CONSISTENCY_CHECK
110 static void
111 los_consistency_check (void)
112 {
113         LOSSection *section;
114         LOSObject *obj;
115         int i;
116         mword memory_usage = 0;
117
118         for (obj = los_object_list; obj; obj = obj->next) {
119                 char *end = obj->data + obj->size;
120                 int start_index, num_chunks;
121
122                 memory_usage += obj->size;
123
124                 if (obj->size > LOS_SECTION_OBJECT_LIMIT)
125                         continue;
126
127                 section = LOS_SECTION_FOR_OBJ (obj);
128
129                 g_assert (end <= (char*)section + LOS_SECTION_SIZE);
130
131                 start_index = LOS_CHUNK_INDEX (obj, section);
132                 num_chunks = (obj->size + sizeof (LOSObject) + LOS_CHUNK_SIZE - 1) >> LOS_CHUNK_BITS;
133                 for (i = start_index; i < start_index + num_chunks; ++i)
134                         g_assert (!section->free_chunk_map [i]);
135         }
136
137         for (i = 0; i < LOS_NUM_FAST_SIZES; ++i) {
138                 LOSFreeChunks *size_chunks;
139                 for (size_chunks = los_fast_free_lists [i]; size_chunks; size_chunks = size_chunks->next_size) {
140                         LOSSection *section = LOS_SECTION_FOR_OBJ (size_chunks);
141                         int j, num_chunks, start_index;
142
143                         if (i == 0)
144                                 g_assert (size_chunks->size >= LOS_NUM_FAST_SIZES * LOS_CHUNK_SIZE);
145                         else
146                                 g_assert (size_chunks->size == i * LOS_CHUNK_SIZE);
147
148                         num_chunks = size_chunks->size >> LOS_CHUNK_BITS;
149                         start_index = LOS_CHUNK_INDEX (size_chunks, section);
150                         for (j = start_index; j < start_index + num_chunks; ++j)
151                                 g_assert (section->free_chunk_map [j]);
152                 }
153         }
154
155         g_assert (los_memory_usage == memory_usage);
156 }
157 #endif
158
159 static void
160 add_free_chunk (LOSFreeChunks *free_chunks, size_t size)
161 {
162         int num_chunks = size >> LOS_CHUNK_BITS;
163
164         free_chunks->size = size;
165
166         if (num_chunks >= LOS_NUM_FAST_SIZES)
167                 num_chunks = 0;
168         free_chunks->next_size = los_fast_free_lists [num_chunks];
169         los_fast_free_lists [num_chunks] = free_chunks;
170 }
171
172 static LOSFreeChunks*
173 get_from_size_list (LOSFreeChunks **list, size_t size)
174 {
175         LOSFreeChunks *free_chunks = NULL;
176         LOSSection *section;
177         int num_chunks, i, start_index;
178
179         g_assert ((size & (LOS_CHUNK_SIZE - 1)) == 0);
180
181         while (*list) {
182                 free_chunks = *list;
183                 if (free_chunks->size >= size)
184                         break;
185                 list = &(*list)->next_size;
186         }
187
188         if (!*list)
189                 return NULL;
190
191         *list = free_chunks->next_size;
192
193         if (free_chunks->size > size)
194                 add_free_chunk ((LOSFreeChunks*)((char*)free_chunks + size), free_chunks->size - size);
195
196         num_chunks = size >> LOS_CHUNK_BITS;
197
198         section = LOS_SECTION_FOR_OBJ (free_chunks);
199
200         start_index = LOS_CHUNK_INDEX (free_chunks, section);
201         for (i = start_index; i < start_index + num_chunks; ++i) {
202                 g_assert (section->free_chunk_map [i]);
203                 section->free_chunk_map [i] = 0;
204         }
205
206         section->num_free_chunks -= size >> LOS_CHUNK_BITS;
207         g_assert (section->num_free_chunks >= 0);
208
209         return free_chunks;
210 }
211
212 static LOSObject*
213 get_los_section_memory (size_t size)
214 {
215         LOSSection *section;
216         LOSFreeChunks *free_chunks;
217         int num_chunks;
218
219         size += LOS_CHUNK_SIZE - 1;
220         size &= ~(LOS_CHUNK_SIZE - 1);
221
222         num_chunks = size >> LOS_CHUNK_BITS;
223
224         g_assert (size > 0 && size - sizeof (LOSObject) <= LOS_SECTION_OBJECT_LIMIT);
225         g_assert (num_chunks > 0);
226
227  retry:
228         if (num_chunks >= LOS_NUM_FAST_SIZES) {
229                 free_chunks = get_from_size_list (&los_fast_free_lists [0], size);
230         } else {
231                 int i;
232                 for (i = num_chunks; i < LOS_NUM_FAST_SIZES; ++i) {
233                         free_chunks = get_from_size_list (&los_fast_free_lists [i], size);
234                         if (free_chunks)
235                                 break;
236                 }
237                 if (!free_chunks)
238                         free_chunks = get_from_size_list (&los_fast_free_lists [0], size);
239         }
240
241         if (free_chunks)
242                 return (LOSObject*)free_chunks;
243
244         if (!sgen_memgov_try_alloc_space (LOS_SECTION_SIZE, SPACE_LOS))
245                 return NULL;
246
247         section = sgen_alloc_os_memory_aligned (LOS_SECTION_SIZE, LOS_SECTION_SIZE, SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE, NULL);
248
249         if (!section)
250                 return NULL;
251
252         free_chunks = (LOSFreeChunks*)((char*)section + LOS_CHUNK_SIZE);
253         free_chunks->size = LOS_SECTION_SIZE - LOS_CHUNK_SIZE;
254         free_chunks->next_size = los_fast_free_lists [0];
255         los_fast_free_lists [0] = free_chunks;
256
257         section->num_free_chunks = LOS_SECTION_NUM_CHUNKS;
258
259         section->free_chunk_map = (unsigned char*)section + sizeof (LOSSection);
260         g_assert (sizeof (LOSSection) + LOS_SECTION_NUM_CHUNKS + 1 <= LOS_CHUNK_SIZE);
261         section->free_chunk_map [0] = 0;
262         memset (section->free_chunk_map + 1, 1, LOS_SECTION_NUM_CHUNKS);
263
264         section->next = los_sections;
265         los_sections = section;
266
267         ++los_num_sections;
268
269         goto retry;
270 }
271
272 static void
273 free_los_section_memory (LOSObject *obj, size_t size)
274 {
275         LOSSection *section = LOS_SECTION_FOR_OBJ (obj);
276         int num_chunks, i, start_index;
277
278         size += LOS_CHUNK_SIZE - 1;
279         size &= ~(LOS_CHUNK_SIZE - 1);
280
281         num_chunks = size >> LOS_CHUNK_BITS;
282
283         g_assert (size > 0 && size - sizeof (LOSObject) <= LOS_SECTION_OBJECT_LIMIT);
284         g_assert (num_chunks > 0);
285
286         section->num_free_chunks += num_chunks;
287         g_assert (section->num_free_chunks <= LOS_SECTION_NUM_CHUNKS);
288
289         /*
290          * We could free the LOS section here if it's empty, but we
291          * can't unless we also remove its free chunks from the fast
292          * free lists.  Instead, we do it in los_sweep().
293          */
294
295         start_index = LOS_CHUNK_INDEX (obj, section);
296         for (i = start_index; i < start_index + num_chunks; ++i) {
297                 g_assert (!section->free_chunk_map [i]);
298                 section->free_chunk_map [i] = 1;
299         }
300
301         add_free_chunk ((LOSFreeChunks*)obj, size);
302 }
303
304 static int pagesize;
305
306 void
307 sgen_los_free_object (LOSObject *obj)
308 {
309 #ifndef LOS_DUMMY
310         size_t size = obj->size;
311         DEBUG (4, fprintf (gc_debug_file, "Freed large object %p, size %lu\n", obj->data, (unsigned long)obj->size));
312         binary_protocol_empty (obj->data, obj->size);
313
314         los_memory_usage -= size;
315         los_num_objects--;
316
317 #ifdef USE_MALLOC
318         free (obj);
319 #else
320         if (size > LOS_SECTION_OBJECT_LIMIT) {
321                 if (!pagesize)
322                         pagesize = mono_pagesize ();
323                 size += sizeof (LOSObject);
324                 size += pagesize - 1;
325                 size &= ~(pagesize - 1);
326                 sgen_free_os_memory (obj, size, SGEN_ALLOC_HEAP);
327                 sgen_memgov_release_space (size, SPACE_LOS);
328         } else {
329                 free_los_section_memory (obj, size + sizeof (LOSObject));
330 #ifdef LOS_CONSISTENCY_CHECKS
331                 los_consistency_check ();
332 #endif
333         }
334 #endif
335 #endif
336 }
337
338 /*
339  * Objects with size >= MAX_SMALL_SIZE are allocated in the large object space.
340  * They are currently kept track of with a linked list.
341  * They don't move, so there is no need to pin them during collection
342  * and we avoid the memcpy overhead.
343  */
344 void*
345 sgen_los_alloc_large_inner (MonoVTable *vtable, size_t size)
346 {
347         LOSObject *obj = NULL;
348         void **vtslot;
349
350         g_assert (size > SGEN_MAX_SMALL_OBJ_SIZE);
351
352 #ifdef LOS_DUMMY
353         if (!los_segment)
354                 los_segment = sgen_alloc_os_memory (LOS_SEGMENT_SIZE, SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE, NULL);
355         los_segment_index = ALIGN_UP (los_segment_index);
356
357         obj = (LOSObject*)(los_segment + los_segment_index);
358         los_segment_index += size + sizeof (LOSObject);
359         g_assert (los_segment_index <= LOS_SEGMENT_SIZE);
360 #else
361         sgen_ensure_free_space (size);
362
363 #ifdef USE_MALLOC
364         obj = malloc (size + sizeof (LOSObject));
365         memset (obj, 0, size + sizeof (LOSObject));
366 #else
367         if (size > LOS_SECTION_OBJECT_LIMIT) {
368                 size_t alloc_size = size;
369                 if (!pagesize)
370                         pagesize = mono_pagesize ();
371                 alloc_size += sizeof (LOSObject);
372                 alloc_size += pagesize - 1;
373                 alloc_size &= ~(pagesize - 1);
374                 if (sgen_memgov_try_alloc_space (alloc_size, SPACE_LOS)) {
375                         obj = sgen_alloc_os_memory (alloc_size, SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE, NULL);
376                         if (obj)
377                                 obj->huge_object = TRUE;
378                 }
379         } else {
380                 obj = get_los_section_memory (size + sizeof (LOSObject));
381                 if (obj)
382                         memset (obj, 0, size + sizeof (LOSObject));
383         }
384 #endif
385 #endif
386         if (!obj)
387                 return NULL;
388         g_assert (!((mword)obj->data & (SGEN_ALLOC_ALIGN - 1)));
389         obj->size = size;
390         vtslot = (void**)obj->data;
391         *vtslot = vtable;
392         sgen_update_heap_boundaries ((mword)obj->data, (mword)obj->data + size);
393         obj->next = los_object_list;
394         los_object_list = obj;
395         los_memory_usage += size;
396         los_num_objects++;
397         DEBUG (4, fprintf (gc_debug_file, "Allocated large object %p, vtable: %p (%s), size: %zd\n", obj->data, vtable, vtable->klass->name, size));
398         binary_protocol_alloc (obj->data, vtable, size);
399
400 #ifdef LOS_CONSISTENCY_CHECK
401         los_consistency_check ();
402 #endif
403
404         return obj->data;
405 }
406
407 void
408 sgen_los_sweep (void)
409 {
410         LOSSection *section, *prev;
411         int i;
412         int num_sections = 0;
413
414         for (i = 0; i < LOS_NUM_FAST_SIZES; ++i)
415                 los_fast_free_lists [i] = NULL;
416
417         prev = NULL;
418         section = los_sections;
419         while (section) {
420                 if (section->num_free_chunks == LOS_SECTION_NUM_CHUNKS) {
421                         LOSSection *next = section->next;
422                         if (prev)
423                                 prev->next = next;
424                         else
425                                 los_sections = next;
426                         sgen_free_os_memory (section, LOS_SECTION_SIZE, SGEN_ALLOC_HEAP);
427                         sgen_memgov_release_space (LOS_SECTION_SIZE, SPACE_LOS);
428                         section = next;
429                         --los_num_sections;
430                         continue;
431                 }
432
433                 for (i = 0; i <= LOS_SECTION_NUM_CHUNKS; ++i) {
434                         if (section->free_chunk_map [i]) {
435                                 int j;
436                                 for (j = i + 1; j <= LOS_SECTION_NUM_CHUNKS && section->free_chunk_map [j]; ++j)
437                                         ;
438                                 add_free_chunk ((LOSFreeChunks*)((char*)section + (i << LOS_CHUNK_BITS)), (j - i) << LOS_CHUNK_BITS);
439                                 i = j - 1;
440                         }
441                 }
442
443                 prev = section;
444                 section = section->next;
445
446                 ++num_sections;
447         }
448
449 #ifdef LOS_CONSISTENCY_CHECK
450         los_consistency_check ();
451 #endif
452
453         /*
454         g_print ("LOS sections: %d  objects: %d  usage: %d\n", num_sections, los_num_objects, los_memory_usage);
455         for (i = 0; i < LOS_NUM_FAST_SIZES; ++i) {
456                 int num_chunks = 0;
457                 LOSFreeChunks *free_chunks;
458                 for (free_chunks = los_fast_free_lists [i]; free_chunks; free_chunks = free_chunks->next_size)
459                         ++num_chunks;
460                 g_print ("  %d: %d\n", i, num_chunks);
461         }
462         */
463
464         g_assert (los_num_sections == num_sections);
465 }
466
467 gboolean
468 sgen_ptr_is_in_los (char *ptr, char **start)
469 {
470         LOSObject *obj;
471
472         *start = NULL;
473         for (obj = los_object_list; obj; obj = obj->next) {
474                 char *end = obj->data + obj->size;
475
476                 if (ptr >= obj->data && ptr < end) {
477                         *start = obj->data;
478                         return TRUE;
479                 }
480         }
481         return FALSE;
482 }
483
484 void
485 sgen_los_iterate_objects (IterateObjectCallbackFunc cb, void *user_data)
486 {
487         LOSObject *obj;
488
489         for (obj = los_object_list; obj; obj = obj->next)
490                 cb (obj->data, obj->size, user_data);
491 }
492
493 gboolean
494 sgen_los_is_valid_object (char *object)
495 {
496         LOSObject *obj;
497
498         for (obj = los_object_list; obj; obj = obj->next) {
499                 if (obj->data == object)
500                         return TRUE;
501         }
502         return FALSE;
503 }
504
505 gboolean
506 mono_sgen_los_describe_pointer (char *ptr)
507 {
508         LOSObject *obj;
509
510         for (obj = los_object_list; obj; obj = obj->next) {
511                 MonoVTable *vtable;
512                 if (obj->data > ptr || obj->data + obj->size <= ptr)
513                         continue;
514
515                 if (obj->size > LOS_SECTION_OBJECT_LIMIT)
516                         fprintf (gc_debug_file, "huge-los-ptr ");
517                 else
518                         fprintf (gc_debug_file, "los-ptr ");
519
520                 vtable = (MonoVTable*)SGEN_LOAD_VTABLE (obj->data);
521
522                 if (obj->data == ptr)
523                         fprintf (gc_debug_file, "(object %s.%s size %d)", 
524                                          vtable->klass->name_space, vtable->klass->name, (int)obj->size);
525                 else
526                         fprintf (gc_debug_file, "(interior-ptr offset %td of %p (%s.%s) size %d)",
527                                          ptr - obj->data, obj->data,
528                                          vtable->klass->name_space, vtable->klass->name, (int)obj->size);
529
530                 return TRUE;
531         }
532         return FALSE;
533 }
534
535 void
536 sgen_los_iterate_live_block_ranges (sgen_cardtable_block_callback callback)
537 {
538         LOSObject *obj;
539         for (obj = los_object_list; obj; obj = obj->next) {
540                 MonoVTable *vt = (MonoVTable*)SGEN_LOAD_VTABLE (obj->data);
541                 if (SGEN_VTABLE_HAS_REFERENCES (vt))
542                         callback ((mword)obj->data, (mword)obj->size);
543         }
544 }
545
546 #ifdef SGEN_HAVE_CARDTABLE
547 void
548 sgen_los_scan_card_table (SgenGrayQueue *queue)
549 {
550         LOSObject *obj;
551
552         for (obj = los_object_list; obj; obj = obj->next) {
553                 sgen_cardtable_scan_object (obj->data, obj->size, NULL, queue);
554         }
555 }
556 #endif
557
558 #endif /* HAVE_SGEN_GC */