2 * Copyright 2001-2003 Ximian, Inc
3 * Copyright 2003-2010 Novell, Inc.
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:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
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.
24 #define PIN_STAGING_AREA_SIZE 1024
26 static void* pin_staging_area
[PIN_STAGING_AREA_SIZE
];
27 static int pin_staging_area_index
;
29 static void** pin_queue
;
30 static int pin_queue_size
= 0;
31 static int next_pin_slot
= 0;
36 pin_staging_area_index
= 0;
40 realloc_pin_queue (void)
42 int new_size
= pin_queue_size
? pin_queue_size
+ pin_queue_size
/2: 1024;
43 void **new_pin
= mono_sgen_alloc_internal_dynamic (sizeof (void*) * new_size
, INTERNAL_MEM_PIN_QUEUE
);
44 memcpy (new_pin
, pin_queue
, sizeof (void*) * next_pin_slot
);
45 mono_sgen_free_internal_dynamic (pin_queue
, sizeof (void*) * pin_queue_size
, INTERNAL_MEM_PIN_QUEUE
);
47 pin_queue_size
= new_size
;
48 DEBUG (4, fprintf (gc_debug_file
, "Reallocated pin queue to size: %d\n", new_size
));
52 evacuate_pin_staging_area (void)
56 g_assert (pin_staging_area_index
>= 0 && pin_staging_area_index
<= PIN_STAGING_AREA_SIZE
);
58 if (pin_staging_area_index
== 0)
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.
66 VALGRIND_MAKE_MEM_DEFINED (pin_staging_area
, pin_staging_area_index
* sizeof (void*));
68 sort_addresses (pin_staging_area
, pin_staging_area_index
);
70 while (next_pin_slot
+ pin_staging_area_index
> pin_queue_size
)
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
;
80 g_assert (next_pin_slot
<= pin_queue_size
);
82 pin_staging_area_index
= 0;
86 pin_stage_ptr (void *ptr
)
88 if (pin_staging_area_index
>= PIN_STAGING_AREA_SIZE
)
89 evacuate_pin_staging_area ();
91 pin_staging_area
[pin_staging_area_index
++] = ptr
;
95 optimized_pin_queue_search (void *addr
)
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
])
105 g_assert (first
== last
);
110 mono_sgen_find_optimized_pin_queue_area (void *start
, void *end
, int *num
)
113 first
= optimized_pin_queue_search (start
);
114 last
= optimized_pin_queue_search (end
);
118 return pin_queue
+ first
;
122 mono_sgen_find_section_pin_queue_start_end (GCMemSection
*section
)
124 DEBUG (6, fprintf (gc_debug_file
, "Pinning from section %p (%p-%p)\n", section
, section
->data
, section
->end_data
));
125 section
->pin_queue_start
= mono_sgen_find_optimized_pin_queue_area (section
->data
, section
->end_data
, §ion
->pin_queue_num_entries
);
126 DEBUG (6, fprintf (gc_debug_file
, "Found %d pinning addresses in section %p\n", section
->pin_queue_num_entries
, section
));