cf4778bf0f06adcc8e0fe760714df3035df8e545
[mono.git] / mono / metadata / sgen-pinning.c
1 #define PIN_STAGING_AREA_SIZE   1024
2
3 static void* pin_staging_area [PIN_STAGING_AREA_SIZE];
4 static int pin_staging_area_index;
5
6 static void
7 init_pinning (void)
8 {
9         pin_staging_area_index = 0;
10 }
11
12 static void
13 evacuate_pin_staging_area (void)
14 {
15         int i;
16
17         g_assert (pin_staging_area_index >= 0 && pin_staging_area_index <= PIN_STAGING_AREA_SIZE);
18
19         /*
20          * The pinning addresses might come from undefined memory, this is normal. Since they
21          * are used in lots of functions, we make the memory defined here instead of having
22          * to add a supression for those functions.
23          */
24         VALGRIND_MAKE_MEM_DEFINED (pin_staging_area, pin_staging_area_index * sizeof (void*));
25
26         sort_addresses (pin_staging_area, pin_staging_area_index);
27
28         while (next_pin_slot + pin_staging_area_index > pin_queue_size)
29                 realloc_pin_queue ();
30
31         pin_queue [next_pin_slot++] = pin_staging_area [0];
32         for (i = 1; i < pin_staging_area_index; ++i) {
33                 void *p = pin_staging_area [i];
34                 if (p != pin_queue [next_pin_slot - 1])
35                         pin_queue [next_pin_slot++] = p;
36         }
37
38         g_assert (next_pin_slot <= pin_queue_size);
39
40         pin_staging_area_index = 0;
41 }
42
43 static void
44 pin_stage_ptr (void *ptr)
45 {
46         if (pin_staging_area_index >= PIN_STAGING_AREA_SIZE)
47                 evacuate_pin_staging_area ();
48
49         pin_staging_area [pin_staging_area_index++] = ptr;
50 }