[sgen] Debugging code to check gray queue section's states.
[mono.git] / mono / metadata / sgen-gray.h
1 /*
2  * sgen-gray.h: Gray queue management.
3  *
4  * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
5  * Copyright (C) 2012 Xamarin Inc
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License 2.0 as published by the Free Software Foundation;
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License 2.0 along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20 #ifndef __MONO_SGEN_GRAY_H__
21 #define __MONO_SGEN_GRAY_H__
22
23 #define SGEN_GRAY_QUEUE_SECTION_SIZE    (128 - 3)
24
25 #ifdef SGEN_CHECK_GRAY_OBJECT_SECTIONS
26 typedef enum {
27         GRAY_QUEUE_SECTION_STATE_FLOATING,
28         GRAY_QUEUE_SECTION_STATE_ENQUEUED,
29         GRAY_QUEUE_SECTION_STATE_FREE_LIST,
30         GRAY_QUEUE_SECTION_STATE_FREED
31 } GrayQueueSectionState;
32 #endif
33
34 /*
35  * This is a stack now instead of a queue, so the most recently added items are removed
36  * first, improving cache locality, and keeping the stack size manageable.
37  */
38 typedef struct _GrayQueueSection GrayQueueSection;
39 struct _GrayQueueSection {
40 #ifdef SGEN_CHECK_GRAY_OBJECT_SECTIONS
41         /*
42          * The dummy is here so that the state doesn't get overwritten
43          * by the internal allocator once the section is freed.
44          */
45         int dummy;
46         GrayQueueSectionState state;
47 #endif
48         int end;
49         GrayQueueSection *next;
50         char *objects [SGEN_GRAY_QUEUE_SECTION_SIZE];
51 };
52
53 typedef struct _SgenGrayQueue SgenGrayQueue;
54
55 typedef void (*GrayQueueAllocPrepareFunc) (SgenGrayQueue*);
56 typedef void (*GrayQueueEnqueueCheckFunc) (char*);
57
58 struct _SgenGrayQueue {
59         GrayQueueSection *first;
60         GrayQueueSection *free_list;
61         GrayQueueAllocPrepareFunc alloc_prepare_func;
62 #ifdef SGEN_CHECK_GRAY_OBJECT_ENQUEUE
63         GrayQueueEnqueueCheckFunc enqueue_check_func;
64 #endif
65         void *alloc_prepare_data;
66 };
67
68 typedef struct _SgenSectionGrayQueue SgenSectionGrayQueue;
69
70 struct _SgenSectionGrayQueue {
71         GrayQueueSection *first;
72         gboolean locked;
73         mono_mutex_t lock;
74 #ifdef SGEN_CHECK_GRAY_OBJECT_ENQUEUE
75         GrayQueueEnqueueCheckFunc enqueue_check_func;
76 #endif
77 };
78
79 void sgen_gray_object_enqueue (SgenGrayQueue *queue, char *obj) MONO_INTERNAL;
80 char* sgen_gray_object_dequeue (SgenGrayQueue *queue) MONO_INTERNAL;
81 GrayQueueSection* sgen_gray_object_dequeue_section (SgenGrayQueue *queue) MONO_INTERNAL;
82 void sgen_gray_object_enqueue_section (SgenGrayQueue *queue, GrayQueueSection *section) MONO_INTERNAL;
83 void sgen_gray_object_queue_init (SgenGrayQueue *queue, GrayQueueEnqueueCheckFunc enqueue_check_func) MONO_INTERNAL;
84 void sgen_gray_object_queue_init_invalid (SgenGrayQueue *queue) MONO_INTERNAL;
85 void sgen_gray_object_queue_init_with_alloc_prepare (SgenGrayQueue *queue, GrayQueueEnqueueCheckFunc enqueue_check_func,
86                 GrayQueueAllocPrepareFunc func, void *data) MONO_INTERNAL;
87 void sgen_gray_object_queue_deinit (SgenGrayQueue *queue) MONO_INTERNAL;
88 void sgen_gray_object_queue_disable_alloc_prepare (SgenGrayQueue *queue) MONO_INTERNAL;
89 void sgen_gray_object_alloc_queue_section (SgenGrayQueue *queue) MONO_INTERNAL;
90 void sgen_gray_object_free_queue_section (GrayQueueSection *section) MONO_INTERNAL;
91
92 void sgen_section_gray_queue_init (SgenSectionGrayQueue *queue, gboolean locked,
93                 GrayQueueEnqueueCheckFunc enqueue_check_func) MONO_INTERNAL;
94 gboolean sgen_section_gray_queue_is_empty (SgenSectionGrayQueue *queue) MONO_INTERNAL;
95 GrayQueueSection* sgen_section_gray_queue_dequeue (SgenSectionGrayQueue *queue) MONO_INTERNAL;
96 void sgen_section_gray_queue_enqueue (SgenSectionGrayQueue *queue, GrayQueueSection *section) MONO_INTERNAL;
97
98 static inline gboolean
99 sgen_gray_object_queue_is_empty (SgenGrayQueue *queue)
100 {
101         return queue->first == NULL;
102 }
103
104 #endif