Merged into single file, added assertions
[mono.git] / mono / metadata / sgen-gray.c
1 /*
2  * Copyright 2001-2003 Ximian, Inc
3  * Copyright 2003-2010 Novell, Inc.
4  * 
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  * 
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  * 
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 #include "config.h"
25 #ifdef HAVE_SGEN_GC
26
27 #include "metadata/sgen-gc.h"
28 #include "utils/mono-counters.h"
29
30 #define GRAY_QUEUE_LENGTH_LIMIT 64
31
32 void
33 sgen_gray_object_alloc_queue_section (SgenGrayQueue *queue)
34 {
35         GrayQueueSection *section;
36
37         if (queue->alloc_prepare_func)
38                 queue->alloc_prepare_func (queue);
39
40         if (queue->free_list) {
41                 /* Use the previously allocated queue sections if possible */
42                 section = queue->free_list;
43                 queue->free_list = section->next;
44         } else {
45                 /* Allocate a new section */
46                 section = sgen_alloc_internal (INTERNAL_MEM_GRAY_QUEUE);
47         }
48
49         section->end = 0;
50
51         /* Link it with the others */
52         section->next = queue->first;
53         queue->first = section;
54 }
55
56 void
57 sgen_gray_object_free_queue_section (GrayQueueSection *section)
58 {
59         sgen_free_internal (section, INTERNAL_MEM_GRAY_QUEUE);
60 }
61
62 /*
63  * The following two functions are called in the inner loops of the
64  * collector, so they need to be as fast as possible.  We have macros
65  * for them in sgen-gc.h.
66  */
67
68 void
69 sgen_gray_object_enqueue (SgenGrayQueue *queue, char *obj)
70 {
71         DEBUG (9, g_assert (obj));
72         //sgen_check_objref (obj);
73         if (G_UNLIKELY (!queue->first || queue->first->end == SGEN_GRAY_QUEUE_SECTION_SIZE))
74                 sgen_gray_object_alloc_queue_section (queue);
75         DEBUG (9, g_assert (queue->first && queue->first->end < SGEN_GRAY_QUEUE_SECTION_SIZE));
76         queue->first->objects [queue->first->end++] = obj;
77
78         DEBUG (9, ++queue->balance);
79 }
80
81 char*
82 sgen_gray_object_dequeue (SgenGrayQueue *queue)
83 {
84         char *obj;
85
86         if (sgen_gray_object_queue_is_empty (queue))
87                 return NULL;
88
89         DEBUG (9, g_assert (queue->first->end));
90
91         obj = queue->first->objects [--queue->first->end];
92
93         if (G_UNLIKELY (queue->first->end == 0)) {
94                 GrayQueueSection *section = queue->first;
95                 queue->first = section->next;
96                 section->next = queue->free_list;
97                 queue->free_list = section;
98         }
99
100         DEBUG (9, --queue->balance);
101
102         return obj;
103 }
104
105 GrayQueueSection*
106 sgen_gray_object_dequeue_section (SgenGrayQueue *queue)
107 {
108         GrayQueueSection *section;
109
110         if (!queue->first)
111                 return NULL;
112
113         section = queue->first;
114         queue->first = section->next;
115
116         section->next = NULL;
117
118         return section;
119 }
120
121 void
122 sgen_gray_object_enqueue_section (SgenGrayQueue *queue, GrayQueueSection *section)
123 {
124         section->next = queue->first;
125         queue->first = section;
126 }
127
128 void
129 sgen_gray_object_queue_init (SgenGrayQueue *queue)
130 {
131         GrayQueueSection *section, *next;
132         int i;
133
134         g_assert (sgen_gray_object_queue_is_empty (queue));
135         DEBUG (9, g_assert (queue->balance == 0));
136
137         /* Free the extra sections allocated during the last collection */
138         i = 0;
139         for (section = queue->free_list; section && i < GRAY_QUEUE_LENGTH_LIMIT - 1; section = section->next)
140                 i ++;
141         if (!section)
142                 return;
143         while (section->next) {
144                 next = section->next;
145                 section->next = next->next;
146                 sgen_gray_object_free_queue_section (next);
147         }
148 }
149
150 void
151 sgen_gray_object_queue_init_with_alloc_prepare (SgenGrayQueue *queue, GrayQueueAllocPrepareFunc func, void *data)
152 {
153         sgen_gray_object_queue_init (queue);
154         queue->alloc_prepare_func = func;
155         queue->alloc_prepare_data = data;
156 }
157
158 #endif