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 if (G_UNLIKELY (!queue->first || queue->first->end == SGEN_GRAY_QUEUE_SECTION_SIZE))
73 sgen_gray_object_alloc_queue_section (queue);
74 DEBUG (9, g_assert (queue->first && queue->first->end < SGEN_GRAY_QUEUE_SECTION_SIZE));
75 queue->first->objects [queue->first->end++] = obj;
77 DEBUG (9, ++queue->balance);
81 sgen_gray_object_dequeue (SgenGrayQueue *queue)
85 if (sgen_gray_object_queue_is_empty (queue))
88 DEBUG (9, g_assert (queue->first->end));
90 obj = queue->first->objects [--queue->first->end];
92 if (G_UNLIKELY (queue->first->end == 0)) {
93 GrayQueueSection *section = queue->first;
94 queue->first = section->next;
95 section->next = queue->free_list;
96 queue->free_list = section;
99 DEBUG (9, --queue->balance);
105 sgen_gray_object_dequeue_section (SgenGrayQueue *queue)
107 GrayQueueSection *section;
112 section = queue->first;
113 queue->first = section->next;
115 section->next = NULL;
121 sgen_gray_object_enqueue_section (SgenGrayQueue *queue, GrayQueueSection *section)
123 section->next = queue->first;
124 queue->first = section;
128 sgen_gray_object_queue_init (SgenGrayQueue *queue)
130 GrayQueueSection *section, *next;
133 g_assert (sgen_gray_object_queue_is_empty (queue));
134 DEBUG (9, g_assert (queue->balance == 0));
136 /* Free the extra sections allocated during the last collection */
138 for (section = queue->free_list; section && i < GRAY_QUEUE_LENGTH_LIMIT - 1; section = section->next)
142 while (section->next) {
143 next = section->next;
144 section->next = next->next;
145 sgen_gray_object_free_queue_section (next);
150 sgen_gray_object_queue_init_with_alloc_prepare (SgenGrayQueue *queue, GrayQueueAllocPrepareFunc func, void *data)
152 sgen_gray_object_queue_init (queue);
153 queue->alloc_prepare_func = func;
154 queue->alloc_prepare_data = data;