Merge branch 'master' of github.com:tgiphil/mono
[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** pin_queue;
30 static int pin_queue_size = 0;
31 static int next_pin_slot = 0;
32
33 static void
34 init_pinning (void)
35 {
36         pin_staging_area_index = 0;
37 }
38
39 static void
40 realloc_pin_queue (void)
41 {
42         int new_size = pin_queue_size? pin_queue_size + pin_queue_size/2: 1024;
43         void **new_pin = get_internal_mem (sizeof (void*) * new_size, INTERNAL_MEM_PIN_QUEUE);
44         memcpy (new_pin, pin_queue, sizeof (void*) * next_pin_slot);
45         free_internal_mem (pin_queue, INTERNAL_MEM_PIN_QUEUE);
46         pin_queue = new_pin;
47         pin_queue_size = new_size;
48         DEBUG (4, fprintf (gc_debug_file, "Reallocated pin queue to size: %d\n", new_size));
49 }
50
51 static void
52 evacuate_pin_staging_area (void)
53 {
54         int i;
55
56         g_assert (pin_staging_area_index >= 0 && pin_staging_area_index <= PIN_STAGING_AREA_SIZE);
57
58         if (pin_staging_area_index == 0)
59                 return;
60
61         /*
62          * The pinning addresses might come from undefined memory, this is normal. Since they
63          * are used in lots of functions, we make the memory defined here instead of having
64          * to add a supression for those functions.
65          */
66         VALGRIND_MAKE_MEM_DEFINED (pin_staging_area, pin_staging_area_index * sizeof (void*));
67
68         sort_addresses (pin_staging_area, pin_staging_area_index);
69
70         while (next_pin_slot + pin_staging_area_index > pin_queue_size)
71                 realloc_pin_queue ();
72
73         pin_queue [next_pin_slot++] = pin_staging_area [0];
74         for (i = 1; i < pin_staging_area_index; ++i) {
75                 void *p = pin_staging_area [i];
76                 if (p != pin_queue [next_pin_slot - 1])
77                         pin_queue [next_pin_slot++] = p;
78         }
79
80         g_assert (next_pin_slot <= pin_queue_size);
81
82         pin_staging_area_index = 0;
83 }
84
85 static void
86 pin_stage_ptr (void *ptr)
87 {
88         if (pin_staging_area_index >= PIN_STAGING_AREA_SIZE)
89                 evacuate_pin_staging_area ();
90
91         pin_staging_area [pin_staging_area_index++] = ptr;
92 }
93
94 static int
95 optimized_pin_queue_search (void *addr)
96 {
97         int first = 0, last = next_pin_slot;
98         while (first < last) {
99                 int middle = first + ((last - first) >> 1);
100                 if (addr <= pin_queue [middle])
101                         last = middle;
102                 else
103                         first = middle + 1;
104         }
105         g_assert (first == last);
106         return first;
107 }
108
109 static void
110 find_optimized_pin_queue_area (void *start, void *end, int *first, int *last)
111 {
112         *first = optimized_pin_queue_search (start);
113         *last = optimized_pin_queue_search (end);
114 }
115
116 static void
117 find_section_pin_queue_start_end (GCMemSection *section)
118 {
119         int start, end;
120         DEBUG (6, fprintf (gc_debug_file, "Pinning from section %p (%p-%p)\n", section, section->data, section->end_data));
121         find_optimized_pin_queue_area (section->data, section->end_data, &start, &end);
122         DEBUG (6, fprintf (gc_debug_file, "Found %d pinning addresses in section %p (%d-%d)\n",
123                                         end - start, section, start, end));
124         section->pin_queue_start = start;
125         section->pin_queue_end = end;
126 }