New tests.
[mono.git] / mono / metadata / sgen-protocol.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 #ifdef BINARY_PROTOCOL
25
26 #include "sgen-protocol.h"
27
28 /* If not null, dump binary protocol to this file */
29 static FILE *binary_protocol_file = NULL;
30
31 #define BINARY_PROTOCOL_BUFFER_SIZE     65536
32
33 static unsigned char binary_protocol_buffer [BINARY_PROTOCOL_BUFFER_SIZE];
34 static int binary_protocol_buffer_index = 0;
35
36 static void
37 flush_binary_protocol_buffer (void)
38 {
39         if (!binary_protocol_file)
40                 return;
41         if (binary_protocol_buffer_index == 0)
42                 return;
43
44         fwrite (binary_protocol_buffer, 1, binary_protocol_buffer_index, binary_protocol_file);
45         fflush (binary_protocol_file);
46
47         binary_protocol_buffer_index = 0;
48 }
49
50 static void
51 protocol_entry (unsigned char type, gpointer data, int size)
52 {
53         if (!binary_protocol_file)
54                 return;
55         if (binary_protocol_buffer_index + 1 + size > BINARY_PROTOCOL_BUFFER_SIZE)
56                 flush_binary_protocol_buffer ();
57
58         binary_protocol_buffer [binary_protocol_buffer_index++] = type;
59         memcpy (binary_protocol_buffer + binary_protocol_buffer_index, data, size);
60         binary_protocol_buffer_index += size;
61
62         g_assert (binary_protocol_buffer_index <= BINARY_PROTOCOL_BUFFER_SIZE);
63 }
64
65 static void
66 binary_protocol_collection (int generation)
67 {
68         SGenProtocolCollection entry = { generation };
69         flush_binary_protocol_buffer ();
70         protocol_entry (SGEN_PROTOCOL_COLLECTION, &entry, sizeof (SGenProtocolCollection));
71 }
72
73 static void
74 binary_protocol_alloc (gpointer obj, gpointer vtable, int size)
75 {
76         SGenProtocolAlloc entry = { obj, vtable, size };
77         protocol_entry (SGEN_PROTOCOL_ALLOC, &entry, sizeof (SGenProtocolAlloc));
78 }
79
80 static void
81 binary_protocol_copy (gpointer from, gpointer to, gpointer vtable, int size)
82 {
83         SGenProtocolCopy entry = { from, to, vtable, size };
84         protocol_entry (SGEN_PROTOCOL_COPY, &entry, sizeof (SGenProtocolCopy));
85 }
86
87 static void
88 binary_protocol_pin (gpointer obj, gpointer vtable, int size)
89 {
90         SGenProtocolPin entry = { obj, vtable, size };
91         protocol_entry (SGEN_PROTOCOL_PIN, &entry, sizeof (SGenProtocolPin));
92 }
93
94 static void
95 binary_protocol_mark (gpointer obj, gpointer vtable, int size)
96 {
97         SGenProtocolMark entry = { obj, vtable, size };
98         protocol_entry (SGEN_PROTOCOL_MARK, &entry, sizeof (SGenProtocolMark));
99 }
100
101 static void
102 binary_protocol_wbarrier (gpointer ptr, gpointer value, gpointer value_vtable)
103 {
104         SGenProtocolWBarrier entry = { ptr, value, value_vtable };
105         protocol_entry (SGEN_PROTOCOL_WBARRIER, &entry, sizeof (SGenProtocolWBarrier));
106 }
107
108 static void
109 binary_protocol_global_remset (gpointer ptr, gpointer value, gpointer value_vtable)
110 {
111         SGenProtocolGlobalRemset entry = { ptr, value, value_vtable };
112         protocol_entry (SGEN_PROTOCOL_GLOBAL_REMSET, &entry, sizeof (SGenProtocolGlobalRemset));
113 }
114
115 static void
116 binary_protocol_ptr_update (gpointer ptr, gpointer old_value, gpointer new_value, gpointer vtable, int size)
117 {
118         SGenProtocolPtrUpdate entry = { ptr, old_value, new_value, vtable, size };
119         protocol_entry (SGEN_PROTOCOL_PTR_UPDATE, &entry, sizeof (SGenProtocolPtrUpdate));
120 }
121
122 static void
123 binary_protocol_cleanup (gpointer ptr, gpointer vtable, int size)
124 {
125         SGenProtocolCleanup entry = { ptr, vtable, size };
126         protocol_entry (SGEN_PROTOCOL_CLEANUP, &entry, sizeof (SGenProtocolCleanup));
127 }
128
129 static void
130 binary_protocol_empty (gpointer start, int size)
131 {
132         SGenProtocolEmpty entry = { start, size };
133         protocol_entry (SGEN_PROTOCOL_EMPTY, &entry, sizeof (SGenProtocolEmpty));
134 }
135
136 static void
137 binary_protocol_thread_restart (gpointer thread)
138 {
139         SGenProtocolThreadRestart entry = { thread };
140         protocol_entry (SGEN_PROTOCOL_THREAD_RESTART, &entry, sizeof (SGenProtocolThreadRestart));
141
142 }
143
144 static void
145 binary_protocol_thread_register (gpointer thread)
146 {
147         SGenProtocolThreadRegister entry = { thread };
148         protocol_entry (SGEN_PROTOCOL_THREAD_REGISTER, &entry, sizeof (SGenProtocolThreadRegister));
149
150 }
151
152 static void
153 binary_protocol_thread_unregister (gpointer thread)
154 {
155         SGenProtocolThreadUnregister entry = { thread };
156         protocol_entry (SGEN_PROTOCOL_THREAD_UNREGISTER, &entry, sizeof (SGenProtocolThreadUnregister));
157
158 }
159
160 static void
161 binary_protocol_missing_remset (gpointer obj, gpointer obj_vtable, int offset, gpointer value, gpointer value_vtable, int value_pinned)
162 {
163         SGenProtocolMissingRemset entry = { obj, obj_vtable, offset, value, value_vtable, value_pinned };
164         protocol_entry (SGEN_PROTOCOL_MISSING_REMSET, &entry, sizeof (SGenProtocolMissingRemset));
165
166 }
167
168 #else
169
170 #define binary_protocol_collection(generation)
171 #define binary_protocol_alloc(obj, vtable, size)
172 #define binary_protocol_copy(from, to, vtable, size)
173 #define binary_protocol_pin(obj, vtable, size)
174 #define binary_protocol_mark(obj, vtable, size)
175 #define binary_protocol_wbarrier(ptr, value, value_vtable)
176 #define binary_protocol_global_remset(ptr, value, value_vtable)
177 #define binary_protocol_ptr_update(ptr, old_value, new_value, vtable, size)
178 #define binary_protocol_cleanup(ptr, vtable, size)
179 #define binary_protocol_empty(start, size)
180 #define binary_protocol_thread_restart(thread)
181 #define binary_protocol_thread_register(thread)
182 #define binary_protocol_thread_unregister(thread)
183 #define binary_protocol_missing_remset(obj, obj_vtable, offset, value, value_vtable, value_pinned)
184
185 #endif