2 * sgen-los.c: Large objects space.
5 * Paolo Molaro (lupus@ximian.com)
7 * Copyright 2005-2010 Novell, Inc (http://www.novell.com)
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 * Copyright 2001-2003 Ximian, Inc
15 * Copyright 2003-2010 Novell, Inc.
16 * Copyright (C) 2012 Xamarin Inc
18 * This library is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU Library General Public
20 * License 2.0 as published by the Free Software Foundation;
22 * This library is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * Library General Public License for more details.
27 * You should have received a copy of the GNU Library General Public
28 * License 2.0 along with this library; if not, write to the Free
29 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
36 #include "metadata/sgen-gc.h"
37 #include "metadata/sgen-protocol.h"
38 #include "metadata/sgen-cardtable.h"
39 #include "metadata/sgen-memory-governor.h"
40 #include "utils/mono-mmap.h"
42 #define LOS_SECTION_SIZE (1024 * 1024)
45 * This shouldn't be much smaller or larger than MAX_SMALL_OBJ_SIZE.
46 * Must be at least sizeof (LOSSection).
48 #define LOS_CHUNK_SIZE 4096
49 #define LOS_CHUNK_BITS 12
51 /* Largest object that can be allocated in a section. */
52 #define LOS_SECTION_OBJECT_LIMIT (LOS_SECTION_SIZE - LOS_CHUNK_SIZE - sizeof (LOSObject))
53 //#define LOS_SECTION_OBJECT_LIMIT 0
54 #define LOS_SECTION_NUM_CHUNKS ((LOS_SECTION_SIZE >> LOS_CHUNK_BITS) - 1)
56 #define LOS_SECTION_FOR_OBJ(obj) ((LOSSection*)((mword)(obj) & ~(mword)(LOS_SECTION_SIZE - 1)))
57 #define LOS_CHUNK_INDEX(obj,section) (((char*)(obj) - (char*)(section)) >> LOS_CHUNK_BITS)
59 #define LOS_NUM_FAST_SIZES 32
61 typedef struct _LOSFreeChunks LOSFreeChunks;
62 struct _LOSFreeChunks {
63 LOSFreeChunks *next_size;
67 typedef struct _LOSSection LOSSection;
70 size_t num_free_chunks;
71 unsigned char *free_chunk_map;
74 LOSObject *los_object_list = NULL;
75 mword los_memory_usage = 0;
77 static LOSSection *los_sections = NULL;
78 static LOSFreeChunks *los_fast_free_lists [LOS_NUM_FAST_SIZES]; /* 0 is for larger sizes */
79 static mword los_num_objects = 0;
80 static int los_num_sections = 0;
83 //#define LOS_CONSISTENCY_CHECK
87 #define LOS_SEGMENT_SIZE (4096 * 1024)
89 static char *los_segment = NULL;
90 static int los_segment_index = 0;
93 #ifdef LOS_CONSISTENCY_CHECK
95 los_consistency_check (void)
100 mword memory_usage = 0;
102 for (obj = los_object_list; obj; obj = obj->next) {
103 char *end = obj->data + obj->size;
104 int start_index, num_chunks;
106 memory_usage += obj->size;
108 if (obj->size > LOS_SECTION_OBJECT_LIMIT)
111 section = LOS_SECTION_FOR_OBJ (obj);
113 g_assert (end <= (char*)section + LOS_SECTION_SIZE);
115 start_index = LOS_CHUNK_INDEX (obj, section);
116 num_chunks = (obj->size + sizeof (LOSObject) + LOS_CHUNK_SIZE - 1) >> LOS_CHUNK_BITS;
117 for (i = start_index; i < start_index + num_chunks; ++i)
118 g_assert (!section->free_chunk_map [i]);
121 for (i = 0; i < LOS_NUM_FAST_SIZES; ++i) {
122 LOSFreeChunks *size_chunks;
123 for (size_chunks = los_fast_free_lists [i]; size_chunks; size_chunks = size_chunks->next_size) {
124 LOSSection *section = LOS_SECTION_FOR_OBJ (size_chunks);
125 int j, num_chunks, start_index;
128 g_assert (size_chunks->size >= LOS_NUM_FAST_SIZES * LOS_CHUNK_SIZE);
130 g_assert (size_chunks->size == i * LOS_CHUNK_SIZE);
132 num_chunks = size_chunks->size >> LOS_CHUNK_BITS;
133 start_index = LOS_CHUNK_INDEX (size_chunks, section);
134 for (j = start_index; j < start_index + num_chunks; ++j)
135 g_assert (section->free_chunk_map [j]);
139 g_assert (los_memory_usage == memory_usage);
144 add_free_chunk (LOSFreeChunks *free_chunks, size_t size)
146 size_t num_chunks = size >> LOS_CHUNK_BITS;
148 free_chunks->size = size;
150 if (num_chunks >= LOS_NUM_FAST_SIZES)
152 free_chunks->next_size = los_fast_free_lists [num_chunks];
153 los_fast_free_lists [num_chunks] = free_chunks;
156 static LOSFreeChunks*
157 get_from_size_list (LOSFreeChunks **list, size_t size)
159 LOSFreeChunks *free_chunks = NULL;
161 size_t i, num_chunks, start_index;
164 g_assert ((size & (LOS_CHUNK_SIZE - 1)) == 0);
168 if (free_chunks->size >= size)
170 list = &(*list)->next_size;
176 *list = free_chunks->next_size;
178 if (free_chunks->size > size)
179 add_free_chunk ((LOSFreeChunks*)((char*)free_chunks + size), free_chunks->size - size);
181 num_chunks = size >> LOS_CHUNK_BITS;
183 section = LOS_SECTION_FOR_OBJ (free_chunks);
185 start_index = LOS_CHUNK_INDEX (free_chunks, section);
186 for (i = start_index; i < start_index + num_chunks; ++i) {
187 g_assert (section->free_chunk_map [i]);
188 section->free_chunk_map [i] = 0;
191 section->num_free_chunks -= size >> LOS_CHUNK_BITS;
192 g_assert (section->num_free_chunks >= 0);
198 get_los_section_memory (size_t size)
201 LOSFreeChunks *free_chunks;
204 size += LOS_CHUNK_SIZE - 1;
205 size &= ~(LOS_CHUNK_SIZE - 1);
207 num_chunks = size >> LOS_CHUNK_BITS;
209 g_assert (size > 0 && size - sizeof (LOSObject) <= LOS_SECTION_OBJECT_LIMIT);
210 g_assert (num_chunks > 0);
213 if (num_chunks >= LOS_NUM_FAST_SIZES) {
214 free_chunks = get_from_size_list (&los_fast_free_lists [0], size);
217 for (i = num_chunks; i < LOS_NUM_FAST_SIZES; ++i) {
218 free_chunks = get_from_size_list (&los_fast_free_lists [i], size);
223 free_chunks = get_from_size_list (&los_fast_free_lists [0], size);
227 return (LOSObject*)free_chunks;
229 if (!sgen_memgov_try_alloc_space (LOS_SECTION_SIZE, SPACE_LOS))
232 section = sgen_alloc_os_memory_aligned (LOS_SECTION_SIZE, LOS_SECTION_SIZE, SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE, NULL);
237 free_chunks = (LOSFreeChunks*)((char*)section + LOS_CHUNK_SIZE);
238 free_chunks->size = LOS_SECTION_SIZE - LOS_CHUNK_SIZE;
239 free_chunks->next_size = los_fast_free_lists [0];
240 los_fast_free_lists [0] = free_chunks;
242 section->num_free_chunks = LOS_SECTION_NUM_CHUNKS;
244 section->free_chunk_map = (unsigned char*)section + sizeof (LOSSection);
245 g_assert (sizeof (LOSSection) + LOS_SECTION_NUM_CHUNKS + 1 <= LOS_CHUNK_SIZE);
246 section->free_chunk_map [0] = 0;
247 memset (section->free_chunk_map + 1, 1, LOS_SECTION_NUM_CHUNKS);
249 section->next = los_sections;
250 los_sections = section;
258 free_los_section_memory (LOSObject *obj, size_t size)
260 LOSSection *section = LOS_SECTION_FOR_OBJ (obj);
261 size_t num_chunks, i, start_index;
263 size += LOS_CHUNK_SIZE - 1;
264 size &= ~(LOS_CHUNK_SIZE - 1);
266 num_chunks = size >> LOS_CHUNK_BITS;
268 g_assert (size > 0 && size - sizeof (LOSObject) <= LOS_SECTION_OBJECT_LIMIT);
269 g_assert (num_chunks > 0);
271 section->num_free_chunks += num_chunks;
272 g_assert (section->num_free_chunks <= LOS_SECTION_NUM_CHUNKS);
275 * We could free the LOS section here if it's empty, but we
276 * can't unless we also remove its free chunks from the fast
277 * free lists. Instead, we do it in los_sweep().
280 start_index = LOS_CHUNK_INDEX (obj, section);
281 for (i = start_index; i < start_index + num_chunks; ++i) {
282 g_assert (!section->free_chunk_map [i]);
283 section->free_chunk_map [i] = 1;
286 add_free_chunk ((LOSFreeChunks*)obj, size);
292 sgen_los_free_object (LOSObject *obj)
295 size_t size = obj->size;
296 SGEN_LOG (4, "Freed large object %p, size %lu", obj->data, (unsigned long)obj->size);
297 binary_protocol_empty (obj->data, obj->size);
299 los_memory_usage -= size;
305 if (size > LOS_SECTION_OBJECT_LIMIT) {
307 pagesize = mono_pagesize ();
308 size += sizeof (LOSObject);
309 size += pagesize - 1;
310 size &= ~(pagesize - 1);
311 sgen_free_os_memory (obj, size, SGEN_ALLOC_HEAP);
312 sgen_memgov_release_space (size, SPACE_LOS);
314 free_los_section_memory (obj, size + sizeof (LOSObject));
315 #ifdef LOS_CONSISTENCY_CHECKS
316 los_consistency_check ();
324 * Objects with size >= MAX_SMALL_SIZE are allocated in the large object space.
325 * They are currently kept track of with a linked list.
326 * They don't move, so there is no need to pin them during collection
327 * and we avoid the memcpy overhead.
330 sgen_los_alloc_large_inner (MonoVTable *vtable, size_t size)
332 LOSObject *obj = NULL;
335 g_assert (size > SGEN_MAX_SMALL_OBJ_SIZE);
336 g_assert ((size & 1) == 0);
339 * size + sizeof (LOSObject) <= SSIZE_MAX - (mono_pagesize () - 1)
343 * size <= SSIZE_MAX - (mono_pagesize () - 1) - sizeof (LOSObject)
345 if (size > SSIZE_MAX - (mono_pagesize () - 1) - sizeof (LOSObject))
350 los_segment = sgen_alloc_os_memory (LOS_SEGMENT_SIZE, SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE, NULL);
351 los_segment_index = ALIGN_UP (los_segment_index);
353 obj = (LOSObject*)(los_segment + los_segment_index);
354 los_segment_index += size + sizeof (LOSObject);
355 g_assert (los_segment_index <= LOS_SEGMENT_SIZE);
357 sgen_ensure_free_space (size);
360 obj = malloc (size + sizeof (LOSObject));
361 memset (obj, 0, size + sizeof (LOSObject));
363 if (size > LOS_SECTION_OBJECT_LIMIT) {
364 size_t alloc_size = size;
366 pagesize = mono_pagesize ();
367 alloc_size += sizeof (LOSObject);
368 alloc_size += pagesize - 1;
369 alloc_size &= ~(pagesize - 1);
370 if (sgen_memgov_try_alloc_space (alloc_size, SPACE_LOS)) {
371 obj = sgen_alloc_os_memory (alloc_size, SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE, NULL);
374 obj = get_los_section_memory (size + sizeof (LOSObject));
376 memset (obj, 0, size + sizeof (LOSObject));
382 g_assert (!((mword)obj->data & (SGEN_ALLOC_ALIGN - 1)));
384 vtslot = (void**)obj->data;
386 sgen_update_heap_boundaries ((mword)obj->data, (mword)obj->data + size);
387 obj->next = los_object_list;
388 los_object_list = obj;
389 los_memory_usage += size;
391 SGEN_LOG (4, "Allocated large object %p, vtable: %p (%s), size: %zd", obj->data, vtable, vtable->klass->name, size);
392 binary_protocol_alloc (obj->data, vtable, size);
394 #ifdef LOS_CONSISTENCY_CHECK
395 los_consistency_check ();
402 sgen_los_sweep (void)
404 LOSSection *section, *prev;
406 int num_sections = 0;
408 for (i = 0; i < LOS_NUM_FAST_SIZES; ++i)
409 los_fast_free_lists [i] = NULL;
412 section = los_sections;
414 if (section->num_free_chunks == LOS_SECTION_NUM_CHUNKS) {
415 LOSSection *next = section->next;
420 sgen_free_os_memory (section, LOS_SECTION_SIZE, SGEN_ALLOC_HEAP);
421 sgen_memgov_release_space (LOS_SECTION_SIZE, SPACE_LOS);
427 for (i = 0; i <= LOS_SECTION_NUM_CHUNKS; ++i) {
428 if (section->free_chunk_map [i]) {
430 for (j = i + 1; j <= LOS_SECTION_NUM_CHUNKS && section->free_chunk_map [j]; ++j)
432 add_free_chunk ((LOSFreeChunks*)((char*)section + (i << LOS_CHUNK_BITS)), (j - i) << LOS_CHUNK_BITS);
438 section = section->next;
443 #ifdef LOS_CONSISTENCY_CHECK
444 los_consistency_check ();
448 g_print ("LOS sections: %d objects: %d usage: %d\n", num_sections, los_num_objects, los_memory_usage);
449 for (i = 0; i < LOS_NUM_FAST_SIZES; ++i) {
451 LOSFreeChunks *free_chunks;
452 for (free_chunks = los_fast_free_lists [i]; free_chunks; free_chunks = free_chunks->next_size)
454 g_print (" %d: %d\n", i, num_chunks);
458 g_assert (los_num_sections == num_sections);
462 sgen_ptr_is_in_los (char *ptr, char **start)
467 for (obj = los_object_list; obj; obj = obj->next) {
468 char *end = obj->data + obj->size;
470 if (ptr >= obj->data && ptr < end) {
479 sgen_los_iterate_objects (IterateObjectCallbackFunc cb, void *user_data)
483 for (obj = los_object_list; obj; obj = obj->next)
484 cb (obj->data, obj->size, user_data);
488 sgen_los_is_valid_object (char *object)
492 for (obj = los_object_list; obj; obj = obj->next) {
493 if (obj->data == object)
500 mono_sgen_los_describe_pointer (char *ptr)
504 for (obj = los_object_list; obj; obj = obj->next) {
506 const char *los_kind;
510 if (obj->data > ptr || obj->data + obj->size <= ptr)
513 size = sgen_los_object_size (obj);
514 pinned = sgen_los_object_is_pinned (obj->data);
516 if (size > LOS_SECTION_OBJECT_LIMIT)
517 los_kind = "huge-los-ptr";
519 los_kind = "los-ptr";
521 vtable = (MonoVTable*)SGEN_LOAD_VTABLE (obj->data);
523 if (obj->data == ptr) {
524 SGEN_LOG (0, "%s (size %d pin %d)\n", los_kind, (int)size, pinned ? 1 : 0);
526 SGEN_LOG (0, "%s (interior-ptr offset %td size %d pin %d)",
527 los_kind, ptr - obj->data, (int)size, pinned ? 1 : 0);
536 sgen_los_iterate_live_block_ranges (sgen_cardtable_block_callback callback)
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);
547 sgen_los_scan_card_table (gboolean mod_union, SgenGrayQueue *queue)
551 for (obj = los_object_list; obj; obj = obj->next) {
554 if (!SGEN_OBJECT_HAS_REFERENCES (obj->data))
558 cards = obj->cardtable_mod_union;
564 sgen_cardtable_scan_object (obj->data, obj->size, cards, mod_union, queue);
569 sgen_los_count_cards (long long *num_total_cards, long long *num_marked_cards)
572 long long total_cards = 0;
573 long long marked_cards = 0;
575 for (obj = los_object_list; obj; obj = obj->next) {
577 guint8 *cards = sgen_card_table_get_card_scan_address ((mword) obj->data);
578 guint8 *cards_end = sgen_card_table_get_card_scan_address ((mword) obj->data + obj->size - 1);
579 mword num_cards = (cards_end - cards) + 1;
581 if (!SGEN_OBJECT_HAS_REFERENCES (obj->data))
584 total_cards += num_cards;
585 for (i = 0; i < num_cards; ++i) {
591 *num_total_cards = total_cards;
592 *num_marked_cards = marked_cards;
596 sgen_los_update_cardtable_mod_union (void)
600 for (obj = los_object_list; obj; obj = obj->next) {
601 if (!SGEN_OBJECT_HAS_REFERENCES (obj->data))
603 obj->cardtable_mod_union = sgen_card_table_update_mod_union (obj->cardtable_mod_union,
604 obj->data, obj->size, NULL);
609 sgen_los_object_size (LOSObject *obj)
611 return obj->size & ~1L;
615 sgen_los_header_for_object (char *data)
618 return (LOSObject*)(data - (int)(&(((LOSObject*)0)->data)));
620 return (LOSObject*)(data - sizeof (LOSObject));
625 sgen_los_pin_object (char *data)
627 LOSObject *obj = sgen_los_header_for_object (data);
628 obj->size = obj->size | 1;
629 binary_protocol_pin (data, (gpointer)SGEN_LOAD_VTABLE (data), sgen_safe_object_get_size ((MonoObject*)data));
633 sgen_los_unpin_object (char *data)
635 LOSObject *obj = sgen_los_header_for_object (data);
636 obj->size = sgen_los_object_size (obj);
640 sgen_los_object_is_pinned (char *data)
642 LOSObject *obj = sgen_los_header_for_object (data);
643 return obj->size & 1;
646 #endif /* HAVE_SGEN_GC */