Implement Dns.GetHostEntry(string.Empty) on Mac OS.
[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         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;
76
77         DEBUG (9, ++queue->balance);
78 }
79
80 char*
81 sgen_gray_object_dequeue (SgenGrayQueue *queue)
82 {
83         char *obj;
84
85         if (sgen_gray_object_queue_is_empty (queue))
86                 return NULL;
87
88         DEBUG (9, g_assert (queue->first->end));
89
90         obj = queue->first->objects [--queue->first->end];
91
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;
97         }
98
99         DEBUG (9, --queue->balance);
100
101         return obj;
102 }
103
104 GrayQueueSection*
105 sgen_gray_object_dequeue_section (SgenGrayQueue *queue)
106 {
107         GrayQueueSection *section;
108
109         if (!queue->first)
110                 return NULL;
111
112         section = queue->first;
113         queue->first = section->next;
114
115         section->next = NULL;
116
117         return section;
118 }
119
120 void
121 sgen_gray_object_enqueue_section (SgenGrayQueue *queue, GrayQueueSection *section)
122 {
123         section->next = queue->first;
124         queue->first = section;
125 }
126
127 void
128 sgen_gray_object_queue_init (SgenGrayQueue *queue)
129 {
130         GrayQueueSection *section, *next;
131         int i;
132
133         g_assert (sgen_gray_object_queue_is_empty (queue));
134         DEBUG (9, g_assert (queue->balance == 0));
135
136         /* Free the extra sections allocated during the last collection */
137         i = 0;
138         for (section = queue->free_list; section && i < GRAY_QUEUE_LENGTH_LIMIT - 1; section = section->next)
139                 i ++;
140         if (!section)
141                 return;
142         while (section->next) {
143                 next = section->next;
144                 section->next = next->next;
145                 sgen_gray_object_free_queue_section (next);
146         }
147 }
148
149 void
150 sgen_gray_object_queue_init_with_alloc_prepare (SgenGrayQueue *queue, GrayQueueAllocPrepareFunc func, void *data)
151 {
152         sgen_gray_object_queue_init (queue);
153         queue->alloc_prepare_func = func;
154         queue->alloc_prepare_data = data;
155 }
156
157 #endif