Add missing copyright notices to Sgen
[mono.git] / mono / metadata / sgen-pinning.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 #define PIN_STAGING_AREA_SIZE   1024
25
26 static void* pin_staging_area [PIN_STAGING_AREA_SIZE];
27 static int pin_staging_area_index;
28
29 static void
30 init_pinning (void)
31 {
32         pin_staging_area_index = 0;
33 }
34
35 static void
36 evacuate_pin_staging_area (void)
37 {
38         int i;
39
40         g_assert (pin_staging_area_index >= 0 && pin_staging_area_index <= PIN_STAGING_AREA_SIZE);
41
42         if (pin_staging_area_index == 0)
43                 return;
44
45         /*
46          * The pinning addresses might come from undefined memory, this is normal. Since they
47          * are used in lots of functions, we make the memory defined here instead of having
48          * to add a supression for those functions.
49          */
50         VALGRIND_MAKE_MEM_DEFINED (pin_staging_area, pin_staging_area_index * sizeof (void*));
51
52         sort_addresses (pin_staging_area, pin_staging_area_index);
53
54         while (next_pin_slot + pin_staging_area_index > pin_queue_size)
55                 realloc_pin_queue ();
56
57         pin_queue [next_pin_slot++] = pin_staging_area [0];
58         for (i = 1; i < pin_staging_area_index; ++i) {
59                 void *p = pin_staging_area [i];
60                 if (p != pin_queue [next_pin_slot - 1])
61                         pin_queue [next_pin_slot++] = p;
62         }
63
64         g_assert (next_pin_slot <= pin_queue_size);
65
66         pin_staging_area_index = 0;
67 }
68
69 static void
70 pin_stage_ptr (void *ptr)
71 {
72         if (pin_staging_area_index >= PIN_STAGING_AREA_SIZE)
73                 evacuate_pin_staging_area ();
74
75         pin_staging_area [pin_staging_area_index++] = ptr;
76 }