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.
24 #define GRAY_QUEUE_SECTION_SIZE (128 - 3)
25 #define GRAY_QUEUE_LENGTH_LIMIT 64
28 * This is a stack now instead of a queue, so the most recently added items are removed
29 * first, improving cache locality, and keeping the stack size manageable.
31 typedef struct _GrayQueueSection GrayQueueSection;
32 struct _GrayQueueSection {
34 GrayQueueSection *next, *prev;
35 char *objects [GRAY_QUEUE_SECTION_SIZE];
38 static GrayQueueSection *gray_queue_start = NULL;
39 static GrayQueueSection *gray_queue_end = NULL;
41 static int gray_queue_balance = 0;
42 static int num_gray_queue_sections = 0;
45 gray_object_alloc_queue_section (void)
47 GrayQueueSection *section;
49 /* Use the previously allocated queue sections if possible */
50 if (!gray_queue_end && gray_queue_start) {
51 gray_queue_end = gray_queue_start;
52 gray_queue_end->end = 0;
55 if (gray_queue_end && gray_queue_end->next) {
56 gray_queue_end = gray_queue_end->next;
57 gray_queue_end->end = 0;
61 /* Allocate a new section */
62 section = get_internal_mem (sizeof (GrayQueueSection), INTERNAL_MEM_GRAY_QUEUE);
63 ++num_gray_queue_sections;
69 /* Link it with the others */
71 gray_queue_end->next = section;
72 section->prev = gray_queue_end;
74 g_assert (!gray_queue_start);
75 gray_queue_start = section;
77 gray_queue_end = section;
81 gray_object_free_queue_section (GrayQueueSection *section)
83 free_internal_mem (section, INTERNAL_MEM_GRAY_QUEUE);
84 --num_gray_queue_sections;
88 * The following three functions are called in the inner loops of the collector, so they
89 * need to be as fast as possible.
93 gray_object_enqueue (char *obj)
96 if (G_UNLIKELY (!gray_queue_end || gray_queue_end->end == GRAY_QUEUE_SECTION_SIZE))
97 gray_object_alloc_queue_section ();
98 g_assert (gray_queue_end && gray_queue_end->end < GRAY_QUEUE_SECTION_SIZE);
99 gray_queue_end->objects [gray_queue_end->end++] = obj;
101 ++gray_queue_balance;
104 static inline gboolean
105 gray_object_queue_is_empty (void)
107 return gray_queue_end == NULL;
111 gray_object_dequeue (void)
115 if (gray_object_queue_is_empty ())
118 g_assert (gray_queue_end->end);
120 obj = gray_queue_end->objects [--gray_queue_end->end];
122 if (G_UNLIKELY (gray_queue_end->end == 0))
123 gray_queue_end = gray_queue_end->prev;
125 --gray_queue_balance;
131 gray_object_queue_init (void)
133 GrayQueueSection *section, *next;
136 g_assert (gray_object_queue_is_empty ());
137 g_assert (sizeof (GrayQueueSection) < MAX_FREELIST_SIZE);
138 g_assert (gray_queue_balance == 0);
140 /* Free the extra sections allocated during the last collection */
142 for (section = gray_queue_start; section && i < GRAY_QUEUE_LENGTH_LIMIT; section = section->next)
146 section->prev->next = NULL;
147 for (; section; section = next) {
148 next = section->next;
149 gray_object_free_queue_section (section);