Implement MachineKey.Protect and MachineKey.Unprotect
[mono.git] / mono / metadata / sgen-internal.c
1 /*
2  * sgen-internal.c: Internal lock-free memory allocator.
3  *
4  * Copyright (C) 2012 Xamarin Inc
5  *
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;
9  *
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.
14  *
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.
18  */
19
20 #include "config.h"
21
22 #ifdef HAVE_SGEN_GC
23
24 #include "utils/mono-counters.h"
25 #include "metadata/sgen-gc.h"
26 #include "utils/mono-mmap.h"
27 #include "utils/lock-free-alloc.h"
28 #include "metadata/sgen-memory-governor.h"
29
30 /* keep each size a multiple of ALLOC_ALIGN */
31 #if SIZEOF_VOID_P == 4
32 static const int allocator_sizes [] = {
33            8,   16,   24,   32,   40,   48,   64,   80,
34           96,  128,  160,  192,  224,  248,  296,  320,
35          384,  448,  504,  528,  584,  680,  816, 1088,
36         1360, 2044, 2336, 2728, 3272, 4092, 5456, 8188 };
37 #else
38 static const int allocator_sizes [] = {
39            8,   16,   24,   32,   40,   48,   64,   80,
40           96,  128,  160,  192,  224,  248,  320,  328,
41          384,  448,  528,  584,  680,  816, 1016, 1088,
42         1360, 2040, 2336, 2728, 3272, 4088, 5456, 8184 };
43 #endif
44
45 #define NUM_ALLOCATORS  (sizeof (allocator_sizes) / sizeof (int))
46
47 static int allocator_block_sizes [NUM_ALLOCATORS];
48
49 static MonoLockFreeAllocSizeClass size_classes [NUM_ALLOCATORS];
50 static MonoLockFreeAllocator allocators [NUM_ALLOCATORS];
51
52 #ifdef HEAVY_STATISTICS
53 static int allocator_sizes_stats [NUM_ALLOCATORS];
54 #endif
55
56 static size_t
57 block_size (size_t slot_size)
58 {
59         static int pagesize = -1;
60
61         int size;
62
63         if (pagesize == -1)
64                 pagesize = mono_pagesize ();
65
66         for (size = pagesize; size < LOCK_FREE_ALLOC_SB_MAX_SIZE; size <<= 1) {
67                 if (slot_size * 2 <= LOCK_FREE_ALLOC_SB_USABLE_SIZE (size))
68                         return size;
69         }
70         return LOCK_FREE_ALLOC_SB_MAX_SIZE;
71 }
72
73 /*
74  * Find the allocator index for memory chunks that can contain @size
75  * objects.
76  */
77 static int
78 index_for_size (size_t size)
79 {
80         int slot;
81         /* do a binary search or lookup table later. */
82         for (slot = 0; slot < NUM_ALLOCATORS; ++slot) {
83                 if (allocator_sizes [slot] >= size)
84                         return slot;
85         }
86         g_assert_not_reached ();
87         return -1;
88 }
89
90 /*
91  * Allocator indexes for the fixed INTERNAL_MEM_XXX types.  -1 if that
92  * type is dynamic.
93  */
94 static int fixed_type_allocator_indexes [INTERNAL_MEM_MAX];
95
96 void
97 sgen_register_fixed_internal_mem_type (int type, size_t size)
98 {
99         int slot;
100
101         g_assert (type >= 0 && type < INTERNAL_MEM_MAX);
102         g_assert (size <= allocator_sizes [NUM_ALLOCATORS - 1]);
103
104         slot = index_for_size (size);
105         g_assert (slot >= 0);
106
107         if (fixed_type_allocator_indexes [type] == -1)
108                 fixed_type_allocator_indexes [type] = slot;
109         else
110                 g_assert (fixed_type_allocator_indexes [type] == slot);
111 }
112
113 static const char*
114 description_for_type (int type)
115 {
116         switch (type) {
117         case INTERNAL_MEM_PIN_QUEUE: return "pin-queue";
118         case INTERNAL_MEM_FRAGMENT: return "fragment";
119         case INTERNAL_MEM_SECTION: return "section";
120         case INTERNAL_MEM_SCAN_STARTS: return "scan-starts";
121         case INTERNAL_MEM_FIN_TABLE: return "fin-table";
122         case INTERNAL_MEM_FINALIZE_ENTRY: return "finalize-entry";
123         case INTERNAL_MEM_FINALIZE_READY_ENTRY: return "finalize-ready-entry";
124         case INTERNAL_MEM_DISLINK_TABLE: return "dislink-table";
125         case INTERNAL_MEM_DISLINK: return "dislink";
126         case INTERNAL_MEM_ROOTS_TABLE: return "roots-table";
127         case INTERNAL_MEM_ROOT_RECORD: return "root-record";
128         case INTERNAL_MEM_STATISTICS: return "statistics";
129         case INTERNAL_MEM_STAT_PINNED_CLASS: return "pinned-class";
130         case INTERNAL_MEM_STAT_REMSET_CLASS: return "remset-class";
131         case INTERNAL_MEM_GRAY_QUEUE: return "gray-queue";
132         case INTERNAL_MEM_MS_TABLES: return "marksweep-tables";
133         case INTERNAL_MEM_MS_BLOCK_INFO: return "marksweep-block-info";
134         case INTERNAL_MEM_MS_BLOCK_INFO_SORT: return "marksweep-block-info-sort";
135         case INTERNAL_MEM_EPHEMERON_LINK: return "ephemeron-link";
136         case INTERNAL_MEM_WORKER_DATA: return "worker-data";
137         case INTERNAL_MEM_WORKER_JOB_DATA: return "worker-job-data";
138         case INTERNAL_MEM_BRIDGE_DATA: return "bridge-data";
139         case INTERNAL_MEM_OLD_BRIDGE_HASH_TABLE: return "old-bridge-hash-table";
140         case INTERNAL_MEM_OLD_BRIDGE_HASH_TABLE_ENTRY: return "old-bridge-hash-table-entry";
141         case INTERNAL_MEM_BRIDGE_HASH_TABLE: return "bridge-hash-table";
142         case INTERNAL_MEM_BRIDGE_HASH_TABLE_ENTRY: return "bridge-hash-table-entry";
143         case INTERNAL_MEM_TARJAN_BRIDGE_HASH_TABLE: return "tarjan-bridge-hash-table";
144         case INTERNAL_MEM_TARJAN_BRIDGE_HASH_TABLE_ENTRY: return "tarjan-bridge-hash-table-entry";
145         case INTERNAL_MEM_TARJAN_OBJ_BUCKET: return "tarjan-bridge-object-buckets";
146         case INTERNAL_MEM_BRIDGE_ALIVE_HASH_TABLE: return "bridge-alive-hash-table";
147         case INTERNAL_MEM_BRIDGE_ALIVE_HASH_TABLE_ENTRY: return "bridge-alive-hash-table-entry";
148         case INTERNAL_MEM_BRIDGE_DEBUG: return "bridge-debug";
149         case INTERNAL_MEM_JOB_QUEUE_ENTRY: return "job-queue-entry";
150         case INTERNAL_MEM_TOGGLEREF_DATA: return "toggleref-data";
151         case INTERNAL_MEM_CARDTABLE_MOD_UNION: return "cardtable-mod-union";
152         case INTERNAL_MEM_BINARY_PROTOCOL: return "binary-protocol";
153         default:
154                 g_assert_not_reached ();
155         }
156 }
157
158 void*
159 sgen_alloc_internal_dynamic (size_t size, int type, gboolean assert_on_failure)
160 {
161         int index;
162         void *p;
163
164         if (size > allocator_sizes [NUM_ALLOCATORS - 1]) {
165                 p = sgen_alloc_os_memory (size, SGEN_ALLOC_INTERNAL | SGEN_ALLOC_ACTIVATE, NULL);
166                 if (!p)
167                         sgen_assert_memory_alloc (NULL, size, description_for_type (type));
168         } else {
169                 index = index_for_size (size);
170
171 #ifdef HEAVY_STATISTICS
172                 ++ allocator_sizes_stats [index];
173 #endif
174
175                 p = mono_lock_free_alloc (&allocators [index]);
176                 if (!p)
177                         sgen_assert_memory_alloc (NULL, size, description_for_type (type));
178                 memset (p, 0, size);
179         }
180
181         MONO_GC_INTERNAL_ALLOC ((mword)p, size, type);
182         return p;
183 }
184
185 void
186 sgen_free_internal_dynamic (void *addr, size_t size, int type)
187 {
188         if (!addr)
189                 return;
190
191         if (size > allocator_sizes [NUM_ALLOCATORS - 1])
192                 sgen_free_os_memory (addr, size, SGEN_ALLOC_INTERNAL);
193         else
194                 mono_lock_free_free (addr, block_size (size));
195
196         MONO_GC_INTERNAL_DEALLOC ((mword)addr, size, type);
197 }
198
199 void*
200 sgen_alloc_internal (int type)
201 {
202         int index, size;
203         void *p;
204
205         index = fixed_type_allocator_indexes [type];
206         g_assert (index >= 0 && index < NUM_ALLOCATORS);
207
208 #ifdef HEAVY_STATISTICS
209         ++ allocator_sizes_stats [index];
210 #endif
211
212         size = allocator_sizes [index];
213
214         p = mono_lock_free_alloc (&allocators [index]);
215         memset (p, 0, size);
216
217         MONO_GC_INTERNAL_ALLOC ((mword)p, size, type);
218
219         return p;
220 }
221
222 void
223 sgen_free_internal (void *addr, int type)
224 {
225         int index;
226
227         if (!addr)
228                 return;
229
230         index = fixed_type_allocator_indexes [type];
231         g_assert (index >= 0 && index < NUM_ALLOCATORS);
232
233         mono_lock_free_free (addr, allocator_block_sizes [index]);
234
235         if (MONO_GC_INTERNAL_DEALLOC_ENABLED ()) {
236                 int size G_GNUC_UNUSED = allocator_sizes [index];
237                 MONO_GC_INTERNAL_DEALLOC ((mword)addr, size, type);
238         }
239 }
240
241 void
242 sgen_dump_internal_mem_usage (FILE *heap_dump_file)
243 {
244         /*
245         int i;
246
247         fprintf (heap_dump_file, "<other-mem-usage type=\"large-internal\" size=\"%lld\"/>\n", large_internal_bytes_alloced);
248         fprintf (heap_dump_file, "<other-mem-usage type=\"pinned-chunks\" size=\"%lld\"/>\n", pinned_chunk_bytes_alloced);
249         for (i = 0; i < INTERNAL_MEM_MAX; ++i) {
250                 fprintf (heap_dump_file, "<other-mem-usage type=\"%s\" size=\"%ld\"/>\n",
251                                 description_for_type (i), unmanaged_allocator.small_internal_mem_bytes [i]);
252         }
253         */
254 }
255
256 void
257 sgen_report_internal_mem_usage (void)
258 {
259         int i G_GNUC_UNUSED;
260 #ifdef HEAVY_STATISTICS
261         printf ("size -> # allocations\n");
262         for (i = 0; i < NUM_ALLOCATORS; ++i)
263                 printf ("%d -> %d\n", allocator_sizes [i], allocator_sizes_stats [i]);
264 #endif
265 }
266
267 void
268 sgen_init_internal_allocator (void)
269 {
270         int i, size;
271
272         for (i = 0; i < INTERNAL_MEM_MAX; ++i)
273                 fixed_type_allocator_indexes [i] = -1;
274
275         for (i = 0; i < NUM_ALLOCATORS; ++i) {
276                 allocator_block_sizes [i] = block_size (allocator_sizes [i]);
277                 mono_lock_free_allocator_init_size_class (&size_classes [i], allocator_sizes [i], allocator_block_sizes [i]);
278                 mono_lock_free_allocator_init_allocator (&allocators [i], &size_classes [i]);
279         }
280
281         for (size = mono_pagesize (); size <= LOCK_FREE_ALLOC_SB_MAX_SIZE; size <<= 1) {
282                 int max_size = LOCK_FREE_ALLOC_SB_USABLE_SIZE (size) / 2;
283                 /*
284                  * we assert that allocator_sizes contains the biggest possible object size
285                  * per block (4K => 4080 / 2 = 2040, 8k => 8176 / 2 = 4088, 16k => 16368 / 2 = 8184 on 64bits),
286                  * so that we do not get different block sizes for sizes that should go to the same one
287                  */
288                 g_assert (allocator_sizes [index_for_size (max_size)] == max_size);
289         }
290 }
291
292 #endif