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;
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 int 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 int num_chunks, i, start_index;
163 g_assert ((size & (LOS_CHUNK_SIZE - 1)) == 0);
167 if (free_chunks->size >= size)
169 list = &(*list)->next_size;
175 *list = free_chunks->next_size;
177 if (free_chunks->size > size)
178 add_free_chunk ((LOSFreeChunks*)((char*)free_chunks + size), free_chunks->size - size);
180 num_chunks = size >> LOS_CHUNK_BITS;
182 section = LOS_SECTION_FOR_OBJ (free_chunks);
184 start_index = LOS_CHUNK_INDEX (free_chunks, section);
185 for (i = start_index; i < start_index + num_chunks; ++i) {
186 g_assert (section->free_chunk_map [i]);
187 section->free_chunk_map [i] = 0;
190 section->num_free_chunks -= size >> LOS_CHUNK_BITS;
191 g_assert (section->num_free_chunks >= 0);
197 get_los_section_memory (size_t size)
200 LOSFreeChunks *free_chunks;
203 size += LOS_CHUNK_SIZE - 1;
204 size &= ~(LOS_CHUNK_SIZE - 1);
206 num_chunks = size >> LOS_CHUNK_BITS;
208 g_assert (size > 0 && size - sizeof (LOSObject) <= LOS_SECTION_OBJECT_LIMIT);
209 g_assert (num_chunks > 0);
212 if (num_chunks >= LOS_NUM_FAST_SIZES) {
213 free_chunks = get_from_size_list (&los_fast_free_lists [0], size);
216 for (i = num_chunks; i < LOS_NUM_FAST_SIZES; ++i) {
217 free_chunks = get_from_size_list (&los_fast_free_lists [i], size);
222 free_chunks = get_from_size_list (&los_fast_free_lists [0], size);
226 return (LOSObject*)free_chunks;
228 if (!sgen_memgov_try_alloc_space (LOS_SECTION_SIZE, SPACE_LOS))
231 section = sgen_alloc_os_memory_aligned (LOS_SECTION_SIZE, LOS_SECTION_SIZE, SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE, NULL);
236 free_chunks = (LOSFreeChunks*)((char*)section + LOS_CHUNK_SIZE);
237 free_chunks->size = LOS_SECTION_SIZE - LOS_CHUNK_SIZE;
238 free_chunks->next_size = los_fast_free_lists [0];
239 los_fast_free_lists [0] = free_chunks;
241 section->num_free_chunks = LOS_SECTION_NUM_CHUNKS;
243 section->free_chunk_map = (unsigned char*)section + sizeof (LOSSection);
244 g_assert (sizeof (LOSSection) + LOS_SECTION_NUM_CHUNKS + 1 <= LOS_CHUNK_SIZE);
245 section->free_chunk_map [0] = 0;
246 memset (section->free_chunk_map + 1, 1, LOS_SECTION_NUM_CHUNKS);
248 section->next = los_sections;
249 los_sections = section;
257 free_los_section_memory (LOSObject *obj, size_t size)
259 LOSSection *section = LOS_SECTION_FOR_OBJ (obj);
260 int num_chunks, i, start_index;
262 size += LOS_CHUNK_SIZE - 1;
263 size &= ~(LOS_CHUNK_SIZE - 1);
265 num_chunks = size >> LOS_CHUNK_BITS;
267 g_assert (size > 0 && size - sizeof (LOSObject) <= LOS_SECTION_OBJECT_LIMIT);
268 g_assert (num_chunks > 0);
270 section->num_free_chunks += num_chunks;
271 g_assert (section->num_free_chunks <= LOS_SECTION_NUM_CHUNKS);
274 * We could free the LOS section here if it's empty, but we
275 * can't unless we also remove its free chunks from the fast
276 * free lists. Instead, we do it in los_sweep().
279 start_index = LOS_CHUNK_INDEX (obj, section);
280 for (i = start_index; i < start_index + num_chunks; ++i) {
281 g_assert (!section->free_chunk_map [i]);
282 section->free_chunk_map [i] = 1;
285 add_free_chunk ((LOSFreeChunks*)obj, size);
291 sgen_los_free_object (LOSObject *obj)
294 size_t size = obj->size;
295 SGEN_LOG (4, "Freed large object %p, size %lu", obj->data, (unsigned long)obj->size);
296 binary_protocol_empty (obj->data, obj->size);
298 los_memory_usage -= size;
304 if (size > LOS_SECTION_OBJECT_LIMIT) {
306 pagesize = mono_pagesize ();
307 size += sizeof (LOSObject);
308 size += pagesize - 1;
309 size &= ~(pagesize - 1);
310 sgen_free_os_memory (obj, size, SGEN_ALLOC_HEAP);
311 sgen_memgov_release_space (size, SPACE_LOS);
313 free_los_section_memory (obj, size + sizeof (LOSObject));
314 #ifdef LOS_CONSISTENCY_CHECKS
315 los_consistency_check ();
323 * Objects with size >= MAX_SMALL_SIZE are allocated in the large object space.
324 * They are currently kept track of with a linked list.
325 * They don't move, so there is no need to pin them during collection
326 * and we avoid the memcpy overhead.
329 sgen_los_alloc_large_inner (MonoVTable *vtable, size_t size)
331 LOSObject *obj = NULL;
334 g_assert (size > SGEN_MAX_SMALL_OBJ_SIZE);
335 g_assert ((size & 1) == 0);
338 * size + sizeof (LOSObject) <= SIZE_MAX - (mono_pagesize () - 1)
342 * size <= SIZE_MAX - (mono_pagesize () - 1) - sizeof (LOSObject)
344 if (size > SIZE_MAX - (mono_pagesize () - 1) - sizeof (LOSObject))
349 los_segment = sgen_alloc_os_memory (LOS_SEGMENT_SIZE, SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE, NULL);
350 los_segment_index = ALIGN_UP (los_segment_index);
352 obj = (LOSObject*)(los_segment + los_segment_index);
353 los_segment_index += size + sizeof (LOSObject);
354 g_assert (los_segment_index <= LOS_SEGMENT_SIZE);
356 sgen_ensure_free_space (size);
359 obj = malloc (size + sizeof (LOSObject));
360 memset (obj, 0, size + sizeof (LOSObject));
362 if (size > LOS_SECTION_OBJECT_LIMIT) {
363 size_t alloc_size = size;
365 pagesize = mono_pagesize ();
366 alloc_size += sizeof (LOSObject);
367 alloc_size += pagesize - 1;
368 alloc_size &= ~(pagesize - 1);
369 if (sgen_memgov_try_alloc_space (alloc_size, SPACE_LOS)) {
370 obj = sgen_alloc_os_memory (alloc_size, SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE, NULL);
373 obj = get_los_section_memory (size + sizeof (LOSObject));
375 memset (obj, 0, size + sizeof (LOSObject));
381 g_assert (!((mword)obj->data & (SGEN_ALLOC_ALIGN - 1)));
383 vtslot = (void**)obj->data;
385 sgen_update_heap_boundaries ((mword)obj->data, (mword)obj->data + size);
386 obj->next = los_object_list;
387 los_object_list = obj;
388 los_memory_usage += size;
390 SGEN_LOG (4, "Allocated large object %p, vtable: %p (%s), size: %zd", obj->data, vtable, vtable->klass->name, size);
391 binary_protocol_alloc (obj->data, vtable, size);
393 #ifdef LOS_CONSISTENCY_CHECK
394 los_consistency_check ();
401 sgen_los_sweep (void)
403 LOSSection *section, *prev;
405 int num_sections = 0;
407 for (i = 0; i < LOS_NUM_FAST_SIZES; ++i)
408 los_fast_free_lists [i] = NULL;
411 section = los_sections;
413 if (section->num_free_chunks == LOS_SECTION_NUM_CHUNKS) {
414 LOSSection *next = section->next;
419 sgen_free_os_memory (section, LOS_SECTION_SIZE, SGEN_ALLOC_HEAP);
420 sgen_memgov_release_space (LOS_SECTION_SIZE, SPACE_LOS);
426 for (i = 0; i <= LOS_SECTION_NUM_CHUNKS; ++i) {
427 if (section->free_chunk_map [i]) {
429 for (j = i + 1; j <= LOS_SECTION_NUM_CHUNKS && section->free_chunk_map [j]; ++j)
431 add_free_chunk ((LOSFreeChunks*)((char*)section + (i << LOS_CHUNK_BITS)), (j - i) << LOS_CHUNK_BITS);
437 section = section->next;
442 #ifdef LOS_CONSISTENCY_CHECK
443 los_consistency_check ();
447 g_print ("LOS sections: %d objects: %d usage: %d\n", num_sections, los_num_objects, los_memory_usage);
448 for (i = 0; i < LOS_NUM_FAST_SIZES; ++i) {
450 LOSFreeChunks *free_chunks;
451 for (free_chunks = los_fast_free_lists [i]; free_chunks; free_chunks = free_chunks->next_size)
453 g_print (" %d: %d\n", i, num_chunks);
457 g_assert (los_num_sections == num_sections);
461 sgen_ptr_is_in_los (char *ptr, char **start)
466 for (obj = los_object_list; obj; obj = obj->next) {
467 char *end = obj->data + obj->size;
469 if (ptr >= obj->data && ptr < end) {
478 sgen_los_iterate_objects (IterateObjectCallbackFunc cb, void *user_data)
482 for (obj = los_object_list; obj; obj = obj->next)
483 cb (obj->data, obj->size, user_data);
487 sgen_los_is_valid_object (char *object)
491 for (obj = los_object_list; obj; obj = obj->next) {
492 if (obj->data == object)
499 mono_sgen_los_describe_pointer (char *ptr)
503 for (obj = los_object_list; obj; obj = obj->next) {
505 const char *los_kind;
509 if (obj->data > ptr || obj->data + obj->size <= ptr)
512 size = sgen_los_object_size (obj);
513 pinned = sgen_los_object_is_pinned (obj->data);
515 if (size > LOS_SECTION_OBJECT_LIMIT)
516 los_kind = "huge-los-ptr";
518 los_kind = "los-ptr";
520 vtable = (MonoVTable*)SGEN_LOAD_VTABLE (obj->data);
522 if (obj->data == ptr) {
523 SGEN_LOG (0, "%s (size %d pin %d)\n", los_kind, (int)size, pinned ? 1 : 0);
525 SGEN_LOG (0, "%s (interior-ptr offset %td size %d pin %d)",
526 los_kind, ptr - obj->data, (int)size, pinned ? 1 : 0);
535 sgen_los_iterate_live_block_ranges (sgen_cardtable_block_callback callback)
538 for (obj = los_object_list; obj; obj = obj->next) {
539 MonoVTable *vt = (MonoVTable*)SGEN_LOAD_VTABLE (obj->data);
540 if (SGEN_VTABLE_HAS_REFERENCES (vt))
541 callback ((mword)obj->data, (mword)obj->size);
546 sgen_los_scan_card_table (gboolean mod_union, SgenGrayQueue *queue)
550 for (obj = los_object_list; obj; obj = obj->next) {
551 guint8 *cards = NULL;
553 cards = obj->cardtable_mod_union;
557 sgen_cardtable_scan_object (obj->data, obj->size, cards, mod_union, queue);
562 sgen_los_update_cardtable_mod_union (void)
566 for (obj = los_object_list; obj; obj = obj->next) {
567 obj->cardtable_mod_union = sgen_card_table_update_mod_union (obj->cardtable_mod_union,
568 obj->data, obj->size, NULL);
573 sgen_los_object_size (LOSObject *obj)
575 return obj->size & ~1L;
579 sgen_los_header_for_object (char *data)
582 return (LOSObject*)(data - (int)(&(((LOSObject*)0)->data)));
584 return (LOSObject*)(data - sizeof (LOSObject));
589 sgen_los_pin_object (char *data)
591 LOSObject *obj = sgen_los_header_for_object (data);
592 obj->size = obj->size | 1;
593 binary_protocol_pin (data, (gpointer)SGEN_LOAD_VTABLE (data), sgen_safe_object_get_size ((MonoObject*)data));
597 sgen_los_unpin_object (char *data)
599 LOSObject *obj = sgen_los_header_for_object (data);
600 obj->size = sgen_los_object_size (obj);
604 sgen_los_object_is_pinned (char *data)
606 LOSObject *obj = sgen_los_header_for_object (data);
607 return obj->size & 1;
610 #endif /* HAVE_SGEN_GC */