Merge branch 'master' of github.com:mono/mono into atsushi
[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 #define GRAY_QUEUE_LENGTH_LIMIT 64
25
26 static GrayQueue gray_queue;
27
28 static void
29 gray_object_alloc_queue_section (GrayQueue *queue)
30 {
31         GrayQueueSection *section;
32
33         if (queue->free_list) {
34                 /* Use the previously allocated queue sections if possible */
35                 section = queue->free_list;
36                 queue->free_list = section->next;
37         } else {
38                 /* Allocate a new section */
39                 section = mono_sgen_alloc_internal (INTERNAL_MEM_GRAY_QUEUE);
40         }
41
42         section->end = 0;
43
44         /* Link it with the others */
45         section->next = queue->first;
46         queue->first = section;
47 }
48
49 static void
50 gray_object_free_queue_section (GrayQueueSection *section)
51 {
52         mono_sgen_free_internal (section, INTERNAL_MEM_GRAY_QUEUE);
53 }
54
55 static inline gboolean
56 gray_object_queue_is_empty (GrayQueue *queue)
57 {
58         return queue->first == NULL;
59 }
60
61 /*
62  * The following two functions are called in the inner loops of the
63  * collector, so they need to be as fast as possible.  We have macros
64  * for them in sgen-gc.h.
65  */
66
67 void
68 mono_sgen_gray_object_enqueue (GrayQueue *queue, char *obj)
69 {
70         DEBUG (9, g_assert (obj));
71         if (G_UNLIKELY (!queue->first || queue->first->end == SGEN_GRAY_QUEUE_SECTION_SIZE))
72                 gray_object_alloc_queue_section (queue);
73         DEBUG (9, g_assert (queue->first && queue->first->end < SGEN_GRAY_QUEUE_SECTION_SIZE));
74         queue->first->objects [queue->first->end++] = obj;
75
76         DEBUG (9, ++queue->balance);
77 }
78
79 char*
80 mono_sgen_gray_object_dequeue (GrayQueue *queue)
81 {
82         char *obj;
83
84         if (gray_object_queue_is_empty (queue))
85                 return NULL;
86
87         DEBUG (9, g_assert (queue->first->end));
88
89         obj = queue->first->objects [--queue->first->end];
90
91         if (G_UNLIKELY (queue->first->end == 0)) {
92                 GrayQueueSection *section = queue->first;
93                 queue->first = section->next;
94                 section->next = queue->free_list;
95                 queue->free_list = section;
96         }
97
98         DEBUG (9, --queue->balance);
99
100         return obj;
101 }
102
103 static void
104 gray_object_queue_init (GrayQueue *queue)
105 {
106         GrayQueueSection *section, *next;
107         int i;
108
109         g_assert (gray_object_queue_is_empty (queue));
110         DEBUG (9, g_assert (queue->balance == 0));
111
112         /* Free the extra sections allocated during the last collection */
113         i = 0;
114         for (section = queue->free_list; section && i < GRAY_QUEUE_LENGTH_LIMIT - 1; section = section->next)
115                 i ++;
116         if (!section)
117                 return;
118         while (section->next) {
119                 next = section->next;
120                 section->next = next->next;
121                 gray_object_free_queue_section (next);
122         }
123 }