Merge pull request #92 from konrad-kruczynski/master
[mono.git] / mono / metadata / sgen-internal.c
1 /*
2  * sgen-internal.c
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  * 
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  * 
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 #ifdef HAVE_SGEN_GC
24
25 #include "utils/mono-counters.h"
26 #include "metadata/sgen-gc.h"
27 #include "utils/lock-free-alloc.h"
28
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 };
35
36 #define NUM_ALLOCATORS  (sizeof (allocator_sizes) / sizeof (int))
37
38 static MonoLockFreeAllocSizeClass size_classes [NUM_ALLOCATORS];
39 static MonoLockFreeAllocator allocators [NUM_ALLOCATORS];
40
41 /*
42  * Find the allocator index for memory chunks that can contain @size
43  * objects.
44  */
45 static int
46 index_for_size (size_t size)
47 {
48         int slot;
49         /* do a binary search or lookup table later. */
50         for (slot = 0; slot < NUM_ALLOCATORS; ++slot) {
51                 if (allocator_sizes [slot] >= size)
52                         return slot;
53         }
54         g_assert_not_reached ();
55         return -1;
56 }
57
58 /*
59  * Allocator indexes for the fixed INTERNAL_MEM_XXX types.  -1 if that
60  * type is dynamic.
61  */
62 static int fixed_type_allocator_indexes [INTERNAL_MEM_MAX];
63
64 void
65 mono_sgen_register_fixed_internal_mem_type (int type, size_t size)
66 {
67         int slot;
68
69         g_assert (type >= 0 && type < INTERNAL_MEM_MAX);
70         g_assert (fixed_type_allocator_indexes [type] == -1);
71
72         slot = index_for_size (size);
73         g_assert (slot >= 0);
74
75         fixed_type_allocator_indexes [type] = slot;
76 }
77
78 void*
79 mono_sgen_alloc_internal_dynamic (size_t size, int type)
80 {
81         int index;
82         void *p;
83
84         if (size > allocator_sizes [NUM_ALLOCATORS - 1])
85                 return mono_sgen_alloc_os_memory (size, TRUE);
86
87         index = index_for_size (size);
88
89         p = mono_lock_free_alloc (&allocators [index]);
90         memset (p, 0, size);
91         return p;
92 }
93
94 void
95 mono_sgen_free_internal_dynamic (void *addr, size_t size, int type)
96 {
97         int index;
98
99         if (!addr)
100                 return;
101
102         if (size > allocator_sizes [NUM_ALLOCATORS - 1])
103                 return mono_sgen_free_os_memory (addr, size);
104
105         index = index_for_size (size);
106
107         mono_lock_free_free (addr);
108 }
109
110 void*
111 mono_sgen_alloc_internal (int type)
112 {
113         int index = fixed_type_allocator_indexes [type];
114         void *p;
115         g_assert (index >= 0 && index < NUM_ALLOCATORS);
116         p = mono_lock_free_alloc (&allocators [index]);
117         memset (p, 0, allocator_sizes [index]);
118         return p;
119 }
120
121 void
122 mono_sgen_free_internal (void *addr, int type)
123 {
124         int index;
125
126         if (!addr)
127                 return;
128
129         index = fixed_type_allocator_indexes [type];
130         g_assert (index >= 0 && index < NUM_ALLOCATORS);
131
132         mono_lock_free_free (addr);
133 }
134
135 void
136 mono_sgen_dump_internal_mem_usage (FILE *heap_dump_file)
137 {
138         /*
139         static char const *internal_mem_names [] = { "pin-queue", "fragment", "section", "scan-starts",
140                                                      "fin-table", "finalize-entry", "dislink-table",
141                                                      "dislink", "roots-table", "root-record", "statistics",
142                                                      "remset", "gray-queue", "store-remset", "marksweep-tables",
143                                                      "marksweep-block-info", "ephemeron-link", "worker-data",
144                                                      "bridge-data", "job-queue-entry" };
145
146         int i;
147
148         fprintf (heap_dump_file, "<other-mem-usage type=\"large-internal\" size=\"%lld\"/>\n", large_internal_bytes_alloced);
149         fprintf (heap_dump_file, "<other-mem-usage type=\"pinned-chunks\" size=\"%lld\"/>\n", pinned_chunk_bytes_alloced);
150         for (i = 0; i < INTERNAL_MEM_MAX; ++i) {
151                 fprintf (heap_dump_file, "<other-mem-usage type=\"%s\" size=\"%ld\"/>\n",
152                                 internal_mem_names [i], unmanaged_allocator.small_internal_mem_bytes [i]);
153         }
154         */
155 }
156
157 void
158 mono_sgen_report_internal_mem_usage (void)
159 {
160         /* FIXME: implement */
161         printf ("not implemented yet\n");
162 }
163
164 void
165 mono_sgen_init_internal_allocator (void)
166 {
167         int i;
168
169         for (i = 0; i < INTERNAL_MEM_MAX; ++i)
170                 fixed_type_allocator_indexes [i] = -1;
171
172         for (i = 0; i < NUM_ALLOCATORS; ++i) {
173                 mono_lock_free_allocator_init_size_class (&size_classes [i], allocator_sizes [i]);
174                 mono_lock_free_allocator_init_allocator (&allocators [i], &size_classes [i]);
175         }
176
177 #ifdef HEAVY_STATISTICS
178         mono_counters_register ("Internal allocs", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_internal_alloc);
179 #endif
180 }
181
182 #endif