2 * sgen-internal.c: Internal lock-free memory allocator.
4 * Copyright (C) 2012 Xamarin Inc
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License 2.0 as published by the Free Software Foundation;
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License 2.0 along with this library; if not, write to the Free
17 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "utils/mono-counters.h"
25 #include "metadata/sgen-gc.h"
26 #include "utils/lock-free-alloc.h"
27 #include "metadata/sgen-memory-governor.h"
29 /* keep each size a multiple of ALLOC_ALIGN */
30 static const int allocator_sizes [] = {
31 8, 16, 24, 32, 40, 48, 64, 80,
32 96, 128, 160, 192, 224, 248, 320, 384,
33 448, 528, 584, 680, 816, 1088, 1360, 2040,
34 2336, 2728, 3272, 4088, 5456, 8184 };
36 #define NUM_ALLOCATORS (sizeof (allocator_sizes) / sizeof (int))
38 static MonoLockFreeAllocSizeClass size_classes [NUM_ALLOCATORS];
39 static MonoLockFreeAllocator allocators [NUM_ALLOCATORS];
42 * Find the allocator index for memory chunks that can contain @size
46 index_for_size (size_t size)
49 /* do a binary search or lookup table later. */
50 for (slot = 0; slot < NUM_ALLOCATORS; ++slot) {
51 if (allocator_sizes [slot] >= size)
54 g_assert_not_reached ();
59 * Allocator indexes for the fixed INTERNAL_MEM_XXX types. -1 if that
62 static int fixed_type_allocator_indexes [INTERNAL_MEM_MAX];
65 sgen_register_fixed_internal_mem_type (int type, size_t size)
69 g_assert (type >= 0 && type < INTERNAL_MEM_MAX);
71 slot = index_for_size (size);
74 if (fixed_type_allocator_indexes [type] == -1)
75 fixed_type_allocator_indexes [type] = slot;
77 g_assert (fixed_type_allocator_indexes [type] == slot);
81 description_for_type (int type)
84 case INTERNAL_MEM_PIN_QUEUE: return "pin-queue";
85 case INTERNAL_MEM_FRAGMENT: return "fragment";
86 case INTERNAL_MEM_SECTION: return "section";
87 case INTERNAL_MEM_SCAN_STARTS: return "scan-starts";
88 case INTERNAL_MEM_FIN_TABLE: return "fin-table";
89 case INTERNAL_MEM_FINALIZE_ENTRY: return "finalize-entry";
90 case INTERNAL_MEM_FINALIZE_READY_ENTRY: return "finalize-ready-entry";
91 case INTERNAL_MEM_DISLINK_TABLE: return "dislink-table";
92 case INTERNAL_MEM_DISLINK: return "dislink";
93 case INTERNAL_MEM_ROOTS_TABLE: return "roots-table";
94 case INTERNAL_MEM_ROOT_RECORD: return "root-record";
95 case INTERNAL_MEM_STATISTICS: return "statistics";
96 case INTERNAL_MEM_STAT_PINNED_CLASS: return "pinned-class";
97 case INTERNAL_MEM_STAT_REMSET_CLASS: return "remset-class";
98 case INTERNAL_MEM_REMSET: return "remset";
99 case INTERNAL_MEM_GRAY_QUEUE: return "gray-queue";
100 case INTERNAL_MEM_STORE_REMSET: return "store-remset";
101 case INTERNAL_MEM_MS_TABLES: return "marksweep-tables";
102 case INTERNAL_MEM_MS_BLOCK_INFO: return "marksweep-block-info";
103 case INTERNAL_MEM_EPHEMERON_LINK: return "ephemeron-link";
104 case INTERNAL_MEM_WORKER_DATA: return "worker-data";
105 case INTERNAL_MEM_WORKER_JOB_DATA: return "worker-job-data";
106 case INTERNAL_MEM_BRIDGE_DATA: return "bridge-data";
107 case INTERNAL_MEM_BRIDGE_HASH_TABLE: return "bridge-hash-table";
108 case INTERNAL_MEM_BRIDGE_HASH_TABLE_ENTRY: return "bridge-hash-table-entry";
109 case INTERNAL_MEM_BRIDGE_ALIVE_HASH_TABLE: return "bridge-alive-hash-table";
110 case INTERNAL_MEM_BRIDGE_ALIVE_HASH_TABLE_ENTRY: return "bridge-alive-hash-table-entry";
111 case INTERNAL_MEM_JOB_QUEUE_ENTRY: return "job-queue-entry";
112 case INTERNAL_MEM_TOGGLEREF_DATA: return "toggleref-data";
113 case INTERNAL_MEM_CARDTABLE_MOD_UNION: return "cardtable-mod-union";
115 g_assert_not_reached ();
120 sgen_alloc_internal_dynamic (size_t size, int type, gboolean assert_on_failure)
125 if (size > allocator_sizes [NUM_ALLOCATORS - 1]) {
126 p = sgen_alloc_os_memory (size, SGEN_ALLOC_INTERNAL | SGEN_ALLOC_ACTIVATE, NULL);
128 sgen_assert_memory_alloc (NULL, size, description_for_type (type));
130 index = index_for_size (size);
132 p = mono_lock_free_alloc (&allocators [index]);
134 sgen_assert_memory_alloc (NULL, size, description_for_type (type));
138 MONO_GC_INTERNAL_ALLOC (p, size, type);
143 sgen_free_internal_dynamic (void *addr, size_t size, int type)
148 if (size > allocator_sizes [NUM_ALLOCATORS - 1])
149 sgen_free_os_memory (addr, size, SGEN_ALLOC_INTERNAL);
151 mono_lock_free_free (addr);
153 MONO_GC_INTERNAL_DEALLOC (addr, size, type);
157 sgen_alloc_internal (int type)
159 int index = fixed_type_allocator_indexes [type];
160 int size = allocator_sizes [index];
162 g_assert (index >= 0 && index < NUM_ALLOCATORS);
163 p = mono_lock_free_alloc (&allocators [index]);
166 MONO_GC_INTERNAL_ALLOC (p, size, type);
172 sgen_free_internal (void *addr, int type)
179 index = fixed_type_allocator_indexes [type];
180 g_assert (index >= 0 && index < NUM_ALLOCATORS);
182 mono_lock_free_free (addr);
184 if (MONO_GC_INTERNAL_DEALLOC_ENABLED ()) {
185 int size = allocator_sizes [index];
186 MONO_GC_INTERNAL_DEALLOC (addr, size, type);
191 sgen_dump_internal_mem_usage (FILE *heap_dump_file)
196 fprintf (heap_dump_file, "<other-mem-usage type=\"large-internal\" size=\"%lld\"/>\n", large_internal_bytes_alloced);
197 fprintf (heap_dump_file, "<other-mem-usage type=\"pinned-chunks\" size=\"%lld\"/>\n", pinned_chunk_bytes_alloced);
198 for (i = 0; i < INTERNAL_MEM_MAX; ++i) {
199 fprintf (heap_dump_file, "<other-mem-usage type=\"%s\" size=\"%ld\"/>\n",
200 description_for_type (i), unmanaged_allocator.small_internal_mem_bytes [i]);
206 sgen_report_internal_mem_usage (void)
208 /* FIXME: implement */
209 printf ("not implemented yet\n");
213 sgen_init_internal_allocator (void)
217 for (i = 0; i < INTERNAL_MEM_MAX; ++i)
218 fixed_type_allocator_indexes [i] = -1;
220 for (i = 0; i < NUM_ALLOCATORS; ++i) {
221 mono_lock_free_allocator_init_size_class (&size_classes [i], allocator_sizes [i]);
222 mono_lock_free_allocator_init_allocator (&allocators [i], &size_classes [i]);