2 * Copyright 2001-2003 Ximian, Inc
3 * Copyright 2003-2010 Novell, Inc.
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:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
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.
27 #include "metadata/sgen-gc.h"
28 #include "utils/mono-counters.h"
30 #define GRAY_QUEUE_LENGTH_LIMIT 64
33 sgen_gray_object_alloc_queue_section (SgenGrayQueue *queue)
35 GrayQueueSection *section;
37 if (queue->alloc_prepare_func)
38 queue->alloc_prepare_func (queue);
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;
45 /* Allocate a new section */
46 section = sgen_alloc_internal (INTERNAL_MEM_GRAY_QUEUE);
51 /* Link it with the others */
52 section->next = queue->first;
53 queue->first = section;
57 sgen_gray_object_free_queue_section (GrayQueueSection *section)
59 sgen_free_internal (section, INTERNAL_MEM_GRAY_QUEUE);
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.
69 sgen_gray_object_enqueue (SgenGrayQueue *queue, char *obj)
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;
78 DEBUG (9, ++queue->balance);
82 sgen_gray_object_dequeue (SgenGrayQueue *queue)
86 if (sgen_gray_object_queue_is_empty (queue))
89 DEBUG (9, g_assert (queue->first->end));
91 obj = queue->first->objects [--queue->first->end];
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;
100 DEBUG (9, --queue->balance);
106 sgen_gray_object_dequeue_section (SgenGrayQueue *queue)
108 GrayQueueSection *section;
113 section = queue->first;
114 queue->first = section->next;
116 section->next = NULL;
122 sgen_gray_object_enqueue_section (SgenGrayQueue *queue, GrayQueueSection *section)
124 section->next = queue->first;
125 queue->first = section;
129 sgen_gray_object_queue_init (SgenGrayQueue *queue)
131 GrayQueueSection *section, *next;
134 g_assert (sgen_gray_object_queue_is_empty (queue));
135 DEBUG (9, g_assert (queue->balance == 0));
137 /* Free the extra sections allocated during the last collection */
139 for (section = queue->free_list; section && i < GRAY_QUEUE_LENGTH_LIMIT - 1; section = section->next)
143 while (section->next) {
144 next = section->next;
145 section->next = next->next;
146 sgen_gray_object_free_queue_section (next);
151 sgen_gray_object_queue_init_with_alloc_prepare (SgenGrayQueue *queue, GrayQueueAllocPrepareFunc func, void *data)
153 sgen_gray_object_queue_init (queue);
154 queue->alloc_prepare_func = func;
155 queue->alloc_prepare_data = data;